View Javadoc

1   /*
2    * Copyright 2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.scaffold.util;
18  
19  
20  import java.util.Properties;
21  import javax.servlet.UnavailableException;
22  
23  
24  /**
25   * Property and Resource utilities.
26   *
27   * @author Ted Husted
28   * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
29   */
30  public class ResourceUtils {
31  
32      /**
33       * Message to log if resource is not found.
34       */
35      private static final String RESOURCE_NOT_FOUND =
36          "Resources not found";
37  
38  
39      /**
40       * Load a properties file.
41       * The path must include the package name(s),
42       * if any, in system directory format and
43       * include the properties extension.
44       *
45       * @param caller The object making the call. Needed to access ClassLoader.
46       * @param path The path to the properties file
47       * @return The initialized Properties object
48       * @throws UnavailableException if resource cannot be found
49       */
50      public static final Properties loadProperties(Object caller,
51              String path) throws Exception {
52  
53          // name = name.replace('.', '/');
54          // if doesn't end in .properties
55          // name += ".properties";
56  
57          java.io.InputStream is = caller.getClass().getClassLoader().getResourceAsStream(path);
58          if (null==is)
59             throw new UnavailableException(RESOURCE_NOT_FOUND);
60  
61          java.io.BufferedInputStream bis = new java.io.BufferedInputStream(is);
62          Properties p = null;
63  
64          if (bis != null) {
65  
66              try {
67                  p = new Properties();
68                  p.load(bis);
69                  bis.close();
70                  is.close();
71              }
72  
73              catch (java.io.IOException e) {
74                  p = null;
75              }
76  
77              finally {
78                  is = null;
79                  bis = null;
80              }
81          }
82  
83          return p;
84  
85      }
86  
87  
88      public static final Properties loadProperties (String path) throws Exception {
89          return loadProperties (new Object(),path);
90      }
91  
92  
93      /**
94       * Load a properties file.
95       * This is a simplified version of <code>loadProperties(Object, String)</code>.
96       *
97       * @param caller The object making the call. Needed to access ClassLoader.
98       * @param path The path to the properties file
99       * @return The initialized Properties object
100      */
101     public static final Properties loadPropertiesFile(String path) {
102 
103         Properties p = null;
104 
105         try {
106 
107             java.io.FileInputStream i = new java.io.FileInputStream(path);
108             p = new java.util.Properties();
109             p.load(i);
110 
111         }
112         catch (Exception e) {
113 
114             p = null;
115 
116         }
117         return p;
118 
119     }
120 }