/* * The MIT License * * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package hudson.model; import hudson.util.StreamTaskListener; import hudson.util.NullStream; import hudson.util.FormValidation; import hudson.Launcher; import hudson.Extension; import hudson.EnvVars; import hudson.slaves.NodeSpecific; import hudson.tools.ToolInstallation; import hudson.tools.ToolDescriptor; import hudson.tools.ToolProperty; import hudson.tools.JDKInstaller; import java.io.File; import java.io.IOException; import java.util.Map; import java.util.List; import java.util.Arrays; import java.util.Collections; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; /** * Information about JDK installation. * * @author Kohsuke Kawaguchi */ public final class JDK extends ToolInstallation implements NodeSpecific, EnvironmentSpecific { @Deprecated // kept for backward compatibility - use getHome() instead private String javaHome; public JDK(String name, String javaHome) { super(name, javaHome, Collections.>emptyList()); } @DataBoundConstructor public JDK(String name, String home, List> properties) { super(name, home, properties); } /** * install directory. * * @deprecated as of 1.304 * Use {@link #getHome()} */ public String getJavaHome() { return getHome(); } @SuppressWarnings({"deprecation"}) public @Override String getHome() { if (javaHome != null) return javaHome; return super.getHome(); } /** * Gets the path to the bin directory. */ public File getBinDir() { return new File(getHome(),"bin"); } /** * Gets the path to 'java'. */ private File getExecutable() { String execName; if(File.separatorChar=='\\') execName = "java.exe"; else execName = "java"; return new File(getHome(),"bin/"+execName); } /** * Returns true if the executable exists. */ public boolean getExists() { return getExecutable().exists(); } /** * Sets PATH and JAVA_HOME from this JDK. */ public void buildEnvVars(Map env) { // see EnvVars javadoc for why this adss PATH. env.put("PATH+JDK",getBinDir().getPath()); env.put("JAVA_HOME",getHome()); } public JDK forNode(Node node, TaskListener log) throws IOException, InterruptedException { return new JDK(getName(), translateFor(node, log)); } public JDK forEnvironment(EnvVars environment) { return new JDK(getName(), environment.expand(getHome())); } /** * Checks if "java" is in PATH on the given node. * *

* If it's not, then the user must specify a configured JDK, * so this is often useful for form field validation. */ public static boolean isDefaultJDKValid(Node n) { try { TaskListener listener = new StreamTaskListener(new NullStream()); Launcher launcher = n.createLauncher(listener); return launcher.launch("java -fullversion",new String[0],listener.getLogger(),null).join()==0; } catch (IOException e) { return false; } catch (InterruptedException e) { return false; } } @Extension public static class DescriptorImpl extends ToolDescriptor { public String getDisplayName() { return "Java Development Kit"; } public @Override JDK[] getInstallations() { return Hudson.getInstance().getJDKs().toArray(new JDK[0]); } // this isn't really synchronized well since the list is Hudson.jdks :( public @Override synchronized void setInstallations(JDK... jdks) { List list = Hudson.getInstance().getJDKs(); list.clear(); list.addAll(Arrays.asList(jdks)); } @Override public List getDefaultInstallers() { return Collections.singletonList(new JDKInstaller(null,false)); } /** * Checks if the JAVA_HOME is a valid JAVA_HOME path. */ public FormValidation doCheckHome(@QueryParameter File value) { // this can be used to check the existence of a file on the server, so needs to be protected Hudson.getInstance().checkPermission(Hudson.ADMINISTER); if(value.exists() && !value.isDirectory()) return FormValidation.error(Messages.Hudson_NotADirectory(value)); File toolsJar = new File(value,"lib/tools.jar"); File mac = new File(value,"lib/dt.jar"); if(!toolsJar.exists() && !mac.exists()) return FormValidation.error(Messages.Hudson_NotJDKDir(value)); return FormValidation.ok(); } } }