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.common.util;
00031
00032 import org.openmobileis.common.util.collection.Array;
00033 import org.openmobileis.common.util.log.*;
00034
00035 import freemarker.template.instruction.ExitInstruction;
00036
00037 import java.io.File;
00038 import java.util.Properties;
00039
00052 public class PersistentPropertiesManager {
00053 private static PersistentPropertiesManager manager;
00054 private final static String defaultPropsName = "misc.properties";
00055 private String propsFile;
00056 private java.util.Properties props = new Properties();
00057
00058 public PersistentPropertiesManager(String propertiersFileName) {
00059 this.initPersistentFilePath(propertiersFileName);
00060 manager = this;
00061 }
00062
00063 private PersistentPropertiesManager() {
00064 String propsFilePath = PropertiesManager.getManager().getProperty("org.openmobileis.common.persistentpropertiesmanager.propfile");
00065 if (propsFilePath == null) {
00066 propsFilePath = System.getProperty("user.dir") + File.separator + "WEB-INF" + File.separator + "conf" + File.separator + defaultPropsName;
00067 }
00068 this.initPersistentFilePath(propsFilePath);
00069 manager = this;
00070 }
00071
00072 private void initPersistentFilePath(String propertiersFileName) {
00073 java.io.FileInputStream input = null;
00074 propsFile = propertiersFileName;
00075 try {
00076 input = new java.io.FileInputStream(propertiersFileName);
00077 props.load(input);
00078 } catch (java.io.IOException e) {
00079 LogManager.traceWarning(LogServices.WEBSERVICE, "ServicePropertiesManager :Can not load " + propsFile + ". Create it.");
00080 props = new Properties();
00081 this.store();
00082 } finally {
00083 if (input != null) {
00084 try {
00085 input.close();
00086 } catch (Exception e) {
00087 LogManager.traceWarning(LogServices.WEBSERVICE, "ServicePropertiesManager :Can not close " + propsFile);
00088 }
00089 }
00090 }
00091 }
00092
00093 public static PersistentPropertiesManager getManager() {
00094 if (manager == null) {
00095 synchronized (PersistentPropertiesManager.class) {
00096 if (manager == null) {
00097 manager = new PersistentPropertiesManager();
00098 }
00099 }
00100 }
00101 return manager;
00102 }
00103
00108 public void saveProperty(String serviceName, String key, String value) {
00109 props.put(serviceName + key, value);
00110 this.store();
00111 }
00112
00117 public String getProperty(String serviceName, String key) {
00118 return (String) props.getProperty(serviceName + key);
00119 }
00120
00121 public Array getPropertyKeysForService(String serviceName) {
00122 Array ret = new Array(10);
00123 String key = null;
00124 for (java.util.Enumeration e = props.keys(); e.hasMoreElements();) {
00125 key = (String) e.nextElement();
00126 if (key.startsWith(serviceName)) {
00127 ret.add(key.substring(serviceName.length(), key.length()));
00128 }
00129 }
00130 return ret;
00131 }
00132
00133 public void deleteProperty(String serviceName, String key) {
00134 props.remove(serviceName + key);
00135 this.store();
00136 }
00137
00138 private void store() {
00139 java.io.FileOutputStream output = null;
00140 String key = null;
00141 String val = null;
00142 try {
00143 File file = new File(this.propsFile);
00144 if (!file.exists()) {
00145 int index = this.propsFile.lastIndexOf(File.separator);
00146 if (index != -1) {
00147 String dir = this.propsFile.substring(0, index);
00148 file = new File(dir);
00149 if (!file.exists()) {
00150 file.mkdirs();
00151 }
00152 }
00153 }
00154 output = new java.io.FileOutputStream(this.propsFile);
00155
00156 for (java.util.Enumeration e = props.keys(); e.hasMoreElements();) {
00157 key = (String) e.nextElement();
00158 val = (String) props.get(key);
00159 output.write((key + "=" + val + "\n").getBytes());
00160 }
00161 output.flush();
00162 } catch (java.io.IOException e) {
00163 LogManager.traceError(LogServices.WEBSERVICE, "PersistentPropertiesManager Impossible to store property file. Error :" + e.toString());
00164 } finally {
00165 if (output != null) {
00166 try {
00167 output.close();
00168 } catch (java.io.IOException e) {
00169 LogManager.traceError(LogServices.WEBSERVICE, "Impossible to close property file : " + propsFile);
00170 }
00171 }
00172 }
00173 }
00174
00175 }