1 16 package com.blandware.atleap.webapp.listener; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.util.CommonConverter; 20 import com.blandware.atleap.common.util.CurrencyConverter; 21 import com.blandware.atleap.search.SearchManager; 22 import com.blandware.atleap.service.core.BackupManager; 23 import com.blandware.atleap.service.core.ContentLocaleManager; 24 import com.blandware.atleap.service.core.MailTemplateManager; 25 import com.blandware.atleap.webapp.menu.MenuRepository; 26 import com.blandware.atleap.webapp.util.core.ApplicationResources; 27 import com.blandware.atleap.webapp.util.core.GlobalProperties; 28 import com.blandware.atleap.webapp.util.core.LocaleUtil; 29 import com.blandware.atleap.webapp.util.core.MenuUtil; 30 import com.blandware.atleap.persistence.exception.BackupFileAccessException; 31 import org.apache.commons.beanutils.ConvertUtils; 32 import org.apache.commons.beanutils.converters.*; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.hibernate.FlushMode; 36 import org.hibernate.Session; 37 import org.hibernate.SessionFactory; 38 import org.springframework.context.ApplicationContext; 39 import org.springframework.dao.DataAccessResourceFailureException; 40 import org.springframework.orm.hibernate3.SessionFactoryUtils; 41 import org.springframework.orm.hibernate3.SessionHolder; 42 import org.springframework.transaction.support.TransactionSynchronizationManager; 43 import org.springframework.web.context.ContextLoaderListener; 44 import org.springframework.web.context.WebApplicationContext; 45 import org.springframework.web.context.support.WebApplicationContextUtils; 46 47 import javax.naming.Context ; 48 import javax.naming.InitialContext ; 49 import javax.servlet.ServletContext ; 50 import javax.servlet.ServletContextEvent ; 51 import javax.servlet.ServletContextListener ; 52 import javax.sql.DataSource ; 53 import java.beans.Introspector ; 54 import java.lang.reflect.Method ; 55 import java.lang.reflect.InvocationTargetException ; 56 import java.sql.Driver ; 57 import java.sql.DriverManager ; 58 import java.util.Date ; 59 import java.util.Enumeration ; 60 import java.util.HashMap ; 61 import java.util.Map ; 62 63 73 public class StartupListener extends ContextLoaderListener implements ServletContextListener { 74 75 78 public static final String DEFAULT_SESSION_FACTORY_BEAN_NAME = "sessionFactory"; 79 80 83 protected String sessionFactoryBeanName = DEFAULT_SESSION_FACTORY_BEAN_NAME; 84 85 88 protected transient Log log = LogFactory.getLog(StartupListener.class); 89 90 93 protected MenuRepository menuRepository = null; 94 95 98 protected ServletContext servletContext = null; 99 100 103 protected ApplicationContext applicationContext = null; 104 105 108 protected SessionFactory sessionFactory = null; 109 110 113 protected String contextPath = null; 114 115 118 public void contextInitialized(ServletContextEvent event) { 119 if ( log.isInfoEnabled() ) { 120 log.info("Initializing context..."); 121 } 122 123 super.contextInitialized(event); 126 127 servletContext = event.getServletContext(); 128 contextPath = servletContext.getInitParameter(Constants.CONTEXT_PATH); 129 130 if ( contextPath == null ) { 131 if ( log.isWarnEnabled() ) { 132 log.warn("No 'contextPath' context parameter, using '/'"); 133 } 134 contextPath = "/"; 135 } else if ( !contextPath.startsWith("/") ) { 136 contextPath = "/" + contextPath; 137 } 138 139 String daoType = servletContext.getInitParameter(Constants.DAO_TYPE); 140 141 if ( daoType == null ) { 143 if ( log.isWarnEnabled() ) { 144 log.warn("No 'daoType' context parameter, using Hibernate"); 145 } 146 daoType = Constants.DAO_TYPE_HIBERNATE; 147 } 148 149 Map config = (HashMap ) servletContext.getAttribute(Constants.CONFIG); 152 153 if ( config == null ) { 154 config = new HashMap (); 155 } 156 157 config.put(Constants.DAO_TYPE, daoType); 159 servletContext.setAttribute(Constants.CONFIG, config); 160 161 if ( log.isDebugEnabled() ) { 163 log.debug("daoType: " + daoType); 164 } 165 166 sessionFactory = lookupSessionFactory(); 168 Session session = null; 169 boolean participate = false; 170 if ( TransactionSynchronizationManager.hasResource(sessionFactory) ) { 171 participate = true; 173 } else { 174 if ( log.isDebugEnabled() ) { 175 log.debug("Opening temporary Hibernate session in StartupListener"); 176 } 177 session = getSession(sessionFactory); 178 TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); 179 } 180 181 applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 182 183 BackupManager backupManager = (BackupManager)applicationContext.getBean(Constants.BACKUP_MANAGER_BEAN); 185 try { 186 backupManager.restore(null, Boolean.FALSE); 187 } catch (BackupFileAccessException e) { 188 if ( log.isDebugEnabled() ) { 189 log.error("Cannot create tables and load initial data during startup", e); 190 } 191 } 192 193 ContentLocaleManager contentLocaleManager = (ContentLocaleManager) applicationContext.getBean(Constants.CONTENT_LOCALE_MANAGER_BEAN); 195 contentLocaleManager.initialize(); 196 LocaleUtil.getInstance(servletContext); 197 198 MenuRepository menuRepository = new MenuUtil(servletContext, contextPath).initializeMenuRepository(); 200 servletContext.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, menuRepository); 201 202 205 GlobalProperties.getInstance(servletContext); 207 208 MailTemplateManager mailTemplateManager = (MailTemplateManager) applicationContext.getBean(Constants.MAIL_TEMPLATE_MANAGER_BEAN); 210 mailTemplateManager.initialize(); 211 212 SearchManager.getInstance(servletContext); 214 215 ApplicationResources.getInstance(servletContext); 217 218 if ( !participate ) { 220 TransactionSynchronizationManager.unbindResource(sessionFactory); 221 if ( log.isDebugEnabled() ) { 222 log.debug("Closing temporary Hibernate session in StartupListener"); 223 } 224 closeSession(session, sessionFactory); 225 } 226 227 registerConverters(); 228 } 229 230 233 protected void registerConverters() { 234 235 ConvertUtils.register(new CommonConverter(), String .class); 239 ConvertUtils.register(new CommonConverter(), Date .class); 240 ConvertUtils.register(new CurrencyConverter(), Double .class); 241 ConvertUtils.register(new CommonConverter(), Object .class); 242 243 ConvertUtils.register(new BooleanConverter(null), Boolean.TYPE); 245 ConvertUtils.register(new BooleanConverter(null), Boolean .class); 246 247 ConvertUtils.register(new ByteConverter(null), Byte.TYPE); 248 ConvertUtils.register(new ByteConverter(null), Byte .class); 249 250 ConvertUtils.register(new CharacterConverter(null), Character.TYPE); 251 ConvertUtils.register(new CharacterConverter(null), Character .class); 252 253 ConvertUtils.register(new DoubleConverter(null), Double.TYPE); 254 ConvertUtils.register(new DoubleConverter(null), Double .class); 255 256 ConvertUtils.register(new FloatConverter(null), Float.TYPE); 257 ConvertUtils.register(new FloatConverter(null), Float .class); 258 259 ConvertUtils.register(new IntegerConverter(null), Integer.TYPE); 260 ConvertUtils.register(new IntegerConverter(null), Integer .class); 261 262 ConvertUtils.register(new LongConverter(null), Long.TYPE); 263 ConvertUtils.register(new LongConverter(null), Long .class); 264 265 ConvertUtils.register(new ShortConverter(null), Short.TYPE); 266 ConvertUtils.register(new ShortConverter(null), Short .class); 267 } 268 269 272 public void contextDestroyed(ServletContextEvent event) { 273 ServletContext servletContext = event.getServletContext(); 274 275 try { 277 Session session = SessionFactoryUtils.getSession(sessionFactory, false); 278 closeSession(session, sessionFactory); 279 } catch ( Exception ex ) { 280 } 282 283 try { 285 sessionFactory.close(); 286 } catch ( Exception ex ) { 287 } 289 sessionFactory = null; 290 291 super.contextDestroyed(event); 293 294 ConvertUtils.deregister(); 295 296 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 297 if ( classLoader == null ) { 298 classLoader = this.getClass().getClassLoader(); 299 } 300 301 try { 303 Context environment = (Context) new InitialContext ().lookup("java:comp/env"); 304 String dataSourceName = servletContext.getInitParameter(Constants.DATASOURCE_NAME); 305 DataSource dataSource = (DataSource ) environment.lookup(dataSourceName); 306 Method method = dataSource.getClass().getMethod("close", (Class []) null); 307 method.invoke(dataSource, (Object []) null); 308 } catch ( Exception ex ) { 309 } 311 312 try { 314 for ( Enumeration e = DriverManager.getDrivers(); e.hasMoreElements(); ) { 315 Driver driver = (Driver ) e.nextElement(); 316 if ( driver.getClass().getClassLoader() == classLoader ) { 317 DriverManager.deregisterDriver(driver); 318 } 319 } 320 } catch ( Throwable e ) { 321 } 323 324 if (servletContext != null) { 325 servletContext.removeAttribute(MenuRepository.MENU_REPOSITORY_KEY); 326 } 327 328 servletContext = null; 329 applicationContext = null; 330 log = null; 331 332 try { 334 Class c = classLoader.loadClass("org.apache.commons.logging.LogFactory"); 335 Method m = c.getMethod("release", new Class [] {ClassLoader .class}); 336 m.invoke(null, new Object [] {classLoader}); 337 } catch (Exception e) { 338 } 340 341 try { 343 Class clazz = classLoader.loadClass("org.apache.log4j.LogManager"); 344 Method method = clazz.getMethod("shutdown", (Class []) null); 345 method.invoke(null, (Object []) null); 346 } catch ( Exception ex ) { 347 } 349 350 Introspector.flushCaches(); 351 } 352 353 356 425 426 432 public void setSessionFactoryBeanName(String sessionFactoryBeanName) { 433 this.sessionFactoryBeanName = sessionFactoryBeanName; 434 } 435 436 442 protected String getSessionFactoryBeanName() { 443 return sessionFactoryBeanName; 444 } 445 446 454 protected SessionFactory lookupSessionFactory() { 455 if ( log.isDebugEnabled() ) { 456 log.debug("Using session factory '" + getSessionFactoryBeanName() + "' for StartupListener"); 457 } 458 WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 459 return (SessionFactory) wac.getBean(getSessionFactoryBeanName()); 460 } 461 462 477 protected Session getSession(SessionFactory sessionFactory) 478 throws DataAccessResourceFailureException { 479 Session session = SessionFactoryUtils.getSession(sessionFactory, true); 480 session.setFlushMode(FlushMode.NEVER); 481 return session; 482 } 483 484 497 protected void closeSession(Session session, SessionFactory sessionFactory) { 498 SessionFactoryUtils.releaseSession(session, sessionFactory); 499 } 500 501 } 502 | Popular Tags |