01: /*
02:
03: Copyright 2004, Martian Software, Inc.
04:
05: Licensed under the Apache License, Version 2.0 (the "License");
06: you may not use this file except in compliance with the License.
07: You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16:
17: */
18:
19: package com.martiansoftware.nailgun.builtins;
20:
21: import java.io.File;
22: import java.net.URL;
23: import java.net.URLClassLoader;
24:
25: import com.martiansoftware.nailgun.NGContext;
26:
27: /**
28: * <p>Provides a means to display and add to the system classpath at runtime.
29: * If called with no arguments, the classpath is displayed. Otherwise, each
30: * argument is turned into a java.io.File and added to the classpath. Relative
31: * paths will be resolved relative to the directory in which the nailgun server
32: * is running. This is very likely to change in the future.</p>
33: *
34: * <p>This is aliased by default to the command "<code>ng-cp</code>".</p>
35: *
36: * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
37: */
38: public class NGClasspath {
39:
40: /**
41: * Adds the specified URL (for a jar or a directory) to the System
42: * ClassLoader. This code was written by antony_miguel and posted on
43: * http://forum.java.sun.com/thread.jsp?forum=32&thread=300557&message=1191210
44: * I assume it has been placed in the public domain.
45: *
46: * @param url the URL of the resource (directory or jar) to add to the
47: * System classpath
48: * @throws Exception if anything goes wrong. The most likely culprit, should
49: * this ever arise, would be that your VM is not using a URLClassLoader as the
50: * System ClassLoader. This would result in a ClassClastException that you
51: * probably can't do much about.
52: */
53: private static void addToSystemClassLoader(URL url)
54: throws Exception {
55: URLClassLoader sysloader = (URLClassLoader) ClassLoader
56: .getSystemClassLoader();
57: Class sysclass = URLClassLoader.class;
58:
59: java.lang.reflect.Method method = sysclass.getDeclaredMethod(
60: "addURL", new Class[] { URL.class });
61: method.setAccessible(true);
62: method.invoke(sysloader, new Object[] { url });
63: }
64:
65: public static void nailMain(NGContext context) throws Exception {
66: String[] args = context.getArgs();
67: if (args.length == 0) {
68: URLClassLoader sysLoader = (URLClassLoader) ClassLoader
69: .getSystemClassLoader();
70: URL[] urls = sysLoader.getURLs();
71: for (int i = 0; i < urls.length; ++i) {
72: context.out.println(urls[i]);
73: }
74: } else {
75: for (int i = 0; i < args.length; ++i) {
76: File file = new File(args[i]);
77: addToSystemClassLoader(file.toURL());
78: }
79: }
80: }
81: }
|