1 package org.hibernate.util; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.hibernate.HibernateException; 7 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.InputStreamReader ; 11 import java.io.Reader ; 12 import java.net.MalformedURLException ; 13 import java.net.URL ; 14 import java.util.Properties ; 15 16 21 public final class ConfigHelper { 22 private static final Log log = LogFactory.getLog(ConfigHelper.class); 23 24 32 public static final URL locateConfig(final String path) { 33 try { 34 return new URL (path); 35 } 36 catch(MalformedURLException e) { 37 return findAsResource(path); 38 } 39 } 40 41 47 public static final URL findAsResource(final String path) { 48 URL url = null; 49 50 url = Thread.currentThread().getContextClassLoader().getResource(path); 53 if (url != null) 54 return url; 55 56 url = ConfigHelper.class.getClassLoader().getResource(path); 58 if (url != null) 59 return url; 60 61 url = ClassLoader.getSystemClassLoader().getResource(path); 63 64 return url; 66 } 67 68 76 public static final InputStream getConfigStream(final String path) throws HibernateException { 77 final URL url = ConfigHelper.locateConfig(path); 78 79 if (url == null) { 80 String msg = "Unable to locate config file: " + path; 81 log.fatal(msg); 82 throw new HibernateException(msg); 83 } 84 85 try { 86 return url.openStream(); 87 } 88 catch(IOException e) { 89 throw new HibernateException("Unable to open config file: " + path, e); 90 } 91 } 92 93 102 public static final Reader getConfigStreamReader(final String path) throws HibernateException { 103 return new InputStreamReader ( getConfigStream(path) ); 104 } 105 106 112 public static final Properties getConfigProperties(String path) throws HibernateException { 113 try { 114 Properties properties = new Properties (); 115 properties.load( getConfigStream(path) ); 116 return properties; 117 } 118 catch(IOException e) { 119 throw new HibernateException("Unable to load properties from specified config file: " + path, e); 120 } 121 } 122 123 private ConfigHelper() {} 124 } 125 | Popular Tags |