001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 * Copyright (c) 2016, Lucee Assosication Switzerland
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either 
009 * version 2.1 of the License, or (at your option) any later version.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public 
017 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
018 * 
019 **/
020package lucee.runtime.type.scope.session;
021
022import lucee.commons.io.log.Log;
023import lucee.runtime.PageContext;
024import lucee.runtime.exp.PageException;
025import lucee.runtime.type.Collection;
026import lucee.runtime.type.Struct;
027import lucee.runtime.type.StructImpl;
028import lucee.runtime.type.scope.Session;
029import lucee.runtime.type.scope.storage.StorageScopeCache;
030import lucee.runtime.type.scope.storage.StorageValue;
031
032public final class SessionCache extends StorageScopeCache implements Session {
033        
034        private static final long serialVersionUID = -875719423763891692L;
035
036        private SessionCache(PageContext pc,String cacheName, String appName,Struct sct, long lastStored) { 
037                super(pc,cacheName,appName,"session",SCOPE_SESSION,sct, lastStored);
038        }
039
040        /**
041         * Constructor of the class, clone existing
042         * @param other
043         */
044        private SessionCache(StorageScopeCache other,boolean deepCopy) {
045                super(other,deepCopy);
046        }
047        
048        @Override
049        public Collection duplicate(boolean deepCopy) {
050        return new SessionCache(this,deepCopy);
051        }
052        
053        /**
054         * load an new instance of the client datasource scope
055         * @param cacheName 
056         * @param appName
057         * @param pc
058         * @return client datasource scope
059         * @throws PageException
060         */
061        public synchronized static Session getInstance(String cacheName, String appName, PageContext pc, Session existing, Log log) throws PageException {
062                StorageValue sv = _loadData(pc, cacheName, appName,"session", log);
063                if(appName!=null && appName.startsWith("no-in-memory-cache-")) existing=null;
064                if(sv!=null) {
065                        long time = sv.lastModified();
066                        
067                        if(existing instanceof StorageScopeCache) {
068                                if(((StorageScopeCache)existing).lastModified()>=time) {
069                                        return existing;
070                                }
071                        }
072                        return new SessionCache(pc,cacheName,appName,sv.getValue(),time);
073                }
074                else if(existing!=null) {
075                        return  existing;
076                }
077                
078                SessionCache session = new SessionCache(pc,cacheName,appName,new StructImpl(),0);
079                session.store(pc.getConfig());
080                return session;
081        }
082        
083
084        public static Session getInstance(String cacheName, String appName, PageContext pc, Session existing, Log log, Session defaultValue) {
085                try {
086                        return getInstance(cacheName, appName, pc,existing, log);
087                }
088                catch (PageException e) {}
089                return defaultValue;
090        }
091        
092        public synchronized static boolean hasInstance(String cacheName, String appName, PageContext pc) {
093                try {
094                        return _loadData(pc, cacheName, appName,"session", null)!=null;
095                } 
096                catch (PageException e) {
097                        return false;
098                }
099}
100        
101
102
103}