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