KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > igfay > util > PropertyUtility


1 package org.igfay.util;
2 import java.net.InetAddress JavaDoc;
3 import java.net.UnknownHostException JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import org.apache.log4j.Logger;
8
9 /**
10  * Description of the Class
11  *
12  *@author bconrad
13  *@created March 27, 2001
14  */

15 public class PropertyUtility {
16
17     private static Logger log = Logger.getLogger(PropertyUtility.class);
18
19     /**
20      * Constructor for the PropertyUtility object
21      */

22     public PropertyUtility() {
23         super();
24     }
25
26
27     /**
28      * addCommandLineProperties: Add properties from -D flags on the command
29      * line.
30      *
31      *@param args The feature to be added to the CommandLineProperties
32      * attribute
33      */

34
35     public static void addCommandLineProperties(String JavaDoc[] args) {
36         // Loop over command line args.
37
// Stop on each arg that begins with "-D" and assume it has the format
38
// 'some-name=some-value'. Parse the name and value tokens,
39
// and add them to the System.properties object.
40

41         log.debug("");
42         if (args == null) {
43             log.debug("No properties specified. Return.");
44             return;
45         }
46         String JavaDoc name = null;
47         String JavaDoc value = null;
48         for (int ix = 0; ix < args.length; ix++) {
49             if (args[ix].length() > 2) {
50                 String JavaDoc swChars = args[ix].substring(0, 2);
51                 if (swChars.equalsIgnoreCase("-D")) {
52                     String JavaDoc sw = args[ix].substring(2);
53                     int indexOfEqualSign = sw.indexOf('=');
54                     if (indexOfEqualSign > 0) {
55                         name = sw.substring(0, indexOfEqualSign);
56                         value = sw.substring(indexOfEqualSign + 1);
57                     } else {
58                         name = sw;
59                         value = "";
60                     }
61                     setProperty(name, value);
62                 }
63                 // -D
64
}
65             // length > 2
66
}
67         // for
68

69         processSelectedProperties();
70         //^^^
71
}
72
73
74     /**
75      * addPropertiesFromParameters Add properties from applet tags
76      *
77      *@param applet The feature to be added to the PropertiesFromParameters
78      * attribute
79      */

80
81     public static void addPropertiesFromParameters(java.applet.Applet JavaDoc applet) {
82         log.debug("Adding applet parameters");
83         String JavaDoc parameters[] = {"DEBUG", "VDTROOT", "PARM", "PARMS", "REDIRECT", "SERVLETHOST", "SERVLETPORT", "DBHOST"};
84         String JavaDoc value = null;
85         for (int i = 0; i < parameters.length; i++) {
86             log.debug("i " + i + " Key: " + parameters[i]);
87             value = applet.getParameter(parameters[i]);
88             log.debug(" value: " + value);
89             if (value != null) {
90                 setProperty(parameters[i], value);
91                 log.debug("addPropertiesFromParameters(" + parameters[i] + ", " + value + ")");
92             }
93         }
94         processSelectedProperties();
95     }
96
97
98     // end getProperty()
99

100     /**
101      * listProperties List System Properties for debugging purposes.
102      */

103     public static void listProperties() {
104         try {
105             Properties JavaDoc pr = System.getProperties();
106             Enumeration JavaDoc enumeration = pr.propertyNames();
107             String JavaDoc enum_key;
108             log.info("listProperties(): ----------------------------");
109             while (enumeration.hasMoreElements()) {
110                 enum_key = (String JavaDoc) enumeration.nextElement();
111                 log.info(
112                         "Key: {" + enum_key + "} Value: {" + pr.getProperty(enum_key) + "}");
113             }
114         } catch (Exception JavaDoc e) {
115             log.warn("*** Exception " + e);
116         }
117     }
118
119
120     /**
121      * Description of the Method
122      */

123     public static void processSelectedProperties() {
124
125     }
126
127
128     /**
129      * propertyEquals
130      *
131      *@param propertyName Description of Parameter
132      *@param value Description of Parameter
133      *@return Description of the Returned Value
134      */

135
136     public static boolean propertyEquals(String JavaDoc propertyName, String JavaDoc value) {
137         try {
138             String JavaDoc xx = System.getProperty(propertyName.toUpperCase(), value);
139             if (xx.equalsIgnoreCase(value)) {
140                 return true;
141             }
142         } catch (Exception JavaDoc e) {
143         }
144         return false;
145     }
146
147
148     /**
149      * replaceProperty replace a user passed string to the System Properties
150      * hashtable.
151      *
152      *@param keyWord Description of Parameter
153      *@param valueString Description of Parameter
154      */

155
156     public static void replaceProperty(String JavaDoc keyWord, String JavaDoc valueString) {
157         try {
158             Properties JavaDoc pr = System.getProperties();
159             log.debug("Adding keyword: "
160                       + keyWord
161                       + " Value: "
162                       + valueString);
163             pr.remove(keyWord);
164             // for replace
165
pr.put(keyWord, valueString);
166         } catch (Exception JavaDoc e) {
167             log.info("*** Exception adding property. keyword: "
168                      + keyWord
169                      + " Value: "
170                      + valueString
171                      + "\n e "
172                      + e);
173         }
174     }
175
176
177     /**
178      * getProperty() General utility to return the value of a given property.
179      * Returns null if not found.
180      *
181      *@param propertyName Description of Parameter
182      *@return The Property value
183      */

184
185     public static String JavaDoc getProperty(String JavaDoc propertyName) {
186         try {
187             String JavaDoc defaultValue = "X$X$X";
188             String JavaDoc xx = getProperty(propertyName, defaultValue);
189             if (xx.equals(defaultValue)) {
190                 return null;
191             }
192             return xx;
193         } catch (Exception JavaDoc e) {
194             log.info("getProperty(S) *** Exception getting property: "
195                      + propertyName
196                      + "\n e: "
197                      + e);
198         }
199         return null;
200     }
201
202
203     // end getProperty()
204

205     /**
206      * getProperty General utility to return the value of a given property.
207      * Returns default value if not found.
208      *
209      *@param propertyName Description of Parameter
210      *@param defaultValue Description of Parameter
211      *@return The Property value
212      */

213
214     public static String JavaDoc getProperty(String JavaDoc propertyName, String JavaDoc defaultValue) {
215         try {
216             // First see if this property exists.
217
String JavaDoc xx = System.getProperty(propertyName);
218             if (xx != null) {
219                 return xx;
220             }
221
222             // Now see if this property exists in a non case-dependent way
223
Properties JavaDoc pr = System.getProperties();
224             Enumeration JavaDoc enumeration = pr.propertyNames();
225             String JavaDoc enumKey;
226             while (enumeration.hasMoreElements()) {
227                 enumKey = (String JavaDoc) enumeration.nextElement();
228                 if (enumKey.equalsIgnoreCase(propertyName)) {
229                     return pr.getProperty(enumKey);
230                 }
231             }
232             // while
233

234         } catch (Exception JavaDoc e) {
235             log.info("*** Exception getting property. propertyName: "
236                      + propertyName + " defaultValue: " + defaultValue + "\n e: " + e);
237         }
238         return defaultValue;
239     }
240
241
242     /*
243      * Utility to get local host name
244      *
245      * @return The HostName value
246      */

247     public static String JavaDoc getHostName() {
248         String JavaDoc HostName;
249         InetAddress JavaDoc localHost = null;
250         try {
251             localHost = InetAddress.getLocalHost();
252         } catch (UnknownHostException JavaDoc e) {
253             //error message
254
}
255         HostName = new String JavaDoc(localHost.getHostName());
256         return HostName;
257     }
258
259
260     public static String JavaDoc getOSString() {
261         if (getProperty("os.name").toUpperCase().indexOf("WIN") >= 0) {
262             return "win";
263         } else if (getProperty("os.name").toUpperCase().indexOf("SUN") >= 0 ||
264                    getProperty("os.name").toUpperCase().indexOf("LIN") >= 0) {
265             return "unix";
266         } else {
267             return "mac";
268         }
269     }
270
271
272     public static String JavaDoc getShortHostName() {
273         String JavaDoc shortHostName = getHostName();
274         if (shortHostName.indexOf(".") > 0) {
275             shortHostName = shortHostName.substring(0, shortHostName.indexOf("."));
276         }
277         return shortHostName;
278     }
279
280
281
282     /**
283      * Gets the FileSeparator attribute of the Utility class
284      *
285      *@return The FileSeparator value
286      */

287     public static String JavaDoc getFileSeparator() {
288         return getProperty("file.separator");
289     }
290
291
292     public static boolean isNeedingWindowsFileSeparator(String JavaDoc aDirectory) {
293         return(isWindows() && !aDirectory.endsWith("/") && !aDirectory.endsWith("\\"));
294     }
295
296     public static boolean isWindows() {
297         return getProperty("os.name").toUpperCase().indexOf("WIN") >= 0;
298     }
299
300
301
302     /**
303      * Set a user passed string to the System Properties hashtable.
304      *
305      *@param keyWord The new Property value
306      *@param value The new Property value
307      */

308     public static void setProperty(String JavaDoc keyWord, String JavaDoc value) {
309         Properties JavaDoc pr = System.getProperties();
310         log.debug("Setting keyword: " + keyWord + " Value: " + value);
311         if (value == null) {
312             pr.remove(keyWord);
313         } else {
314             pr.put(keyWord, value);
315         }
316
317     }
318     public static void main(String JavaDoc[] args){
319         listProperties();
320     }
321
322 }
323
Popular Tags