From 850e20fa7ae7034980fa492eca882ec5c478f316 Mon Sep 17 00:00:00 2001 From: Imran Zahid Date: Sun, 16 Feb 2014 13:54:42 -0500 Subject: [PATCH] Add the ability to use hibernate.cfg.xml rather than snow.properties --- .../britesnow/snow/web/ApplicationLoader.java | 96 +++++++++++++++---- 1 file changed, 77 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/britesnow/snow/web/ApplicationLoader.java b/src/main/java/com/britesnow/snow/web/ApplicationLoader.java index f624420..19f498c 100644 --- a/src/main/java/com/britesnow/snow/web/ApplicationLoader.java +++ b/src/main/java/com/britesnow/snow/web/ApplicationLoader.java @@ -5,10 +5,9 @@ import java.io.File; import java.io.FileReader; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.io.IOException; +import java.io.InputStream; +import java.util.*; import javax.servlet.ServletContext; @@ -23,6 +22,10 @@ import com.google.inject.Injector; import com.google.inject.Module; import com.google.inject.util.Modules; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; /** * For Internal use, don't use. @@ -91,6 +94,18 @@ protected ApplicationLoader load(Map overrideProperties, Module.. appProperties.put(key, overrideProperties.get(key)); } } + + boolean hasHibernate = false; + String hibernateFileProperty = appProperties.get("snow.hibernate.configuration"); + if (hibernateFileProperty != null) { + Map hibernateProperties = loadHibernateProperties(hibernateFileProperty); + if (hibernateProperties.size() > 0) { + hasHibernate = true; + for (String key : hibernateProperties.keySet()) { + appProperties.put(key, hibernateProperties.get(key)); + } + } + } // build the webApplicationModules from the properties String applicationConfigClassStr = appProperties.get("snow.webApplicationModules"); @@ -117,9 +132,8 @@ protected ApplicationLoader load(Map overrideProperties, Module.. // build the defaultModules List defaultModules = new ArrayList(); defaultModules.add(new DefaultApplicationModule(applicationPackageBase)); - - boolean hasHibernate = (appProperties != null && MapUtil.hasKeyStartsWith(appProperties, "hibernate.")); - if (hasHibernate) { + + if (hasHibernate || MapUtil.hasKeyStartsWith(appProperties, "hibernate.")) { defaultModules.add(new DefaultHibernateModule()); } @@ -189,14 +203,12 @@ protected Map loadAppProperties() { try { appDirProperties.load(new FileReader(appDirPropertiesFile)); } catch (Exception e) { - Throwables.propagate(e); + logger.error("Unable to load application properties", e); } // override the appProperties with the WebAppRoperties appProperties.putAll(appDirProperties); } - } else { - appFolder = getWebAppFolder(); } PropertyPostProcessor propertyPostProcessor = getPropertyPostProcessor(); @@ -207,15 +219,15 @@ protected Map loadAppProperties() { String propertyPostProcessorClassName = (String) appProperties.get("snow.propertyPostProcessorClass"); if (propertyPostProcessorClassName != null) { try { - Class propertyPostProcessorClass = (Class) Class.forName(propertyPostProcessorClassName); + Class propertyPostProcessorClass = (Class) + Class.forName(propertyPostProcessorClassName); if (propertyPostProcessorClass != null) { propertyPostProcessor = propertyPostProcessorClass.newInstance(); } } catch (Exception e) { - logger.error("Cannot load or process the PropertyPostProcess class: " + propertyPostProcessorClassName - + "\nException: " - + e.getMessage()); + logger.error("Cannot load or process the PropertyPostProcess class: " + + propertyPostProcessorClassName, e); } } } @@ -224,9 +236,7 @@ protected Map loadAppProperties() { appProperties = propertyPostProcessor.processProperties(appProperties); } } catch (Exception e) { - logger.error("Cannot process PropertyPostProcess class: " + propertyPostProcessor - + "\nException: " - + e.getMessage()); + logger.error("Cannot process PropertyPostProcess class: " + propertyPostProcessor, e); } return appProperties; @@ -241,7 +251,7 @@ PropertyPostProcessor getPropertyPostProcessor() { /** * Allow to programatically set a propertyPostProcessor to the appLoader. Usually used for Unit Testing. * - * @param propertyPostProcessor + * @param propertyPostProcessor PropertyPostProcessor */ void setPropertyPostProcessor(PropertyPostProcessor propertyPostProcessor) { this.propertyPostProcessor = propertyPostProcessor; @@ -261,9 +271,57 @@ private File fixWebAppFolder(File webAppFolder) { // is the case, need to go one parent up if (".".equals(webAppFolderName)) { webAppFolder = webAppFolder.getParentFile(); - webAppFolderName = webAppFolder.getName(); } return webAppFolder; } + + private Map loadHibernateProperties(String hibernateFileProperty) { + Map properties = new HashMap(); + InputStream stream = getResourceAsStream(hibernateFileProperty); + if (stream != null) { + try { + SAXReader reader = new SAXReader(); + Document doc = reader.read(stream); + Element sfNode = doc.getRootElement().element("session-factory"); + Iterator itr = sfNode.elementIterator("property"); + while (itr.hasNext()) { + Element node = (Element) itr.next(); + String name = node.attributeValue("name"); + String value = node.getText().trim(); + if (!name.startsWith("hibernate")) { + name = "hibernate." + name; + } + properties.put(name, value); + } + stream.close(); + } + catch (DocumentException e) { + logger.error("Unable to parse hibernate properties", e); + } + catch (IOException e) { + logger.error("Unable to parse hibernate properties", e); + } + } + return properties; + } + + private InputStream getResourceAsStream(String resource) { + String stripped = resource.startsWith("/") ? resource.substring(1) : resource; + InputStream stream = null; + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader != null) { + stream = classLoader.getResourceAsStream(stripped); + } + if (stream == null) { + stream = ApplicationLoader.class.getResourceAsStream(resource); + } + if (stream == null) { + stream = ApplicationLoader.class.getClassLoader().getResourceAsStream(stripped); + } + if (stream == null) { + logger.error(resource + " not found"); + } + return stream; + } } \ No newline at end of file