From 3ef743203f673dbed59e6fc019ee30b0ea888025 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 15 Aug 2012 21:42:45 +0200 Subject: [PATCH] [JENKINS-14788] pull-up checkJavaVersion from ssh-plugin to be used by other ComputerLaunchers to check installed java version --- .../ManagedWindowsServiceLauncher.java | 18 ++++--- .../java/hudson/slaves/ComputerLauncher.java | 48 +++++++++++++++++-- .../hudson/slaves/Messages.properties | 3 ++ .../hudson/slaves/Messages_es.properties | 3 ++ .../hudson/slaves/Messages_ja.properties | 2 + 5 files changed, 61 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/hudson/os/windows/ManagedWindowsServiceLauncher.java b/core/src/main/java/hudson/os/windows/ManagedWindowsServiceLauncher.java index 4f72187a0a..1d33914d47 100644 --- a/core/src/main/java/hudson/os/windows/ManagedWindowsServiceLauncher.java +++ b/core/src/main/java/hudson/os/windows/ManagedWindowsServiceLauncher.java @@ -46,13 +46,7 @@ import hudson.util.IOUtils; import hudson.util.Secret; import hudson.util.jna.DotNet; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintStream; -import java.io.StringReader; +import java.io.*; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; @@ -237,9 +231,10 @@ public class ManagedWindowsServiceLauncher extends ComputerLauncher { try {// does Java exist? logger.println("Checking if Java exists"); WindowsRemoteProcessLauncher wrpl = new WindowsRemoteProcessLauncher(name,auth); - Process proc = wrpl.launch("\"" +java + "\" -fullversion","c:\\"); + Process proc = wrpl.launch("\"" +java + "\" -version","c:\\"); proc.getOutputStream().close(); - IOUtils.copy(proc.getInputStream(),logger); + StringWriter console = new StringWriter(); + IOUtils.copy(proc.getInputStream(), console); proc.getInputStream().close(); int exitCode = proc.waitFor(); if (exitCode==1) {// we'll get this error code if Java is not found @@ -254,12 +249,15 @@ public class ManagedWindowsServiceLauncher extends ComputerLauncher { WindowsRemoteFileSystem fs = new WindowsRemoteFileSystem(name, createSmbAuth()); fs.mkdirs(javaDir); - + jdki.install(new WindowsRemoteLauncher(listener,wrpl), Platform.WINDOWS, fs, listener, javaDir ,path+"\\jdk.exe"); + } else { + checkJavaVersion(logger, java, new BufferedReader(new StringReader(console.toString()))); } } catch (Exception e) { e.printStackTrace(listener.error("Failed to prepare Java")); + return; } // this just doesn't work --- trying to obtain the type or check the existence of smb://server/C$/ results in "access denied" diff --git a/core/src/main/java/hudson/slaves/ComputerLauncher.java b/core/src/main/java/hudson/slaves/ComputerLauncher.java index c499fd24b4..5cc5e125ce 100644 --- a/core/src/main/java/hudson/slaves/ComputerLauncher.java +++ b/core/src/main/java/hudson/slaves/ComputerLauncher.java @@ -30,9 +30,10 @@ import hudson.remoting.Channel; import hudson.util.DescriptorList; import hudson.util.StreamTaskListener; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.Locale; /** * Extension point to allow control over how {@link Computer}s are "launched", @@ -159,4 +160,45 @@ public abstract class ComputerLauncher extends AbstractDescribableImpl LIST = new DescriptorList(ComputerLauncher.class); + + /** + * Given the output of "java -version" in r, determine if this + * version of Java is supported, or throw {@link IOException}. + * + * @param logger + * where to log the output + * @param javaCommand + * the command executed, used for logging + * @param r + * the output of "java -version" + */ + protected void checkJavaVersion(final PrintStream logger, String javaCommand, + final BufferedReader r) + throws IOException { + String line; + while (null != (line = r.readLine())) { + line = line.toLowerCase(); + if (line.startsWith("java version \"") + || line.startsWith("openjdk version \"")) { + final String versionStr = line.substring( + line.indexOf('\"') + 1, line.lastIndexOf('\"')); + logger.println(Messages.ComputerLauncher_JavaVersionResult(javaCommand, versionStr)); + + // parse as a number and we should be OK as all we care about is up through the first dot. + try { + final Number version = + NumberFormat.getNumberInstance(Locale.US).parse(versionStr); + if(version.doubleValue() < 1.5) { + throw new IOException(Messages + .ComputerLauncher_NoJavaFound(line)); + } + } catch(final ParseException e) { + throw new IOException(Messages.ComputerLauncher_NoJavaFound(line)); + } + return; + } + } + logger.println(Messages.ComputerLauncher_UknownJavaVersion(javaCommand)); + throw new IOException(Messages.ComputerLauncher_UknownJavaVersion(javaCommand)); + } } diff --git a/core/src/main/resources/hudson/slaves/Messages.properties b/core/src/main/resources/hudson/slaves/Messages.properties index 0bacbd5a14..0d038f8c42 100644 --- a/core/src/main/resources/hudson/slaves/Messages.properties +++ b/core/src/main/resources/hudson/slaves/Messages.properties @@ -38,3 +38,6 @@ SimpleScheduledRetentionStrategy.displayName=Take this slave on-line according t EnvironmentVariablesNodeProperty.displayName=Environment variables SlaveComputer.DisconnectedBy=Disconnected by {0}{1} NodeDescripter.CheckName.Mandatory=Name is mandatory +ComputerLauncher.NoJavaFound=Java version {0} was found but 1.5 or later is needed. +ComputerLauncher.JavaVersionResult={0} -version returned {1}. +ComputerLauncher.UknownJavaVersion=Couldn''t figure out the Java version of {0} diff --git a/core/src/main/resources/hudson/slaves/Messages_es.properties b/core/src/main/resources/hudson/slaves/Messages_es.properties index dc9041d6c2..77be1c95a5 100644 --- a/core/src/main/resources/hudson/slaves/Messages_es.properties +++ b/core/src/main/resources/hudson/slaves/Messages_es.properties @@ -38,3 +38,6 @@ EnvironmentVariablesNodeProperty.displayName=Variables de entorno SlaveComputer.DisconnectedBy=Desconectado por {0}{1} NodeProvisioner.EmptyString= NodeDescripter.CheckName.Mandatory=El nombre es obligatorio +ComputerLauncher.NoJavaFound=Se encontró la versión de Java {0}, sin embargo se necesita la versión 1.5 o posterior +ComputerLauncher.JavaVersionResult={0} [SSH] {1} -version ha retornado {2}. +ComputerLauncher.UknownJavaVersion=Imposible de averiguar la versión de Java en {0} diff --git a/core/src/main/resources/hudson/slaves/Messages_ja.properties b/core/src/main/resources/hudson/slaves/Messages_ja.properties index 539f7e6a43..1e2242f0f7 100644 --- a/core/src/main/resources/hudson/slaves/Messages_ja.properties +++ b/core/src/main/resources/hudson/slaves/Messages_ja.properties @@ -38,3 +38,5 @@ SimpleScheduledRetentionStrategy.displayName=\u30b9\u30b1\u30b8\u30e5\u30fc\u30e EnvironmentVariablesNodeProperty.displayName=\u74b0\u5883\u5909\u6570 SlaveComputer.DisconnectedBy={0}\u304c\u30aa\u30d5\u30e9\u30a4\u30f3\u306b\u3057\u3066\u3044\u307e\u3059\u3002{1} NodeDescripter.CheckName.Mandatory=\u30ce\u30fc\u30c9\u540d\u306f\u5fc5\u9808\u3067\u3059\u3002 +ComputerLauncher.NoJavaFound=Java\u306e\u30d0\u30fc\u30b8\u30e7\u30f3 {0} \u304c\u898b\u3064\u304b\u308a\u307e\u3057\u305f\u304c\u30011.5\u4ee5\u964d\u304c\u5fc5\u8981\u3067\u3059\u3002 +ComputerLauncher.UknownJavaVersion={0} \u306eJava\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u4e0d\u660e\u3067\u3059\u3002 -- GitLab