1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package nl.toolforge.karma.launcher;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.StringTokenizer;
28
29 public class KarmaLauncher {
30
31 public static final String KARMA_CLASSPATH = "KARMA_CLASSPATH";
32
33
34
35 private static KarmaLauncher instance = null;
36
37 public static synchronized KarmaLauncher getInstance() {
38 if (instance == null) {
39 instance = new KarmaLauncher();
40 }
41 return instance;
42 }
43
44 private KarmaLauncher() {
45
46 }
47
48 public void invoke(String className, String methodName, Object[] args,
49 String[] classPathEntries) throws ClassNotFoundException,
50 NoSuchMethodException, IllegalAccessException,
51 InvocationTargetException, IOException, FileNotFoundException {
52 File[] entries = null;
53
54 if (classPathEntries != null) {
55 entries = new File[classPathEntries.length];
56
57 for (int i = 0; i < classPathEntries.length; i++) {
58 entries[i] = new File(classPathEntries[i]);
59 }
60 }
61 invoke(className, methodName, args, entries);
62 }
63
64 public void invoke(String className, String methodName, Object[] args,
65 File[] classPathEntries) throws ClassNotFoundException,
66 NoSuchMethodException, IllegalAccessException,
67 InvocationTargetException, IOException, FileNotFoundException {
68
69 KarmaClassLoader classLoader = new KarmaClassLoader(this.getClass()
70 .getClassLoader());
71 if (classPathEntries != null) {
72 for (int i = 0; i < classPathEntries.length; i++) {
73 try {
74 classLoader.addClassPathEntry(classPathEntries[i]);
75 } catch (FileNotFoundException e) {
76 System.out.println("[karma-launcher] WARNING: Classpath entry '" + classPathEntries[i] + "' does not exist.");
77 }
78 }
79 }
80 Class clazz = Class.forName(className, true, classLoader);
81
82
83 Class[] argTypes = new Class[args.length];
84 for (int i = 0; i < argTypes.length; i++) {
85 argTypes[i] = args[i].getClass();
86 }
87 Method main = clazz.getDeclaredMethod(methodName,
88 argTypes);
89
90 main.invoke(null, args);
91 }
92
93 public static void main(String[] args) {
94 if ((args == null) || (args.length == 0)) {
95 throw new RuntimeException(
96 "No class specified that should contain \"main\" method.");
97 }
98 String mainMethodClassName = args[0];
99
100 String[] applicationArgs = new String[args.length - 1];
101 System.arraycopy(args, 1, applicationArgs, 0, args.length - 1);
102
103 String[] classPathEntries = null;
104 String karmaClassPath = System.getProperty(KARMA_CLASSPATH);
105 if (karmaClassPath != null) {
106 StringTokenizer tokenizer = new StringTokenizer(karmaClassPath, File.pathSeparator);
107
108 int n = tokenizer.countTokens();
109 classPathEntries = new String[n];
110
111 int i = 0;
112 while (i < n) {
113 classPathEntries[i++] = tokenizer.nextToken();
114 }
115 }
116
117 System.setSecurityManager(new KarmaLauncherSecurityManager());
118
119 KarmaLauncher launcher = getInstance();
120 try {
121 launcher.invoke(mainMethodClassName, "main",
122 new Object[] { applicationArgs }, classPathEntries);
123 } catch (Exception e) {
124 e.printStackTrace();
125 }
126 }
127 }