View Javadoc

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