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