1 17 package org.alfresco.web.app; 18 19 import java.util.List ; 20 21 import javax.servlet.ServletContext ; 22 import javax.servlet.ServletContextEvent ; 23 import javax.servlet.ServletContextListener ; 24 import javax.servlet.http.HttpSessionEvent ; 25 import javax.servlet.http.HttpSessionListener ; 26 import javax.transaction.UserTransaction ; 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 56 public class ContextListener implements ServletContextListener , HttpSessionListener 57 { 58 private static Log logger = LogFactory.getLog(ContextListener.class); 59 60 private ServletContext servletContext; 61 62 65 public void contextInitialized(ServletContextEvent event) 66 { 67 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 UserTransaction tx = null; 80 NodeRef companySpaceNodeRef = null; 81 try 82 { 83 tx = transactionService.getUserTransaction(); 84 tx.begin(); 85 authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName()); 86 87 StoreRef storeRef = Repository.getStoreRef(servletContext); 89 90 if (nodeService.exists(storeRef) == false) 92 { 93 throw new AlfrescoRuntimeException("Store not created prior to application startup: " + storeRef); 94 } 95 96 NodeRef rootNodeRef = nodeService.getRootNode(storeRef); 98 99 String rootPath = Application.getRootPath(servletContext); 101 if (rootPath == null) 102 { 103 throw new AlfrescoRuntimeException("Root path has not been configured"); 104 } 105 106 List <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 companySpaceNodeRef = nodes.get(0); 114 Application.setCompanyRootId(companySpaceNodeRef.getId()); 115 116 tx.commit(); 118 } 119 catch (Throwable e) 120 { 121 try 123 { 124 if (tx != null) 125 { 126 tx.rollback(); 127 } 128 } 129 catch (Exception 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 ex) {} 141 } 142 } 143 144 147 public void contextDestroyed(ServletContextEvent 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 e.printStackTrace(); 159 } 160 } 161 162 165 public void sessionCreated(HttpSessionEvent event) 166 { 167 if (logger.isDebugEnabled()) logger.debug("HTTP session created: " + event.getSession().getId()); 168 } 169 170 173 public void sessionDestroyed(HttpSessionEvent 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 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 |