GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / org / codehaus / groovy / grails / cli / support / GrailsStarter.java
blob318bc402a0293389df35aabed8e4d69951726d25
1 /* Copyright 2004-2005 Graeme Rocher
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 package org.codehaus.groovy.grails.cli.support;
17 import org.codehaus.groovy.tools.LoaderConfiguration;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.InputStream;
22 import java.io.IOException;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.net.URL;
27 /**
28 * @author Graeme Rocher
29 * @since 1.0
30 * <p/>
31 * Created: Nov 29, 2007
33 public class GrailsStarter {
34 static void printUsage() {
35 System.out.println("possible programs are 'groovyc','groovy','console', and 'groovysh'");
36 System.exit(1);
40 public static void rootLoader(String args[]) {
41 String conf = System.getProperty("groovy.starter.conf",null);
42 LoaderConfiguration lc = new LoaderConfiguration();
44 // evaluate parameters
45 boolean hadMain=false, hadConf=false, hadCP=false;
46 int argsOffset = 0;
47 while (args.length-argsOffset>0 && !(hadMain && hadConf && hadCP)) {
48 if (args[argsOffset].equals("--classpath")) {
49 if (hadCP) break;
50 if (args.length==argsOffset+1) {
51 exit("classpath parameter needs argument");
53 lc.addClassPath(args[argsOffset+1]);
54 argsOffset+=2;
55 } else if (args[argsOffset].equals("--main")) {
56 if (hadMain) break;
57 if (args.length==argsOffset+1) {
58 exit("main parameter needs argument");
60 lc.setMainClass(args[argsOffset+1]);
61 argsOffset+=2;
62 } else if (args[argsOffset].equals("--conf")) {
63 if (hadConf) break;
64 if (args.length==argsOffset+1) {
65 exit("conf parameter needs argument");
67 conf=args[argsOffset+1];
68 argsOffset+=2;
69 } else {
70 break;
74 // we need to know the class we want to start
75 if (lc.getMainClass()==null && conf==null) {
76 exit("no configuration file or main class specified");
79 // copy arguments for main class
80 String[] newArgs = new String[args.length-argsOffset];
81 for (int i=0; i<newArgs.length; i++) {
82 newArgs[i] = args[i+argsOffset];
84 // load configuration file
85 if (conf!=null) {
86 try {
87 lc.configure(new FileInputStream(conf));
88 } catch (Exception e) {
89 System.err.println("exception while configuring main class loader:");
90 exit(e);
93 // create loader and execute main class
94 GrailsRootLoader loader = new GrailsRootLoader(lc);
96 String javaVersion = System.getProperty("java.version");
97 String grailsHome = System.getProperty("grails.home");
99 if(javaVersion != null && grailsHome != null) {
100 javaVersion = javaVersion.substring(0,3);
101 File vmConfig = new File(grailsHome +"/conf/groovy-starter-java-"+javaVersion+".conf");
102 if(vmConfig.exists()) {
103 InputStream in = null;
104 try {
105 in = new FileInputStream(vmConfig);
106 LoaderConfiguration vmLoaderConfig = new LoaderConfiguration();
107 vmLoaderConfig.setRequireMain(false);
108 vmLoaderConfig.configure(in);
109 URL[] vmSpecificClassPath = vmLoaderConfig.getClassPathUrls();
110 for (int i = 0; i < vmSpecificClassPath.length; i++) {
111 URL url = vmSpecificClassPath[i];
112 loader.addURL(url);
114 } catch (IOException e) {
115 System.out.println("WARNING: I/O error reading VM specific classpath ["+vmConfig+"]: " + e.getMessage() );
117 finally {
118 try {
119 if(in != null) in.close();
120 } catch (IOException e) {
121 // ignore
128 Method m=null;
129 try {
130 Class c = loader.loadClass(lc.getMainClass());
131 m = c.getMethod("main", new Class[]{String[].class});
132 } catch (ClassNotFoundException e1) {
133 exit(e1);
134 } catch (SecurityException e2) {
135 exit(e2);
136 } catch (NoSuchMethodException e2) {
137 exit(e2);
139 try {
140 m.invoke(null, new Object[]{newArgs});
141 } catch (IllegalArgumentException e3) {
142 exit(e3);
143 } catch (IllegalAccessException e3) {
144 exit(e3);
145 } catch (InvocationTargetException e3) {
146 exit(e3);
150 private static void exit(Exception e) {
151 e.printStackTrace();
152 System.exit(1);
155 private static void exit(String msg) {
156 System.err.println(msg);
157 System.exit(1);
160 // after migration from classworlds to the rootloader rename
161 // the rootLoader method to main and remove this method as
162 // well as the classworlds method
163 public static void main(String args[]) {
164 try {
165 rootLoader(args);
166 } catch (Throwable t) {
167 System.out.println("Error starting Grails: " + t.getMessage());
168 t.printStackTrace(System.err);
169 System.exit(1);