PersistentPropertiesManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
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()) { //test if directory exist and create it.
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                         // since the method #store is not available in jdk1.1.6, dot it manually
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 }

Generated on Mon Jan 11 21:19:16 2010 for OpenMobileIS by  doxygen 1.5.4