1 29 30 package com.caucho.loader; 31 32 import java.util.Collection ; 33 import java.util.Enumeration ; 34 import java.util.Properties ; 35 import java.util.Set ; 36 37 42 public class EnvironmentProperties extends Properties { 43 private static Properties _origSystemProperties; 44 private static Properties _envSystemProperties; 45 46 private transient EnvironmentLocal<Properties > _envProps 47 = new EnvironmentLocal<Properties >(); 48 49 private Properties _global; 50 51 public EnvironmentProperties(Properties global) 52 { 53 _global = global; 54 } 55 56 public EnvironmentProperties() 57 { 58 this(new Properties ()); 59 } 60 61 public static void enableEnvironmentSystemProperties(boolean isEnable) 62 { 63 if (_origSystemProperties == null) { 64 _origSystemProperties = System.getProperties(); 65 _envSystemProperties = new EnvironmentProperties(_origSystemProperties); 66 } 67 68 if (isEnable) 69 System.setProperties(_envSystemProperties); 70 else 71 System.setProperties(_origSystemProperties); 72 } 73 74 public Properties getGlobalProperties() 75 { 76 return _global; 77 } 78 79 public void setGlobalProperties(Properties global) 80 { 81 _global = global; 82 } 83 84 public int size() 85 { 86 return getEnvironmentProperties().size(); 87 } 88 89 public boolean isEmpty() 90 { 91 return getEnvironmentProperties().isEmpty(); 92 } 93 94 public Enumeration keys() 95 { 96 return getEnvironmentProperties().keys(); 97 } 98 99 public Enumeration elements() 100 { 101 return getEnvironmentProperties().elements(); 102 } 103 104 public boolean contains(Object value) 105 { 106 return getEnvironmentProperties().contains(value); 107 } 108 109 public boolean containsValue(Object value) 110 { 111 return getEnvironmentProperties().containsValue(value); 112 } 113 114 public boolean containsKey(Object value) 115 { 116 return getEnvironmentProperties().containsKey(value); 117 } 118 119 public Object get(String key) 120 { 121 Properties props = getEnvironmentProperties(); 122 123 String value = props.getProperty(key); 124 125 if (value != null) 126 return value; 127 else 128 return _global.get(key); 129 } 130 131 public Object get(Object key) 132 { 133 return get((String ) key); 134 } 135 136 public String getProperty(String key) 137 { 138 Properties props = getEnvironmentProperties(); 139 140 String value = props.getProperty(key); 141 142 if (value != null) 143 return value; 144 else 145 return _global.getProperty(key); 146 } 147 148 public String getProperty(String key, String defaultValue) 149 { 150 Properties props = getEnvironmentProperties(); 151 152 String value = props.getProperty(key); 153 154 if (value != null) 155 return value; 156 else 157 return _global.getProperty(key, defaultValue); 158 } 159 160 public Enumeration propertyNames() 161 { 162 return getEnvironmentProperties().propertyNames(); 163 } 164 165 public String put(String key, String value) 166 { 167 return (String ) getPutEnvironmentProperties().put(key, value); 168 } 169 170 public Object setProperty(String key, String value) 171 { 172 return put(key, value); 173 } 174 175 public Object put(Object key, Object value) 176 { 177 return getPutEnvironmentProperties().put(key, value); 178 } 179 180 public String remove(Object key) 181 { 182 return (String ) getPutEnvironmentProperties().remove(key); 183 } 184 185 191 192 public void clear() 193 { 194 getPutEnvironmentProperties().clear(); 195 } 196 197 public Object clone() 198 { 199 return getEnvironmentProperties().clone(); 200 } 201 202 public String toString() 203 { 204 return getEnvironmentProperties().toString(); 205 } 206 207 public Set keySet() 208 { 209 return getPutEnvironmentProperties().keySet(); 210 } 211 212 public Set entrySet() 213 { 214 return getPutEnvironmentProperties().entrySet(); 215 } 216 217 public Collection values() 218 { 219 return getPutEnvironmentProperties().values(); 220 } 221 222 public boolean equals(Object o) 223 { 224 return getEnvironmentProperties().equals(o); 225 } 226 227 public int hashCode() 228 { 229 return getEnvironmentProperties().hashCode(); 230 } 231 232 private Properties getEnvironmentProperties() 233 { 234 Properties props = _envProps.get(); 235 236 if (props != null) 237 return props; 238 else 239 return _global; 240 } 241 242 private synchronized Properties getPutEnvironmentProperties() 243 { 244 Properties props = _envProps.getLevel(); 245 246 if (props == null) { 247 props = new Properties (); 248 Properties parentProps = _envProps.get(); 249 if (parentProps == null) 250 parentProps = _global; 251 252 props.putAll(parentProps); 253 254 _envProps.set(props); 255 256 return props; 257 } 258 else 259 return props; 260 } 261 262 public Object writeReplace() throws java.io.ObjectStreamException 263 { 264 return getEnvironmentProperties(); 265 } 266 } 267 | Popular Tags |