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.openmobileis.embedded.util;
00031
00032 import org.openmobileis.common.util.collection.Array;
00033 import org.openmobileis.common.util.log.*;
00034
00035 import java.io.File;
00036 import java.util.Properties;
00037
00050 public class ServicePropertiesManager {
00051 private static ServicePropertiesManager manager;
00052 private final static String defaultPropsName = "misc.properties";
00053 private String propsFile;
00054 private java.util.Properties props = new Properties();
00055
00056 public ServicePropertiesManager(String propertiersFileName) {
00057 java.io.FileInputStream input = null;
00058 propsFile = propertiersFileName;
00059 try {
00060 input = new java.io.FileInputStream(propertiersFileName);
00061 props.load(input);
00062 } catch (java.io.IOException e) {
00063 LogManager.traceWarning(LogServices.WEBSERVICE, "ServicePropertiesManager :Can not load " + propsFile+". Create it.");
00064 props = new Properties();
00065 this.store();
00066 } finally {
00067 if (input != null) {
00068 try {
00069 input.close();
00070 } catch (Exception e) {
00071 LogManager.traceWarning(LogServices.WEBSERVICE, "ServicePropertiesManager :Can not close " + propsFile);
00072 }
00073 }
00074 }
00075 manager = this;
00076 }
00077
00078 private ServicePropertiesManager() {
00079 this(System.getProperty("user.dir") + File.separator + "WEB-INF"+ File.separator + "conf" + File.separator + defaultPropsName);
00080 }
00081
00082 public static ServicePropertiesManager getManager () {
00083 if (manager == null) {
00084 synchronized (ServicePropertiesManager.class) {
00085 if (manager == null) {
00086 manager = new ServicePropertiesManager();
00087 }
00088 }
00089 }
00090 return manager;
00091 }
00092
00097 public void saveProperty(String serviceName, String key, String value) {
00098 props.put(serviceName+key,value);
00099 this.store();
00100 }
00101
00106 public String getProperty(String serviceName, String key) {
00107 return (String)props.getProperty(serviceName+key);
00108 }
00109
00110 public Array getPropertyKeysForService(String serviceName) {
00111 Array ret = new Array(10);
00112 String key = null;
00113 for (java.util.Enumeration e = props.keys(); e.hasMoreElements();) {
00114 key = (String)e.nextElement();
00115 if (key.startsWith(serviceName)) {
00116 ret.add(key.substring(serviceName.length(), key.length()));
00117 }
00118 }
00119 return ret;
00120 }
00121
00122 public void deleteProperty(String serviceName, String key) {
00123 props.remove(serviceName+key);
00124 this.store();
00125 }
00126
00127 private void store() {
00128 java.io.FileOutputStream output = null;
00129 String key = null;
00130 String val = null;
00131 try {
00132 output = new java.io.FileOutputStream(propsFile);
00133
00134 for (java.util.Enumeration e = props.keys(); e.hasMoreElements();) {
00135 key = (String)e.nextElement();
00136 val = (String)props.get(key);
00137 output.write((key + "=" + val + "\n").getBytes());
00138 }
00139 output.flush();
00140 } catch (java.io.IOException e) {
00141 LogManager.traceError(LogServices.WEBSERVICE, "Impossible to store property file : " + propsFile + " key " + key + " value " + val);
00142
00143 } finally {
00144 if (output != null) {
00145 try {
00146 output.close();
00147 } catch (java.io.IOException e) {
00148 LogManager.traceError(LogServices.WEBSERVICE, "Impossible to close property file : " + propsFile);
00149 }
00150 }
00151 }
00152 }
00153
00154
00155 }