KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > launcher > Main


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.launcher;
9
10 import java.io.File JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.net.URLClassLoader JavaDoc;
14 import java.security.CodeSource JavaDoc;
15 import java.security.PermissionCollection JavaDoc;
16 import java.security.Permissions JavaDoc;
17 import java.security.Policy JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 /**
24  * PhoenixLoader is the class that bootstraps and sets up engine ClassLoader.
25  * It also sets up a default policy that gives full permissions to engine code.
26  *
27  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
28  */

29 public final class Main
30 {
31     private static final String JavaDoc MAIN_CLASS =
32         "org.apache.avalon.phoenix.frontends.CLIMain";
33     private static final String JavaDoc LOADER_JAR = "phoenix-loader.jar";
34
35     private static Object JavaDoc c_frontend;
36
37     /**
38      * Main entry point for Phoenix.
39      *
40      * @param args the command line arguments
41      * @throws Exception if an error occurs
42      */

43     public static final void main( final String JavaDoc[] args )
44         throws Exception JavaDoc
45     {
46         final int exitCode =
47             startup( args, new HashMap JavaDoc(), true );
48         System.exit( exitCode );
49     }
50
51     /**
52      * Method to call to startup Phoenix from an
53      * external (calling) application. Protected to allow
54      * access from DaemonLauncher.
55      *
56      * @param args the command line arg array
57      * @param data a set of extra parameters to pass to embeddor
58      * @param blocking false if the current thread is expected to return.
59      *
60      * @return the exit code which should be used to exit the JVM
61      *
62      * @throws Exception if an error occurs
63      */

64     protected static final int startup( final String JavaDoc[] args,
65                                         final Map JavaDoc data,
66                                         final boolean blocking )
67         throws Exception JavaDoc
68     {
69         int exitCode;
70         try
71         {
72             //setup new Policy manager
73
Policy.setPolicy( new FreeNEasyPolicy() );
74
75             //Create engine ClassLoader
76
final URL JavaDoc[] urls = getEngineClassPath();
77             final URLClassLoader JavaDoc classLoader = new URLClassLoader JavaDoc( urls );
78
79             data.put( "common.classloader", ClassLoader.getSystemClassLoader() );
80             data.put( "container.classloader", classLoader );
81             data.put( "phoenix.home", new File JavaDoc( findPhoenixHome() ) );
82
83             //Setup context classloader
84
Thread.currentThread().setContextClassLoader( classLoader );
85
86             //Create main launcher
87
final Class JavaDoc clazz = classLoader.loadClass( MAIN_CLASS );
88             final Class JavaDoc[] paramTypes =
89                 new Class JavaDoc[]{args.getClass(), Map JavaDoc.class, Boolean.TYPE};
90             final Method JavaDoc method = clazz.getMethod( "main", paramTypes );
91             c_frontend = clazz.newInstance();
92
93             //kick the tires and light the fires....
94
final Integer JavaDoc integer = (Integer JavaDoc)method.invoke(
95                 c_frontend, new Object JavaDoc[]{args, data, new Boolean JavaDoc( blocking )} );
96             exitCode = integer.intValue();
97         }
98         catch( final Exception JavaDoc e )
99         {
100             e.printStackTrace();
101             exitCode = 1;
102         }
103         return exitCode;
104     }
105
106     /**
107      * Method to call to shutdown Phoenix from an
108      * external (calling) application. Protected to allow
109      * access from DaemonLauncher.
110      */

111     protected static final void shutdown()
112     {
113         if( null == c_frontend )
114         {
115             return;
116         }
117
118         try
119         {
120             final Class JavaDoc clazz = c_frontend.getClass();
121             final Method JavaDoc method = clazz.getMethod( "shutdown", new Class JavaDoc[ 0 ] );
122
123             //Lets put this sucker to sleep
124
method.invoke( c_frontend, new Object JavaDoc[ 0 ] );
125         }
126         catch( final Exception JavaDoc e )
127         {
128             e.printStackTrace();
129         }
130         finally
131         {
132             c_frontend = null;
133         }
134     }
135
136     /**
137      * Create a ClassPath for the engine.
138      *
139      * @return the set of URLs that engine uses to load
140      * @throws Exception if unable to aquire classpath
141      */

142     private static URL JavaDoc[] getEngineClassPath()
143         throws Exception JavaDoc
144     {
145         final ArrayList JavaDoc urls = new ArrayList JavaDoc();
146
147         final File JavaDoc dir = findEngineLibDir();
148         final File JavaDoc[] files = dir.listFiles();
149         for( int i = 0; i < files.length; i++ )
150         {
151             final File JavaDoc file = files[ i ];
152             if( file.getName().endsWith( ".jar" ) )
153             {
154                 urls.add( file.toURL() );
155             }
156         }
157
158         return (URL JavaDoc[])urls.toArray( new URL JavaDoc[ urls.size() ] );
159     }
160
161     /**
162      * Find directory to load engine specific libraries from.
163      *
164      * @return the lib dir
165      * @throws Exception if unable to aquire directory
166      */

167     private static File JavaDoc findEngineLibDir()
168         throws Exception JavaDoc
169     {
170         final String JavaDoc phoenixHome = findPhoenixHome();
171         final String JavaDoc engineLibDir =
172             phoenixHome + File.separator + "bin" + File.separator + "lib";
173         final File JavaDoc dir = new File JavaDoc( engineLibDir ).getCanonicalFile();
174         if( !dir.exists() )
175         {
176             throw new Exception JavaDoc( "Unable to locate engine lib directory at " + engineLibDir );
177         }
178         return dir;
179     }
180
181     /**
182      * Utility method to find the home directory
183      * of Phoenix and make sure system property is
184      * set to it.
185      *
186      * @return the location of phoenix directory
187      * @throws Exception if unable to locate directory
188      */

189     private static String JavaDoc findPhoenixHome()
190         throws Exception JavaDoc
191     {
192         String JavaDoc phoenixHome = System.getProperty( "phoenix.home", null );
193         if( null == phoenixHome )
194         {
195             final File JavaDoc loaderDir = findLoaderDir();
196             phoenixHome = loaderDir.getAbsoluteFile().getParentFile() + File.separator;
197         }
198
199         phoenixHome = (new File JavaDoc( phoenixHome )).getCanonicalFile().toString();
200         System.setProperty( "phoenix.home", phoenixHome );
201         return phoenixHome;
202     }
203
204     /**
205      * Finds the LOADER_JAR file in the classpath.
206      */

207     private static final File JavaDoc findLoaderDir()
208         throws Exception JavaDoc
209     {
210         final String JavaDoc classpath = System.getProperty( "java.class.path" );
211         final String JavaDoc pathSeparator = System.getProperty( "path.separator" );
212         final StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( classpath, pathSeparator );
213
214         while( tokenizer.hasMoreTokens() )
215         {
216             final String JavaDoc element = tokenizer.nextToken();
217
218             if( element.endsWith( LOADER_JAR ) )
219             {
220                 File JavaDoc file = (new File JavaDoc( element )).getCanonicalFile();
221                 file = file.getParentFile();
222                 return file;
223             }
224         }
225
226         throw new Exception JavaDoc( "Unable to locate " + LOADER_JAR + " in classpath" );
227     }
228
229     /**
230      * Default polic class to give every code base all permssions.
231      * Will be replaced once the kernel loads.
232      */

233     private static class FreeNEasyPolicy
234         extends Policy JavaDoc
235     {
236         public PermissionCollection JavaDoc getPermissions( final CodeSource JavaDoc codeSource )
237         {
238             final Permissions JavaDoc permissions = new Permissions JavaDoc();
239             permissions.add( new java.security.AllPermission JavaDoc() );
240             return permissions;
241         }
242
243         public void refresh()
244         {
245         }
246     }
247 }
248
Popular Tags