Topic Text   Topic Comments (0)   Topic Properties   Topic Information vidhyalakshmi_R01@inf...
Topic title: test Tuesday January 7, 2014 12:47:43

Download topic text | View in monospace font | Tab width set to 4 (change to 8)

Files in topic:  
[Jump to] PropertyManager.java   {+152,-0}

[Add General Comment] to topic.

File PropertyManager.java (Revision 1.0) [Add File Comment] [Top]
 
1 package com.bofa.fast.du.utilities;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Enumeration;
6 import java.util.Locale;
7 import java.util.Properties;
8 import java.util.ResourceBundle;
9
10
11 public class PropertyManager {
12
13 private static Properties prop = null;
14
15 private static final boolean THROW_ON_LOAD_FAILURE = true;
16 private static final boolean LOAD_AS_RESOURCE_BUNDLE = false;
17 private static final String SUFFIX = ".properties";
18
19
20 static {
21 loadProperties("docutil/duconfig.properties");
22
23 }
24
25 /**
26 * Looks up a resource named 'name' in the classpath. The resource must map
27 * to a file with .properties extention. The name is assumed to be absolute
28 * and can use either "/" or "." for package segment separation with an
29 * optional leading "/" and optional ".properties" suffix. Thus, the
30 * following names refer to the same resource:
31 * <pre>
32 * some.pkg.Resource
33 * some.pkg.Resource.properties
34 * some/pkg/Resource
35 * some/pkg/Resource.properties
36 * /some/pkg/Resource
37 * /some/pkg/Resource.properties
38 * </pre>
39 *
40 * @param name classpath resource name [may not be null]
41 * @param loader classloader through which to load the resource [null
42 * is equivalent to the application loader]
43 *
44 * @return resource converted to java.util.Properties [may be null if the
45 * resource was not found and THROW_ON_LOAD_FAILURE is false]
46 * @throws IllegalArgumentException if the resource was not found and
47 * THROW_ON_LOAD_FAILURE is true
48 */
49 public static void loadProperties (String name, ClassLoader loader)
50 {
51 if (name == null){
52 throw new IllegalArgumentException ("null input: name");
53 }
54
55 if (name.startsWith ("/")){
56 name = name.substring (1);
57 }
58
59 if (name.endsWith (SUFFIX)){
60 name = name.substring (0, name.length () - SUFFIX.length ());
61 }
62 Properties result = null;
63
64 InputStream in = null;
65 try
66 {
67 if (loader == null) {
68 loader = ClassLoader.getSystemClassLoader ();
69 }
70
71
72 if (LOAD_AS_RESOURCE_BUNDLE)
73 {
74 name = name.replace ('/', '.');
75 // Throws MissingResourceException on lookup failures:
76 final ResourceBundle rb = ResourceBundle.getBundle (name,
77 Locale.getDefault (), loader);
78
79 result = new Properties ();
80 for (Enumeration<String> keys = rb.getKeys (); keys.hasMoreElements ();)
81 {
82 final String key = keys.nextElement();
83 final String value = rb.getString (key);
84
85 result.put (key, value);
86 }
87 }
88 else
89 {
90 name = name.replace ('.', '/');
91
92 if (! name.endsWith (SUFFIX)){
93 name = name.concat (SUFFIX);
94 }
95
96 // Returns null on lookup failures:
97 in = loader.getResourceAsStream (name);
98 if (in != null)
99 {
100 result = new Properties ();
101 result.load (in); // Can throw IOException
102 }
103 }
104 }
105 catch (Exception e)
106 {
107 result = null;
108 }
109 finally
110 {
111 if (in != null){
112
113 try { in.close ();
114 } catch (IOException ex) {}
115 }
116 }
117
118 if (THROW_ON_LOAD_FAILURE && (result == null))
119 {
120 throw new IllegalArgumentException ("could not load [" + name + "]"+
121 " as " + (LOAD_AS_RESOURCE_BUNDLE
122 ? "a resource bundle"
123 : "a classloader resource"));
124 }
125
126 prop = result;
127 }
128
129 /**
130 * A convenience overload of {@link #loadProperties(String, ClassLoader)}
131 * that uses the current thread's context classloader.
132 */
133 public static void loadProperties (final String name)
134 {
135 loadProperties (name,
136 Thread.currentThread().getContextClassLoader());
137 }
138
139 public static String getProperty(String key){
140 String val = null;
141 if(prop != null && key != null){
142 val = prop.getProperty(key);
143 }
144 return val;
145 }
146
147 public static Properties getProperties() {
148 return prop;
149 }
150 } // End of class
 
  
Legend:
Removed 
Changed
 Added

[Add General Comment] to topic.