KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > instantiation > ClassLoaderUtil


1 package org.jbpm.instantiation;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import org.jbpm.graph.def.ProcessDefinition;
8
9 /**
10  * provides centralized classloader lookup.
11  */

12 public class ClassLoaderUtil {
13
14   public static Class JavaDoc loadClass(String JavaDoc className) {
15     try {
16       return getClassLoader().loadClass(className);
17     } catch (ClassNotFoundException JavaDoc e) {
18       throw new RuntimeException JavaDoc("class not found '"+className+"'", e);
19     }
20   }
21   
22   public static ClassLoader JavaDoc getClassLoader() {
23     // if this is made configurable, make sure it's done with the
24
// jvm system properties
25
// System.getProperty("jbpm.classloader")
26
// - 'jbpm'
27
// - 'context'
28
return ClassLoaderUtil.class.getClassLoader();
29   }
30   
31   public static InputStream JavaDoc getStream(String JavaDoc resource) {
32     return getClassLoader().getResourceAsStream(resource);
33   }
34
35   /**
36    * searches the given resource, first on the root of the classpath and if not
37    * not found there, in the given directory.
38    */

39   public static InputStream JavaDoc getStream(String JavaDoc resource, String JavaDoc directory) {
40     InputStream JavaDoc is = getClassLoader().getResourceAsStream(resource);
41     if (is==null) {
42       is = getClassLoader().getResourceAsStream(directory+"/"+resource);
43     }
44     return is;
45   }
46
47   public static Properties JavaDoc getProperties(String JavaDoc resource, String JavaDoc directory) {
48     Properties JavaDoc properties = new Properties JavaDoc();
49     try {
50       properties.load(getStream(resource, directory));
51     } catch (IOException JavaDoc e) {
52       throw new RuntimeException JavaDoc("couldn't load properties file '"+resource+"'", e);
53     }
54     return properties;
55   }
56
57   public static ClassLoader JavaDoc getProcessClassLoader(ProcessDefinition processDefinition) {
58     return new ProcessClassLoader(ClassLoaderUtil.class.getClassLoader(), processDefinition);
59   }
60
61 }
62
Popular Tags