01: /*
02: * Copyright (C) 2007 Jared Alexander Spigner
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: * jspigner@openjx.org
19: *
20: * JXPluginLoader.java
21: *
22: * Created on June 8, 2007, 12:54 AM
23: *
24: */
25:
26: package org.openjx.conf;
27:
28: import java.net.URL;
29: import java.net.URLClassLoader;
30:
31: import java.lang.reflect.Method;
32:
33: import javax.swing.JOptionPane;
34:
35: import org.openjx.display.JXMessageBox;
36: import org.openjx.display.JXViewer;
37:
38: /**
39: * This class is capable of loading a OpenJX plugin into the system during the
40: * runtime. Part of doing this is adding Java classes to the class path at
41: * runtime, this is done by hacking the class path =).
42: *
43: * @author Jared Spigner
44: */
45: public class JXPluginLoader {
46: public static final Class[] parameters = new Class[] { URL.class };
47:
48: /**
49: * This is the constructor for the JXPluginLoader class. It creates a new
50: * instance of JXPluginLoader.
51: */
52: public JXPluginLoader() {
53: }
54:
55: /**
56: * This method adds a location to the class path at runtime. The location
57: * may even be a remote url.
58: *
59: * @param u is the url or file system pathname we want to add to the
60: * class path.
61: * @param jxViewer points to an instance of the JXViewer class.
62: */
63: public static void addURL(URL u, JXViewer jxViewer) {
64: try {
65: URLClassLoader sysloader = (URLClassLoader) ClassLoader
66: .getSystemClassLoader();
67: Class sysclass = URLClassLoader.class;
68:
69: Method method = sysclass.getDeclaredMethod("addURL",
70: parameters);
71: method.setAccessible(true);
72: method.invoke(sysloader, new Object[] { u });
73:
74: URL[] urls = sysloader.getURLs();
75: } catch (Exception e) {
76: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
77: "Plugin Loader Failure", "" + e, jxViewer);
78: }
79: }
80:
81: }
|