KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > datashare > plugins > PropertiesManager > PropertiesManagerAdapter


1 /* ----- BEGIN LICENSE BLOCK -----
2  * Version: MPL 1.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the DataShare server.
15  *
16  * The Initial Developer of the Original Code is
17  * Ball Aerospace & Technologies Corp, Fairborn, Ohio
18  * Portions created by the Initial Developer are Copyright (C) 2001
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s): Charles Wood <cwood@ball.com>
22  * Bart Carlton <bcarlton@ball.com>
23  *
24  * ----- END LICENSE BLOCK ----- */

25 /* RCS $Id: PropertiesManagerAdapter.java,v 1.2 2002/01/29 20:58:52 lizellaman Exp $
26  * $Log: PropertiesManagerAdapter.java,v $
27  * Revision 1.2 2002/01/29 20:58:52 lizellaman
28  * Added LoggingInterface, modified the PropertiesInterface implementation
29  *
30  * Revision 1.1.1.1 2001/10/23 13:37:20 lizellaman
31  * initial sourceforge release
32  *
33  */

34
35 package org.datashare.plugins.PropertiesManager;
36
37 import java.util.Hashtable JavaDoc;
38 import java.util.Properties JavaDoc;
39 import java.util.Enumeration JavaDoc;
40 import java.io.BufferedReader JavaDoc;
41 import java.io.FileReader JavaDoc;
42 import java.net.URL JavaDoc;
43 import java.util.StringTokenizer JavaDoc;
44 import java.lang.Exception JavaDoc;
45
46 import org.datashare.PropertiesInterface;
47 import org.datashare.SessionUtilities;
48
49 /**
50  * Generic version of the PropertiesInterface. It uses three sources of
51  * properties to modify the initial values passed in through the hashTable
52  * parameter of the setParameters method. There are three sources of
53  * properties. The first is from a file if a key called fileName is contained
54  * in the hashtable provided. The second source of properties is the system
55  * properties, and finally they can be provided at the command line as well.
56  *
57  * @author Bart Carlton (Charles Wood)
58  * @date January-29-2002
59  * @version 2.0
60  */

61 public class PropertiesManagerAdapter implements PropertiesInterface
62   {
63   protected Hashtable JavaDoc localHash = null;
64   protected String JavaDoc[] commandArgs = null;
65   private String JavaDoc fileNameKey = "propertiesFile"; // the parameter that contains our properties file name, if any
66
private String JavaDoc fileName = null;
67
68   /**
69    * constructor
70    *
71    */

72   public PropertiesManagerAdapter()
73     {
74     }
75
76  /**
77    * Sets the hashtable containing the default properties to be used
78    *
79    * param localHash the hashtable that contains the intial properties
80    * list and their default values
81    *
82    */

83   public void setParameters(Hashtable JavaDoc localHash)
84      {
85      this.localHash = localHash;
86      }
87
88  /**
89    * Sets the command line arguments string to be used for altering
90    * values in the local hashtable.
91    *
92    * param args the string array that is to be processed as the
93    * command line arguments
94    *
95    */

96   public void setCommandLineArgs(String JavaDoc[] args)
97     {
98     commandArgs = args;
99     }
100
101   /**
102    * Returns the modified hashtable after setting properties based on the property
103    * file provided in the hahstable, the system properties
104    * and the command line arguments string if it has been set.
105    *
106    * Returns hashtable with the newly set values
107    *
108    */

109   public Hashtable JavaDoc updateProperties()
110     {
111     SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
112                        SessionUtilities.getLoggingInterface().GENERALSTATUS,
113                        "updateProperties()...");
114     String JavaDoc fileName2 = fileName; // so we can tell if the properties manager file gets changed (so we can load new file)
115
getPropertiesFromFile();
116     getPropertiesFromSystemProperties();
117     getPropertiesFromCommandLine();
118     // if new 'propertiesFile' specified, run again
119
if(localHash.containsKey(fileNameKey) &&
120                        !((String JavaDoc)localHash.get(fileNameKey)).equals(fileName2) )
121        {
122        getPropertiesFromFile();
123        getPropertiesFromSystemProperties();
124        getPropertiesFromCommandLine();
125        }
126
127     return localHash;
128     }
129
130   /**
131    * Modifies the properties hashtable [localHash] based on a
132    * properties file. The filename must be provided in the hashtable
133    * with its key as "fileName". New properties are added in addition
134    * to old ones being modified.
135    *
136    */

