KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > launcher > Main


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.launcher;
88
89 import java.io.File JavaDoc;
90 import java.lang.reflect.Method JavaDoc;
91 import java.net.URL JavaDoc;
92 import java.net.URLClassLoader JavaDoc;
93 import java.security.Policy JavaDoc;
94 import java.util.HashMap JavaDoc;
95 import java.util.Map JavaDoc;
96
97 /**
98  * LoomLoader is the class that bootstraps and sets up engine ClassLoader. It
99  * also sets up a default policy that gives full permissions to engine code.
100  *
101  * @author Peter Donald
102  */

103 public final class Main
104 {
105     private static Object JavaDoc c_frontend;
106     private static ShutdownHook c_hook;
107
108     /**
109      * Main entry point for Loom.
110      *
111      * @param args the command line arguments
112      * @throws java.lang.Exception if an error occurs
113      */

114     public static final void main( final String JavaDoc[] args )
115         throws Exception JavaDoc
116     {
117         final int exitCode =
118             startup( args, new HashMap JavaDoc(), true );
119         System.exit( exitCode );
120     }
121
122     /**
123      * Method to call to startup Loom from an external (calling) application.
124      * Protected to allow access from DaemonLauncher.
125      *
126      * @param options the command line arg array
127      * @param data a set of extra parameters to pass to embeddor
128      * @param blocking false if the current thread is expected to return.
129      * @return the exit code which should be used to exit the JVM
130      */

131     protected static final int startup( final String JavaDoc[] options,
132                                         final Map JavaDoc data,
133                                         final boolean blocking )
134     {
135         int exitCode;
136         try
137         {
138             //setup new Policy manager
139
Policy.setPolicy( new FreeNEasyPolicy() );
140
141             //Create engine ClassLoader
142
final File JavaDoc homeDir =
143                 LauncherUtils.findLoomHome( "loom.home", "loom-launcher.jar" );
144
145             final URL JavaDoc[] urls =
146                 LauncherUtils.generateClassPath( homeDir, "container/lib" );
147             final URLClassLoader JavaDoc classLoader = new URLClassLoader JavaDoc( urls );
148
149             data.put( ClassLoader JavaDoc.class.getName() + "/common",
150                       ClassLoader.getSystemClassLoader() );
151             data.put( ClassLoader JavaDoc.class.getName() + "/container",
152                       classLoader );
153             data.put( File JavaDoc.class.getName() + "/home", homeDir );
154
155             //Setup context classloader
156
Thread.currentThread().setContextClassLoader( classLoader );
157
158             //Create main launcher
159
final Class JavaDoc clazz =
160                 classLoader.loadClass( "org.codehaus.loom.frontends.CLIMain" );
161             final Class JavaDoc[] paramTypes =
162                 new Class JavaDoc[]{options.getClass(), Map JavaDoc.class, Boolean.TYPE};
163             final Method JavaDoc method = clazz.getMethod( "main", paramTypes );
164
165             final Object JavaDoc frontend;
166             synchronized( Main.class )
167             {
168                 c_frontend = clazz.newInstance();
169                 frontend = c_frontend;
170             }
171
172             //By default add a shutdown hook that will invoke
173
//shutdown on container when JVM shuts down
174
final String JavaDoc enableShutdownHook =
175                 System.getProperty( "disable.shutdown.hook" );
176             if( !"false".equals( enableShutdownHook ) )
177             {
178                 c_hook = new ShutdownHook();
179                 Runtime.getRuntime().addShutdownHook( c_hook );
180             }
181
182             //kick the tires and light the fires....
183
final Object JavaDoc[] args =
184                 new Object JavaDoc[]{options, data, new Boolean JavaDoc( blocking )};
185             final Integer JavaDoc integer =
186                 (Integer JavaDoc)method.invoke( frontend, args );
187             exitCode = integer.intValue();
188         }
189         catch( final Exception JavaDoc e )
190         {
191             e.printStackTrace();
192             exitCode = 1;
193         }
194         return exitCode;
195     }
196
197     /**
198      * Method to call to shutdown Loom from an external (calling) application.
199      * Protected to allow access from DaemonLauncher.
200      */

201     protected static final void shutdown()
202     {
203         final Object JavaDoc frontend;
204         synchronized( Main.class )
205         {
206             frontend = c_frontend;
207             c_frontend = null;
208         }
209         if( null == frontend )
210         {
211             return;
212         }
213         if( null != c_hook && c_hook != Thread.currentThread() )
214         {
215             //Null hook so it is not tried to be removed
216
//when we are shutting down. (Attempting to remove
217
//hook during shutdown raises an exception).
218

219             Runtime.getRuntime().removeShutdownHook( c_hook );
220             c_hook = null;
221         }
222
223         try
224         {
225             final Class JavaDoc clazz = frontend.getClass();
226             final Method JavaDoc method = clazz.getMethod( "shutdown",
227                                                    new Class JavaDoc[ 0 ] );
228
229             //Lets put this sucker to sleep
230
method.invoke( frontend, new Object JavaDoc[ 0 ] );
231         }
232         catch( final Exception JavaDoc e )
233         {
234             e.printStackTrace();
235         }
236     }
237 }
238
Popular Tags