1 8 package org.apache.avalon.phoenix.launcher; 9 10 import java.io.File ; 11 import java.lang.reflect.Method ; 12 import java.net.URL ; 13 import java.net.URLClassLoader ; 14 import java.security.CodeSource ; 15 import java.security.PermissionCollection ; 16 import java.security.Permissions ; 17 import java.security.Policy ; 18 import java.util.ArrayList ; 19 import java.util.Map ; 20 import java.util.StringTokenizer ; 21 import java.util.HashMap ; 22 23 29 public final class Main 30 { 31 private static final String MAIN_CLASS = 32 "org.apache.avalon.phoenix.frontends.CLIMain"; 33 private static final String LOADER_JAR = "phoenix-loader.jar"; 34 35 private static Object c_frontend; 36 37 43 public static final void main( final String [] args ) 44 throws Exception 45 { 46 final int exitCode = 47 startup( args, new HashMap (), true ); 48 System.exit( exitCode ); 49 } 50 51 64 protected static final int startup( final String [] args, 65 final Map data, 66 final boolean blocking ) 67 throws Exception 68 { 69 int exitCode; 70 try 71 { 72 Policy.setPolicy( new FreeNEasyPolicy() ); 74 75 final URL [] urls = getEngineClassPath(); 77 final URLClassLoader classLoader = new URLClassLoader ( urls ); 78 79 data.put( "common.classloader", ClassLoader.getSystemClassLoader() ); 80 data.put( "container.classloader", classLoader ); 81 data.put( "phoenix.home", new File ( findPhoenixHome() ) ); 82 83 Thread.currentThread().setContextClassLoader( classLoader ); 85 86 final Class clazz = classLoader.loadClass( MAIN_CLASS ); 88 final Class [] paramTypes = 89 new Class []{args.getClass(), Map .class, Boolean.TYPE}; 90 final Method method = clazz.getMethod( "main", paramTypes ); 91 c_frontend = clazz.newInstance(); 92 93 final Integer integer = (Integer )method.invoke( 95 c_frontend, new Object []{args, data, new Boolean ( blocking )} ); 96 exitCode = integer.intValue(); 97 } 98 catch( final Exception e ) 99 { 100 e.printStackTrace(); 101 exitCode = 1; 102 } 103 return exitCode; 104 } 105 106 111 protected static final void shutdown() 112 { 113 if( null == c_frontend ) 114 { 115 return; 116 } 117 118 try 119 { 120 final Class clazz = c_frontend.getClass(); 121 final Method method = clazz.getMethod( "shutdown", new Class [ 0 ] ); 122 123 method.invoke( c_frontend, new Object [ 0 ] ); 125 } 126 catch( final Exception e ) 127 { 128 e.printStackTrace(); 129 } 130 finally 131 { 132 c_frontend = null; 133 } 134 } 135 136 142 private static URL [] getEngineClassPath() 143 throws Exception 144 { 145 final ArrayList urls = new ArrayList (); 146 147 final File dir = findEngineLibDir(); 148 final File [] files = dir.listFiles(); 149 for( int i = 0; i < files.length; i++ ) 150 { 151 final File file = files[ i ]; 152 if( file.getName().endsWith( ".jar" ) ) 153 { 154 urls.add( file.toURL() ); 155 } 156 } 157 158 return (URL [])urls.toArray( new URL [ urls.size() ] ); 159 } 160 161 167 private static File findEngineLibDir() 168 throws Exception 169 { 170 final String phoenixHome = findPhoenixHome(); 171 final String engineLibDir = 172 phoenixHome + File.separator + "bin" + File.separator + "lib"; 173 final File dir = new File ( engineLibDir ).getCanonicalFile(); 174 if( !dir.exists() ) 175 { 176 throw new Exception ( "Unable to locate engine lib directory at " + engineLibDir ); 177 } 178 return dir; 179 } 180 181 189 private static String findPhoenixHome() 190 throws Exception 191 { 192 String phoenixHome = System.getProperty( "phoenix.home", null ); 193 if( null == phoenixHome ) 194 { 195 final File loaderDir = findLoaderDir(); 196 phoenixHome = loaderDir.getAbsoluteFile().getParentFile() + File.separator; 197 } 198 199 phoenixHome = (new File ( phoenixHome )).getCanonicalFile().toString(); 200 System.setProperty( "phoenix.home", phoenixHome ); 201 return phoenixHome; 202 } 203 204 207 private static final File findLoaderDir() 208 throws Exception 209 { 210 final String classpath = System.getProperty( "java.class.path" ); 211 final String pathSeparator = System.getProperty( "path.separator" ); 212 final StringTokenizer tokenizer = new StringTokenizer ( classpath, pathSeparator ); 213 214 while( tokenizer.hasMoreTokens() ) 215 { 216 final String element = tokenizer.nextToken(); 217 218 if( element.endsWith( LOADER_JAR ) ) 219 { 220 File file = (new File ( element )).getCanonicalFile(); 221 file = file.getParentFile(); 222 return file; 223 } 224 } 225 226 throw new Exception ( "Unable to locate " + LOADER_JAR + " in classpath" ); 227 } 228 229 233 private static class FreeNEasyPolicy 234 extends Policy 235 { 236 public PermissionCollection getPermissions( final CodeSource codeSource ) 237 { 238 final Permissions permissions = new Permissions (); 239 permissions.add( new java.security.AllPermission () ); 240 return permissions; 241 } 242 243 public void refresh() 244 { 245 } 246 } 247 } 248 | Popular Tags |