1 23 24 29 30 package com.sun.jdo.api.persistence.enhancer; 31 32 import java.util.Properties ; 33 34 import java.io.PrintWriter ; 35 36 import java.lang.reflect.Method ; 37 import java.lang.reflect.InvocationTargetException ; 38 39 import com.sun.jdo.api.persistence.enhancer.util.Support; 40 41 42 47 public class PersistenceLauncher { 48 49 static private final PrintWriter err = new PrintWriter (System.out, true); 52 static private final PrintWriter out = new PrintWriter (System.out, true); 53 static private final String prefix = "PersistenceLauncher.main() : "; 55 58 private PersistenceLauncher() { 59 } 60 61 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 86 public static void main(String [] args) 87 throws ClassNotFoundException , 88 NoSuchMethodException , 89 SecurityException , 90 IllegalAccessException , 91 IllegalArgumentException , 92 InvocationTargetException { 93 103 104 final String classpath = System.getProperty("java.class.path"); 106 boolean noEnhancement = false; 107 boolean debug = false; 108 boolean timing = false; 109 Properties enhancerSettings = new Properties (); 110 String targetClassname = null; 111 String [] targetClassArgs = null; 112 for (int i = 0; i < args.length; i++) { 113 String arg = args[i]; 114 if (arg.equals("-h") || arg.equals("--help")) { usage(); 117 118 return; 120 } 121 if (arg.equals("-n") || arg.equals("--noEnhancement")) { noEnhancement = false; 124 continue; 125 } 126 if (arg.equals("-t") || arg.equals("--timing")) { timing = true; 129 enhancerSettings.setProperty(EnhancerClassLoader.DO_SIMPLE_TIMING, 130 "true"); continue; 132 } 133 if (arg.equals("-d") || arg.equals("--debug")) { debug = true; 136 enhancerSettings.setProperty(EnhancerClassLoader.VERBOSE_LEVEL, 137 EnhancerClassLoader.VERBOSE_LEVEL_DEBUG); 138 continue; 139 } 140 if (arg.equals("-w") || arg.equals("--warn")) { debug = false; 143 enhancerSettings.setProperty(EnhancerClassLoader.VERBOSE_LEVEL, 144 EnhancerClassLoader.VERBOSE_LEVEL_WARN); 145 continue; 146 } 147 if (arg.equals("-q") || arg.equals("--quiet")) { debug = false; 150 enhancerSettings.setProperty(EnhancerClassLoader.VERBOSE_LEVEL, 151 EnhancerClassLoader.VERBOSE_LEVEL_QUIET); 152 continue; 153 } 154 155 targetClassname = arg; 157 158 i++; 160 final int length = args.length - i; 161 targetClassArgs = new String [length]; 162 System.arraycopy(args, i, targetClassArgs, 0, length); 163 break; 164 } 165 166 if (debug) { 168 out.println(prefix + "..."); out.println("settings and arguments:"); out.println(" classpath = " + classpath); out.println(" noEnhancement = " + noEnhancement); out.println(" debug = " + debug); out.println(" enhancerSettings = {"); enhancerSettings.list(out); 175 out.println(" }"); out.println(" targetClassname = " + targetClassname); out.print(" targetClassArgs = { "); for (int i = 0; i < targetClassArgs.length; i++) { 179 out.print(targetClassArgs[i] + " "); } 181 out.println("}"); } 183 184 if (targetClassname == null) { 186 usage(); 187 throw new IllegalArgumentException ("targetClassname == null"); } 189 190 final ClassLoader loader; 192 if (noEnhancement) { 193 if (debug) { 194 out.println(prefix + "using system class loader"); } 196 loader = PersistenceLauncher.class.getClassLoader(); 198 } else { 199 if (debug) { 200 out.println(prefix + "creating enhancer class loader"); } 202 final Properties settings = enhancerSettings; 203 final PrintWriter out = PersistenceLauncher.out; 204 loader = new EnhancerClassLoader(classpath, settings, out); 205 } 206 207 Class clazz; 209 Method main; 210 try { 211 final String mname = "main"; final Class [] mparams = new Class []{ String [].class }; 213 final boolean init = true; 214 if (debug) { 215 out.println(prefix + "getting method " + targetClassname + "." + mname + "(String[])"); } 218 clazz = Class.forName(targetClassname, init, loader); 219 main = clazz.getDeclaredMethod(mname, mparams); 220 } catch (ClassNotFoundException e) { 221 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 e) { 230 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 e) { 239 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 try { 251 final Object [] margs = new Object []{ targetClassArgs }; 252 if (debug) { 253 out.println("invoking method " + clazz.getName() + "." + main.getName() + "(String[])"); } 256 main.invoke(null, margs); 257 } catch (IllegalAccessException e) { 258 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 e) { 267 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 e) { 276 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."); out.flush(); 293 err.flush(); 294 } 295 } 296 } 297 | Popular Tags |