KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > PersistenceLauncher


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * PersistenceLauncher.java
26  *
27  * Created on July 3, 2000, 8:22 AM
28  */

29
30 package com.sun.jdo.api.persistence.enhancer;
31
32 import java.util.Properties JavaDoc;
33
34 import java.io.PrintWriter JavaDoc;
35
36 import java.lang.reflect.Method JavaDoc;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38
39 import com.sun.jdo.api.persistence.enhancer.util.Support;
40
41
42 /**
43  * Application launcher for persistence-capable classes.
44  * @author Martin Zaun
45  * @version 1.1
46  */

47 public class PersistenceLauncher {
48
49     // chose whether to separate or join out and err channels
50
//static private final PrintWriter err = new PrintWriter(System.err, true);
51
static private final PrintWriter JavaDoc err = new PrintWriter JavaDoc(System.out, true);
52     static private final PrintWriter JavaDoc out = new PrintWriter JavaDoc(System.out, true);
53     static private final String JavaDoc prefix = "PersistenceLauncher.main() : ";//NOI18N
54

55     /**
56      * Creates new PersistenceLauncher.
57      */

58     private PersistenceLauncher() {
59     }
60
61     /**
62      * Prints usage message.
63      */

64     static void usage()
65     {
66         out.flush();
67         err.println("PersistenceLauncher:");
68         err.println(" usage: <options> ... <target class name> <args> ...");
69         err.println(" options:");
70         err.println(" -h | --help");
71         err.println(" -n | --noEnhancement");
72         err.println(" -q | --quiet");
73         err.println(" -w | --warn");
74         err.println(" -d | --debug");
75         err.println(" -t | --timing");
76         err.println(" class names have to be fully qualified");
77         err.println("done.");
78         err.println();
79         err.flush();
80     }
81
82     /**
83      * Creates a class loader and launches a target class.
84      * @param args the command line arguments
85      */

86     public static void main(String JavaDoc[] args)
87         throws ClassNotFoundException JavaDoc,
88         NoSuchMethodException JavaDoc,
89         SecurityException JavaDoc,
90         IllegalAccessException JavaDoc,
91         IllegalArgumentException JavaDoc,
92         InvocationTargetException JavaDoc {
93 /*
94         message("property PersistenceExecutor.TAG_REPOSITORY = "
95                 + System.getProperty("PersistenceExecutor.TAG_REPOSITORY"));
96         message("property PersistenceExecutor.TAG_CLASSPATH = "
97                 + System.getProperty("PersistenceExecutor.TAG_CLASSPATH"));
98         message("property PersistenceExecutor.TAG_LIBRARY = "
99                 + System.getProperty("PersistenceExecutor.TAG_LIBRARY"));
100         message("property PersistenceExecutor.TAG_CLASSNAME = "
101                 + System.getProperty("PersistenceExecutor.TAG_CLASSNAME"));
102 */

103
104         // get launcher options
105
final String JavaDoc classpath = System.getProperty("java.class.path");
106         boolean noEnhancement = false;
107         boolean debug = false;
108         boolean timing = false;
109         Properties JavaDoc enhancerSettings = new Properties JavaDoc();
110         String JavaDoc targetClassname = null;
111         String JavaDoc[] targetClassArgs = null;
112         for (int i = 0; i < args.length; i++) {
113             String JavaDoc arg = args[i];
114             if (arg.equals("-h")//NOI18N
115
|| arg.equals("--help")) {//NOI18N
116
usage();
117
118                 // exit gently
119
return;
120             }
121             if (arg.equals("-n")//NOI18N
122
|| arg.equals("--noEnhancement")) {//NOI18N
123
noEnhancement = false;
124                 continue;
125             }
126             if (arg.equals("-t")//NOI18N
127
|| arg.equals("--timing")) {//NOI18N
128
timing = true;
129                 enhancerSettings.setProperty(EnhancerClassLoader.DO_SIMPLE_TIMING,
130                                              "true");//NOI18N
131
continue;
132             }
133             if (arg.equals("-d")//NOI18N
134
|| arg.equals("--debug")) {//NOI18N
135
debug = true;
136                 enhancerSettings.setProperty(EnhancerClassLoader.VERBOSE_LEVEL,
137                                              EnhancerClassLoader.VERBOSE_LEVEL_DEBUG);
138                 continue;
139             }
140             if (arg.equals("-w")//NOI18N
141
|| arg.equals("--warn")) {//NOI18N
142
debug = false;
143                 enhancerSettings.setProperty(EnhancerClassLoader.VERBOSE_LEVEL,
144                                              EnhancerClassLoader.VERBOSE_LEVEL_WARN);
145                 continue;
146             }
147             if (arg.equals("-q")//NOI18N
148
|| arg.equals("--quiet")) {//NOI18N
149
debug = false;
150                 enhancerSettings.setProperty(EnhancerClassLoader.VERBOSE_LEVEL,
151                                              EnhancerClassLoader.VERBOSE_LEVEL_QUIET);
152                 continue;
153             }
154
155             // get target class name
156
targetClassname = arg;
157
158             // copy remaining arguments and leave loop
159
i++;
160             final int length = args.length - i;
161             targetClassArgs = new String JavaDoc[length];
162             System.arraycopy(args, i, targetClassArgs, 0, length);
163             break;
164         }
165
166         // debugging oputput
167
if (debug) {
168             out.println(prefix + "...");//NOI18N
169
out.println("settings and arguments:");//NOI18N
170
out.println(" classpath = " + classpath);//NOI18N
171
out.println(" noEnhancement = " + noEnhancement);//NOI18N
172
out.println(" debug = " + debug);//NOI18N
173
out.println(" enhancerSettings = {");//NOI18N
174
enhancerSettings.list(out);
175             out.println(" }");//NOI18N
176
out.println(" targetClassname = " + targetClassname);//NOI18N
177
out.print(" targetClassArgs = { ");//NOI18N
178
for (int i = 0; i < targetClassArgs.length; i++) {
179                 out.print(targetClassArgs[i] + " ");//NOI18N
180
}
181             out.println("}");//NOI18N
182
}
183
184         // check options
185
if (targetClassname == null) {
186             usage();
187             throw new IllegalArgumentException JavaDoc("targetClassname == null");//NOI18N
188
}
189
190         // get class loader
191
final ClassLoader JavaDoc loader;
192         if (noEnhancement) {
193             if (debug) {
194                 out.println(prefix + "using system class loader");//NOI18N
195
}
196             //out.println("using system class loader");
197
loader = PersistenceLauncher.class.getClassLoader();
198         } else {
199             if (debug) {
200                 out.println(prefix + "creating enhancer class loader");//NOI18N
201
}
202             final Properties JavaDoc settings = enhancerSettings;
203             final PrintWriter JavaDoc out = PersistenceLauncher.out;
204             loader = new EnhancerClassLoader(classpath, settings, out);
205         }
206
207         // get target class' main method
208
Class JavaDoc clazz;
209         Method JavaDoc main;
210         try {
211             final String JavaDoc mname = "main";//NOI18N
212
final Class JavaDoc[] mparams = new Class JavaDoc[]{ String JavaDoc[].class };
213             final boolean init = true;
214             if (debug) {
215                 out.println(prefix + "getting method "//NOI18N
216
+ targetClassname + "." + mname + "(String[])");//NOI18N
217
}
218             clazz = Class.forName(targetClassname, init, loader);
219             main = clazz.getDeclaredMethod(mname, mparams);
220         } catch (ClassNotFoundException JavaDoc e) {
221             // log exception only
222
if (debug) {
223                 out.flush();
224                 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
225                 e.printStackTrace(err);
226                 err.flush();
227             }
228             throw e;
229         } catch (NoSuchMethodException JavaDoc e) {
230             // log exception only
231
if (debug) {
232                 out.flush();
233                 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
234                 e.printStackTrace(err);
235                 err.flush();
236             }
237             throw e;
238         } catch (SecurityException JavaDoc e) {
239             // log exception only
240
if (debug) {
241                 out.flush();
242                 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
243                 e.printStackTrace(err);
244                 err.flush();
245             }
246             throw e;
247         }
248
249         // invoke target class' main method
250
try {
251             final Object JavaDoc[] margs = new Object JavaDoc[]{ targetClassArgs };
252             if (debug) {
253                 out.println("invoking method " + clazz.getName()//NOI18N
254
+ "." + main.getName() + "(String[])");//NOI18N
255
}
256             main.invoke(null, margs);
257         } catch (IllegalAccessException JavaDoc e) {
258             // log exception only
259
if (debug) {
260                 out.flush();
261                 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
262                 e.printStackTrace(err);
263                 err.flush();
264             }
265             throw e;
266         } catch (IllegalArgumentException JavaDoc e) {
267             // log exception only
268
if (debug) {
269                 out.flush();
270                 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
271                 e.printStackTrace(err);
272                 err.flush();
273             }
274             throw e;
275         } catch (InvocationTargetException JavaDoc e) {
276             // log exception only
277
if (debug) {
278                 out.flush();
279                 err.println("PersistenceLauncher: EXCEPTION SEEN: " + e);
280                 e.printStackTrace(err);
281                 err.flush();
282             }
283             throw e;
284         } finally {
285             if (timing) {
286                 Support.timer.print();
287             }
288         }
289
290         if (debug) {
291             out.println(prefix + "done.");//NOI18N
292
out.flush();
293             err.flush();
294         }
295     }
296 }
297
Popular Tags