KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > ConfigHelper


1 // $Id: ConfigHelper.java,v 1.2 2004/06/28 23:58:08 epbernard Exp $
2
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 JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.Reader JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 /**
17  * A simple class to centralize logic needed to locate config files on the system.
18  *
19  * @author Steve
20  */

21 public final class ConfigHelper {
22     private static final Log log = LogFactory.getLog(ConfigHelper.class);
23
24     /** Try to locate a local URL representing the incoming path. The first attempt
25      * assumes that the incoming path is an actual URL string (file://, etc). If this
26      * does not work, then the next attempts try to locate this UURL as a java system
27      * resource.
28      *
29      * @param path The path representing the config location.
30      * @return An appropriate URL or null.
31      */

32     public static final URL JavaDoc locateConfig(final String JavaDoc path) {
33         try {
34             return new URL JavaDoc(path);
35         }
36         catch(MalformedURLException JavaDoc e) {
37             return findAsResource(path);
38         }
39     }
40
41     /** Try to locate a local URL representing the incoming path. This method <b>only</b>
42      * attempts to locate this UURL as a java system resource.
43      *
44      * @param path The path representing the config location.
45      * @return An appropriate URL or null.
46      */

47     public static final URL JavaDoc findAsResource(final String JavaDoc path) {
48         URL JavaDoc url = null;
49
50         // First, try to locate this resource through the current
51
// context classloader.
52
url = Thread.currentThread().getContextClassLoader().getResource(path);
53         if (url != null)
54             return url;
55
56         // Next, try to locate this resource through this class's classloader
57
url = ConfigHelper.class.getClassLoader().getResource(path);
58         if (url != null)
59             return url;
60
61         // Next, try to locate this resource through the system classloader
62
url = ClassLoader.getSystemClassLoader().getResource(path);
63
64         // Anywhere else we should look?
65
return url;
66     }
67
68     /** Open an InputStream to the URL represented by the incoming path. First makes a call
69      * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
70      * {@link java.net.URL#openStream()} is then called to obtain the stream.
71      *
72      * @param path The path representing the config location.
73      * @return An input stream to the requested config resource.
74      * @throws HibernateException Unable to open stream to that resource.
75      */

76     public static final InputStream JavaDoc getConfigStream(final String JavaDoc path) throws HibernateException {
77         final URL JavaDoc url = ConfigHelper.locateConfig(path);
78
79         if (url == null) {
80             String JavaDoc 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 JavaDoc e) {
89             throw new HibernateException("Unable to open config file: " + path, e);
90         }
91     }
92
93     /** Open an Reader to the URL represented by the incoming path. First makes a call
94      * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
95      * {@link java.net.URL#openStream()} is then called to obtain a stream, which is then
96      * wrapped in a Reader.
97      *
98      * @param path The path representing the config location.
99      * @return An input stream to the requested config resource.
100      * @throws HibernateException Unable to open reader to that resource.
101      */

102     public static final Reader JavaDoc getConfigStreamReader(final String JavaDoc path) throws HibernateException {
103         return new InputStreamReader JavaDoc( getConfigStream(path) );
104     }
105
106     /** Loads a properties instance based on the data at the incoming config location.
107      *
108      * @param path The path representing the config location.
109      * @return The loaded properties instance.
110      * @throws HibernateException Unable to load properties from that resource.
111      */

112     public static final Properties JavaDoc getConfigProperties(String JavaDoc path) throws HibernateException {
113         try {
114             Properties JavaDoc properties = new Properties JavaDoc();
115             properties.load( getConfigStream(path) );
116             return properties;
117         }
118         catch(IOException JavaDoc 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