Java tutorial
/** * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. * If a copy of the MPL was not distributed with this file, You can obtain one at * http://mozilla.org/MPL/2.0/. * * This Source Code Form is also subject to the terms of the Health-Related Additional * Disclaimer of Warranty and Limitation of Liability available at * http://www.carewebframework.org/licensing/disclaimer. */ package org.openmrs.cwf.api.util; import java.io.InputStream; import java.util.Properties; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.core.io.Resource; import org.openmrs.api.context.Context; /** * Class for initializing openMRS environment. */ public class OpenMRSInit implements ApplicationContextAware, BeanFactoryPostProcessor { private static final Log log = LogFactory.getLog(OpenMRSInit.class); private final Properties properties = new Properties(); private final String propertiesFile; public OpenMRSInit(String propertiesFile) { this.propertiesFile = propertiesFile; } public void init() throws Exception { try { log.info("Performing OpenMRS startup..."); Context.startup(properties.getProperty("connection.url"), properties.getProperty("connection.username"), properties.getProperty("connection.password"), properties); log.info("Completed OpenMRS startup."); } finally { Context.closeSession(); } } public void destroy() { log.info("Performing OpenMRS shutdown..."); Context.shutdown(); } @Override public void setApplicationContext(ApplicationContext appContext) throws BeansException { Resource resource = appContext.getResource(propertiesFile); InputStream is = null; try { is = resource.getInputStream(); properties.load(is); } catch (Exception e) { log.error("Error loading startup properties file: " + propertiesFile, e); } finally { IOUtils.closeQuietly(is); } } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException { } }