1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jdo.impl.enhancer;
18
19 import java.util.Properties;
20
21 import java.io.PrintWriter;
22
23 import java.lang.reflect.Method;
24 import java.lang.reflect.InvocationTargetException;
25
26 import org.apache.jdo.impl.enhancer.util.Support;
27
28
29
30 /***
31 * Application launcher for persistence-capable classes.
32 *
33 * @author Martin Zaun
34 */
35 public class PersistenceLauncher {
36
37
38
39 static private final PrintWriter err = new PrintWriter(System.out, true);
40 static private final PrintWriter out = new PrintWriter(System.out, true);
41 static private final String prefix = "PersistenceLauncher.main() : ";
42
43 /***
44 * Creates new PersistenceLauncher.
45 */
46 private PersistenceLauncher() {
47 }
48
49 /***
50 * Prints usage message.
51 */
52 static void usage()
53 {
54 out.flush();
55 err.println("PersistenceLauncher:");
56 err.println(" usage: <options> ... <target class name> <args> ...");
57 err.println(" options:");
58 err.println(" -h | --help");
59 err.println(" -n | --noEnhancement");
60 err.println(" -q | --quiet");
61 err.println(" -w | --warn");
62 err.println(" -d | --debug");
63 err.println(" -t | --timing");
64 err.println(" class names have to be fully qualified");
65 err.println("done.");
66 err.println();
67 err.flush();
68 }
69
70 /***
71 * Creates a class loader and launches a target class.
72 * @param args the command line arguments
73 */
74 public static void main(String[] args)
75 throws ClassNotFoundException,
76 NoSuchMethodException,
77 SecurityException,
78 IllegalAccessException,
79 IllegalArgumentException,
80 InvocationTargetException {
81
82
83
84
85
86
87
88
89
90
91
92
93 final String classpath = System.getProperty("java.class.path");
94 boolean noEnhancement = false;
95 boolean debug = false;
96 boolean timing = false;
97 Properties enhancerSettings = new Properties();
98 String targetClassname = null;
99 String[] targetClassArgs = null;
100 for (int i = 0; i < args.length; i++) {
101 String arg = args[i];
102 if (arg.equals("-h")
103 || arg.equals("--help")) {
104 usage();
105
106
107 return;
108 }
109 if (arg.equals("-n")
110 || arg.equals("--noEnhancement")) {
111 noEnhancement = false;
112 continue;
113 }
114 if (arg.equals("-t")
115 || arg.equals("--timing")) {
116 timing = true;
117 enhancerSettings.setProperty(EnhancerClassLoader.DO_TIMING_STATISTICS,
118 "true");
119 continue;
120 }
121 if (arg.equals("-d")
122 || arg.equals("--debug")) {
123 debug = true;
124 enhancerSettings.setProperty(EnhancerClassLoader.VERBOSE_LEVEL,
125 EnhancerClassLoader.VERBOSE_LEVEL_DEBUG);
126 continue;
127 }
128 if (arg.equals("-w")
129 || arg.equals("--warn")) {
130 debug = false;
131 enhancerSettings.setProperty(EnhancerClassLoader.VERBOSE_LEVEL,
132 EnhancerClassLoader.VERBOSE_LEVEL_WARN);
133 continue;
134 }
135 if (arg.equals("-q")
136 || arg.equals("--quiet")) {
137 debug = false;
138 enhancerSettings.setProperty(EnhancerClassLoader.VERBOSE_LEVEL,
139 EnhancerClassLoader.VERBOSE_LEVEL_QUIET);
140 continue;
141 }
142
143
144 targetClassname = arg;
145
146
147 i++;
148 final int length = args.length - i;
149 targetClassArgs = new String[length];
150 System.arraycopy(args, i, targetClassArgs, 0, length);
151 break;
152 }
153
154
155 if (debug) {
156 out.println(prefix + "...");
157 out.println("settings and arguments:");
158 out.println(" classpath = " + classpath);
159 out.println(" noEnhancement = " + noEnhancement);
160 out.println(" debug = " + debug);
161 out.println(" enhancerSettings = {");
162 enhancerSettings.list(out);
163 out.println(" }");
164 out.println(" targetClassname = " + targetClassname);
165 out.print(" targetClassArgs = { ");
166 for (int i = 0; i < targetClassArgs.length; i++) {
167 out.print(targetClassArgs[i] + " ");
168 }
169 out.println("}");
170 }
171
172
173 if (targetClassname == null) {
174 usage();
175 throw new IllegalArgumentException("targetClassname == null");
176 }
177
178
179 final ClassLoader loader;
180 if (noEnhancement) {
181 if (debug) {
182 out.println(prefix + "using system class loader");
183 }
184
185 loader = PersistenceLauncher.class.getClassLoader();
186 } else {
187 if (debug) {
188 out.println(prefix + "creating enhancer class loader");
189 }
190 final Properties settings = enhancerSettings;
191 final PrintWriter out = PersistenceLauncher.out;
192 loader = new EnhancerClassLoader(classpath, settings, out);
193 }
194
195
196 Class clazz;
197 Method main;
198 try {
199 final String mname = "main";
200 final Class[] mparams = new Class[]{ String[].class };
201 final boolean init = true;
202 if (debug) {
203 out.println(prefix + "getting method "
204 + targetClassname + "." + mname + "(String[])");
205 }
206 clazz = Class.forName(targetClassname, init, loader);
207 main = clazz.getDeclaredMethod(mname, mparams);
208 } catch (ClassNotFoundException e) {
209
210 if (debug) {
211 out.flush();
212 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
213 e.printStackTrace(err);
214 err.flush();
215 }
216 throw e;
217 } catch (NoSuchMethodException e) {
218
219 if (debug) {
220 out.flush();
221 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
222 e.printStackTrace(err);
223 err.flush();
224 }
225 throw e;
226 } catch (SecurityException e) {
227
228 if (debug) {
229 out.flush();
230 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
231 e.printStackTrace(err);
232 err.flush();
233 }
234 throw e;
235 }
236
237
238 try {
239 final Object[] margs = new Object[]{ targetClassArgs };
240 if (debug) {
241 out.println("invoking method " + clazz.getName()
242 + "." + main.getName() + "(String[])");
243 }
244 main.invoke(null, margs);
245 } catch (IllegalAccessException e) {
246
247 if (debug) {
248 out.flush();
249 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
250 e.printStackTrace(err);
251 err.flush();
252 }
253 throw e;
254 } catch (IllegalArgumentException e) {
255
256 if (debug) {
257 out.flush();
258 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
259 e.printStackTrace(err);
260 err.flush();
261 }
262 throw e;
263 } catch (InvocationTargetException e) {
264
265 if (debug) {
266 out.flush();
267 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
268 e.printStackTrace(err);
269 err.flush();
270 }
271 throw e;
272 } finally {
273 if (timing) {
274 Support.timer.print();
275 }
276 }
277
278 if (debug) {
279 out.println(prefix + "done.");
280 out.flush();
281 err.flush();
282 }
283 }
284 }