001/** 002 * Copyright 2014 Tampere University of Technology, Pori Department 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package core.tut.pori.context; 017 018import java.util.Date; 019 020import javax.servlet.ServletContext; 021import javax.servlet.ServletContextEvent; 022import javax.servlet.ServletContextListener; 023 024import org.apache.log4j.Logger; 025 026import core.tut.pori.properties.PropertyHandler; 027import core.tut.pori.utils.StringUtils; 028 029/** 030 * Initializes and destroys the web application (service) context, and loads all other handlers during context initialization. 031 * 032 * This class can be used to retrieve instances of various handlers and classes instantiated by the handlers. 033 * 034 * One should never close the handlers or the instances returned by the handlers manually, as they are managed automatically. 035 * The handlers themselves are generally immutable and no modifications to the initialized contexts are possible. 036 * 037 */ 038public class ServiceInitializer implements ServletContextListener{ 039 private static final String LOG4J_CONFIGURATION_FILE = "../log4j2.xml"; 040 private static final String LOG4J_SYSTEM_PROPERTY = "log4j.configurationFile"; 041 private static Logger LOGGER = null; 042 private static DAOHandler DAO_HANDLER = null; 043 private static ServiceHandler SERVICE_HANDLER = null; 044 private static PropertyHandler PROPERTY_HANDLER = null; 045 private static ExecutorHandler EXECUTOR_HANDLER = null; 046 private static EventHandler EVENT_HANDLER = null; 047 private static WebSocketHandler WEBSOCKET_HANDLER = null; 048 049 @Override 050 public void contextDestroyed(ServletContextEvent servletContextEvent) { 051 if(WEBSOCKET_HANDLER != null){ // on failed initialization this may be null 052 WEBSOCKET_HANDLER.close(); 053 WEBSOCKET_HANDLER = null; 054 } 055 056 if(EXECUTOR_HANDLER != null){ // on failed initialization this may be null 057 EXECUTOR_HANDLER.close(); 058 EXECUTOR_HANDLER = null; 059 } 060 if(SERVICE_HANDLER != null){ // on failed initialization this may be null 061 SERVICE_HANDLER.close(); 062 SERVICE_HANDLER = null; 063 } 064 if(DAO_HANDLER != null){ // on failed initialization this may be null 065 DAO_HANDLER.close(); 066 DAO_HANDLER = null; 067 } 068 if(PROPERTY_HANDLER != null){ // on failed initialization this may be null 069 PROPERTY_HANDLER.close(); 070 PROPERTY_HANDLER = null; 071 } 072 if(EVENT_HANDLER != null){ // on failed initialization this may be null 073 EVENT_HANDLER.close(); // close as last the last one in case the other handler send something on destruction 074 EVENT_HANDLER = null; 075 } 076 LOGGER.info("Context destroyed."); 077 LOGGER = null; 078 } 079 080 @Override 081 public void contextInitialized(ServletContextEvent servletContextEvent) { 082 Date started = new Date(); 083 System.setProperty(LOG4J_SYSTEM_PROPERTY, LOG4J_CONFIGURATION_FILE); // Load logger configuration 084 LOGGER = Logger.getLogger(ServiceInitializer.class); 085 ServletContext context = servletContextEvent.getServletContext(); 086 PROPERTY_HANDLER = new PropertyHandler(context); 087 DAO_HANDLER = new DAOHandler(); 088 SERVICE_HANDLER = new ServiceHandler(); 089 EXECUTOR_HANDLER = new ExecutorHandler(); 090 EVENT_HANDLER = new EventHandler(); 091 WEBSOCKET_HANDLER = new WebSocketHandler(); 092 LOGGER.info("Context initialized in "+StringUtils.getDurationString(started, new Date())); 093 } 094 095 /** 096 * 097 * @return Property handler instance. Note that you should NOT close the instance, it will be automatically closed. 098 */ 099 public static PropertyHandler getPropertyHandler(){ 100 return PROPERTY_HANDLER; 101 } 102 103 /** 104 * 105 * @return Service handler instance. Note that you should NOT close the instance, it will be automatically closed. 106 */ 107 public static ServiceHandler getServiceHandler(){ 108 return SERVICE_HANDLER; 109 } 110 111 /** 112 * 113 * @return DAO handler instance. Note that you should NOT close the instance, it will be automatically closed. 114 */ 115 public static DAOHandler getDAOHandler(){ 116 return DAO_HANDLER; 117 } 118 119 /** 120 * 121 * @return scheduler handler instance. Note: you should not close the instance, it will be closed automatically. 122 */ 123 public static ExecutorHandler getExecutorHandler(){ 124 return EXECUTOR_HANDLER; 125 } 126 127 /** 128 * 129 * @return event handler instance. Note: you should not close the instance, it will be closed automatically. 130 */ 131 public static EventHandler getEventHandler(){ 132 return EVENT_HANDLER; 133 } 134 135 /** 136 * 137 * @return session handler instance. Note: you should not close the instance, it will be closed automatically. 138 */ 139 public static SessionHandler getSessionHandler(){ 140 return SessionHandler.getSessionHandler(); 141 } 142 143 /** 144 * 145 * @return web socket handler instance. Note: you should not close the instance, it will be closed automatically. 146 */ 147 public static WebSocketHandler getWebSocketHandler(){ 148 return WEBSOCKET_HANDLER; 149 } 150}