}
}
if ( classWorldFile == null )
{
throw new CommandLineException( "Cannot find plexus-classworlds in " + coreDir );
}
URLClassLoader cl;
try
{
cl = new URLClassLoader( new URL[] { classWorldFile.toURI().toURL() }, null );
}
catch ( MalformedURLException e )
{
throw new CommandLineException( "Cannot conver to url: " + classWorldFile, e );
}
class ExitSecurityException
extends SecurityException
{
private static final long serialVersionUID = 5650869246591762064L;
private int status;
public ExitSecurityException( int status )
{
this.status = status;
}
public int getStatus()
{
return status;
}
}
try
{
Class<?> c = cl.loadClass( "org.codehaus.plexus.classworlds.launcher.Launcher" );
Method m = c.getMethod( "mainWithExitCode", new Class[] { String[].class } );
SecurityManager oldSm = System.getSecurityManager();
try
{
System.setSecurityManager( new SecurityManager()
{
public void checkPermission( Permission perm )
{
// ok
}
public void checkExit( int status )
{
throw new ExitSecurityException( status );
}
} );
}
catch ( AccessControlException e )
{
throw new CommandLineException( "Error isntalling securitymanager", e );
}
cli.createArgument().setValue( "-f" );
cli.createArgument().setValue( cli.getWorkingDirectory().getAbsolutePath() + "/pom.xml" );
PrintStream oldOut = System.out;
PrintStream oldErr = System.err;
String oldCwConf = System.getProperty( "classworlds.conf" );
String oldMavenHome = System.getProperty( "maven.home" );
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader( cl );// ClassLoader.getSystemClassLoader() );
FileOutputStream logWriter = new FileOutputStream( logFile );
System.setOut( new PrintStream( logWriter ) );
System.setErr( new PrintStream( logWriter ) );
System.setProperty( "classworlds.conf", new File( mavenHome, "bin/m2.conf" ).getAbsolutePath() );
System.setProperty( "maven.home", mavenHome );
return ( (Integer) m.invoke( null, new Object[] { cli.getArguments() } ) ).intValue();
}
catch ( ExitSecurityException e )
{
oldOut.println( "exit security exception caught: status=" + e.getStatus() );
return e.getStatus();
}
finally
{
System.setOut( oldOut );
System.setErr( oldErr );
if ( oldCwConf == null )
{
System.getProperties().remove( "classworlds.conf" );
}
else
{
System.setProperty( "classworlds.conf", oldCwConf );
}
if ( oldMavenHome == null )
{
System.getProperties().remove( "maven.home" );
}
else
{
System.setProperty( "maven.home", oldMavenHome );
}
Thread.currentThread().setContextClassLoader( oldCl );
System.setSecurityManager( oldSm );
}
}
catch ( ClassNotFoundException e )
{
throw new CommandLineException( "Cannot load classworlds launcher", e );
}
catch ( NoSuchMethodException e )
{
throw new CommandLineException( "Cannot find classworlds launcher's main method", e );
}
catch ( IllegalArgumentException e )
{
throw new CommandLineException( "Error executing classworlds launcher's main method", e );
}
catch ( InvocationTargetException e )
{
if ( e.getCause() instanceof ExitSecurityException )
{
return ( (ExitSecurityException) e.getCause() ).getStatus();
}
throw new CommandLineException( "Error executing classworlds launcher's main method", e );
}
catch ( IllegalAccessException e )
{
throw new CommandLineException( "Error executing classworlds launcher's main method", e );
}
}