Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

ServicePropertiesManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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.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         // since the method #store is not available in jdk1.1.6, dot it manually
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 }

Generated on Thu Oct 6 10:06:33 2005 for OpenMobileIS by  doxygen 1.4.3