1 87 package org.codehaus.loom.launcher; 88 89 import java.io.File ; 90 import java.lang.reflect.Method ; 91 import java.net.URL ; 92 import java.net.URLClassLoader ; 93 import java.security.Policy ; 94 import java.util.HashMap ; 95 import java.util.Map ; 96 97 103 public final class Main 104 { 105 private static Object c_frontend; 106 private static ShutdownHook c_hook; 107 108 114 public static final void main( final String [] args ) 115 throws Exception 116 { 117 final int exitCode = 118 startup( args, new HashMap (), true ); 119 System.exit( exitCode ); 120 } 121 122 131 protected static final int startup( final String [] options, 132 final Map data, 133 final boolean blocking ) 134 { 135 int exitCode; 136 try 137 { 138 Policy.setPolicy( new FreeNEasyPolicy() ); 140 141 final File homeDir = 143 LauncherUtils.findLoomHome( "loom.home", "loom-launcher.jar" ); 144 145 final URL [] urls = 146 LauncherUtils.generateClassPath( homeDir, "container/lib" ); 147 final URLClassLoader classLoader = new URLClassLoader ( urls ); 148 149 data.put( ClassLoader .class.getName() + "/common", 150 ClassLoader.getSystemClassLoader() ); 151 data.put( ClassLoader .class.getName() + "/container", 152 classLoader ); 153 data.put( File .class.getName() + "/home", homeDir ); 154 155 Thread.currentThread().setContextClassLoader( classLoader ); 157 158 final Class clazz = 160 classLoader.loadClass( "org.codehaus.loom.frontends.CLIMain" ); 161 final Class [] paramTypes = 162 new Class []{options.getClass(), Map .class, Boolean.TYPE}; 163 final Method method = clazz.getMethod( "main", paramTypes ); 164 165 final Object frontend; 166 synchronized( Main.class ) 167 { 168 c_frontend = clazz.newInstance(); 169 frontend = c_frontend; 170 } 171 172 final String 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 final Object [] args = 184 new Object []{options, data, new Boolean ( blocking )}; 185 final Integer integer = 186 (Integer )method.invoke( frontend, args ); 187 exitCode = integer.intValue(); 188 } 189 catch( final Exception e ) 190 { 191 e.printStackTrace(); 192 exitCode = 1; 193 } 194 return exitCode; 195 } 196 197 201 protected static final void shutdown() 202 { 203 final Object 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 219 Runtime.getRuntime().removeShutdownHook( c_hook ); 220 c_hook = null; 221 } 222 223 try 224 { 225 final Class clazz = frontend.getClass(); 226 final Method method = clazz.getMethod( "shutdown", 227 new Class [ 0 ] ); 228 229 method.invoke( frontend, new Object [ 0 ] ); 231 } 232 catch( final Exception e ) 233 { 234 e.printStackTrace(); 235 } 236 } 237 } 238 | Popular Tags |