diff --git a/core/src/main/java/hudson/slaves/Channels.java b/core/src/main/java/hudson/slaves/Channels.java index 971d7b15d2..09214dc9a4 100644 --- a/core/src/main/java/hudson/slaves/Channels.java +++ b/core/src/main/java/hudson/slaves/Channels.java @@ -29,17 +29,15 @@ import hudson.FilePath; import hudson.model.Computer; import hudson.model.TaskListener; import hudson.remoting.Channel; -import hudson.remoting.Which; import hudson.remoting.Launcher; import hudson.remoting.SocketInputStream; import hudson.remoting.SocketOutputStream; -import hudson.util.ArgumentListBuilder; import hudson.util.ClasspathBuilder; +import hudson.util.JVMBuilder; import hudson.util.StreamCopyThread; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -148,7 +146,7 @@ public class Channels { * can be sent over the channel.) But if you have jars that are known to be necessary by the new JVM, * setting it here will improve the classloading performance (by avoiding remote class file transfer.) * Classes in this classpath will also take precedence over any other classes that's sent via the channel - * later, so it's also useful for making sure you gee the version of the classes you want. + * later, so it's also useful for making sure you get the version of the classes you want. * @param systemProperties * If the new JVM should have a certain system properties set. Can be null. * @@ -157,25 +155,51 @@ public class Channels { * @since 1.300 */ public static Channel newJVM(String displayName, TaskListener listener, FilePath workDir, ClasspathBuilder classpath, Map systemProperties) throws IOException { + JVMBuilder vmb = new JVMBuilder(); + vmb.systemProperties(systemProperties); + + return newJVM(displayName,listener,vmb,workDir,classpath); + } + + /** + * Launches a new JVM with the given classpath, establish a communication channel, + * and return a {@link Channel} to it. + * + * @param displayName + * Human readable name of what this JVM represents. For example "Selenium grid" or "Hadoop". + * This token is used for messages to {@code listener}. + * @param listener + * The progress of the launcher and the failure information will be sent here. Must not be null. + * @param workDir + * If non-null, the new JVM will have this directory as the working directory. This must be a local path. + * @param classpath + * The classpath of the new JVM. Can be null if you just need {@code slave.jar} (and everything else + * can be sent over the channel.) But if you have jars that are known to be necessary by the new JVM, + * setting it here will improve the classloading performance (by avoiding remote class file transfer.) + * Classes in this classpath will also take precedence over any other classes that's sent via the channel + * later, so it's also useful for making sure you get the version of the classes you want. + * @param vmb + * A partially configured {@link JVMBuilder} that allows the caller to fine-tune the launch parameter. + * + * @return + * never null + * @since 1.361 + */ + public static Channel newJVM(String displayName, TaskListener listener, JVMBuilder vmb, FilePath workDir, ClasspathBuilder classpath) throws IOException { ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress("localhost",0)); serverSocket.setSoTimeout(10*1000); - ArgumentListBuilder args = new ArgumentListBuilder(); - args.add(new File(System.getProperty("java.home"),"bin/java")); - if(systemProperties!=null) - args.addKeyValuePairs("-D",systemProperties); // use -cp + FQCN instead of -jar since remoting.jar can be rebundled (like in the case of the swarm plugin.) - args.add("-cp").add(Which.jarFile(Channel.class)).add(Launcher.class.getName()); + vmb.classpath().addJarOf(Channel.class); + vmb.mainClass(Launcher.class); - // build up a classpath if(classpath!=null) - args.add("-cp").add(classpath); - - args.add("-connectTo","localhost:"+serverSocket.getLocalPort()); + vmb.args().add("-cp").add(classpath); + vmb.args().add("-connectTo","localhost:"+serverSocket.getLocalPort()); listener.getLogger().println("Starting "+displayName); - Proc p = new LocalLauncher(listener).launch().cmds(args).stdout(listener).pwd(workDir).start(); + Proc p = vmb.launch(new LocalLauncher(listener)).stdout(listener).pwd(workDir).start(); Socket s = serverSocket.accept(); serverSocket.close(); @@ -185,5 +209,6 @@ public class Channels { new BufferedOutputStream(new SocketOutputStream(s)),null,p); } + private static final Logger LOGGER = Logger.getLogger(Channels.class.getName()); }