001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package Loader;
018:
019: import java.io.File;
020:
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.net.URLClassLoader;
024:
025: import java.lang.reflect.InvocationTargetException;
026: import java.lang.reflect.Method;
027:
028: import java.util.StringTokenizer;
029:
030: /**
031: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
032: * @version CVS $Id: Loader.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class Loader {
035:
036: static boolean verbose = true;
037:
038: static final String REPOSITORIES = "loader.jar.repositories";
039: static final String MAIN_CLASS = "loader.main.class";
040: static final String VERBOSE_PROPERTY = "loader.verbose";
041: static final String CLASSPATH_PROPERTY = "loader.class.path";
042:
043: class RepositoryClassLoader extends URLClassLoader {
044:
045: public RepositoryClassLoader(ClassLoader parent) {
046: super (new URL[0], parent);
047: }
048:
049: public void addRepository(File repository) {
050: if (verbose)
051: System.out.println("Processing repository: "
052: + repository);
053:
054: if (repository.exists() && repository.isDirectory()) {
055: File[] jars = repository.listFiles();
056:
057: for (int i = 0; i < jars.length; i++) {
058: if (jars[i].getAbsolutePath().endsWith(".jar")) {
059: try {
060: URL url = jars[i].toURL();
061: if (verbose)
062: System.out.println("Adding jar: "
063: + jars[i]);
064: super .addURL(url);
065: } catch (MalformedURLException e) {
066: throw new IllegalArgumentException(e
067: .toString());
068: }
069: }
070: }
071: }
072: }
073:
074: public void addFile(File file) throws MalformedURLException {
075: if (verbose)
076: System.out.println("Adding path: " + file);
077: super .addURL(file.toURL());
078: }
079: }
080:
081: public static void main(String[] args) throws Exception {
082: new Loader().run(args);
083: }
084:
085: void run(String[] args) throws Exception {
086: String repositories = System.getProperty(REPOSITORIES);
087: if (repositories == null) {
088: System.out.println("Loader requires the '" + REPOSITORIES
089: + "' property to be set");
090: System.exit(1);
091: }
092:
093: String mainClass = System.getProperty(MAIN_CLASS);
094: if (mainClass == null) {
095: System.out.println("Loader requires the '" + MAIN_CLASS
096: + "' property to be set");
097: System.exit(1);
098: }
099:
100: String verboseProperty = System.getProperty(VERBOSE_PROPERTY);
101: if (verboseProperty != null)
102: verbose = Boolean.valueOf(verboseProperty).booleanValue();
103: String classPath = System.getProperty(CLASSPATH_PROPERTY);
104:
105: if (verbose)
106: System.out
107: .println("-------------------- Loading --------------------");
108:
109: RepositoryClassLoader classLoader = new RepositoryClassLoader(
110: this .getClass().getClassLoader());
111: StringTokenizer st;
112:
113: if (classPath != null) {
114: // Load classpath entries before jar repositories to allow
115: // WEB-INF/classes overriding WEB-INF/lib
116: st = new StringTokenizer(classPath, File.pathSeparator);
117: while (st.hasMoreTokens()) {
118: classLoader.addFile(new File(st.nextToken()));
119: }
120: }
121:
122: st = new StringTokenizer(repositories, File.pathSeparator);
123: while (st.hasMoreTokens()) {
124: classLoader.addRepository(new File(st.nextToken()));
125: }
126:
127: Thread.currentThread().setContextClassLoader(classLoader);
128:
129: if (verbose)
130: System.out
131: .println("-------------------- Executing -----------------");
132: if (verbose)
133: System.out.println("Main Class: " + mainClass);
134:
135: invokeMain(classLoader, mainClass, args);
136: }
137:
138: void invokeMain(ClassLoader classloader, String classname,
139: String[] args) throws IllegalAccessException,
140: InvocationTargetException, NoSuchMethodException,
141: ClassNotFoundException {
142: Class invokedClass = classloader.loadClass(classname);
143:
144: Class[] methodParamTypes = new Class[1];
145: methodParamTypes[0] = args.getClass();
146:
147: Method main = invokedClass.getDeclaredMethod("main",
148: methodParamTypes);
149:
150: Object[] methodParams = new Object[1];
151: methodParams[0] = args;
152:
153: main.invoke(null, methodParams);
154: }
155: }
|