1 25 34 35 package org.datashare.plugins.PropertiesManager; 36 37 import java.util.Hashtable ; 38 import java.util.Properties ; 39 import java.util.Enumeration ; 40 import java.io.BufferedReader ; 41 import java.io.FileReader ; 42 import java.net.URL ; 43 import java.util.StringTokenizer ; 44 import java.lang.Exception ; 45 46 import org.datashare.PropertiesInterface; 47 import org.datashare.SessionUtilities; 48 49 61 public class PropertiesManagerAdapter implements PropertiesInterface 62 { 63 protected Hashtable localHash = null; 64 protected String [] commandArgs = null; 65 private String fileNameKey = "propertiesFile"; private String fileName = null; 67 68 72 public PropertiesManagerAdapter() 73 { 74 } 75 76 83 public void setParameters(Hashtable localHash) 84 { 85 this.localHash = localHash; 86 } 87 88 96 public void setCommandLineArgs(String [] args) 97 { 98 commandArgs = args; 99 } 100 101 109 public Hashtable updateProperties() 110 { 111 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 112 SessionUtilities.getLoggingInterface().GENERALSTATUS, 113 "updateProperties()..."); 114 String fileName2 = fileName; getPropertiesFromFile(); 116 getPropertiesFromSystemProperties(); 117 getPropertiesFromCommandLine(); 118 if(localHash.containsKey(fileNameKey) && 120 !((String )localHash.get(fileNameKey)).equals(fileName2) ) 121 { 122 getPropertiesFromFile(); 123 getPropertiesFromSystemProperties(); 124 getPropertiesFromCommandLine(); 125 } 126 127 return localHash; 128 } 129 130 137 protected void getPropertiesFromFile() 138 { 139 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 140 SessionUtilities.getLoggingInterface().GENERALSTATUS, 141 "getPropertiesFromFile()..."); 142 String line; 143 int tokenCount = 0; 144 BufferedReader bin = null; 145 146 if(localHash != null 147 && localHash.containsKey(fileNameKey) 148 && localHash.get(fileNameKey) != null 149 && !((String )localHash.get(fileNameKey)).equals("")) 150 { 151 try 153 { 154 boolean finished = false; 155 String dir = ""; 156 String path = System.getProperty("java.class.path","."); 157 StringTokenizer st2 = new StringTokenizer (path,System.getProperty("path.separator").toString()); 158 159 while(!finished) 160 { 161 fileName = dir + (String )localHash.get(fileNameKey); 162 try{ 163 bin = new BufferedReader (new FileReader (fileName)); 164 finished = true; 165 } 166 catch(Exception 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 String shortString; 183 if(line.indexOf("#") != -1) 184 shortString = line.substring(0,line.indexOf("#")); 185 else 186 shortString = new String (line); 187 StringTokenizer st = new StringTokenizer (shortString, "="); 188 tokenCount = st.countTokens(); 189 if (tokenCount >= 2) 190 { 191 String tempKey = st.nextToken().trim(); 192 String tempValue = st.nextToken().trim(); 200 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 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 } } 219 catch(Exception e) 220 { 221 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING, 222 SessionUtilities.getLoggingInterface().GENERALSTATUS, 223 "Could not load properties file-> " + fileName + "("+e.getClass()+")"); 224 } 226 } 227 else 228 { 229 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 230 SessionUtilities.getLoggingInterface().GENERALSTATUS, 231 "No properties file given"); 232 } 233 } 234 235 240 protected void getPropertiesFromSystemProperties() 241 { 242 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 243 SessionUtilities.getLoggingInterface().GENERALSTATUS, 244 "getPropertiesFromSystemProperties()..."); 245 Properties propertiesHash = System.getProperties(); 248 249 if(localHash != null) 250 { 251 for(Enumeration propertiesEnum = propertiesHash.keys(); propertiesEnum.hasMoreElements();) 252 { 253 String tempKey = (String )propertiesEnum.nextElement(); 254 localHash.put(tempKey, propertiesHash.get(tempKey)); 255 } 256 } 257 } 258 259 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 paramsEnum = localHash.keys(); paramsEnum.hasMoreElements();) 273 { 274 String tempKey = (String )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 296 private String 297 getArg(String args[], String arg) 298 { 299 String option = "-" + arg.toLowerCase(); 300 String 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 |