Java tutorial
/*$Id: ApplicationContextLoaderH3.java 11964 2008-12-03 10:23:00Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2008 Jens Vindvad * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.util.hibernate3.spring; import java.util.Iterator; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.orm.hibernate3.LocalSessionFactoryBean; /** * Default class for accessing ApplicationContext loader. Requires a system property applicationContextConfig * which points to point to a file that creates a application context with bean name "default". * * @author Jens Vindvad, Jens.Vindvad@abm-utvikling.no * @author $Author: jens $ * @version $Rev: 11964 $ * @since 2004-11-25 (Rev. 51) * @date $Date: 2008-12-03 11:23:00 +0100 (Wed, 03 Dec 2008) $ */ public final class ApplicationContextLoaderH3 { public static final String CONFIG_FILE_KEY = "applicationContextConfig"; private static final Log logger = (Log) LogFactory.getLog(ApplicationContextLoaderH3.class); private static ApplicationContextLoaderH3 instance = new ApplicationContextLoaderH3(); private static IllegalArgumentException resultingException; private XmlBeanFactory factory; private String configFile; private ApplicationContext applicationContext; private ApplicationContextLoaderH3() { init(); } public void init() { configFile = System.getProperty(CONFIG_FILE_KEY); if (applicationContext != null) { Map beans = applicationContext.getBeansOfType(LocalSessionFactoryBean.class); for (Iterator iterator = beans.values().iterator(); iterator.hasNext();) { LocalSessionFactoryBean localSessionFactoryBean = (LocalSessionFactoryBean) iterator.next(); // localSessionFactoryBean.dropDatabaseSchema(); try { localSessionFactoryBean.destroy(); } catch (HibernateException e) { throw new RuntimeException(e); } } } applicationContext = null; Resource res = null; try { if (configFile == null) { throw new IllegalArgumentException("you need to set the system property " + CONFIG_FILE_KEY); } res = new FileSystemResource(configFile); if (!res.exists()) { res = new ClassPathResource(configFile); } if (!res.exists()) { res = new UrlResource(configFile); } if (!res.exists()) { throw new IllegalArgumentException("The resource " + configFile + " could not be loaded as a url, classpath resource or a file"); } factory = new XmlBeanFactory(res); } catch (Exception e) { logger.error("Problem when loading application context", e); if (!(e instanceof IllegalArgumentException)) { resultingException = new IllegalArgumentException("Problem creating context " + e.getMessage()); resultingException.initCause(e); } else { resultingException = (IllegalArgumentException) e; } } } public static ApplicationContextLoaderH3 getInstance() { if (resultingException != null) { throw resultingException; } return instance; } public SessionFactory getHibSessionFactory() { if (resultingException != null) { throw resultingException; } return (SessionFactory) instance.getApplicationContext().getBean("abmSessionFactory"); } public SessionFactory getUserSessionFactory() { if (resultingException != null) { throw resultingException; } return (SessionFactory) instance.getApplicationContext().getBean("abmUserSessionFactory"); } public ApplicationContext getApplicationContext() { if (applicationContext == null) { applicationContext = (ApplicationContext) factory.getBean("default"); } return applicationContext; } }