KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > app > ContextListener


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.app;
18
19 import java.util.List JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.ServletContextEvent JavaDoc;
23 import javax.servlet.ServletContextListener JavaDoc;
24 import javax.servlet.http.HttpSessionEvent JavaDoc;
25 import javax.servlet.http.HttpSessionListener JavaDoc;
26 import javax.transaction.UserTransaction JavaDoc;
27
28 import org.alfresco.error.AlfrescoRuntimeException;
29 import org.alfresco.repo.security.authentication.AuthenticationComponent;
30 import org.alfresco.service.ServiceRegistry;
31 import org.alfresco.service.cmr.repository.NodeRef;
32 import org.alfresco.service.cmr.repository.NodeService;
33 import org.alfresco.service.cmr.repository.StoreRef;
34 import org.alfresco.service.cmr.search.SearchService;
35 import org.alfresco.service.cmr.security.AuthenticationService;
36 import org.alfresco.service.namespace.NamespaceService;
37 import org.alfresco.service.transaction.TransactionService;
38 import org.alfresco.web.app.portlet.AlfrescoFacesPortlet;
39 import org.alfresco.web.app.servlet.AuthenticationHelper;
40 import org.alfresco.web.bean.repository.Repository;
41 import org.alfresco.web.bean.repository.User;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.quartz.Scheduler;
45 import org.quartz.SchedulerException;
46 import org.springframework.web.context.WebApplicationContext;
47 import org.springframework.web.context.support.WebApplicationContextUtils;
48
49 /**
50  * ServletContextListener implementation that initialises the application.
51  *
52  * NOTE: This class must appear after the Spring context loader listener
53  *
54  * @author gavinc
55  */

56 public class ContextListener implements ServletContextListener JavaDoc, HttpSessionListener JavaDoc
57 {
58    private static Log logger = LogFactory.getLog(ContextListener.class);
59
60    private ServletContext JavaDoc servletContext;
61
62    /**
63     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
64     */

65    public void contextInitialized(ServletContextEvent JavaDoc event)
66    {
67       // make sure that the spaces store in the repository exists
68
this.servletContext = event.getServletContext();
69       WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
70       ServiceRegistry registry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
71       TransactionService transactionService = registry.getTransactionService();
72       NodeService nodeService = registry.getNodeService();
73       SearchService searchService = registry.getSearchService();
74       NamespaceService namespaceService = registry.getNamespaceService();
75       AuthenticationComponent authenticationComponent = (AuthenticationComponent) ctx
76             .getBean("authenticationComponent");
77
78       // repo bootstrap code for our client
79
UserTransaction JavaDoc tx = null;
80       NodeRef companySpaceNodeRef = null;
81       try
82       {
83          tx = transactionService.getUserTransaction();
84          tx.begin();
85          authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
86
87          // get and setup the initial store ref from config
88
StoreRef storeRef = Repository.getStoreRef(servletContext);
89
90          // check the repository exists, create if it doesn't
91
if (nodeService.exists(storeRef) == false)
92          {
93             throw new AlfrescoRuntimeException("Store not created prior to application startup: " + storeRef);
94          }
95
96          // get hold of the root node
97
NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
98
99          // see if the company home space is present
100
String JavaDoc rootPath = Application.getRootPath(servletContext);
101          if (rootPath == null)
102          {
103             throw new AlfrescoRuntimeException("Root path has not been configured");
104          }
105
106          List JavaDoc<NodeRef> nodes = searchService.selectNodes(rootNodeRef, rootPath, null, namespaceService, false);
107          if (nodes.size() == 0)
108          {
109             throw new AlfrescoRuntimeException("Root path not created prior to application startup: " + rootPath);
110          }
111
112          // Extract company space id and store it in the Application object
113
companySpaceNodeRef = nodes.get(0);
114          Application.setCompanyRootId(companySpaceNodeRef.getId());
115          
116          // commit the transaction
117
tx.commit();
118       }
119       catch (Throwable JavaDoc e)
120       {
121          // rollback the transaction
122
try
123          {
124             if (tx != null)
125             {
126                tx.rollback();
127             }
128          }
129          catch (Exception JavaDoc ex) {}
130          
131          logger.error("Failed to initialise ", e);
132          throw new AlfrescoRuntimeException("Failed to initialise ", e);
133       }
134       finally
135       {
136           try
137           {
138              authenticationComponent.clearCurrentSecurityContext();
139           }
140           catch (Exception JavaDoc ex) {}
141       }
142    }
143
144    /**
145     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
146     */

147    public void contextDestroyed(ServletContextEvent JavaDoc event)
148    {
149       WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
150       Scheduler quartz = (Scheduler) ctx.getBean("schedulerFactory");
151       try
152       {
153          quartz.shutdown(true);
154       }
155       catch (SchedulerException e)
156       {
157          // TODO Auto-generated catch block
158
e.printStackTrace();
159       }
160    }
161
162    /**
163     * Session created listener
164     */

165    public void sessionCreated(HttpSessionEvent JavaDoc event)
166    {
167       if (logger.isDebugEnabled()) logger.debug("HTTP session created: " + event.getSession().getId());
168    }
169
170    /**
171     * Session destroyed listener
172     */

173    public void sessionDestroyed(HttpSessionEvent JavaDoc event)
174    {
175       if (logger.isDebugEnabled()) logger.debug("HTTP session destroyed: " + event.getSession().getId());
176
177       User user;
178       if (Application.inPortalServer() == false)
179       {
180          user = (User)event.getSession().getAttribute(AuthenticationHelper.AUTHENTICATION_USER);
181       }
182       else
183       {
184          user = (User)event.getSession().getAttribute(AlfrescoFacesPortlet.MANAGED_BEAN_PREFIX + AuthenticationHelper.AUTHENTICATION_USER);
185       }
186       
187       if (user != null)
188       {
189          // invalidate ticket and clear the Security context for this thread
190
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
191          AuthenticationService authService = (AuthenticationService)ctx.getBean("authenticationService");
192          authService.invalidateTicket(user.getTicket());
193          authService.clearCurrentSecurityContext();
194          event.getSession().removeAttribute(AuthenticationHelper.AUTHENTICATION_USER);
195       }
196    }
197 }
198
Popular Tags