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

PropertiesManager.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  * 
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)
00100                         throws java.io.IOException {
00101                 java.io.InputStream in = null;
00102 
00103                 try {
00104                         in = this.getClass().getResourceAsStream(name);
00105                 } catch (Exception e) {
00106                 }
00107                 return in;
00108         }
00109 
00120         public void addPropertiesFile(String filename) throws IOException {
00121                 InputStream in = this.getRessourceAsStream("/properties" + filename);
00122                 if (in == null) {
00123                         in = this.getRessourceAsStream(filename);
00124                 }
00125                 if (in == null)
00126                         throw new IOException("Error File not found :" + filename);
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 = this.getRessourceAsStream("/properties" + prop_file);
00167                 if (in == null) {
00168                         in = this.getRessourceAsStream(prop_file);
00169                 }
00170                 if (in == null)
00171                         throw new IOException("Error File not found :" + prop_file);
00172                 try {
00173                         prop.load(this.convertPropertiesFile(in));
00174                 } finally {
00175                         in.close();
00176                 }
00177                 return prop;
00178         }
00179 
00187         public java.util.Properties getPropertiesFromPath(String prop_file)
00188                         throws java.io.IOException {
00189                 Properties prop = new Properties();
00190                 InputStream in = new java.io.FileInputStream(prop_file);
00191                 try {
00192                         prop.load(this.convertPropertiesFile(new BufferedInputStream(in)));
00193                 } finally {
00194                         in.close();
00195                 }
00196                 return prop;
00197         }
00198 
00204         public java.util.Properties getProperties() {
00205                 return globalProperties;
00206         }
00207 
00216         public String getProperty(String key, String defaultValue) {
00217                 return globalProperties.getProperty(key, defaultValue);
00218         }
00219 
00227         public String getProperty(String key) {
00228                 return globalProperties.getProperty(key);
00229         }
00230 
00237         public void addProperty(String key, String value) {
00238                 globalProperties.put(key, value);
00239         }
00240 
00241         protected InputStream convertPropertiesFile(InputStream inStream)
00242                         throws java.io.IOException {
00243                 int r;
00244                 char c;
00245                 ByteArrayOutputStream out = new ByteArrayOutputStream();
00246                 StringBuffer propertyName = new StringBuffer();
00247 
00248                 boolean readPropName = false;
00249                 while ((r = inStream.read()) != -1) {
00250                         c = (char) r;
00251                         if (c == '$') {
00252                                 readPropName = true;
00253                                 propertyName.setLength(0);
00254                                 continue;
00255                         } else if (readPropName) {
00256                                 if (((c >= 'a') && (c <= 'z')) || (c == '.')
00257                                                 || ((c >= 'A') && (c <= 'Z'))
00258                                                 || ((c >= '0') && (c <= '9'))) { // test end of the property. Property use normal caractere and dot (.)
00259                                         propertyName.append(c);
00260                                         continue;
00261                                 } else { // end property name
00262                                         readPropName = false;
00263                                         String propName = propertyName.toString();
00264                                         String prop = System.getProperty(propName);
00265                                         if (prop == null) {
00266                                                 prop = globalProperties.getProperty(propName, "null");
00267                                         }
00268 
00269                                         // test if the property contains anti slash value. They must be doubled because
00270                                         // property load remove slash. PB found when using user.home property on windows
00271                                         int index = prop.indexOf("\\");
00272                                         if (index != -1) {
00273                                                 byte[] tab = prop.getBytes();
00274                                                 for (int i = 0; i < tab.length; i++) {
00275                                                         if (tab[i] == '\\') {
00276                                                                 out.write('\\');
00277                                                         }
00278                                                         out.write(tab[i]);
00279                                                 }
00280                                         } else {
00281                                                 out.write(prop.getBytes());
00282                                         }
00283                                 }
00284                         }
00285                         out.write(c);
00286                 }
00287                 ByteArrayInputStream retin = new ByteArrayInputStream(out.toByteArray());
00288                 return retin;
00289         }
00290 
00291 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4