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 
00055 public class PropertiesManager {
00056 
00057         public static PropertiesManager instance = null;
00058 
00059         public Properties globalProperties = new java.util.Properties();
00060 
00061         protected PropertiesManager() {
00062                 super();
00063         }
00064 
00068         public void removeAllProperties() {
00069                 globalProperties = new java.util.Properties();
00070         }
00071 
00076         public static PropertiesManager getManager() {
00077                 if (instance == null) {
00078                         synchronized (PropertiesManager.class) {
00079                                 if (instance == null) {
00080                                         instance = new PropertiesManager();
00081                                         ApplicationContextManager.getManager().addManager(instance);
00082                                 }
00083                         }
00084                 }
00085                 return instance;
00086         }
00087 
00094         public static void registerInstance(PropertiesManager manager) {
00095                 instance = manager;
00096                 ApplicationContextManager.getManager().addManager(instance);
00097         }
00098 
00099         public java.io.InputStream getRessourceAsStream(String name) throws java.io.IOException {
00100                 java.io.InputStream in = this.getClass().getResourceAsStream(name);
00101                 return in;
00102         }
00103 
00114         public void addPropertiesFile(String filename) throws IOException {
00115       InputStream in = null;
00116       try   {
00117         in = this.getRessourceAsStream("/properties" + filename);
00118         if (in == null) in = this.getRessourceAsStream(filename);
00119         if (in == null) in = new FileInputStream(filename);
00120       } catch (IOException ex)  {
00121         try {
00122           in = this.getRessourceAsStream(filename);
00123         } catch (IOException exi)    {
00124           in = new FileInputStream(filename);
00125         }
00126       }
00127         try {
00128                 globalProperties.load(this.convertPropertiesFile(in));
00129         } finally {
00130                 in.close();
00131         }
00132 
00133         }
00134 
00142         public void addPropertiesFileFromFilePath(String filename)
00143                         throws java.io.IOException {
00144                 InputStream in = new java.io.FileInputStream(filename);
00145                 try {
00146                         globalProperties.load(this.convertPropertiesFile(in));
00147                 } finally {
00148                         in.close();
00149                 }
00150         }
00151 
00163         public java.util.Properties getProperties(String prop_file)
00164                         throws java.io.IOException {
00165                 Properties prop = new Properties();
00166         InputStream in = null;
00167         try {
00168           in = this.getRessourceAsStream("/properties" + prop_file);
00169           if (in == null) in = this.getRessourceAsStream(prop_file);
00170           if (in == null) in = new FileInputStream(prop_file);
00171        } catch (IOException ex)    {
00172           try   {
00173             in = this.getRessourceAsStream(prop_file);
00174           } catch (IOException exi)  {
00175             in = new FileInputStream(prop_file);
00176           }
00177         }
00178                 try {
00179                         prop.load(this.convertPropertiesFile(in));
00180                 } finally {
00181                         in.close();
00182                 }
00183                 return prop;
00184         }
00185 
00193         public java.util.Properties getPropertiesFromPath(String prop_file)
00194                         throws java.io.IOException {
00195                 Properties prop = new Properties();
00196                 InputStream in = new java.io.FileInputStream(prop_file);
00197                 try {
00198                         prop.load(this.convertPropertiesFile(new BufferedInputStream(in)));
00199                 } finally {
00200                         in.close();
00201                 }
00202                 return prop;
00203         }
00204 
00210         public java.util.Properties getProperties() {
00211                 return globalProperties;
00212         }
00213 
00222         public String getProperty(String key, String defaultValue) {
00223                 return globalProperties.getProperty(key, defaultValue);
00224         }
00225 
00233         public String getProperty(String key) {
00234                 return globalProperties.getProperty(key);
00235         }
00236 
00243         public void addProperty(String key, String value) {
00244                 globalProperties.put(key, value);
00245         }
00246 
00247         protected InputStream convertPropertiesFile(InputStream inStream)
00248                         throws java.io.IOException {
00249                 int r;
00250                 char c;
00251                 ByteArrayOutputStream out = new ByteArrayOutputStream();
00252                 StringBuffer propertyName = new StringBuffer();
00253 
00254                 boolean readPropName = false;
00255                 while ((r = inStream.read()) != -1) {
00256                         c = (char) r;
00257                         if (c == '$') {
00258                                 readPropName = true;
00259                                 propertyName.setLength(0);
00260                                 continue;
00261                         } else if (readPropName) {
00262                                 if (((c >= 'a') && (c <= 'z')) || (c == '.')
00263                                                 || ((c >= 'A') && (c <= 'Z'))
00264                                                 || ((c >= '0') && (c <= '9'))) { // test end of the property. Property use normal caractere and dot (.)
00265                                         propertyName.append(c);
00266                                         continue;
00267                                 } else { // end property name
00268                                         readPropName = false;
00269                                         String propName = propertyName.toString();
00270                                         String prop = System.getProperty(propName);
00271                                         if (prop == null) {
00272                                                 prop = globalProperties.getProperty(propName, "null");
00273                                         }
00274 
00275                                         // test if the property contains anti slash value. They must be doubled because
00276                                         // property load remove slash. PB found when using user.home property on windows
00277                                         int index = prop.indexOf("\\");
00278                                         if (index != -1) {
00279                                                 byte[] tab = prop.getBytes();
00280                                                 for (int i = 0; i < tab.length; i++) {
00281                                                         if (tab[i] == '\\') {
00282                                                                 out.write('\\');
00283                                                         }
00284                                                         out.write(tab[i]);
00285                                                 }
00286                                         } else {
00287                                                 out.write(prop.getBytes());
00288                                         }
00289                                 }
00290                         }
00291                         out.write(c);
00292                 }
00293                 ByteArrayInputStream retin = new ByteArrayInputStream(out.toByteArray());
00294                 return retin;
00295         }
00296 
00297 }

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