001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.context;
023
024import java.util.Map;
025
026import org.granite.config.GraniteConfig;
027import org.granite.config.flex.ServicesConfig;
028
029/**
030 * @author Franck WOLFF
031 */
032public abstract class GraniteContext {
033
034        public static final String SESSION_LAST_ACCESSED_TIME_KEY = "org.granite.session.lastAccessedTime";
035
036    private static ThreadLocal<GraniteContext> instance = new ThreadLocal<GraniteContext>() {
037        @Override
038        protected GraniteContext initialValue() {
039            return (null);
040        }
041    };
042
043    private final GraniteConfig graniteConfig;
044    private final ServicesConfig servicesConfig;
045    private final AMFContext amfContext;
046    private final String sessionId;
047    private final String clientType;
048
049    public GraniteContext(GraniteConfig graniteConfig, ServicesConfig servicesConfig, String sessionId) {
050        this(graniteConfig, servicesConfig, sessionId, null);
051    }
052    public GraniteContext(GraniteConfig graniteConfig, ServicesConfig servicesConfig, String sessionId, String clientType) {
053        this.servicesConfig = servicesConfig;
054        this.graniteConfig = graniteConfig;
055        this.amfContext = new AMFContextImpl();
056        this.sessionId = sessionId;
057        this.clientType = clientType != null ? clientType : "as3";
058    }
059
060    public static GraniteContext getCurrentInstance() {
061        return instance.get();
062    }
063
064    protected static void setCurrentInstance(GraniteContext context) {
065        instance.set(context);
066    }
067
068    public static void release() {
069        instance.set(null);
070    }
071
072    public ServicesConfig getServicesConfig() {
073        return servicesConfig;
074    }
075
076    public GraniteConfig getGraniteConfig() {
077        return graniteConfig;
078    }
079
080    public AMFContext getAMFContext() {
081        return amfContext;
082    }
083    
084    public String getClientType() {
085        return clientType;
086    }
087    
088    public String getSessionId() {
089        return sessionId;
090    }
091    
092    public abstract Object getSessionLock();
093
094    public abstract Map<String, String> getInitialisationMap();
095    public abstract Map<String, Object> getApplicationMap();
096    public abstract Map<String, Object> getSessionMap();
097    public abstract Map<String, Object> getSessionMap(boolean create);
098    public abstract Map<String, Object> getRequestMap();
099}