1 28 29 30 package org.objectweb.jonas.common; 31 32 import java.io.BufferedWriter ; 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.FileNotFoundException ; 37 import java.io.FileWriter ; 38 import java.io.IOException ; 39 import java.util.Enumeration ; 40 import java.util.Hashtable ; 41 import java.util.Properties ; 42 import java.util.StringTokenizer ; 43 import javax.naming.Context ; 44 45 46 69 public class JProp { 70 71 74 public static final String JONASPREFIX = "jonas"; 75 76 79 public static final String DOMAIN_NAME = "domain.name"; 80 81 84 public static final String JONAS_NAME = "jonas.name"; 85 86 89 public static final String JONAS_DEF_NAME = "jonas"; 90 91 94 private static final String JONAS_BASE = "jonas.base"; 95 96 99 private static final String CONFIG_DIR = "conf"; 100 101 104 private static Properties systEnv = System.getProperties(); 105 106 109 private static String jonasBase = systEnv.getProperty(JONAS_BASE); 110 111 114 private static String installRoot = systEnv.getProperty("install.root"); 115 116 119 private static String fileSeparator = systEnv.getProperty("file.separator"); 120 121 124 private static String homeUser = systEnv.getProperty("user.home"); 125 126 129 private String configFileXml = null; 130 131 134 private Properties configFileEnv = new Properties (); 135 136 139 private Properties allEnv = null; 140 141 144 private String propFileName = null; 145 146 149 private static JProp unique = null; 150 151 155 private static Hashtable multiple = new Hashtable (); 156 157 162 private JProp(String fileName) throws Exception { 163 readFile(fileName); 164 } 165 166 170 private JProp() throws Exception { 171 readFile(JONASPREFIX); 172 } 173 174 180 private JProp(String fileName, Properties props) throws Exception { 181 writePropsToFile(fileName, props); 182 } 183 184 190 private JProp(String fileName, String txt) throws Exception { 191 writeXmlToFile(fileName, txt); 192 } 193 194 200 public static JProp getInstance() throws Exception { 201 if (unique == null) { 202 unique = new JProp(); 203 } 204 return unique; 205 } 206 207 214 public static JProp getInstance(String fileName) throws Exception { 215 if (!multiple.containsKey(fileName)) { 216 multiple.put(fileName, new JProp(fileName)); 217 } 218 return (JProp) multiple.get(fileName); 219 } 220 221 229 public static JProp getInstance(String fileName, Properties props) throws Exception { 230 if (!multiple.containsKey(fileName)) { 231 multiple.put(fileName, new JProp(fileName, props)); 232 } 233 return (JProp) multiple.get(fileName); 234 } 235 236 242 private void writePropsToFile(String fileName, Properties props) throws Exception { 243 244 if (jonasBase == null) { 246 throw new Exception ("JOnAS configuration error: environment property jonas.base not set!"); 247 } 248 jonasBase = jonasBase.trim(); 249 250 propFileName = jonasBase + fileSeparator + CONFIG_DIR + fileSeparator + fileName + ".properties"; 252 253 try { 254 FileOutputStream os = new FileOutputStream (propFileName); 255 props.store(os, "This file is generated by JOnAS"); 256 os.close(); 257 } catch (FileNotFoundException e) { 258 propFileName = null; 260 throw e; 261 } 262 configFileEnv = (Properties ) props.clone(); 263 allEnv = configFileEnv; 264 } 265 266 267 273 private void writeXmlToFile(String fileName, String txt) throws Exception { 274 275 if (jonasBase == null) { 277 throw new Exception ("JOnAS configuration error: environment property jonas.base not set!"); 278 } 279 jonasBase = jonasBase.trim(); 280 281 propFileName = jonasBase + fileSeparator + CONFIG_DIR + fileSeparator + fileName + ".properties"; 283 284 try { 285 BufferedWriter out = new BufferedWriter (new FileWriter (new File (propFileName))); 286 out.write(txt); 287 out.flush(); 288 out.close(); 289 } catch (FileNotFoundException e) { 290 propFileName = null; 292 throw e; 293 } 294 } 295 296 297 302 private void readFile(String fileName) throws Exception { 303 304 if (jonasBase == null) { 306 throw new Exception ("JOnAS configuration error: environment property jonas.base not set!"); 307 } 308 jonasBase = jonasBase.trim(); 309 310 String fileFullPathname = jonasBase + fileSeparator + CONFIG_DIR + fileSeparator + fileName; 312 313 if (fileFullPathname.toLowerCase().endsWith(".xml")) { 314 readXmlFile(fileFullPathname); 315 } else { 316 readPropsFile(fileFullPathname); 317 } 318 } 319 320 321 327 private void readPropsFile(String fileName) throws Exception { 328 329 this.propFileName = fileName; 331 332 if (!fileName.endsWith(".properties")) { 333 propFileName += ".properties"; 334 } 335 336 File f = null; 337 try { 338 f = new File (propFileName); 339 FileInputStream is = new FileInputStream (f); 340 configFileEnv.load(is); 341 } catch (FileNotFoundException e) { 342 throw new FileNotFoundException ("Cannot find properties for " + propFileName); 343 } catch (IOException e) { 344 System.err.println(e); 345 } 346 347 allEnv = (Properties ) configFileEnv.clone(); 348 if (f.getName().equalsIgnoreCase("jonas.properties")) { 350 for (Enumeration e = systEnv.keys(); e.hasMoreElements();) { 351 Object key = e.nextElement(); 352 String value = ((String ) systEnv.get(key)).trim(); 353 allEnv.put(key, (Object ) value); 354 } 355 356 String serverName; 357 if (!systEnv.containsKey(JONAS_NAME)) { 358 allEnv.put(JONAS_NAME, JONAS_DEF_NAME); 359 } 360 serverName = ((String ) allEnv.get(JONAS_NAME)).trim(); 361 362 if (!allEnv.containsKey(DOMAIN_NAME) && !systEnv.containsKey(DOMAIN_NAME)) { 363 allEnv.put(DOMAIN_NAME, serverName); 364 } 365 } 366 } 367 368 373 private void readXmlFile(String fileName) throws Exception { 374 375 this.propFileName = fileName; 377 378 try { 379 File f = new File (propFileName); 380 int length = (int) f.length(); 381 FileInputStream fis = new FileInputStream (f); 382 byte[] buffer = new byte[length]; 383 fis.read(buffer); 384 fis.close(); 385 configFileXml = new String (buffer); 386 } catch (FileNotFoundException e) { 387 throw new FileNotFoundException ("Cannot find file " + propFileName); 388 } catch (IOException e) { 389 System.err.println(e); 390 } 391 } 392 393 397 public static String getInstallRoot() { 398 if (installRoot == null) { 400 throw new RuntimeException ("JOnAS configuration error: System property install.root not set!"); 401 } 402 return installRoot; 403 } 404 405 409 public static String getJonasBase() { 410 return jonasBase; 411 } 412 413 417 public static String getWorkDir() { 418 return jonasBase + File.separator + "work"; 419 } 420 421 426 public String getPropFile() { 427 return propFileName; 428 } 429 430 435 public Properties getEnv() { 436 return allEnv; 437 } 438 439 440 445 public Properties getConfigFileEnv() { 446 return configFileEnv; 447 } 448 449 454 public String getConfigFileXml() { 455 return configFileXml; 456 } 457 458 464 public String getValue(String key, String defaultVal) { 465 String retProperty = allEnv.getProperty(key, defaultVal); 466 return retProperty.trim(); 467 } 468 469 475 public String getValue(String key) { 476 477 String retProperty = allEnv.getProperty(key); 478 if (retProperty != null) { 479 retProperty = retProperty.trim(); 480 } 481 return retProperty; 482 } 483 484 490 public boolean getValueAsBoolean(String key, boolean def) { 491 boolean ret = def; 492 String value = this.getValue(key); 493 if (value != null && value.equalsIgnoreCase("true")) { 494 ret = true; 495 } 496 return ret; 497 } 498 499 505 public String [] getValueAsArray(String key) { 506 507 String [] res = null; 508 String value = this.getValue(key); 509 if (value != null) { 510 StringTokenizer st = new StringTokenizer (value, ","); 511 res = new String [st.countTokens()]; 512 int i = 0; 513 while (st.hasMoreTokens()) { 514 res[i++] = st.nextToken().trim(); 515 } 516 } 517 return res; 518 } 519 520 524 public String toString() { 525 String s = new String (); 526 for (Enumeration e = this.configFileEnv.keys(); e.hasMoreElements();) { 527 Object key = e.nextElement(); 528 Object value = this.configFileEnv.get(key); 529 s = s.concat(" " + key + " = " + value + "\n"); 530 } 531 if (s.length() > 0) { 532 s = s.substring(0, s.length() - 1); 534 } 535 return s; 536 } 537 538 544 public void env2Ctx(Context ctx) throws Exception { 545 Enumeration e = configFileEnv.propertyNames(); 546 String key = null; 547 while (e.hasMoreElements()) { 548 key = (String ) e.nextElement(); 549 ctx.bind(key, configFileEnv.getProperty(key, "")); 550 } 551 } 552 553 558 public static void main(String args[]) { 559 560 JProp jonasProperties = null; 561 try { 562 jonasProperties = JProp.getInstance(); 563 } catch (Exception e) { 564 System.err.println(e); 565 System.exit(2); 566 } 567 for (Enumeration e = jonasProperties.configFileEnv.keys(); e.hasMoreElements();) { 568 Object key = e.nextElement(); 569 Object value = jonasProperties.configFileEnv.get(key); 570 System.out.println(key.toString() + "=" + value.toString()); 571 } 572 } 573 574 575 } 576 | Popular Tags |