1 package org.jbpm; 2 3 import java.util.Iterator ; 4 import java.util.Properties ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jbpm.instantiation.ClassLoaderUtil; 9 10 public class JbpmConfiguration { 16 17 private JbpmConfiguration() {} 18 19 static Properties properties = null; 20 21 private static Properties getProperties() { 22 if (properties==null) { 23 properties = ClassLoaderUtil.getProperties("jbpm.properties", "org/jbpm"); 24 25 Iterator iter = properties.keySet().iterator(); 26 while (iter.hasNext()) { 27 String key = (String ) iter.next(); 28 log.debug(key+"="+properties.getProperty(key)); 29 } 30 } 31 return properties; 32 } 33 34 public static String getString(String key) { 35 return getProperties().getProperty(key); 36 } 37 38 public static String getString( String key, String defaultValue ) { 39 return getProperties().getProperty( key, defaultValue ); 40 } 41 42 public static long getLong( String key, long defaultValue ) { 43 long value = defaultValue; 44 45 String valueText = getProperties().getProperty( key ); 46 if ( valueText != null ) { 47 try { 48 value = Long.parseLong( valueText ); 49 } catch (NumberFormatException e) { 50 throw new RuntimeException ( "jbpm configuration property '" + key + "' is not parsable to a long : '" + valueText + "'", e ); 51 } 52 } 53 54 return value; 55 } 56 57 public static boolean getBoolean(String key, boolean defaultValue) { 58 boolean value = defaultValue; 59 60 String valueText = getProperties().getProperty( key ); 61 if ( valueText != null ) { 62 try { 63 value = new Boolean ( valueText ).booleanValue(); 64 } catch (NumberFormatException e) { 65 throw new RuntimeException ( "jbpm configuration property '" + key + "' is not parsable to a long : '" + valueText + "'", e ); 66 } 67 } 68 69 return value; 70 } 71 72 public static Object getObject(String key) { 73 return getObject(key, null); 74 } 75 76 public static Object getObject(String key, Object defaultValue) { 77 Object instance = null; 78 String className = getProperties().getProperty(key); 79 if (className==null) { 80 instance = defaultValue; 81 } else { 82 try { 83 Class clazz = ClassLoaderUtil.loadClass( className ); 84 instance = clazz.newInstance(); 85 } catch (Exception e) { 86 throw new RuntimeException ( "couldn't instantiate class " + className + " configured in property " + key ); 87 } 88 } 89 return instance; 90 } 91 92 private static final Log log = LogFactory.getLog(JbpmConfiguration.class); 93 } 94 | Popular Tags |