1 /* Copyright (c) 2001-2014, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
foo()30 
31 
32 package org.hsqldb.util;
33 
34 import java.lang.reflect.InvocationTargetException;
35 import java.lang.reflect.Method;
36 import java.util.ArrayList;
37 
38 /**
39  * Invokes the static main(String[]) method from each class specified.
40  *
41  * This class <b>will System.exit()</b> if any invocation fails.
42  *
43  * @author Blaine Simpson (blaine dot simpson at admc dot com)
44  * @since    HSQLDB 1.8.0
45  * @version  $Revision: 5337 $, $Date: 2014-01-24 14:26:47 -0500 (Fri, 24 Jan 2014) $
46  */
47 public class MainInvoker {
48 
49     /*
50      * This class currently consists of just a static utility.
51      * It may or may not make sense to make this into a class with real
52      * instances that can keep track of status of stuff invoked by it.
53      */
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     /**
62      * Invokes the static main(String[]) method from each specified class.
63      * This method <b>will System.exit()</b> if any invocation fails.
64      *
65      * Note that multiple class invocations are delimited by empty-string
66      * parameters.  How the user supplies these empty strings is determined
67      * entirely by the caller's environment.  From Windows this can
68      * generally be accomplished with double-quotes like "".  From all
69      * popular UNIX shells, this can be accomplished with single or
70      * double-quotes:  '' or "".
71      *
72      * @param sa Run java org.hsqldb.util.MainInvoker --help for syntax help
73      */
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     public static final String LS = System.getProperty("line.separator");
112     private static String SYNTAX_MSG =
113         "    java org.hsqldb.util.MainInvoker "
114         + "[package1.Class1 [arg1a arg1b...] \"\"]... \\\n"
115         + "    packageX.ClassX [argXa argXb...]\n" + "OR\n"
116         + "    java org.hsqldb.util.MainInvoker --help\n\n"
117         + "Note that you can only invoke classes in 'named' (non-default) "
118         + "packages.  Delimit multiple classes with empty strings.";
119     static {
120         if (!LS.equals("\n")) {
121             SYNTAX_MSG = SYNTAX_MSG.replaceAll("\n", LS);
122         }
123     }
124 
125     /**
126      * Invokes the static main(String[]) method from each specified class.
127      */
128     public static void invoke(String className,
129                               String[] args)
130                               throws ClassNotFoundException,
131                                      NoSuchMethodException,
132                                      IllegalAccessException,
133                                      InvocationTargetException {
134 
135         Class    c;
136         Method   method;
137         Class[]  stringArrayCA = { emptyStringArray.getClass() };
138         Object[] objectArray   = { (args == null) ? emptyStringArray
139                                                   : args };
140 
141         c      = Class.forName(className);
142         method = c.getMethod("main", stringArrayCA);
143 
144         method.invoke(null, objectArray);
145 
146         //System.err.println(c.getName() + ".main() invoked");
147     }
148 }
149