001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: RuntimeClassLoader.java 11735 2008-01-30 09:56:22Z elu $
023: */
024:
025: package com.bostechcorp.cbesb.common.util;
026:
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.FileNotFoundException;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.net.MalformedURLException;
033: import java.net.URL;
034: import java.net.URLClassLoader;
035: import java.util.HashMap;
036: import java.util.Properties;
037: import java.util.Vector;
038:
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: public class RuntimeClassLoader {
043: // private static final String OUR_CLASSPATH = "CBESB_CLASSPATH";
044: // private static final String OUR_CLASSPATH_SYSPROP = "cbesb.classpath";
045:
046: private static final String OUR_CLASSPATH_SYS_KEY1 = "cbesb.sys.classpath.1";
047: private static final String OUR_CLASSPATH_SYS_KEY2 = "cbesb.sys.classpath.2";
048:
049: private static URL[] runtimeUrls = null;
050: private static final Log logger = LogFactory
051: .getLog(RuntimeClassLoader.class);
052: private static Vector<URL> runtimeUrlsList = new Vector<URL>();
053: private static HashMap<String, Vector<URL>> saUrlsMap = new HashMap<String, Vector<URL>>();
054:
055: /*
056: * Extract the runtime URLs from the environment variable.
057: *
058: */
059: static {
060: String pathSeparator = System.getProperty("path.separator");
061: String fileSeparator = System.getProperty("file.separator");
062:
063: // String ourClasspath = System.getenv(OUR_CLASSPATH);
064:
065: String ourClasspath = "";
066:
067: String cbesb_home = EsbPathHelper.getCbesbHomeDir();
068: // String syspropPath = System.getProperty(OUR_CLASSPATH_SYSPROP);
069: // if (syspropPath != null) ourClasspath = syspropPath;
070:
071: Properties cpProp = new Properties();
072: String cp1, cp2;
073:
074: String configFile = cbesb_home + fileSeparator + "config"
075: + fileSeparator + "cbesb_classpath.conf";
076: try {
077: cpProp.load(new FileInputStream(configFile));
078: cp1 = cpProp.getProperty(OUR_CLASSPATH_SYS_KEY1);
079:
080: if (cp1 != null) {
081: String[] ourClassJars = cp1.split(";");
082: for (int i = 0; i < ourClassJars.length; i++) {
083: ourClasspath += pathSeparator
084: + cbesb_home
085: + fileSeparator
086: + ourClassJars[i].replace('/',
087: fileSeparator.charAt(0));
088: }
089: }
090:
091: cp2 = cpProp.getProperty(OUR_CLASSPATH_SYS_KEY2);
092:
093: if (cp2 != null) {
094: String[] ourClassJars = cp2.split(";");
095: for (int i = 0; i < ourClassJars.length; i++) {
096: ourClasspath += pathSeparator
097: + ourClassJars[i].replace('/',
098: fileSeparator.charAt(0));
099: }
100: }
101:
102: } catch (FileNotFoundException e1) {
103: logger.error("Exception in RuntimeClassLoader(): The '"
104: + configFile + "' file does not exit.\""
105: + e1.getMessage());
106: if (logger.isDebugEnabled()) {
107: logger.debug("Exception in RuntimeClassLoader(): The '"
108: + configFile + "' file does not exit.\"", e1);
109: }
110: } catch (IOException e1) {
111: logger.error("Error reading from the '" + configFile
112: + "' file :" + e1.getMessage());
113: if (logger.isDebugEnabled()) {
114: logger.debug("Error reading from the '" + configFile
115: + "' file :", e1);
116: }
117: }
118:
119: logger.debug("ESB Classpath:" + ourClasspath);
120: if (ourClasspath != null) {
121: try {
122: String[] ourClasspathElements = ourClasspath
123: .split(pathSeparator);
124: runtimeUrls = new URL[ourClasspathElements.length];
125: for (int i = 0; i < ourClasspathElements.length; i++) {
126: try {
127: runtimeUrls[i] = new URL(
128: ourClasspathElements[i]);
129: runtimeUrlsList.add(runtimeUrls[i]);
130: } catch (MalformedURLException e) {
131: // if its malformed then try to evaluate it as a file
132: try {
133: runtimeUrls[i] = new File(
134: ourClasspathElements[i]).toURL();
135: runtimeUrlsList.add(runtimeUrls[i]);
136:
137: } catch (IOException ioe) {
138: throw e;
139: }
140: }
141: }
142: } catch (MalformedURLException e) {
143: Log log = LogFactory.getLog(RuntimeClassLoader.class);
144: log.error("ChainBuilder ESB class path: ' "
145: + ourClasspath + "' is malformed: " + e);
146: runtimeUrls = null;
147: }
148: }
149: }
150:
151: /*
152: * Add to the SA URL list
153: */
154: public static void addToPath(String saName, String newPath) {
155: logger.debug("addToPath(" + saName + ", " + newPath + ")\n\n");
156:
157: try {
158: URL newUrl = null;
159: try {
160: newUrl = new URL(newPath);
161: } catch (MalformedURLException e) {
162: // if its malformed then try to evaluate it as a file
163: newUrl = new File(newPath).toURL();
164: }
165: Vector<URL> saList = saUrlsMap.get(saName);
166: if (saList == null) {
167: saList = new Vector<URL>();
168: saUrlsMap.put(saName, saList);
169: }
170: saList.add(newUrl);
171: } catch (Exception e) {
172: Log log = LogFactory.getLog(RuntimeClassLoader.class);
173: log.error("error in RuntimeClassLoader addToPath("
174: + newPath + ") " + e);
175: }
176: }
177:
178: /*
179: * Contructs a URLClassLoader from the CBESB_CLASSPATH environment variable or the
180: * cbesb.classpath system property.
181: *
182: * @return a URLClassLoader to use for runtime objects such as translates and parsers.
183: */
184: public static ClassLoader getClassLoader() {
185: return RuntimeClassLoader.getClassLoader(null);
186: }
187:
188: /*
189: * Contructs a URLClassLoader from the CBESB_CLASSPATH environment variable.
190: *
191: * @param parent An object to use as the source of the parent class loader
192: * @return a URLClassLoader to use for runtime objects such as translates and parsers.
193: */
194: public static ClassLoader getClassLoader(Object parent) {
195: ClassLoader parentCl = null;
196: if (parent != null)
197: parentCl = parent.getClass().getClassLoader();
198: else
199: parentCl = (URLClassLoader) ClassLoader
200: .getSystemClassLoader();
201: ;
202: // add the system class path to the runtime URL list
203: URLClassLoader systemLoader = (URLClassLoader) ClassLoader
204: .getSystemClassLoader();
205: URL[] sysUrls = systemLoader.getURLs();
206:
207: URL[] newUrls = new URL[runtimeUrlsList.size() + sysUrls.length];
208: runtimeUrlsList.toArray(newUrls);
209: int dest = runtimeUrlsList.size();
210: for (int i = 0; i < sysUrls.length; i++) {
211: newUrls[dest++] = sysUrls[i];
212: }
213: ClassLoader runtimeCl = null;
214: if (newUrls.length > 0)
215: runtimeCl = new URLClassLoader(newUrls, parentCl);
216: else
217: runtimeCl = parentCl;
218: return runtimeCl;
219: }
220:
221: /*
222: * Contructs a service assembly specific URLClassLoader from the CBESB_CLASSPATH environment
223: * variable and the SA URL list.
224: *
225: * @param saName The service assembly name
226: * @param parent An object to use as the source of the parent class loader
227: * @return a URLClassLoader to use for runtime objects such as translates and parsers.
228: */
229: public static ClassLoader getClassLoader(String saName,
230: Object parent) {
231: if (saName == null)
232: return getClassLoader(parent);
233: ClassLoader parentCl = null;
234: if (parent != null)
235: parentCl = parent.getClass().getClassLoader();
236: else
237: parentCl = (URLClassLoader) ClassLoader
238: .getSystemClassLoader();
239: // calculate the total url list size
240: int urlCount = runtimeUrlsList.size();
241: Vector<URL> saUrlsList = saUrlsMap.get(saName);
242: if (saUrlsList != null)
243: urlCount += saUrlsList.size();
244: // add the system class path to the runtime URL list
245: URLClassLoader systemLoader = (URLClassLoader) ClassLoader
246: .getSystemClassLoader();
247: URL[] sysUrls = systemLoader.getURLs();
248: urlCount += sysUrls.length;
249:
250: URL[] newUrls = new URL[urlCount];
251: // populate the url list
252: runtimeUrlsList.toArray(newUrls);
253: int dest = runtimeUrlsList.size();
254: if (saUrlsList != null) {
255: int src = 0;
256: for (; src < saUrlsList.size(); src++)
257: newUrls[dest++] = saUrlsList.elementAt(src);
258: }
259: for (int i = 0; i < sysUrls.length; i++)
260: newUrls[dest++] = sysUrls[i];
261:
262: // construct the class loader
263: ClassLoader runtimeCl = null;
264: if (newUrls.length > 0)
265: runtimeCl = new URLClassLoader(newUrls, parentCl);
266: else
267: runtimeCl = parentCl;
268: return runtimeCl;
269: }
270:
271: public static void clearSaPath(String saName) {
272: saUrlsMap.remove(saName);
273: }
274:
275: /*
276: * Try to get a resource InputStream based on the classpath elements. Use this to read files
277: * located in our classpath (things like common groovyscript).
278: *
279: * @param resource a resource (file) name
280: * @return an input stream.
281: * @throws IOException if the resource can not be located
282: */
283: public static InputStream getInputStream(String resourceName)
284: throws IOException {
285: if (runtimeUrls != null) {
286: ClassLoader resourceLoader = RuntimeClassLoader
287: .getClassLoader();
288: return resourceLoader.getResourceAsStream(resourceName);
289: }
290: throw new IOException("resource \"" + resourceName
291: + "\" not found");
292: }
293:
294: }
|