package hudson.maven.agent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.codehaus.classworlds.ClassRealm;
import org.codehaus.classworlds.ClassWorld;
import org.codehaus.classworlds.DefaultClassRealm;
import org.codehaus.classworlds.Launcher;
import org.codehaus.classworlds.NoSuchRealmException;
private static Launcher launcher;
public static void main(String[] args)
throws Exception {
main(new File(args[0]),new File(args[1]),new File(args[2]),Integer.parseInt(args[3]),
args.length==4?null:new File(args[4]));
}
public static void main(File m2Home, File remotingJar, File interceptorJar,
int tcpPort, File interceptorOverrideJar)
throws Exception {
try {
m2Home = m2Home.getCanonicalFile();
} catch (IOException e) {
}
if(!m2Home.exists()) {
System.err.println("No such directory exists: "+m2Home);
System.exit(1);
}
versionCheck();
System.setProperty("maven.home",m2Home.getPath());
System.setProperty("maven.interceptor",interceptorJar.getPath());
System.setProperty("maven.interceptor.override",
(interceptorOverrideJar!=null?interceptorOverrideJar:interceptorJar).getPath());
boolean is206OrLater = !new File(m2Home,"core").exists();
launcher = new Launcher();
launcher.setSystemClassLoader(Main.class.getClassLoader());
launcher.configure(Main.class.getResourceAsStream(
is206OrLater?"classworlds-2.0.6.conf":"classworlds.conf"));
ClassRealm remoting = new DefaultClassRealm(launcher.getWorld(),"hudson-remoting", launcher.getSystemClassLoader());
remoting.setParent(launcher.getWorld().getRealm("plexus.core.maven"));
remoting.addConstituent(remotingJar.toURI().toURL());
final Socket s = new Socket((String)null,tcpPort);
Class remotingLauncher = remoting.loadClass("hudson.remoting.Launcher");
remotingLauncher.getMethod("main",new Class[]{InputStream.class,OutputStream.class}).invoke(null,
new Object[]{
new BufferedInputStream(new FilterInputStream(s.getInputStream()) {
public void close()
throws IOException {
s.shutdownInput();
}
}),
new BufferedOutputStream(new RealFilterOutputStream(s.getOutputStream()) {
public void close()
throws IOException {
s.shutdownOutput();
}
})
});
System.exit(0);
}
String v = System.getProperty("java.class.version");
if(v!=null) {
try {
if(Float.parseFloat(v)<49.0) {
System.err.println("Native maven support requires Java 1.5 or later, but this Maven is using "+System.getProperty("java.home"));
System.err.println("Please use the freestyle project.");
System.exit(1);
}
} catch (NumberFormatException e) {
}
}
}
public static int launch(String[] args)
throws NoSuchMethodException, IllegalAccessException, NoSuchRealmException, InvocationTargetException, ClassNotFoundException {
ClassWorld world = launcher.getWorld();
Set builtinRealms = new HashSet(world.getRealms());
try {
launcher.launch(args);
} finally {
Set all = new HashSet(world.getRealms());
all.removeAll(builtinRealms);
for (Iterator itr = all.iterator(); itr.hasNext();) {
ClassRealm cr = (ClassRealm) itr.next();
world.disposeRealm(cr.getId());
}
}
return launcher.getExitCode();
}
}