001    package railo.runtime.type.scope;
002    
003    import java.util.ArrayList;
004    import java.util.Enumeration;
005    import java.util.HashSet;
006    import java.util.Iterator;
007    import java.util.List;
008    import java.util.Set;
009    
010    import javax.servlet.http.HttpSession;
011    import javax.servlet.http.HttpSessionBindingEvent;
012    import javax.servlet.http.HttpSessionBindingListener;
013    
014    import railo.runtime.PageContext;
015    import railo.runtime.engine.ThreadLocalPageContext;
016    import railo.runtime.listener.ApplicationContext;
017    import railo.runtime.type.Collection;
018    import railo.runtime.type.scope.storage.MemoryScope;
019    import railo.runtime.type.util.KeyConstants;
020    
021    /**
022     * 
023     */
024    public final class JSession extends ScopeSupport implements Session,HttpSessionBindingListener,MemoryScope {
025        
026            public static final Collection.Key SESSION_ID = KeyConstants._sessionid;
027            private static Set<Collection.Key> FIX_KEYS=new HashSet<Collection.Key>();
028            static {
029                    FIX_KEYS.add(KeyConstants._sessionid);
030                    FIX_KEYS.add(KeyConstants._urltoken);
031            }
032    
033            
034            private String name;
035        private long timespan=-1;
036        private HttpSession httpSession;
037        private long lastAccess;
038            private long created;
039    
040        /**
041         * constructor of the class
042         */
043        public JSession() {
044            super(true,"session",SCOPE_SESSION);
045            setDisplayName("Scope Session (Type J2ee)");
046            this.created=System.currentTimeMillis();
047        }
048    
049        @Override
050            public void touchBeforeRequest(PageContext pc) {
051                    
052                ApplicationContext appContext = pc.getApplicationContext();
053                timespan=appContext.getSessionTimeout().getMillis();
054                this.name=appContext.getName();
055                HttpSession hs = pc.getSession();
056                String id="";
057                try{
058                        if(hs!=null)this.httpSession=hs;
059                        if(httpSession!=null) {
060                                id = httpSession.getId();
061                                if(httpSession.getMaxInactiveInterval()<(timespan/1000))
062                                    httpSession.setMaxInactiveInterval((int)(timespan/1000));
063                        }
064                        
065                    }
066                catch(Throwable t) {
067                    
068                }
069             
070    
071                lastAccess=System.currentTimeMillis();
072            setEL(KeyConstants._sessionid,id);
073            setEL(KeyConstants._urltoken,"CFID="+pc.getCFID()+"&CFTOKEN="+pc.getCFToken()+"&jsessionid="+id);
074            }
075    
076            public void touchAfterRequest(PageContext pc) {
077                    
078            }
079            @Override
080            public void release() {
081                    release(ThreadLocalPageContext.get());
082            }
083            
084            @Override
085            public void release(PageContext pc) {
086            if(httpSession!=null){
087                    try {
088                            Object key;
089                            Enumeration e = httpSession.getAttributeNames();
090                            while(e.hasMoreElements()) {
091                                    // TODO set inative time new
092                                    key=e.nextElement();
093                                    if(key.equals(name))httpSession.removeAttribute(name);
094                            }
095                            name=null;
096                            timespan=-1;
097                            httpSession=null;
098                            lastAccess=-1;
099                    }
100                    catch(Throwable t) {}
101            }
102            super.release(pc);
103        }
104    
105        @Override
106        public long getLastAccess() {
107            return lastAccess;
108        }
109    
110        @Override
111        public long getTimeSpan() {
112            return timespan;
113        }
114    
115        @Override
116        public boolean isExpired() {
117            return (getLastAccess()+getTimeSpan())<System.currentTimeMillis();
118        }
119    
120        @Override
121        public void valueBound(HttpSessionBindingEvent event) {
122            
123        }
124    
125        @Override
126        public void valueUnbound(HttpSessionBindingEvent event) {
127            clear();
128        }
129            
130        @Override
131        public void touch() {
132                    lastAccess=System.currentTimeMillis();
133            }
134    
135            @Override
136            public long getCreated() {
137                    return created;
138            }
139            
140            public Collection.Key[] pureKeys() {
141                    List<Collection.Key> keys=new ArrayList<Collection.Key>();
142                    Iterator<Key> it = keyIterator();
143                    Collection.Key key;
144                    while(it.hasNext()){
145                            key=it.next();
146                            if(!FIX_KEYS.contains(key))keys.add(key);
147                    }
148                    return keys.toArray(new Collection.Key[keys.size()]);
149            }
150    
151            @Override
152            public void resetEnv(PageContext pc) {
153                    created=System.currentTimeMillis();
154                    lastAccess=System.currentTimeMillis();
155                    touchBeforeRequest(pc);
156            }
157    }