1 30 31 32 package org.hsqldb.util; 33 34 import java.util.ArrayList ; 35 import java.lang.reflect.Method ; 36 import java.lang.reflect.InvocationTargetException ; 37 38 47 public class MainInvoker { 48 49 54 private static String [] emptyStringArray = new String [0]; 55 56 private static void syntaxFailure() { 57 System.err.println(SYNTAX_MSG); 58 System.exit(2); 59 } 60 61 74 public static void main(String [] sa) { 75 76 if (sa.length > 0 && sa[0].equals("--help")) { 77 System.err.println(SYNTAX_MSG); 78 System.exit(0); 79 } 80 81 ArrayList outList = new ArrayList (); 82 int curInArg = -1; 83 84 try { 85 while (++curInArg < sa.length) { 86 if (sa[curInArg].length() < 1) { 87 if (outList.size() < 1) { 88 syntaxFailure(); 89 } 90 91 invoke((String ) outList.remove(0), 92 (String []) outList.toArray(emptyStringArray)); 93 outList.clear(); 94 } else { 95 outList.add(sa[curInArg]); 96 } 97 } 98 99 if (outList.size() < 1) { 100 syntaxFailure(); 101 } 102 103 invoke((String ) outList.remove(0), 104 (String []) outList.toArray(emptyStringArray)); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 System.exit(1); 108 } 109 } 110 111 private static final String SYNTAX_MSG = 112 " java org.hsqldb.util.MainInvoker " 113 + "[package1.Class1 [arg1a arg1b...] \"\"]... \\\n" 114 + " packageX.ClassX [argXa argXb...]\nOR\n" 115 + " java org.hsqldb.util.MainInvoker --help\n\n" 116 + "Note that you can only invoke classes in 'named' (non-default) " 117 + "packages. Delimit multiple classes with empty strings."; 118 119 122 public static void invoke(String className, 123 String [] args) 124 throws ClassNotFoundException , 125 NoSuchMethodException , 126 IllegalAccessException , 127 InvocationTargetException { 128 129 Class c; 130 Method method; 131 Class [] stringArrayCA = { emptyStringArray.getClass() }; 132 Object [] objectArray = { (args == null) ? emptyStringArray 133 : args }; 134 135 c = Class.forName(className); 136 method = c.getMethod("main", stringArrayCA); 137 138 method.invoke(null, objectArray); 139 140 } 142 } 143 | Popular Tags |