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 java.io.File;
00036 import java.util.Properties;
00037 
00050 public class PersistentPropertiesManager {
00051   private static PersistentPropertiesManager manager;
00052   private final static String defaultPropsName = "misc.properties";
00053   private String propsFile;
00054   private java.util.Properties props = new Properties();
00055 
00056         public PersistentPropertiesManager(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 PersistentPropertiesManager() {
00079             this(System.getProperty("user.dir") + File.separator + "WEB-INF"+ File.separator + "conf" + File.separator + defaultPropsName);
00080         }
00081 
00082   public static PersistentPropertiesManager getManager () {
00083     if (manager == null)  {
00084       synchronized (PersistentPropertiesManager.class) {
00085         if (manager == null)  {
00086           manager = new PersistentPropertiesManager();
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         // since the method #store is not available in jdk1.1.6, dot it manually
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 }

Generated on Mon Dec 4 11:03:28 2006 for OpenMobileIS by  doxygen 1.5.1-p1