KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > MainInvoker


1 /* Copyright (c) 2001-2005, 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  */

30
31
32 package org.hsqldb.util;
33
34 import java.util.ArrayList JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36 import java.lang.reflect.InvocationTargetException JavaDoc;
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, unsaved@users
44  * @since HSQLDB 1.8.0
45  * @version $Revision: 1.6 $
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 JavaDoc[] emptyStringArray = new String JavaDoc[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 JavaDoc[] 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 JavaDoc outList = new ArrayList JavaDoc();
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 JavaDoc) outList.remove(0),
92                            (String JavaDoc[]) 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 JavaDoc) outList.remove(0),
104                    (String JavaDoc[]) outList.toArray(emptyStringArray));
105         } catch (Exception JavaDoc e) {
106             e.printStackTrace();
107             System.exit(1);
108         }
109     }
110
111     private static final String JavaDoc 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     /**
120      * Invokes the static main(String[]) method from each specified class.
121      */

122     public static void invoke(String JavaDoc className,
123                               String JavaDoc[] args)
124                               throws ClassNotFoundException JavaDoc,
125                                      NoSuchMethodException JavaDoc,
126                                      IllegalAccessException JavaDoc,
127                                      InvocationTargetException JavaDoc {
128
129         Class JavaDoc c;
130         Method JavaDoc method;
131         Class JavaDoc[] stringArrayCA = { emptyStringArray.getClass() };
132         Object JavaDoc[] 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         //System.err.println(c.getName() + ".main() invoked");
141
}
142 }
143
Popular Tags