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
;
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
;
28 * @author Graeme Rocher
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'");
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;
47 while (args
.length
-argsOffset
>0 && !(hadMain
&& hadConf
&& hadCP
)) {
48 if (args
[argsOffset
].equals("--classpath")) {
50 if (args
.length
==argsOffset
+1) {
51 exit("classpath parameter needs argument");
53 lc
.addClassPath(args
[argsOffset
+1]);
55 } else if (args
[argsOffset
].equals("--main")) {
57 if (args
.length
==argsOffset
+1) {
58 exit("main parameter needs argument");
60 lc
.setMainClass(args
[argsOffset
+1]);
62 } else if (args
[argsOffset
].equals("--conf")) {
64 if (args
.length
==argsOffset
+1) {
65 exit("conf parameter needs argument");
67 conf
=args
[argsOffset
+1];
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
87 lc
.configure(new FileInputStream(conf
));
88 } catch (Exception e
) {
89 System
.err
.println("exception while configuring main class loader:");
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;
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
];
114 } catch (IOException e
) {
115 System
.out
.println("WARNING: I/O error reading VM specific classpath ["+vmConfig
+"]: " + e
.getMessage() );
119 if(in
!= null) in
.close();
120 } catch (IOException e
) {
130 Class c
= loader
.loadClass(lc
.getMainClass());
131 m
= c
.getMethod("main", new Class
[]{String
[].class});
132 } catch (ClassNotFoundException e1
) {
134 } catch (SecurityException e2
) {
136 } catch (NoSuchMethodException e2
) {
140 m
.invoke(null, new Object
[]{newArgs
});
141 } catch (IllegalArgumentException e3
) {
143 } catch (IllegalAccessException e3
) {
145 } catch (InvocationTargetException e3
) {
150 private static void exit(Exception e
) {
155 private static void exit(String msg
) {
156 System
.err
.println(msg
);
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
[]) {
166 } catch (Throwable t
) {
167 System
.out
.println("Error starting Grails: " + t
.getMessage());
168 t
.printStackTrace(System
.err
);