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