1 18 19 20 package sync4j.framework.config; 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 25 import java.net.URL ; 26 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.Properties ; 30 import java.util.Collections ; 31 32 import sync4j.framework.tools.beans.BeanFactory; 33 import sync4j.framework.config.ConfigurationException; 34 35 import org.apache.commons.lang.StringUtils; 36 37 54 public class Configuration 55 implements java.io.Serializable { 56 57 59 61 private final Map internalMap = Collections.synchronizedMap(new HashMap ()); 62 63 private static Configuration singleton; 64 private transient ClassLoader classLoader = null; 65 66 68 72 public ClassLoader getClassLoader() { 73 return classLoader; 74 } 75 76 80 public void setClassLoader(ClassLoader classLoader) { 81 this.classLoader = classLoader; 82 } 83 84 86 protected Configuration() { 87 } 88 89 public static synchronized Configuration getConfiguration() { 90 if (singleton == null) { 91 singleton = new Configuration(); 92 } 93 94 return singleton; 95 } 96 97 99 public String getStringValue(String name, String def) 100 throws ConfigurationException { 101 String value = (String )internalMap.get(name); 102 103 return (value == null) ? def : value; 104 } 105 106 public String getStringValue(String name) 107 throws ConfigurationException { 108 String value = (String )internalMap.get(name); 109 110 if (value == null) { 111 throw new ConfigurationException("No " + name + " property defined"); 112 } 113 114 return value; 115 } 116 117 133 public String [] getStringArray(String name, char separator) 134 throws ConfigurationException { 135 String value = (String )internalMap.get(name); 136 137 if (value == null) { 138 throw new ConfigurationException("No " + name + " property defined"); 139 } 140 141 return StringUtils.split(value, String.valueOf(separator)); 146 } 147 148 public int getIntValue(String name, int def) 149 throws ConfigurationException { 150 Integer value = (Integer )internalMap.get(name); 151 152 return (value == null) ? def : value.intValue(); 153 } 154 155 public int getIntValue(String name) 156 throws ConfigurationException { 157 Integer value = (Integer )internalMap.get(name); 158 159 if (value == null) { 160 throw new ConfigurationException("No " + name + " property defined"); 161 } 162 163 return value.intValue(); 164 } 165 166 public boolean getBoolValue(String name, boolean def) 167 throws ConfigurationException { 168 Boolean value = (Boolean )internalMap.get(name); 169 170 return (value == null) ? def : value.booleanValue(); 171 } 172 173 public boolean getBoolValue(String name) 174 throws ConfigurationException { 175 Boolean value = (Boolean )internalMap.get(name); 176 177 if (value == null) { 178 throw new ConfigurationException("No " + name + " property defined"); 179 } 180 181 return value.booleanValue(); 182 } 183 184 public Object getValue(String name, Object def) 185 throws ConfigurationException { 186 Object value = internalMap.get(name); 187 188 return (value == null) ? def : value; 189 } 190 191 public Object getValue(String name) 192 throws ConfigurationException { 193 Object value = internalMap.get(name); 194 195 if (value == null) { 196 throw new ConfigurationException("No " + name + " property defined"); 197 } 198 199 return value; 200 } 201 202 212 public Object getClassInstance(String name) 213 throws ConfigurationException { 214 String value = (String )internalMap.get(name); 215 216 if (value == null) { 217 throw new ConfigurationException("No " + name + " property defined"); 218 } 219 220 try { 221 return Class.forName(value).newInstance(); 222 } catch (Exception e) { 223 throw new ConfigurationException( "Error in creating an instance of " 224 + value 225 , e ); 226 } 227 } 228 229 239 public Object getBeanInstance(String name) 240 throws ConfigurationException { 241 String value = (String )internalMap.get(name); 242 243 if (value == null) { 244 throw new ConfigurationException("No " + name + " property defined"); 245 } 246 247 try { 248 return BeanFactory.getBeanInstance(classLoader, value); 249 } catch (Exception e) { 250 throw new ConfigurationException( "Error in creating an instance of " 251 + value 252 , e ); 253 } 254 } 255 256 263 public Object [] getBeanArray(String name, char separator) 264 throws ConfigurationException { 265 String [] values = getStringArray(name, separator); 266 267 Object [] ret = new Object [values.length]; 268 269 int i = 0; 270 try { 271 for (; i<ret.length; ++i) { 272 ret[i] = BeanFactory.getBeanInstance(classLoader, values[i]); 273 } 274 } catch (Exception e) { 275 throw new ConfigurationException ( "Error in creating the bean " 276 + values[i] 277 , e ); 278 } 279 280 return ret; 281 } 282 283 300 public Object getBeanInstanceByName(String beanName) { 301 try { 302 return BeanFactory.getBeanInstance(classLoader, beanName); 303 } catch (Exception e) { 304 throw new ConfigurationException( "Error in creating an instance of " 305 + beanName 306 , e ); 307 } 308 } 309 310 public synchronized void load(InputStream is) throws IOException { 311 Properties properties = new Properties (); 312 313 properties.load(is); 314 315 internalMap.putAll(properties); 316 } 317 318 public synchronized void load(String uri) 319 throws IOException { 320 load(new URL (uri).openStream()); 321 } 322 323 public String toString() { 324 StringBuffer sb = new StringBuffer (); 325 326 sb.append(getClass().getName()).append(" - "); 327 sb.append(internalMap.toString()).append(" - "); 328 sb.append("classLoader: " + classLoader); 329 330 return sb.toString(); 331 } 332 333 } | Popular Tags |