137   protected void getPropertiesFromFile()
138     {
139     SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
140                     SessionUtilities.getLoggingInterface().GENERALSTATUS,
141                     "getPropertiesFromFile()...");
142     String JavaDoc line;
143     int tokenCount = 0;
144     BufferedReader JavaDoc bin = null;
145
146     if(localHash != null
147              && localHash.containsKey(fileNameKey)
148              && localHash.get(fileNameKey) != null
149              && !((String JavaDoc)localHash.get(fileNameKey)).equals(""))
150       {
151       //read in properties from the named file and write them into the hashtable provided
152
try
153         {
154         boolean finished = false;
155         String JavaDoc dir = "";
156         String JavaDoc path = System.getProperty("java.class.path",".");
157         StringTokenizer JavaDoc st2 = new StringTokenizer JavaDoc(path,System.getProperty("path.separator").toString());
158         
159         while(!finished)
160            {
161            fileName = dir + (String JavaDoc)localHash.get(fileNameKey);
162            try{
163               bin = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
164               finished = true;
165               }
166            catch(Exception JavaDoc e)
167               {
168               }
169            if(st2.hasMoreTokens())
170               dir = st2.nextToken() + System.getProperty("file.separator").toString();
171            else
172               finished = true;
173            }
174         SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
175               SessionUtilities.getLoggingInterface().GENERALSTATUS,
176               "Loaded properties file-> " + fileName);
177         while ((line=bin.readLine())!= null)
178           {
179           try
180             {
181             // get all of line up to first comment character
182
String JavaDoc shortString;
183             if(line.indexOf("#") != -1)
184                shortString = line.substring(0,line.indexOf("#"));
185             else
186                shortString = new String JavaDoc(line);
187             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(shortString, "=");
188             tokenCount = st.countTokens();
189             if (tokenCount >= 2)
190               {
191               String JavaDoc tempKey = st.nextToken().trim();
192               String JavaDoc tempValue = st.nextToken().trim(); //gets at least the beginning of the value
193
//// if the properties value contains an "=" it must be added back in and
194
//// the rest of the property added to tempValue
195
//while (st.hasMoreTokens())
196
// {
197
// tempValue = tempValue + "=" + st.nextToken().trim();
198
// }
199

200               // cannot add new variable, only overwrite old value of variable already in table
201
if (localHash != null && localHash.containsKey(tempKey) )
202                 {
203                 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
204                     SessionUtilities.getLoggingInterface().GENERALSTATUS,
205                     "replacing table value for " + tempKey);
206                 localHash.put(tempKey, tempValue);
207                 }
208               }
209             }
210           catch(Exception JavaDoc ee)
211             {
212             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
213                  SessionUtilities.getLoggingInterface().GENERALSTATUS,
214                  "Trouble loading properties file-> ");
215             SessionUtilities.getLoggingInterface().logException(SessionUtilities.getLoggingInterface().ERROR, ee);
216             }
217           } //end of while loop
218
}
219       catch(Exception JavaDoc e)
220         {
221         SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING,
222                    SessionUtilities.getLoggingInterface().GENERALSTATUS,
223                    "Could not load properties file-> " + fileName + "("+e.getClass()+")");
224         //e.printStackTrace();
225
}
226       }
227     else
228       {
229       SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
230          SessionUtilities.getLoggingInterface().GENERALSTATUS,
231          "No properties file given");
232       }
233     }
234
235   /**
236    * Modifies the properties hashtable [localHash] based on system properties.
237    * New properties are added in addition to old ones being modified.
238    *
239    */

240   protected void getPropertiesFromSystemProperties()
241     {
242     SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
243        SessionUtilities.getLoggingInterface().GENERALSTATUS,
244        "getPropertiesFromSystemProperties()...");
245     //read in properties from the system properties, and write them into the hashtable
246
// provided
247
Properties JavaDoc propertiesHash = System.getProperties();
248
249     if(localHash != null)
250        {
251        for(Enumeration JavaDoc propertiesEnum = propertiesHash.keys(); propertiesEnum.hasMoreElements();)
252          {
253          String JavaDoc tempKey = (String JavaDoc)propertiesEnum.nextElement();
254          localHash.put(tempKey, propertiesHash.get(tempKey));
255          }
256        }
257     }
258
259   /**
260    * Modifies the properties hashtable [localHash] based on any command
261    * line options. If a property is not in the hashtable already, it is
262    * not added to the properties hashtable.
263    *
264    */

265   protected void getPropertiesFromCommandLine()
266     {
267     if (commandArgs != null)
268       {
269       SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
270          SessionUtilities.getLoggingInterface().GENERALSTATUS,
271          "Getting properties from command line...");
272       for(Enumeration JavaDoc paramsEnum = localHash.keys(); paramsEnum.hasMoreElements();)
273         {
274         String JavaDoc tempKey = (String JavaDoc)paramsEnum.nextElement();
275         if (getArg(commandArgs, tempKey) != null)
276           {
277           SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
278              SessionUtilities.getLoggingInterface().GENERALSTATUS,
279              "setting " + tempKey + " to " + getArg(commandArgs, tempKey));
280           localHash.put(tempKey, getArg(commandArgs, tempKey));
281           }
282         }
283       }
284     }
285
286
287   /**
288    * Used to find the parameters as specified by getOptions
289    *
290    * @param args the command line args if this instance is an application,
291    * null otherwise
292    * @param arg String that specifies the parameter to be retrieved
293    * @return the value that corresponds to the specified parameter
294    *
295    */

296   private String JavaDoc
297   getArg(String JavaDoc args[], String JavaDoc arg)
298     {
299     String JavaDoc option = "-" + arg.toLowerCase();
300     String JavaDoc retval = null;
301     for (int i = 0 ; i < args.length ; i++)
302       {
303       if (args[i].toLowerCase().equals(option))
304         {
305         if (++i < args.length)
306           {
307           retval = args[i];
308           }
309         break;
310         }
311       }
312     return(retval);
313     }
314
315   }
316
317
Popular Tags