PropertiesManager.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  *
00027  */
00028 
00029 package org.openmobileis.common.util;
00030 
00031 import java.util.Properties;
00032 import java.io.*;
00033 
00034 import org.openmobileis.common.context.ApplicationContextManager;
00035 import org.openmobileis.common.util.log.LogManager;
00036 
00056 public class PropertiesManager implements PropertiesService {
00057 
00058         public static PropertiesManager instance = null;
00059 
00060         public Properties globalProperties = new java.util.Properties();
00061 
00062         protected PropertiesManager() {
00063                 super();
00064         }
00065 
00069         public void removeAllProperties() {
00070                 globalProperties = new java.util.Properties();
00071         }
00072 
00077         public static PropertiesManager getManager() {
00078                 if (instance == null) {
00079                         synchronized (PropertiesManager.class) {
00080                                 if (instance == null) {
00081                                         instance = new PropertiesManager();
00082                                         ApplicationContextManager.getManager().addManager(instance);
00083                                 }
00084                         }
00085                 }
00086                 return instance;
00087         }
00088 
00095         public static void registerInstance(PropertiesManager manager) {
00096                 if (instance == null) {
00097                         instance = manager;
00098                         ApplicationContextManager.getManager().addManager(instance);
00099         } else  {
00100                 LogManager.traceDebug(0, "PropertiesManager registerManager already done no use");
00101         }
00102         }
00103 
00104         public java.io.InputStream getRessourceAsStream(String name) throws java.io.IOException {
00105                 java.io.InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
00106                 return in;
00107         }
00108 
00119         public void addPropertiesFile(String filename) throws IOException {
00120       InputStream in = null;
00121       try   {
00122         in = this.getRessourceAsStream("/properties" + filename);
00123         if (in == null) in = this.getRessourceAsStream(filename);
00124         if (in == null) in = new FileInputStream(filename);
00125       } catch (IOException ex)  {
00126         try {
00127           in = this.getRessourceAsStream(filename);
00128         } catch (IOException exi)    {
00129           in = new FileInputStream(filename);
00130         }
00131       }
00132         try {
00133                 globalProperties.load(this.convertPropertiesFile(in));
00134         } finally {
00135                 in.close();
00136         }
00137 
00138         }
00139 
00147         public void addPropertiesFileFromFilePath(String filename)
00148                         throws java.io.IOException {
00149                 InputStream in = null;
00150                 File file  = new File(filename);
00151                 if (file.exists()) in = new java.io.FileInputStream(filename);
00152                 else in = this.getRessourceAsStream(filename);
00153                 try {
00154                         globalProperties.load(this.convertPropertiesFile(in));
00155                 } finally {
00156                         in.close();
00157                 }
00158         }
00159 
00171         public java.util.Properties getProperties(String prop_file)
00172                         throws java.io.IOException {
00173                 Properties prop = new Properties();
00174         InputStream in = null;
00175         try {
00176           in = this.getRessourceAsStream("/properties" + prop_file);
00177           if (in == null) in = this.getRessourceAsStream(prop_file);
00178           if (in == null) in = new FileInputStream(prop_file);
00179        } catch (IOException ex)    {
00180           try   {
00181             in = this.getRessourceAsStream(prop_file);
00182           } catch (IOException exi)  {
00183             in = new FileInputStream(prop_file);
00184           }
00185         }
00186                 try {
00187                         prop.load(this.convertPropertiesFile(in));
00188                 } finally {
00189                         in.close();
00190                 }
00191                 return prop;
00192         }
00193 
00201         public java.util.Properties getPropertiesFromPath(String prop_file)     throws java.io.IOException {
00202                 System.out.println("getPropertiesFromPath "+prop_file);
00203                 Properties prop = new Properties();
00204                 InputStream in = null;
00205                 File file  = new File(prop_file);
00206                 if (file.exists()) in = new java.io.FileInputStream(prop_file);
00207                 else in = this.getRessourceAsStream(prop_file);
00208                 try {
00209                         prop.load(this.convertPropertiesFile(new BufferedInputStream(in)));
00210                 } finally {
00211                         in.close();
00212                 }
00213                 return prop;
00214         }
00215 
00221         public java.util.Properties getProperties() {
00222                 return globalProperties;
00223         }
00224 
00233         public String getProperty(String key, String defaultValue) {
00234                 return globalProperties.getProperty(key, defaultValue);
00235         }
00236 
00244         public String getProperty(String key) {
00245                 return globalProperties.getProperty(key);
00246         }
00247 
00254         public void addProperty(String key, String value) {
00255                 globalProperties.put(key, value);
00256         }
00257 
00258         protected InputStream convertPropertiesFile(InputStream inStream)
00259                         throws java.io.IOException {
00260                 int r;
00261                 char c;
00262                 ByteArrayOutputStream out = new ByteArrayOutputStream();
00263                 StringBuffer propertyName = new StringBuffer();
00264 
00265                 boolean readPropName = false;
00266                 while ((r = inStream.read()) != -1) {
00267                         c = (char) r;
00268                         if (c == '$') {
00269                                 readPropName = true;
00270                                 propertyName.setLength(0);
00271                                 continue;
00272                         } else if (readPropName) {
00273                                 if (((c >= 'a') && (c <= 'z')) || (c == '.')
00274                                                 || ((c >= 'A') && (c <= 'Z'))
00275                                                 || ((c >= '0') && (c <= '9'))) { // test end of the property. Property use normal caractere and dot (.)
00276                                         propertyName.append(c);
00277                                         continue;
00278                                 } else { // end property name
00279                                         readPropName = false;
00280                                         String propName = propertyName.toString();
00281                                         String prop = System.getProperty(propName);
00282                                         if (prop == null) {
00283                                                 prop = globalProperties.getProperty(propName, "null");
00284                                         }
00285 
00286                                         // test if the property contains anti slash value. They must be doubled because
00287                                         // property load remove slash. PB found when using user.home property on windows
00288                                         int index = prop.indexOf("\\");
00289                                         if (index != -1) {
00290                                                 byte[] tab = prop.getBytes();
00291                                                 for (int i = 0; i < tab.length; i++) {
00292                                                         if (tab[i] == '\\') {
00293                                                                 out.write('\\');
00294                                                         }
00295                                                         out.write(tab[i]);
00296                                                 }
00297                                         } else {
00298                                                 out.write(prop.getBytes());
00299                                         }
00300                                 }
00301                         }
00302                         out.write(c);
00303                 }
00304                 ByteArrayInputStream retin = new ByteArrayInputStream(out.toByteArray());
00305                 return retin;
00306         }
00307 
00308 }

Generated on Tue May 22 23:01:11 2007 for OpenMobileIS by  doxygen 1.5.1-p1