001: /* ----- BEGIN LICENSE BLOCK -----
002: * Version: MPL 1.1
003: *
004: * The contents of this file are subject to the Mozilla Public License Version
005: * 1.1 (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS" basis,
010: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: * for the specific language governing rights and limitations under the
012: * License.
013: *
014: * The Original Code is the DataShare server.
015: *
016: * The Initial Developer of the Original Code is
017: * Ball Aerospace & Technologies Corp, Fairborn, Ohio
018: * Portions created by the Initial Developer are Copyright (C) 2001
019: * the Initial Developer. All Rights Reserved.
020: *
021: * Contributor(s): Charles Wood <cwood@ball.com>
022: * Bart Carlton <bcarlton@ball.com>
023: *
024: * ----- END LICENSE BLOCK ----- */
025: /* RCS $Id: PropertiesManagerAdapter.java,v 1.2 2002/01/29 20:58:52 lizellaman Exp $
026: * $Log: PropertiesManagerAdapter.java,v $
027: * Revision 1.2 2002/01/29 20:58:52 lizellaman
028: * Added LoggingInterface, modified the PropertiesInterface implementation
029: *
030: * Revision 1.1.1.1 2001/10/23 13:37:20 lizellaman
031: * initial sourceforge release
032: *
033: */
034:
035: package org.datashare.plugins.PropertiesManager;
036:
037: import java.util.Hashtable;
038: import java.util.Properties;
039: import java.util.Enumeration;
040: import java.io.BufferedReader;
041: import java.io.FileReader;
042: import java.net.URL;
043: import java.util.StringTokenizer;
044: import java.lang.Exception;
045:
046: import org.datashare.PropertiesInterface;
047: import org.datashare.SessionUtilities;
048:
049: /**
050: * Generic version of the PropertiesInterface. It uses three sources of
051: * properties to modify the initial values passed in through the hashTable
052: * parameter of the setParameters method. There are three sources of
053: * properties. The first is from a file if a key called fileName is contained
054: * in the hashtable provided. The second source of properties is the system
055: * properties, and finally they can be provided at the command line as well.
056: *
057: * @author Bart Carlton (Charles Wood)
058: * @date January-29-2002
059: * @version 2.0
060: */
061: public class PropertiesManagerAdapter implements PropertiesInterface {
062: protected Hashtable localHash = null;
063: protected String[] commandArgs = null;
064: private String fileNameKey = "propertiesFile"; // the parameter that contains our properties file name, if any
065: private String fileName = null;
066:
067: /**
068: * constructor
069: *
070: */
071: public PropertiesManagerAdapter() {
072: }
073:
074: /**
075: * Sets the hashtable containing the default properties to be used
076: *
077: * param localHash the hashtable that contains the intial properties
078: * list and their default values
079: *
080: */
081: public void setParameters(Hashtable localHash) {
082: this .localHash = localHash;
083: }
084:
085: /**
086: * Sets the command line arguments string to be used for altering
087: * values in the local hashtable.
088: *
089: * param args the string array that is to be processed as the
090: * command line arguments
091: *
092: */
093: public void setCommandLineArgs(String[] args) {
094: commandArgs = args;
095: }
096:
097: /**
098: * Returns the modified hashtable after setting properties based on the property
099: * file provided in the hahstable, the system properties
100: * and the command line arguments string if it has been set.
101: *
102: * Returns hashtable with the newly set values
103: *
104: */
105: public Hashtable updateProperties() {
106: SessionUtilities.getLoggingInterface().debugMsg(
107: SessionUtilities.getLoggingInterface().DEBUG,
108: SessionUtilities.getLoggingInterface().GENERALSTATUS,
109: "updateProperties()...");
110: String fileName2 = fileName; // so we can tell if the properties manager file gets changed (so we can load new file)
111: getPropertiesFromFile();
112: getPropertiesFromSystemProperties();
113: getPropertiesFromCommandLine();
114: // if new 'propertiesFile' specified, run again
115: if (localHash.containsKey(fileNameKey)
116: && !((String) localHash.get(fileNameKey))
117: .equals(fileName2)) {
118: getPropertiesFromFile();
119: getPropertiesFromSystemProperties();
120: getPropertiesFromCommandLine();
121: }
122:
123: return localHash;
124: }
125:
126: /**
127: * Modifies the properties hashtable [localHash] based on a
128: * properties file. The filename must be provided in the hashtable
129: * with its key as "fileName". New properties are added in addition
130: * to old ones being modified.
131: *
132: */
133: protected void getPropertiesFromFile() {
134: SessionUtilities.getLoggingInterface().debugMsg(
135: SessionUtilities.getLoggingInterface().DEBUG,
136: SessionUtilities.getLoggingInterface().GENERALSTATUS,
137: "getPropertiesFromFile()...");
138: String line;
139: int tokenCount = 0;
140: BufferedReader bin = null;
141:
142: if (localHash != null && localHash.containsKey(fileNameKey)
143: && localHash.get(fileNameKey) != null
144: && !((String) localHash.get(fileNameKey)).equals("")) {
145: //read in properties from the named file and write them into the hashtable provided
146: try {
147: boolean finished = false;
148: String dir = "";
149: String path = System
150: .getProperty("java.class.path", ".");
151: StringTokenizer st2 = new StringTokenizer(path, System
152: .getProperty("path.separator").toString());
153:
154: while (!finished) {
155: fileName = dir
156: + (String) localHash.get(fileNameKey);
157: try {
158: bin = new BufferedReader(new FileReader(
159: fileName));
160: finished = true;
161: } catch (Exception e) {
162: }
163: if (st2.hasMoreTokens())
164: dir = st2.nextToken()
165: + System.getProperty("file.separator")
166: .toString();
167: else
168: finished = true;
169: }
170: SessionUtilities
171: .getLoggingInterface()
172: .debugMsg(
173: SessionUtilities.getLoggingInterface().DEBUG,
174: SessionUtilities.getLoggingInterface().GENERALSTATUS,
175: "Loaded properties file-> " + fileName);
176: while ((line = bin.readLine()) != null) {
177: try {
178: // get all of line up to first comment character
179: String shortString;
180: if (line.indexOf("#") != -1)
181: shortString = line.substring(0, line
182: .indexOf("#"));
183: else
184: shortString = new String(line);
185: StringTokenizer st = new StringTokenizer(
186: shortString, "=");
187: tokenCount = st.countTokens();
188: if (tokenCount >= 2) {
189: String tempKey = st.nextToken().trim();
190: String tempValue = st.nextToken().trim(); //gets at least the beginning of the value
191: //// if the properties value contains an "=" it must be added back in and
192: //// the rest of the property added to tempValue
193: //while (st.hasMoreTokens())
194: // {
195: // tempValue = tempValue + "=" + st.nextToken().trim();
196: // }
197:
198: // cannot add new variable, only overwrite old value of variable already in table
199: if (localHash != null
200: && localHash.containsKey(tempKey)) {
201: SessionUtilities
202: .getLoggingInterface()
203: .debugMsg(
204: SessionUtilities
205: .getLoggingInterface().DEBUG,
206: SessionUtilities
207: .getLoggingInterface().GENERALSTATUS,
208: "replacing table value for "
209: + tempKey);
210: localHash.put(tempKey, tempValue);
211: }
212: }
213: } catch (Exception ee) {
214: SessionUtilities
215: .getLoggingInterface()
216: .debugMsg(
217: SessionUtilities
218: .getLoggingInterface().ERROR,
219: SessionUtilities
220: .getLoggingInterface().GENERALSTATUS,
221: "Trouble loading properties file-> ");
222: SessionUtilities
223: .getLoggingInterface()
224: .logException(
225: SessionUtilities
226: .getLoggingInterface().ERROR,
227: ee);
228: }
229: } //end of while loop
230: } catch (Exception e) {
231: SessionUtilities
232: .getLoggingInterface()
233: .debugMsg(
234: SessionUtilities.getLoggingInterface().WARNING,
235: SessionUtilities.getLoggingInterface().GENERALSTATUS,
236: "Could not load properties file-> "
237: + fileName + "(" + e.getClass()
238: + ")");
239: //e.printStackTrace();
240: }
241: } else {
242: SessionUtilities
243: .getLoggingInterface()
244: .debugMsg(
245: SessionUtilities.getLoggingInterface().DEBUG,
246: SessionUtilities.getLoggingInterface().GENERALSTATUS,
247: "No properties file given");
248: }
249: }
250:
251: /**
252: * Modifies the properties hashtable [localHash] based on system properties.
253: * New properties are added in addition to old ones being modified.
254: *
255: */
256: protected void getPropertiesFromSystemProperties() {
257: SessionUtilities.getLoggingInterface().debugMsg(
258: SessionUtilities.getLoggingInterface().DEBUG,
259: SessionUtilities.getLoggingInterface().GENERALSTATUS,
260: "getPropertiesFromSystemProperties()...");
261: //read in properties from the system properties, and write them into the hashtable
262: // provided
263: Properties propertiesHash = System.getProperties();
264:
265: if (localHash != null) {
266: for (Enumeration propertiesEnum = propertiesHash.keys(); propertiesEnum
267: .hasMoreElements();) {
268: String tempKey = (String) propertiesEnum.nextElement();
269: localHash.put(tempKey, propertiesHash.get(tempKey));
270: }
271: }
272: }
273:
274: /**
275: * Modifies the properties hashtable [localHash] based on any command
276: * line options. If a property is not in the hashtable already, it is
277: * not added to the properties hashtable.
278: *
279: */
280: protected void getPropertiesFromCommandLine() {
281: if (commandArgs != null) {
282: SessionUtilities
283: .getLoggingInterface()
284: .debugMsg(
285: SessionUtilities.getLoggingInterface().DEBUG,
286: SessionUtilities.getLoggingInterface().GENERALSTATUS,
287: "Getting properties from command line...");
288: for (Enumeration paramsEnum = localHash.keys(); paramsEnum
289: .hasMoreElements();) {
290: String tempKey = (String) paramsEnum.nextElement();
291: if (getArg(commandArgs, tempKey) != null) {
292: SessionUtilities
293: .getLoggingInterface()
294: .debugMsg(
295: SessionUtilities
296: .getLoggingInterface().DEBUG,
297: SessionUtilities
298: .getLoggingInterface().GENERALSTATUS,
299: "setting "
300: + tempKey
301: + " to "
302: + getArg(commandArgs,
303: tempKey));
304: localHash
305: .put(tempKey, getArg(commandArgs, tempKey));
306: }
307: }
308: }
309: }
310:
311: /**
312: * Used to find the parameters as specified by getOptions
313: *
314: * @param args the command line args if this instance is an application,
315: * null otherwise
316: * @param arg String that specifies the parameter to be retrieved
317: * @return the value that corresponds to the specified parameter
318: *
319: */
320: private String getArg(String args[], String arg) {
321: String option = "-" + arg.toLowerCase();
322: String retval = null;
323: for (int i = 0; i < args.length; i++) {
324: if (args[i].toLowerCase().equals(option)) {
325: if (++i < args.length) {
326: retval = args[i];
327: }
328: break;
329: }
330: }
331: return (retval);
332: }
333:
334: }
|