001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.util;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.util.Properties;
027:
028: import org.jbpm.JbpmException;
029: import org.jbpm.graph.def.ProcessDefinition;
030: import org.jbpm.instantiation.ProcessClassLoader;
031:
032: /**
033: * provides centralized classloader lookup.
034: */
035: public class ClassLoaderUtil {
036:
037: public static Class loadClass(String className) {
038: try {
039: return getClassLoader().loadClass(className);
040: } catch (ClassNotFoundException e) {
041: throw new JbpmException("class not found '" + className
042: + "'", e);
043: }
044: }
045:
046: public static ClassLoader getClassLoader() {
047: // if this is made configurable, make sure it's done with the
048: // jvm system properties
049: // System.getProperty("jbpm.classloader")
050: // - 'jbpm'
051: // - 'context'
052: //
053: // or something like Thread.currentThread().getContextClassLoader();
054: return ClassLoaderUtil.class.getClassLoader();
055: }
056:
057: public static InputStream getStream(String resource) {
058: return getClassLoader().getResourceAsStream(resource);
059: }
060:
061: public static Properties getProperties(String resource) {
062: Properties properties = new Properties();
063: try {
064: properties.load(getStream(resource));
065: } catch (IOException e) {
066: throw new JbpmException("couldn't load properties file '"
067: + resource + "'", e);
068: }
069: return properties;
070: }
071:
072: /**
073: * searches the given resource, first on the root of the classpath and if not
074: * not found there, in the given directory.
075: public static InputStream getStream(String resource, String directory) {
076: InputStream is = getClassLoader().getResourceAsStream(resource);
077: if (is==null) {
078: is = getClassLoader().getResourceAsStream(directory+"/"+resource);
079: }
080: return is;
081: }
082:
083: public static Properties getProperties(String resource, String directory) {
084: Properties properties = new Properties();
085: try {
086: properties.load(getStream(resource, directory));
087: } catch (IOException e) {
088: throw new JbpmException("couldn't load properties file '"+resource+"'", e);
089: }
090: return properties;
091: }
092: */
093:
094: public static ClassLoader getProcessClassLoader(
095: ProcessDefinition processDefinition) {
096: return new ProcessClassLoader(ClassLoaderUtil.class
097: .getClassLoader(), processDefinition);
098: }
099:
100: }
|