001: /*
002: $Id: GroovyStarter.java 4098 2006-10-10 16:09:48Z blackdrag $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package org.codehaus.groovy.tools;
047:
048: import java.lang.reflect.*;
049: import java.io.FileInputStream;
050:
051: /**
052: * Helper class to help classworlds to load classes.
053: */
054: public class GroovyStarter {
055:
056: static void printUsage() {
057: System.out
058: .println("possible programs are 'groovyc','groovy','console','grok' and 'groovysh'");
059: System.exit(1);
060: }
061:
062: public static void rootLoader(String args[]) {
063: String conf = System.getProperty("groovy.starter.conf", null);
064: LoaderConfiguration lc = new LoaderConfiguration();
065:
066: // evaluate parameters
067: boolean hadMain = false, hadConf = false, hadCP = false;
068: int argsOffset = 0;
069: while (args.length - argsOffset > 0
070: && !(hadMain && hadConf && hadCP)) {
071: if (args[argsOffset].equals("--classpath")) {
072: if (hadCP)
073: break;
074: if (args.length == argsOffset + 1) {
075: exit("classpath parameter needs argument");
076: }
077: lc.addClassPath(args[argsOffset + 1]);
078: argsOffset += 2;
079: } else if (args[argsOffset].equals("--main")) {
080: if (hadMain)
081: break;
082: if (args.length == argsOffset + 1) {
083: exit("main parameter needs argument");
084: }
085: lc.setMainClass(args[argsOffset + 1]);
086: argsOffset += 2;
087: } else if (args[argsOffset].equals("--conf")) {
088: if (hadConf)
089: break;
090: if (args.length == argsOffset + 1) {
091: exit("conf parameter needs argument");
092: }
093: conf = args[argsOffset + 1];
094: argsOffset += 2;
095: } else {
096: break;
097: }
098: }
099:
100: // we need to know the class we want to start
101: if (lc.getMainClass() == null && conf == null) {
102: exit("no configuration file or main class specified");
103: }
104:
105: // copy arguments for main class
106: String[] newArgs = new String[args.length - argsOffset];
107: for (int i = 0; i < newArgs.length; i++) {
108: newArgs[i] = args[i + argsOffset];
109: }
110: // load configuration file
111: if (conf != null) {
112: try {
113: lc.configure(new FileInputStream(conf));
114: } catch (Exception e) {
115: System.err
116: .println("exception while configuring main class loader:");
117: exit(e);
118: }
119: }
120: // create loader and execute main class
121: ClassLoader loader = new RootLoader(lc);
122: Method m = null;
123: try {
124: Class c = loader.loadClass(lc.getMainClass());
125: m = c.getMethod("main", new Class[] { String[].class });
126: } catch (ClassNotFoundException e1) {
127: exit(e1);
128: } catch (SecurityException e2) {
129: exit(e2);
130: } catch (NoSuchMethodException e2) {
131: exit(e2);
132: }
133: try {
134: m.invoke(null, new Object[] { newArgs });
135: } catch (IllegalArgumentException e3) {
136: exit(e3);
137: } catch (IllegalAccessException e3) {
138: exit(e3);
139: } catch (InvocationTargetException e3) {
140: exit(e3);
141: }
142: }
143:
144: private static void exit(Exception e) {
145: e.printStackTrace();
146: System.exit(1);
147: }
148:
149: private static void exit(String msg) {
150: System.err.println(msg);
151: System.exit(1);
152: }
153:
154: // after migration from classworlds to the rootloader rename
155: // the rootLoader method to main and remove this method as
156: // well as the classworlds method
157: /* public static void main(String args[],ClassWorld classWorld ) {
158: classworlds(args,classWorld);
159: }*/
160:
161: public static void main(String args[]) {
162: try {
163: rootLoader(args);
164: } catch (Throwable t) {
165: t.printStackTrace();
166: }
167: }
168:
169: /*public static void classworlds(String oldArgs[],ClassWorld classWorld ) {
170: try {
171: // Creates a realm with *just* the system classloader
172: ClassRealm system = classWorld.newRealm("system");
173:
174: // Get the groovy realm
175: ClassRealm groovy = classWorld.getRealm("groovy");
176:
177: // import everything from the system realm, because imports
178: // are searched *first* in Classworlds
179: groovy.importFrom("system", "");
180:
181: //add tools.jar to classpath
182: String tools = System.getProperty("tools.jar");
183: if (tools!=null) {
184: URL ref = (new File(tools)).toURI().toURL();
185: groovy.addConstituent(ref);
186: }
187:
188: if (oldArgs.length==0) {
189: printUsage();
190: System.exit(1);
191: }
192:
193: String program = oldArgs[0].toLowerCase();
194: String[] args = new String[oldArgs.length-1];
195: for (int i=0; i<args.length; i++) {
196: args[i] = oldArgs[i+1];
197: }
198:
199: if (program.equals("groovyc")) {
200: org.codehaus.groovy.tools.FileSystemCompiler.main(args);
201: } else if (program.equals("groovy")) {
202: GroovyMain.main(args);
203: } else if (program.equals("console")) {
204: // work around needed, because the console is compiled after this files
205: Class c = Class.forName("groovy.ui.Console");
206: Method m= c.getMethod("main", new Class[]{String[].class});
207: m.invoke(null, new Object[]{args});
208: } else if (program.equals("groovysh")) {
209: InteractiveShell.main(args);
210: } else if (program.equals("grok")) {
211: org.codehaus.groovy.tools.Grok.main(args);
212: } else {
213: System.out.println("unknown program "+program);
214: printUsage();
215: System.exit(1);
216: }
217:
218: } catch (Exception e) {
219: e.printStackTrace();
220: System.exit(1);
221: }
222:
223: }*/
224:
225: }
|