KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Loader


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 import java.io.File JavaDoc;
17
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.net.URLClassLoader JavaDoc;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24
25 import java.util.StringTokenizer JavaDoc;
26
27 /**
28  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
29  * @version CVS $Id: Loader.java 30932 2004-07-29 17:35:38Z vgritsenko $
30  */

31 public class Loader {
32
33     static final boolean VERBOSE = true;
34     
35     static final String JavaDoc REPOSITORIES = "loader.jar.repositories";
36     static final String JavaDoc MAIN_CLASS = "loader.main.class";
37     
38     class RepositoryClassLoader extends URLClassLoader JavaDoc {
39
40         public RepositoryClassLoader(ClassLoader JavaDoc parent) {
41             super(new URL JavaDoc[0], parent);
42         }
43             
44         public void addRepository(File JavaDoc repository) {
45             if (VERBOSE) System.out.println("Processing repository: " + repository);
46
47             if (repository.exists() && repository.isDirectory()) {
48                 File JavaDoc[] jars = repository.listFiles();
49     
50                 for (int i = 0; i < jars.length; i++) {
51                     if (jars[i].getAbsolutePath().endsWith(".jar")) {
52                         try {
53                             URL JavaDoc url = jars[i].toURL();
54                             if (VERBOSE) System.out.println("Adding jar: " + jars[i]);
55                             super.addURL(url);
56                         } catch (MalformedURLException JavaDoc e) {
57                             throw new IllegalArgumentException JavaDoc(e.toString());
58                         }
59                     }
60                 }
61             }
62         }
63     }
64
65     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
66         new Loader JavaDoc().run(args);
67     }
68     
69     void run(String JavaDoc[] args) throws Exception JavaDoc
70     {
71         String JavaDoc repositories = System.getProperty(REPOSITORIES);
72         if (repositories == null) {
73             System.out.println("Loader requires the '" + REPOSITORIES + "' property to be set");
74             System.exit(1);
75         }
76             
77         String JavaDoc mainClass = System.getProperty(MAIN_CLASS);
78         if (mainClass == null) {
79             System.out.println("Loader requires the '" + MAIN_CLASS + "' property to be set");
80             System.exit(1);
81         }
82
83         if (VERBOSE) System.out.println("-------------------- Loading --------------------");
84
85         RepositoryClassLoader classLoader = new RepositoryClassLoader(this.getClass().getClassLoader());
86
87         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(repositories, File.pathSeparator);
88         while (st.hasMoreTokens()) {
89             classLoader.addRepository(new File JavaDoc(st.nextToken()));
90         }
91
92         Thread.currentThread().setContextClassLoader(classLoader);
93
94         if (VERBOSE) System.out.println("-------------------- Executing -----------------");
95         if (VERBOSE) System.out.println("Main Class: " + mainClass);
96             
97         invokeMain(classLoader, mainClass, args);
98     }
99         
100     void invokeMain(ClassLoader JavaDoc classloader, String JavaDoc classname, String JavaDoc[] args)
101     throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc, ClassNotFoundException JavaDoc
102     {
103         Class JavaDoc invokedClass = classloader.loadClass(classname);
104         
105         Class JavaDoc[] methodParamTypes = new Class JavaDoc[1];
106         methodParamTypes[0] = args.getClass();
107         
108         Method JavaDoc main = invokedClass.getDeclaredMethod("main", methodParamTypes);
109         
110         Object JavaDoc[] methodParams = new Object JavaDoc[1];
111         methodParams[0] = args;
112         
113         main.invoke(null, methodParams);
114     }
115 }
Popular Tags