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.traceInfo(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 Properties prop = new Properties();
00203 InputStream in = null;
00204 File file = new File(prop_file);
00205 if (file.exists()) in = new java.io.FileInputStream(prop_file);
00206 else in = this.getRessourceAsStream(prop_file);
00207 if (in != null) {
00208 try {
00209 prop.load(this.convertPropertiesFile(new BufferedInputStream(in)));
00210 } finally {
00211 in.close();
00212 }
00213 } else {
00214
00215 throw new IOException("PropertiesManager getPropertiesFromPath file not found :"+prop_file);
00216 }
00217 return prop;
00218 }
00219
00225 public java.util.Properties getProperties() {
00226 return globalProperties;
00227 }
00228
00237 public String getProperty(String key, String defaultValue) {
00238 return globalProperties.getProperty(key, defaultValue);
00239 }
00240
00248 public String getProperty(String key) {
00249 return globalProperties.getProperty(key);
00250 }
00251
00258 public void addProperty(String key, String value) {
00259 if (value != null) globalProperties.put(key, value);
00260 }
00261
00268 public void removeProperty(String key) {
00269 globalProperties.remove(key);
00270 }
00271
00272 protected InputStream convertPropertiesFile(InputStream inStream)
00273 throws java.io.IOException {
00274 int r;
00275 char c;
00276 ByteArrayOutputStream out = new ByteArrayOutputStream();
00277 StringBuffer propertyName = new StringBuffer();
00278
00279 boolean readPropName = false;
00280 while ((r = inStream.read()) != -1) {
00281 c = (char) r;
00282 if (c == '$') {
00283 readPropName = true;
00284 propertyName.setLength(0);
00285 continue;
00286 } else if (readPropName) {
00287 if (((c >= 'a') && (c <= 'z')) || (c == '.')
00288 || ((c >= 'A') && (c <= 'Z'))
00289 || ((c >= '0') && (c <= '9'))) {
00290 propertyName.append(c);
00291 continue;
00292 } else {
00293 readPropName = false;
00294 String propName = propertyName.toString();
00295 String prop = System.getProperty(propName);
00296 if (prop == null) {
00297 prop = globalProperties.getProperty(propName, "null");
00298 }
00299
00300
00301
00302 int index = prop.indexOf("\\");
00303 if (index != -1) {
00304 byte[] tab = prop.getBytes();
00305 for (int i = 0; i < tab.length; i++) {
00306 if (tab[i] == '\\') {
00307 out.write('\\');
00308 }
00309 out.write(tab[i]);
00310 }
00311 } else {
00312 out.write(prop.getBytes());
00313 }
00314 }
00315 }
00316 out.write(c);
00317 }
00318 ByteArrayInputStream retin = new ByteArrayInputStream(out.toByteArray());
00319 return retin;
00320 }
00321
00322 }