001    package railo.runtime.type.scope;
002    
003    import java.util.Enumeration;
004    
005    import javax.servlet.http.HttpSession;
006    import javax.servlet.http.HttpSessionBindingEvent;
007    import javax.servlet.http.HttpSessionBindingListener;
008    
009    import railo.runtime.PageContext;
010    import railo.runtime.type.Collection;
011    import railo.runtime.type.KeyImpl;
012    import railo.runtime.type.scope.storage.MemoryScope;
013    import railo.runtime.type.scope.storage.StorageScope;
014    import railo.runtime.util.ApplicationContext;
015    
016    /**
017     * 
018     */
019    public final class JSession extends ScopeSupport implements SessionPlus,HttpSessionBindingListener,MemoryScope {
020        
021            //public static final Collection.Key URL_TOKEN = KeyImpl.intern("urltoken");
022            public static final Collection.Key SESSION_ID = KeyImpl.intern("sessionid");
023            
024            private String name;
025        private long timespan=-1;
026        private HttpSession httpSession;
027        private long lastAccess;
028    
029        /**
030         * constructor of the class
031         */
032        public JSession() {
033            super(true,"session",SCOPE_SESSION);
034            setDisplayName("Scope Session (Type J2ee)");
035        }
036    
037        /**
038         * @see railo.runtime.type.scope.ScopeSupport#initialize(railo.runtime.PageContext)
039             */
040            public void touchBeforeRequest(PageContext pc) {
041                    
042                ApplicationContext appContext = pc.getApplicationContext();
043                timespan=appContext.getSessionTimeout().getMillis();
044                this.name=appContext.getName();
045                HttpSession hs = pc.getSession();
046                String id="";
047                try{
048                        if(hs!=null)this.httpSession=hs;
049                        if(httpSession!=null) {
050                                id = httpSession.getId();
051                                if(httpSession.getMaxInactiveInterval()<(timespan/1000))
052                                    httpSession.setMaxInactiveInterval((int)(timespan/1000));
053                        }
054                        
055                    }
056                catch(Throwable t) {
057                    
058                }
059             
060    
061                lastAccess=System.currentTimeMillis();
062            setEL(SESSION_ID,id);
063            setEL(StorageScope.URLTOKEN,"CFID="+pc.getCFID()+"&CFTOKEN="+pc.getCFToken()+"&jsessionid="+id);
064            }
065    
066            public void touchAfterRequest(PageContext pc) {
067                    
068            }
069            
070        public void release() {
071            if(httpSession!=null){
072                    try {
073                            Object key;
074                            Enumeration e = httpSession.getAttributeNames();
075                            while(e.hasMoreElements()) {
076                                    // TODO set inative time new
077                                    key=e.nextElement();
078                                    if(key.equals(name))httpSession.removeAttribute(name);
079                            }
080                            name=null;
081                            timespan=-1;
082                            httpSession=null;
083                            lastAccess=-1;
084                    }
085                    catch(Throwable t) {}
086            }
087            super.release();
088        }
089    
090        /**
091         * @see railo.runtime.type.scope.Session#getLastAccess()
092         */
093        public long getLastAccess() {
094            return lastAccess;
095        }
096    
097        /**
098         * @see railo.runtime.type.scope.Session#getTimeSpan()
099         */
100        public long getTimeSpan() {
101            return timespan;
102        }
103    
104        /**
105         * @see railo.runtime.type.scope.Session#isExpired()
106         */
107        public boolean isExpired() {
108            return (getLastAccess()+getTimeSpan())<System.currentTimeMillis();
109        }
110    
111        /**
112         * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
113         */
114        public void valueBound(HttpSessionBindingEvent event) {
115            
116        }
117    
118        /**
119         * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
120         */
121        public void valueUnbound(HttpSessionBindingEvent event) {
122            clear();
123        }
124            
125        /**
126         *
127         * @see railo.runtime.type.scope.Session#touch()
128         */
129        public void touch() {
130                    lastAccess=System.currentTimeMillis();
131            }
132    
133    }