001: // $Id: ConfigHelper.java 9972 2006-05-31 16:59:05Z steve.ebersole@jboss.com $
002: package org.hibernate.util;
003:
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.InputStreamReader;
007: import java.io.Reader;
008: import java.net.MalformedURLException;
009: import java.net.URL;
010: import java.util.Properties;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.hibernate.HibernateException;
015: import org.hibernate.cfg.Environment;
016:
017: /**
018: * A simple class to centralize logic needed to locate config files on the system.
019: *
020: * @author Steve
021: */
022: public final class ConfigHelper {
023: private static final Log log = LogFactory
024: .getLog(ConfigHelper.class);
025:
026: /** Try to locate a local URL representing the incoming path. The first attempt
027: * assumes that the incoming path is an actual URL string (file://, etc). If this
028: * does not work, then the next attempts try to locate this UURL as a java system
029: * resource.
030: *
031: * @param path The path representing the config location.
032: * @return An appropriate URL or null.
033: */
034: public static final URL locateConfig(final String path) {
035: try {
036: return new URL(path);
037: } catch (MalformedURLException e) {
038: return findAsResource(path);
039: }
040: }
041:
042: /**
043: * Try to locate a local URL representing the incoming path.
044: * This method <b>only</b> attempts to locate this URL as a
045: * java system resource.
046: *
047: * @param path The path representing the config location.
048: * @return An appropriate URL or null.
049: */
050: public static final URL findAsResource(final String path) {
051: URL url = null;
052:
053: // First, try to locate this resource through the current
054: // context classloader.
055: ClassLoader contextClassLoader = Thread.currentThread()
056: .getContextClassLoader();
057: if (contextClassLoader != null) {
058: url = contextClassLoader.getResource(path);
059: }
060: if (url != null)
061: return url;
062:
063: // Next, try to locate this resource through this class's classloader
064: url = ConfigHelper.class.getClassLoader().getResource(path);
065: if (url != null)
066: return url;
067:
068: // Next, try to locate this resource through the system classloader
069: url = ClassLoader.getSystemClassLoader().getResource(path);
070:
071: // Anywhere else we should look?
072: return url;
073: }
074:
075: /** Open an InputStream to the URL represented by the incoming path. First makes a call
076: * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
077: * {@link java.net.URL#openStream()} is then called to obtain the stream.
078: *
079: * @param path The path representing the config location.
080: * @return An input stream to the requested config resource.
081: * @throws HibernateException Unable to open stream to that resource.
082: */
083: public static final InputStream getConfigStream(final String path)
084: throws HibernateException {
085: final URL url = ConfigHelper.locateConfig(path);
086:
087: if (url == null) {
088: String msg = "Unable to locate config file: " + path;
089: log.fatal(msg);
090: throw new HibernateException(msg);
091: }
092:
093: try {
094: return url.openStream();
095: } catch (IOException e) {
096: throw new HibernateException("Unable to open config file: "
097: + path, e);
098: }
099: }
100:
101: /** Open an Reader to the URL represented by the incoming path. First makes a call
102: * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
103: * {@link java.net.URL#openStream()} is then called to obtain a stream, which is then
104: * wrapped in a Reader.
105: *
106: * @param path The path representing the config location.
107: * @return An input stream to the requested config resource.
108: * @throws HibernateException Unable to open reader to that resource.
109: */
110: public static final Reader getConfigStreamReader(final String path)
111: throws HibernateException {
112: return new InputStreamReader(getConfigStream(path));
113: }
114:
115: /** Loads a properties instance based on the data at the incoming config location.
116: *
117: * @param path The path representing the config location.
118: * @return The loaded properties instance.
119: * @throws HibernateException Unable to load properties from that resource.
120: */
121: public static final Properties getConfigProperties(String path)
122: throws HibernateException {
123: try {
124: Properties properties = new Properties();
125: properties.load(getConfigStream(path));
126: return properties;
127: } catch (IOException e) {
128: throw new HibernateException(
129: "Unable to load properties from specified config file: "
130: + path, e);
131: }
132: }
133:
134: private ConfigHelper() {
135: }
136:
137: public static InputStream getResourceAsStream(String resource) {
138: String stripped = resource.startsWith("/") ? resource
139: .substring(1) : resource;
140:
141: InputStream stream = null;
142: ClassLoader classLoader = Thread.currentThread()
143: .getContextClassLoader();
144: if (classLoader != null) {
145: stream = classLoader.getResourceAsStream(stripped);
146: }
147: if (stream == null) {
148: stream = Environment.class.getResourceAsStream(resource);
149: }
150: if (stream == null) {
151: stream = Environment.class.getClassLoader()
152: .getResourceAsStream(stripped);
153: }
154: if (stream == null) {
155: throw new HibernateException(resource + " not found");
156: }
157: return stream;
158: }
159:
160: public static InputStream getUserResourceAsStream(String resource) {
161: boolean hasLeadingSlash = resource.startsWith("/");
162: String stripped = hasLeadingSlash ? resource.substring(1)
163: : resource;
164:
165: InputStream stream = null;
166:
167: ClassLoader classLoader = Thread.currentThread()
168: .getContextClassLoader();
169: if (classLoader != null) {
170: stream = classLoader.getResourceAsStream(resource);
171: if (stream == null && hasLeadingSlash) {
172: stream = classLoader.getResourceAsStream(stripped);
173: }
174: }
175:
176: if (stream == null) {
177: stream = Environment.class.getClassLoader()
178: .getResourceAsStream(resource);
179: }
180: if (stream == null && hasLeadingSlash) {
181: stream = Environment.class.getClassLoader()
182: .getResourceAsStream(stripped);
183: }
184:
185: if (stream == null) {
186: throw new HibernateException(resource + " not found");
187: }
188:
189: return stream;
190: }
191:
192: }
|