00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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'))) {
00276 propertyName.append(c);
00277 continue;
00278 } else {
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
00287
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 }