View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.container.session;
18  
19  import javax.servlet.http.HttpSession;
20  import javax.servlet.http.HttpSessionBindingEvent;
21  import javax.servlet.http.HttpSessionEvent;
22  
23  import org.apache.jetspeed.container.session.PortalSessionsManager;
24  import org.apache.jetspeed.container.session.PortletApplicationSessionMonitor;
25  import org.apache.jetspeed.services.JetspeedPortletServices;
26  import org.apache.jetspeed.services.PortletServices;
27  
28  /***
29   * PortletApplicationSessionMonitorImpl
30   *
31   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
32   * @version $Id: $
33   */
34  public class PortletApplicationSessionMonitorImpl implements PortletApplicationSessionMonitor 
35  {
36      private static final long serialVersionUID = -6729032046828426324L;
37      
38      private String contextPath;    
39      private String portalSessionId;
40      private long portalSessionKey;
41      private transient HttpSession session;
42      private boolean forceInvalidate;
43  
44      public PortletApplicationSessionMonitorImpl(String contextPath, String portalSessionId, long portalSessionKey)
45      {
46          this(contextPath, portalSessionId, portalSessionKey, true);
47      }
48      
49      public PortletApplicationSessionMonitorImpl(String contextPath, String portalSessionId, long portalSessionKey, boolean forceInvalidate)
50      {
51          this.contextPath = contextPath;
52          this.portalSessionId = portalSessionId;
53          this.portalSessionKey = portalSessionKey;
54          this.forceInvalidate = forceInvalidate;
55      }
56      
57      /* (non-Javadoc)
58       * @see org.apache.jetspeed.container.session.PortletApplicationSessionMonitor#getPortalSessionKey()
59       */
60      public long getPortalSessionKey()
61      {
62          return portalSessionKey;
63      }
64      
65      /* (non-Javadoc)
66       * @see org.apache.jetspeed.container.session.PortletApplicationSessionMonitor#getPortalSessionId()
67       */
68      public String getPortalSessionId()
69      {
70          return portalSessionId;
71      }
72      
73      /* (non-Javadoc)
74       * @see org.apache.jetspeed.container.session.PortletApplicationSessionMonitor#getContextPath()
75       */
76      public String getContextPath()
77      {
78          return contextPath;
79      }
80      
81      /* (non-Javadoc)
82       * @see org.apache.jetspeed.container.session.PortletApplicationSessionMonitor#invalidateSession()
83       */
84      public void invalidateSession()
85      {
86          if ( session != null )
87          {
88              HttpSession thisSession = session;
89              session = null;
90              if (forceInvalidate)
91              {
92                  try
93                  {
94                      thisSession.invalidate();
95                  }
96                  catch (Exception ise)
97                  {
98                      // ignore
99                  }
100             }
101         }        
102     }
103 
104     /* (non-Javadoc)
105      * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
106      */
107     public void valueBound(HttpSessionBindingEvent event)
108     {
109         this.session = event.getSession();
110     }
111 
112     /* (non-Javadoc)
113      * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
114      */
115     public void valueUnbound(HttpSessionBindingEvent event)
116     {
117         if ( session != null )
118         {
119             PortalSessionsManager manager = getManager(); 
120             if ( manager != null )
121             {
122                 manager.sessionDestroyed(this);
123             }
124             session = null;
125         }
126     }
127 
128     /* (non-Javadoc)
129      * @see javax.servlet.http.HttpSessionActivationListener#sessionDidActivate(javax.servlet.http.HttpSessionEvent)
130      */
131     public void sessionDidActivate(HttpSessionEvent event)
132     {
133         this.session = event.getSession();
134         PortalSessionsManager manager = getManager(); 
135         if ( manager != null )
136         {
137             manager.sessionDidActivate(this);
138         }
139     }
140 
141     /* (non-Javadoc)
142      * @see javax.servlet.http.HttpSessionActivationListener#sessionWillPassivate(javax.servlet.http.HttpSessionEvent)
143      */
144     public void sessionWillPassivate(HttpSessionEvent event)
145     {
146         PortalSessionsManager manager = getManager(); 
147         if ( manager != null )
148         {
149             manager.sessionWillPassivate(this);
150         }
151         session = null;
152     }
153     
154     private PortalSessionsManager getManager()
155     {
156         PortletServices services = JetspeedPortletServices.getSingleton();
157         if (services != null)
158         {
159             return (PortalSessionsManager)services.getService(PortalSessionsManager.SERVICE_NAME);
160         }
161         return null;
162     }
163 }