01: /*
02: * ChainBuilder ESB
03: * Visual Enterprise Integration
04: *
05: * Copyright (C) 2006 Bostech Corporation
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the
09: * Free Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15: * for more details.
16: *
17: * You should have received a copy of the GNU General Public License along with
18: * this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: *
21: *
22: * $Id: ClassPathHacker.java 6618 2007-04-11 01:05:28Z zjin $
23: */
24: package com.bostechcorp.cbesb.common.util;
25:
26: import java.io.File;
27: import java.io.IOException;
28: import java.lang.reflect.Method;
29: import java.net.URL;
30: import java.net.URLClassLoader;
31:
32: public class ClassPathHacker {
33:
34: private static final Class[] parameters = new Class[] { URL.class };
35:
36: public static void addFile(String s) throws IOException {
37: File f = new File(s);
38: addFile(f);
39: }//end method
40:
41: public static void addFile(File f) throws IOException {
42: addURL(f.toURL());
43: }//end method
44:
45: public static void addURL(URL u) throws IOException {
46:
47: URLClassLoader sysloader = (URLClassLoader) ClassLoader
48: .getSystemClassLoader();
49: if (sysloader.findResource(u.getFile()) != null)
50: return;
51: URL[] urls = sysloader.getURLs();
52: for (int i = 0; i < urls.length; i++) {
53: if (urls[i].equals(u)) {
54: return;
55: }
56: }
57:
58: Class sysclass = URLClassLoader.class;
59:
60: try {
61: Method method = sysclass.getDeclaredMethod("addURL",
62: parameters);
63: method.setAccessible(true);
64: method.invoke(sysloader, new Object[] { u });
65: } catch (Throwable t) {
66: throw new IOException(
67: "Error, could not add URL to system classloader");
68: }//end try catch
69:
70: }//end method
71:
72: }//end class
|