KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jdbclogger > core > util > ConfigHelper


1 package net.sourceforge.jdbclogger.core.util;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 import net.sourceforge.jdbclogger.core.config.Environment;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * A simple class to centralize logic needed to locate config files on the system.
33  *
34  * @author Catalin Kormos (latest modification by $Author: catalean $)
35  * @version $Revision: 83 $ $Date: 2007-07-08 00:00:58 +0300 (Sun, 08 Jul 2007) $
36  */

37 public final class ConfigHelper
38 {
39     private static final Log log = LogFactory.getLog(ConfigHelper.class);
40     
41     /** Try to locate a local URL representing the incoming path. The first attempt
42      * assumes that the incoming path is an actual URL string (file://, etc). If this
43      * does not work, then the next attempts try to locate this UURL as a java system
44      * resource.
45      *
46      * @param path The path representing the config location.
47      * @return An appropriate URL or null.
48      */

49     public static final URL JavaDoc locateConfig(final String JavaDoc path) {
50         try {
51             return new URL JavaDoc(path);
52         } catch(MalformedURLException JavaDoc e) {
53             return findAsResource(path);
54         }
55     }
56     
57     /**
58      * Try to locate a local URL representing the incoming path.
59      * This method <b>only</b> attempts to locate this URL as a
60      * java system resource.
61      *
62      * @param path The path representing the config location.
63      * @return An appropriate URL or null.
64      */

65     public static final URL JavaDoc findAsResource(final String JavaDoc path) {
66         URL JavaDoc url = null;
67         
68         // First, try to locate this resource through the current
69
// context classloader.
70
ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
71         if (contextClassLoader!=null) {
72             url = contextClassLoader.getResource(path);
73         }
74         if (url != null)
75             return url;
76         
77         // Next, try to locate this resource through this class's classloader
78
url = ConfigHelper.class.getClassLoader().getResource(path);
79         if (url != null)
80             return url;
81         
82         // Next, try to locate this resource through the system classloader
83
url = ClassLoader.getSystemClassLoader().getResource(path);
84         
85         // Anywhere else we should look?
86
return url;
87     }
88     
89     /** Open an InputStream to the URL represented by the incoming path. First makes a call
90      * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
91      * {@link java.net.URL#openStream()} is then called to obtain the stream.
92      *
93      * @param path The path representing the config location.
94      * @return An input stream to the requested config resource.
95      */

96     public static final InputStream JavaDoc getConfigStream(final String JavaDoc path) {
97         final URL JavaDoc url = ConfigHelper.locateConfig(path);
98         
99         if (url == null) {
100             String JavaDoc msg = "Unable to locate config file: " + path;
101             log.error(msg);
102             return null;
103         }
104         
105         try {
106             return url.openStream();
107         } catch(IOException JavaDoc e) {
108             log.error("Unable to open config file: " + path, e);
109         }
110         return null;
111     }
112     
113     /** Open an Reader to the URL represented by the incoming path. First makes a call
114      * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
115      * {@link java.net.URL#openStream()} is then called to obtain a stream, which is then
116      * wrapped in a Reader.
117      *
118      * @param path The path representing the config location.
119      * @return An input stream to the requested config resource.
120      */

121     public static final Reader JavaDoc getConfigStreamReader(final String JavaDoc path) {
122         return new InputStreamReader JavaDoc( getConfigStream(path) );
123     }
124     
125     /** Loads a properties instance based on the data at the incoming config location.
126      *
127      * @param path The path representing the config location.
128      * @return The loaded properties instance.
129      */

130     public static final Properties JavaDoc getConfigProperties(String JavaDoc path) {
131         try
132         {
133             Properties JavaDoc properties = new Properties JavaDoc();
134             properties.load( getConfigStream(path) );
135             return properties;
136         }
137         catch(IOException JavaDoc e) {
138             log.error("Unable to load properties from specified config file: " + path, e);
139         }
140         return null;
141     }
142     
143     private ConfigHelper() {}
144     
145     public static InputStream JavaDoc getResourceAsStream(String JavaDoc resource) {
146         String JavaDoc stripped = resource.startsWith("/") ?
147             resource.substring(1) : resource;
148         
149         InputStream JavaDoc stream = null;
150         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
151         if (classLoader!=null) {
152             stream = classLoader.getResourceAsStream( stripped );
153         }
154         if ( stream == null ) {
155             Environment.class.getResourceAsStream( resource );
156         }
157         if ( stream == null ) {
158             stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
159         }
160         if ( stream == null ) {
161             log.error(resource + " not found" );
162         }
163         return stream;
164     }
165     /**
166      * @param fileName
167      * @return
168      */

169     public static Enumeration JavaDoc<URL JavaDoc> getResources(String JavaDoc fileName){
170         ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
171         try {
172             return contextClassLoader.getResources(fileName);
173         }
174         catch (IOException JavaDoc e) {
175             log.error("No custom drivers found");
176         }
177         
178         return null;
179     }
180 }
181
Popular Tags