KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > maven > agent > Main


1 package hudson.maven.agent;
2
3 import org.codehaus.classworlds.ClassRealm;
4 import org.codehaus.classworlds.Launcher;
5 import org.codehaus.classworlds.NoSuchRealmException;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12
13 /**
14  * Entry point for launching Maven and Hudson remoting in the same VM,
15  * in the classloader layout that Maven expects.
16  *
17  * <p>
18  * The actual Maven execution will be started by the program sent
19  * through remoting.
20  *
21  * @author Kohsuke Kawaguchi
22  */

23 public class Main {
24     /**
25      * Used to pass the classworld instance to the code running inside the remoting system.
26      */

27     private static Launcher launcher;
28
29     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
30         main(new File JavaDoc(args[0]),new File JavaDoc(args[1]),new File JavaDoc(args[2]));
31     }
32
33     public static void main(File JavaDoc m2Home, File JavaDoc remotingJar, File JavaDoc interceptorJar) throws Exception JavaDoc {
34         System.setProperty("maven.home",m2Home.getPath());
35         System.setProperty("maven.interceptor",interceptorJar.getPath());
36
37         // load the default realms
38
launcher = new Launcher();
39         launcher.setSystemClassLoader(Main.class.getClassLoader());
40         launcher.configure(Main.class.getResourceAsStream("classworlds.conf"));
41
42         // have it eventually delegate to this class so that this can be visible
43

44         // create a realm for loading remoting subsystem.
45
// this needs to be able to see maven.
46
ClassRealm remoting = launcher.getWorld().getRealm("plexus.core.maven").createChildRealm("hudson-remoting");
47         remoting.addConstituent(remotingJar.toURL());
48
49         // we'll use stdin/out to talk to the host,
50
// so make sure Maven won't touch them later
51
OutputStream JavaDoc os = System.out;
52         System.setOut(System.err);
53         InputStream JavaDoc is = System.in;
54         System.setIn(new ByteArrayInputStream JavaDoc(new byte[0]));
55
56         Class JavaDoc remotingLauncher = remoting.loadClass("hudson.remoting.Launcher");
57         remotingLauncher.getMethod("main",InputStream JavaDoc.class,OutputStream JavaDoc.class).invoke(null,is,os);
58         System.exit(0);
59     }
60
61     /**
62      * Called by the code in remoting to launch.
63      */

64     public static int launch(String JavaDoc[] args) throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, NoSuchRealmException, InvocationTargetException JavaDoc, ClassNotFoundException JavaDoc {
65         launcher.launch(args);
66         return launcher.getExitCode();
67     }
68 }
Popular Tags