package com.songbx; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.HibernateException; import org.hibernate.cfg.Environment; import org.hibernate.util.PropertiesHelper; public class ReadProperties { private static final Properties GLOBAL_PROPERTIES; private static final Log log = LogFactory.getLog(Environment.class); public static final String PASS ="hibernate.connection.password"; public static Properties maskOut(Properties props, String key) { Properties clone = ( Properties ) props.clone(); if ( clone.get( key ) != null ) { clone.setProperty( key, "****" ); } return clone; } static{ GLOBAL_PROPERTIES = new Properties(); try { InputStream stream = getResourceAsStream("/hibernate.properties"); try { GLOBAL_PROPERTIES.load(stream); log.info( "loaded properties from resource hibernate.properties: " + PropertiesHelper.maskOut(GLOBAL_PROPERTIES, PASS) ); } catch (Exception e) { log.error("problem loading properties from hibernate.properties"); } finally { try{ stream.close(); } catch (IOException ioe){ log.error("could not close stream on hibernate.properties", ioe); } } } catch (HibernateException he) { log.info("hibernate.properties not found"); } try { GLOBAL_PROPERTIES.putAll( System.getProperties() ); } catch (SecurityException se) { log.warn("could not copy system properties, system properties will be ignored"); } } public static 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 = Environment.class.getResourceAsStream( resource ); } if ( stream == null ) { stream = Environment.class.getClassLoader().getResourceAsStream( stripped ); } if ( stream == null ) { throw new HibernateException( resource + " not found" ); } return stream; } }