KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > JbpmConfiguration


1 package org.jbpm;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.jbpm.instantiation.ClassLoaderUtil;
9
10 /**
11  * access the configuration of jbpm from the jbpm.properties file and sets up
12  * properties. The jbpm.properties file is accessed from the classpath root or
13  * the org.jbpm package.
14  *
15  */
public class JbpmConfiguration {
16   
17   private JbpmConfiguration() {}
18   
19   static Properties JavaDoc properties = null;
20  
21   private static Properties JavaDoc getProperties() {
22     if (properties==null) {
23       properties = ClassLoaderUtil.getProperties("jbpm.properties", "org/jbpm");
24       
25       Iterator JavaDoc iter = properties.keySet().iterator();
26       while (iter.hasNext()) {
27         String JavaDoc key = (String JavaDoc) iter.next();
28         log.debug(key+"="+properties.getProperty(key));
29       }
30     }
31     return properties;
32   }
33
34   public static String JavaDoc getString(String JavaDoc key) {
35     return getProperties().getProperty(key);
36   }
37
38   public static String JavaDoc getString( String JavaDoc key, String JavaDoc defaultValue ) {
39     return getProperties().getProperty( key, defaultValue );
40   }
41   
42   public static long getLong( String JavaDoc key, long defaultValue ) {
43     long value = defaultValue;
44     
45     String JavaDoc valueText = getProperties().getProperty( key );
46     if ( valueText != null ) {
47       try {
48         value = Long.parseLong( valueText );
49       } catch (NumberFormatException JavaDoc e) {
50         throw new RuntimeException JavaDoc( "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 JavaDoc key, boolean defaultValue) {
58     boolean value = defaultValue;
59     
60     String JavaDoc valueText = getProperties().getProperty( key );
61     if ( valueText != null ) {
62       try {
63         value = new Boolean JavaDoc( valueText ).booleanValue();
64       } catch (NumberFormatException JavaDoc e) {
65         throw new RuntimeException JavaDoc( "jbpm configuration property '" + key + "' is not parsable to a long : '" + valueText + "'", e );
66       }
67     }
68     
69     return value;
70   }
71
72   public static Object JavaDoc getObject(String JavaDoc key) {
73     return getObject(key, null);
74   }
75
76   public static Object JavaDoc getObject(String JavaDoc key, Object JavaDoc defaultValue) {
77     Object JavaDoc instance = null;
78     String JavaDoc className = getProperties().getProperty(key);
79     if (className==null) {
80       instance = defaultValue;
81     } else {
82       try {
83         Class JavaDoc clazz = ClassLoaderUtil.loadClass( className );
84         instance = clazz.newInstance();
85       } catch (Exception JavaDoc e) {
86         throw new RuntimeException JavaDoc( "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