001: package org.igfay.util;
002:
003: import java.net.InetAddress;
004: import java.net.UnknownHostException;
005: import java.util.Enumeration;
006: import java.util.Properties;
007:
008: import org.apache.log4j.Logger;
009:
010: /**
011: * Description of the Class
012: *
013: *@author bconrad
014: *@created March 27, 2001
015: */
016: public class PropertyUtility {
017:
018: private static Logger log = Logger.getLogger(PropertyUtility.class);
019:
020: /**
021: * Constructor for the PropertyUtility object
022: */
023: public PropertyUtility() {
024: super ();
025: }
026:
027: /**
028: * addCommandLineProperties: Add properties from -D flags on the command
029: * line.
030: *
031: *@param args The feature to be added to the CommandLineProperties
032: * attribute
033: */
034:
035: public static void addCommandLineProperties(String[] args) {
036: // Loop over command line args.
037: // Stop on each arg that begins with "-D" and assume it has the format
038: // 'some-name=some-value'. Parse the name and value tokens,
039: // and add them to the System.properties object.
040:
041: log.debug("");
042: if (args == null) {
043: log.debug("No properties specified. Return.");
044: return;
045: }
046: String name = null;
047: String value = null;
048: for (int ix = 0; ix < args.length; ix++) {
049: if (args[ix].length() > 2) {
050: String swChars = args[ix].substring(0, 2);
051: if (swChars.equalsIgnoreCase("-D")) {
052: String sw = args[ix].substring(2);
053: int indexOfEqualSign = sw.indexOf('=');
054: if (indexOfEqualSign > 0) {
055: name = sw.substring(0, indexOfEqualSign);
056: value = sw.substring(indexOfEqualSign + 1);
057: } else {
058: name = sw;
059: value = "";
060: }
061: setProperty(name, value);
062: }
063: // -D
064: }
065: // length > 2
066: }
067: // for
068:
069: processSelectedProperties();
070: //^^^
071: }
072:
073: /**
074: * addPropertiesFromParameters Add properties from applet tags
075: *
076: *@param applet The feature to be added to the PropertiesFromParameters
077: * attribute
078: */
079:
080: public static void addPropertiesFromParameters(
081: java.applet.Applet applet) {
082: log.debug("Adding applet parameters");
083: String parameters[] = { "DEBUG", "VDTROOT", "PARM", "PARMS",
084: "REDIRECT", "SERVLETHOST", "SERVLETPORT", "DBHOST" };
085: String value = null;
086: for (int i = 0; i < parameters.length; i++) {
087: log.debug("i " + i + " Key: " + parameters[i]);
088: value = applet.getParameter(parameters[i]);
089: log.debug(" value: " + value);
090: if (value != null) {
091: setProperty(parameters[i], value);
092: log.debug("addPropertiesFromParameters("
093: + parameters[i] + ", " + value + ")");
094: }
095: }
096: processSelectedProperties();
097: }
098:
099: // end getProperty()
100:
101: /**
102: * listProperties List System Properties for debugging purposes.
103: */
104: public static void listProperties() {
105: try {
106: Properties pr = System.getProperties();
107: Enumeration enumeration = pr.propertyNames();
108: String enum_key;
109: log.info("listProperties(): ----------------------------");
110: while (enumeration.hasMoreElements()) {
111: enum_key = (String) enumeration.nextElement();
112: log.info("Key: {" + enum_key + "} Value: {"
113: + pr.getProperty(enum_key) + "}");
114: }
115: } catch (Exception e) {
116: log.warn("*** Exception " + e);
117: }
118: }
119:
120: /**
121: * Description of the Method
122: */
123: public static void processSelectedProperties() {
124:
125: }
126:
127: /**
128: * propertyEquals
129: *
130: *@param propertyName Description of Parameter
131: *@param value Description of Parameter
132: *@return Description of the Returned Value
133: */
134:
135: public static boolean propertyEquals(String propertyName,
136: String value) {
137: try {
138: String xx = System.getProperty(propertyName.toUpperCase(),
139: value);
140: if (xx.equalsIgnoreCase(value)) {
141: return true;
142: }
143: } catch (Exception e) {
144: }
145: return false;
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 keyWord,
157: String valueString) {
158: try {
159: Properties pr = System.getProperties();
160: log.debug("Adding keyword: " + keyWord + " Value: "
161: + valueString);
162: pr.remove(keyWord);
163: // for replace
164: pr.put(keyWord, valueString);
165: } catch (Exception e) {
166: log.info("*** Exception adding property. keyword: "
167: + keyWord + " Value: " + valueString + "\n e " + e);
168: }
169: }
170:
171: /**
172: * getProperty() General utility to return the value of a given property.
173: * Returns null if not found.
174: *
175: *@param propertyName Description of Parameter
176: *@return The Property value
177: */
178:
179: public static String getProperty(String propertyName) {
180: try {
181: String defaultValue = "X$X$X";
182: String xx = getProperty(propertyName, defaultValue);
183: if (xx.equals(defaultValue)) {
184: return null;
185: }
186: return xx;
187: } catch (Exception e) {
188: log.info("getProperty(S) *** Exception getting property: "
189: + propertyName + "\n e: " + e);
190: }
191: return null;
192: }
193:
194: // end getProperty()
195:
196: /**
197: * getProperty General utility to return the value of a given property.
198: * Returns default value if not found.
199: *
200: *@param propertyName Description of Parameter
201: *@param defaultValue Description of Parameter
202: *@return The Property value
203: */
204:
205: public static String getProperty(String propertyName,
206: String defaultValue) {
207: try {
208: // First see if this property exists.
209: String xx = System.getProperty(propertyName);
210: if (xx != null) {
211: return xx;
212: }
213:
214: // Now see if this property exists in a non case-dependent way
215: Properties pr = System.getProperties();
216: Enumeration enumeration = pr.propertyNames();
217: String enumKey;
218: while (enumeration.hasMoreElements()) {
219: enumKey = (String) enumeration.nextElement();
220: if (enumKey.equalsIgnoreCase(propertyName)) {
221: return pr.getProperty(enumKey);
222: }
223: }
224: // while
225:
226: } catch (Exception e) {
227: log.info("*** Exception getting property. propertyName: "
228: + propertyName + " defaultValue: " + defaultValue
229: + "\n e: " + e);
230: }
231: return defaultValue;
232: }
233:
234: /*
235: * Utility to get local host name
236: *
237: * @return The HostName value
238: */
239: public static String getHostName() {
240: String HostName;
241: InetAddress localHost = null;
242: try {
243: localHost = InetAddress.getLocalHost();
244: } catch (UnknownHostException e) {
245: //error message
246: }
247: HostName = new String(localHost.getHostName());
248: return HostName;
249: }
250:
251: public static String getOSString() {
252: if (getProperty("os.name").toUpperCase().indexOf("WIN") >= 0) {
253: return "win";
254: } else if (getProperty("os.name").toUpperCase().indexOf("SUN") >= 0
255: || getProperty("os.name").toUpperCase().indexOf("LIN") >= 0) {
256: return "unix";
257: } else {
258: return "mac";
259: }
260: }
261:
262: public static String getShortHostName() {
263: String shortHostName = getHostName();
264: if (shortHostName.indexOf(".") > 0) {
265: shortHostName = shortHostName.substring(0, shortHostName
266: .indexOf("."));
267: }
268: return shortHostName;
269: }
270:
271: /**
272: * Gets the FileSeparator attribute of the Utility class
273: *
274: *@return The FileSeparator value
275: */
276: public static String getFileSeparator() {
277: return getProperty("file.separator");
278: }
279:
280: public static boolean isNeedingWindowsFileSeparator(
281: String aDirectory) {
282: return (isWindows() && !aDirectory.endsWith("/") && !aDirectory
283: .endsWith("\\"));
284: }
285:
286: public static boolean isWindows() {
287: return getProperty("os.name").toUpperCase().indexOf("WIN") >= 0;
288: }
289:
290: /**
291: * Set a user passed string to the System Properties hashtable.
292: *
293: *@param keyWord The new Property value
294: *@param value The new Property value
295: */
296: public static void setProperty(String keyWord, String value) {
297: Properties pr = System.getProperties();
298: log.debug("Setting keyword: " + keyWord + " Value: " + value);
299: if (value == null) {
300: pr.remove(keyWord);
301: } else {
302: pr.put(keyWord, value);
303: }
304:
305: }
306:
307: public static void main(String[] args) {
308: listProperties();
309: }
310:
311: }
|