00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package org.objectweb.jonas.common;
00031
00032 import java.io.BufferedWriter;
00033 import java.io.File;
00034 import java.io.FileInputStream;
00035 import java.io.FileOutputStream;
00036 import java.io.FileNotFoundException;
00037 import java.io.FileWriter;
00038 import java.io.IOException;
00039 import java.util.Enumeration;
00040 import java.util.Hashtable;
00041 import java.util.Properties;
00042 import java.util.StringTokenizer;
00043 import javax.naming.Context;
00044
00045
00069 public class JProp {
00070
00074 public static final String JONASPREFIX = "jonas";
00075
00079 public static final String DOMAIN_NAME = "domain.name";
00080
00084 public static final String JONAS_NAME = "jonas.name";
00085
00089 public static final String JONAS_DEF_NAME = "jonas";
00090
00094 private static final String JONAS_BASE = "jonas.base";
00095
00099 private static final String CONFIG_DIR = "conf";
00100
00104 private static Properties systEnv = System.getProperties();
00105
00109 private static String jonasBase = systEnv.getProperty(JONAS_BASE);
00110
00114 private static String installRoot = systEnv.getProperty("install.root");
00115
00119 private static String fileSeparator = systEnv.getProperty("file.separator");
00120
00124 private static String homeUser = systEnv.getProperty("user.home");
00125
00129 private String configFileXml = null;
00130
00134 private Properties configFileEnv = new Properties();
00135
00139 private Properties allEnv = null;
00140
00144 private String propFileName = null;
00145
00149 private static JProp unique = null;
00150
00155 private static Hashtable multiple = new Hashtable();
00156
00162 private JProp(String fileName) throws Exception {
00163 readFile(fileName);
00164 }
00165
00170 private JProp() throws Exception {
00171 readFile(JONASPREFIX);
00172 }
00173
00180 private JProp(String fileName, Properties props) throws Exception {
00181 writePropsToFile(fileName, props);
00182 }
00183
00190 private JProp(String fileName, String txt) throws Exception {
00191 writeXmlToFile(fileName, txt);
00192 }
00193
00200 public static JProp getInstance() throws Exception {
00201 if (unique == null) {
00202 unique = new JProp();
00203 }
00204 return unique;
00205 }
00206
00214 public static JProp getInstance(String fileName) throws Exception {
00215 if (!multiple.containsKey(fileName)) {
00216 multiple.put(fileName, new JProp(fileName));
00217 }
00218 return (JProp) multiple.get(fileName);
00219 }
00220
00229 public static JProp getInstance(String fileName, Properties props) throws Exception {
00230 if (!multiple.containsKey(fileName)) {
00231 multiple.put(fileName, new JProp(fileName, props));
00232 }
00233 return (JProp) multiple.get(fileName);
00234 }
00235
00242 private void writePropsToFile(String fileName, Properties props) throws Exception {
00243
00244
00245 if (jonasBase == null) {
00246 throw new Exception("JOnAS configuration error: environment property jonas.base not set!");
00247 }
00248 jonasBase = jonasBase.trim();
00249
00250
00251 propFileName = jonasBase + fileSeparator + CONFIG_DIR + fileSeparator + fileName + ".properties";
00252
00253 try {
00254 FileOutputStream os = new FileOutputStream(propFileName);
00255 props.store(os, "This file is generated by JOnAS");
00256 os.close();
00257 } catch (FileNotFoundException e) {
00258
00259 propFileName = null;
00260 throw e;
00261 }
00262 configFileEnv = (Properties) props.clone();
00263 allEnv = configFileEnv;
00264 }
00265
00266
00273 private void writeXmlToFile(String fileName, String txt) throws Exception {
00274
00275
00276 if (jonasBase == null) {
00277 throw new Exception("JOnAS configuration error: environment property jonas.base not set!");
00278 }
00279 jonasBase = jonasBase.trim();
00280
00281
00282 propFileName = jonasBase + fileSeparator + CONFIG_DIR + fileSeparator + fileName + ".properties";
00283
00284 try {
00285 BufferedWriter out = new BufferedWriter(new FileWriter(new File(propFileName)));
00286 out.write(txt);
00287 out.flush();
00288 out.close();
00289 } catch (FileNotFoundException e) {
00290
00291 propFileName = null;
00292 throw e;
00293 }
00294 }
00295
00296
00302 private void readFile(String fileName) throws Exception {
00303
00304
00305 if (jonasBase == null) {
00306 throw new Exception("JOnAS configuration error: environment property jonas.base not set!");
00307 }
00308 jonasBase = jonasBase.trim();
00309
00310
00311 String fileFullPathname = jonasBase + fileSeparator + CONFIG_DIR + fileSeparator + fileName;
00312
00313 if (fileFullPathname.toLowerCase().endsWith(".xml")) {
00314 readXmlFile(fileFullPathname);
00315 } else {
00316 readPropsFile(fileFullPathname);
00317 }
00318 }
00319
00320
00327 private void readPropsFile(String fileName) throws Exception {
00328
00329
00330 this.propFileName = fileName;
00331
00332 if (!fileName.endsWith(".properties")) {
00333 propFileName += ".properties";
00334 }
00335
00336 File f = null;
00337 try {
00338 f = new File(propFileName);
00339 FileInputStream is = new FileInputStream(f);
00340 configFileEnv.load(is);
00341 } catch (FileNotFoundException e) {
00342 throw new FileNotFoundException("Cannot find properties for " + propFileName);
00343 } catch (IOException e) {
00344 System.err.println(e);
00345 }
00346
00347 allEnv = (Properties) configFileEnv.clone();
00348
00349 if (f.getName().equalsIgnoreCase("jonas.properties")) {
00350 for (Enumeration e = systEnv.keys(); e.hasMoreElements();) {
00351 Object key = e.nextElement();
00352 String value = ((String) systEnv.get(key)).trim();
00353 allEnv.put(key, (Object) value);
00354 }
00355
00356 String serverName;
00357 if (!systEnv.containsKey(JONAS_NAME)) {
00358 allEnv.put(JONAS_NAME, JONAS_DEF_NAME);
00359 }
00360 serverName = ((String) allEnv.get(JONAS_NAME)).trim();
00361
00362 if (!allEnv.containsKey(DOMAIN_NAME) && !systEnv.containsKey(DOMAIN_NAME)) {
00363 allEnv.put(DOMAIN_NAME, serverName);
00364 }
00365 }
00366 }
00367
00373 private void readXmlFile(String fileName) throws Exception {
00374
00375
00376 this.propFileName = fileName;
00377
00378 try {
00379 File f = new File(propFileName);
00380 int length = (int) f.length();
00381 FileInputStream fis = new FileInputStream(f);
00382 byte[] buffer = new byte[length];
00383 fis.read(buffer);
00384 fis.close();
00385 configFileXml = new String(buffer);
00386 } catch (FileNotFoundException e) {
00387 throw new FileNotFoundException("Cannot find file " + propFileName);
00388 } catch (IOException e) {
00389 System.err.println(e);
00390 }
00391 }
00392
00397 public static String getInstallRoot() {
00398
00399 if (installRoot == null) {
00400 throw new RuntimeException("JOnAS configuration error: System property install.root not set!");
00401 }
00402 return installRoot;
00403 }
00404
00409 public static String getJonasBase() {
00410 return jonasBase;
00411 }
00412
00417 public static String getWorkDir() {
00418 return jonasBase + File.separator + "work";
00419 }
00420
00426 public String getPropFile() {
00427 return propFileName;
00428 }
00429
00435 public Properties getEnv() {
00436 return allEnv;
00437 }
00438
00439
00445 public Properties getConfigFileEnv() {
00446 return configFileEnv;
00447 }
00448
00454 public String getConfigFileXml() {
00455 return configFileXml;
00456 }
00457
00464 public String getValue(String key, String defaultVal) {
00465 String retProperty = allEnv.getProperty(key, defaultVal);
00466 return retProperty.trim();
00467 }
00468
00475 public String getValue(String key) {
00476
00477 String retProperty = allEnv.getProperty(key);
00478 if (retProperty != null) {
00479 retProperty = retProperty.trim();
00480 }
00481 return retProperty;
00482 }
00483
00490 public boolean getValueAsBoolean(String key, boolean def) {
00491 boolean ret = def;
00492 String value = this.getValue(key);
00493 if (value != null && value.equalsIgnoreCase("true")) {
00494 ret = true;
00495 }
00496 return ret;
00497 }
00498
00505 public String[] getValueAsArray(String key) {
00506
00507 String [] res = null;
00508 String value = this.getValue(key);
00509 if (value != null) {
00510 StringTokenizer st = new StringTokenizer(value, ",");
00511 res = new String [st.countTokens()];
00512 int i = 0;
00513 while (st.hasMoreTokens()) {
00514 res[i++] = st.nextToken().trim();
00515 }
00516 }
00517 return res;
00518 }
00519
00524 public String toString() {
00525 String s = new String();
00526 for (Enumeration e = this.configFileEnv.keys(); e.hasMoreElements();) {
00527 Object key = e.nextElement();
00528 Object value = this.configFileEnv.get(key);
00529 s = s.concat(" " + key + " = " + value + "\n");
00530 }
00531 if (s.length() > 0) {
00532
00533 s = s.substring(0, s.length() - 1);
00534 }
00535 return s;
00536 }
00537
00544 public void env2Ctx(Context ctx) throws Exception {
00545 Enumeration e = configFileEnv.propertyNames();
00546 String key = null;
00547 while (e.hasMoreElements()) {
00548 key = (String) e.nextElement();
00549 ctx.bind(key, configFileEnv.getProperty(key, ""));
00550 }
00551 }
00552
00558 public static void main(String args[]) {
00559
00560 JProp jonasProperties = null;
00561 try {
00562 jonasProperties = JProp.getInstance();
00563 } catch (Exception e) {
00564 System.err.println(e);
00565 System.exit(2);
00566 }
00567 for (Enumeration e = jonasProperties.configFileEnv.keys(); e.hasMoreElements();) {
00568 Object key = e.nextElement();
00569 Object value = jonasProperties.configFileEnv.get(key);
00570 System.out.println(key.toString() + "=" + value.toString());
00571 }
00572 }
00573
00574
00575 }