View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21  package org.miloss.fgsms.common;
22  
23  import java.io.InputStream;
24  import java.util.Enumeration;
25  import java.util.Locale;
26  import java.util.Properties;
27  import java.util.ResourceBundle;
28  
29  /**
30   * Loads a properties file from the classpathfrom
31   * http://www.javaworld.com/javaqa/2003-08/01-qa-0808-property.html?page=2
32   *
33   * @author AO
34   */
35  public abstract class PropertyLoader {
36  
37      /**
38       * Looks up a resource named 'name' in the classpath. The resource must map
39       * to a file with .properties extention. The name is assumed to be absolute
40       * and can use either "/" or "." for package segment separation with an
41       * optional leading "/" and optional ".properties" suffix. Thus, the
42       * following names refer to the same resource:
43       * <pre>
44       * some.pkg.Resource
45       * some.pkg.Resource.properties
46       * some/pkg/Resource
47       * some/pkg/Resource.properties
48       * /some/pkg/Resource
49       * /some/pkg/Resource.properties
50       * </pre>
51       *
52       * @param name classpath resource name [may not be null]
53       * @param loader classloader through which to load the resource [null is
54       * equivalent to the application loader]
55       *
56       * @return resource converted to java.util.Properties [may be null if the
57       * resource was not found and THROW_ON_LOAD_FAILURE is false]
58       * @throws IllegalArgumentException if the resource was not found and
59       * THROW_ON_LOAD_FAILURE is true
60       */
61      public static Properties loadProperties(String name, ClassLoader loader) {
62          if (name == null) {
63              throw new IllegalArgumentException("null input: name");
64          }
65  
66          if (name.startsWith("/")) {
67              name = name.substring(1);
68          }
69  
70          if (name.endsWith(SUFFIX)) {
71              name = name.substring(0, name.length() - SUFFIX.length());
72          }
73  
74          Properties result = null;
75  
76          InputStream in = null;
77          try {
78              if (loader == null) {
79                  loader = ClassLoader.getSystemClassLoader();
80              }
81  
82              if (LOAD_AS_RESOURCE_BUNDLE) {
83                  name = name.replace('/', '.');
84                  // Throws MissingResourceException on lookup failures:
85                  final ResourceBundle rb = ResourceBundle.getBundle(name,
86                          Locale.getDefault(), loader);
87  
88                  result = new Properties();
89                  for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
90                      final String key = (String) keys.nextElement();
91                      final String value = rb.getString(key);
92  
93                      result.put(key, value);
94                  }
95              } else {
96                  name = name.replace('.', '/');
97  
98                  if (!name.endsWith(SUFFIX)) {
99                      name = name.concat(SUFFIX);
100                 }
101 
102                 // Returns null on lookup failures:
103                 in = loader.getResourceAsStream(name);
104                 if (in != null) {
105                     result = new Properties();
106                     result.load(in); // Can throw IOException
107                 }
108             }
109         } catch (Exception e) {
110             result = null;
111         } finally {
112             if (in != null) {
113                 try {
114                     in.close();
115                 } catch (Throwable ignore) {
116                 }
117             }
118         }
119 
120         if (THROW_ON_LOAD_FAILURE && (result == null)) {
121             throw new IllegalArgumentException("could not load [" + name + "]"
122                     + " as " + (LOAD_AS_RESOURCE_BUNDLE
123                     ? "a resource bundle"
124                     : "a classloader resource"));
125         }
126 
127         return result;
128     }
129 /*
130     public static String FindFilePath(String name, ClassLoader loader) {
131         if (name == null) {
132             throw new IllegalArgumentException("null input: name");
133         }
134 
135         if (name.startsWith("/")) {
136             name = name.substring(1);
137         }
138 
139         if (name.endsWith(SUFFIX)) {
140             name = name.substring(0, name.length() - SUFFIX.length());
141         }
142 
143         Properties result = null;
144 
145         InputStream in = null;
146         try {
147             if (loader == null) {
148                 loader = ClassLoader.getSystemClassLoader();
149             }
150 
151             if (LOAD_AS_RESOURCE_BUNDLE) {
152                 name = name.replace('/', '.');
153                 // Throws MissingResourceException on lookup failures:
154                 final ResourceBundle rb = ResourceBundle.getBundle(name,
155                         Locale.getDefault(), loader);
156 
157                 result = new Properties();
158                 for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
159                     final String key = (String) keys.nextElement();
160                     final String value = rb.getString(key);
161 
162                     result.put(key, value);
163                 }
164             } else {
165                 name = name.replace('.', '/');
166 
167                 //if (! name.endsWith (SUFFIX))
168                 //    name = name.concat (SUFFIX);
169 
170                 // Returns null on lookup failures:
171                 return name;
172             }
173         } catch (Exception e) {
174             result = null;
175         } finally {
176             if (in != null) {
177                 try {
178                     in.close();
179                 } catch (Throwable ignore) {
180                 }
181             }
182         }
183 
184         if (THROW_ON_LOAD_FAILURE && (result == null)) {
185             throw new IllegalArgumentException("could not load [" + name + "]"
186                     + " as " + (LOAD_AS_RESOURCE_BUNDLE
187                     ? "a resource bundle"
188                     : "a classloader resource"));
189         }
190 
191         return "";
192     }
193 */
194     /**
195      * A convenience overload of {@link #loadProperties(String, ClassLoader)}
196      * that uses the current thread's context classloader.
197      */
198     public static Properties loadProperties(final String name) {
199         return loadProperties(name,
200                 Thread.currentThread().getContextClassLoader());
201     }
202     private static final boolean THROW_ON_LOAD_FAILURE = true;
203     private static final boolean LOAD_AS_RESOURCE_BUNDLE = false;
204     private static final String SUFFIX = ".properties";
205 } // End of class