JDK.java 7.5 KB
Newer Older
K
kohsuke 已提交
1 2 3
/*
 * The MIT License
 * 
4
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
K
kohsuke 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 
 * 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.
 */
K
kohsuke 已提交
24 25
package hudson.model;

K
kohsuke 已提交
26 27
import hudson.util.StreamTaskListener;
import hudson.util.NullStream;
K
kohsuke 已提交
28
import hudson.util.FormValidation;
K
kohsuke 已提交
29
import hudson.Launcher;
30
import hudson.Extension;
31
import hudson.EnvVars;
32 33 34
import hudson.slaves.NodeSpecific;
import hudson.tools.ToolInstallation;
import hudson.tools.ToolDescriptor;
35
import hudson.tools.ToolInstaller;
K
kohsuke 已提交
36
import hudson.tools.ToolProperty;
37
import hudson.util.XStream2;
K
kohsuke 已提交
38

K
kohsuke 已提交
39
import java.io.File;
K
kohsuke 已提交
40
import java.io.IOException;
41
import java.lang.reflect.Constructor;
K
kohsuke 已提交
42
import java.util.Map;
43
import java.util.List;
K
kohsuke 已提交
44 45
import java.util.Arrays;
import java.util.Collections;
46 47
import java.util.logging.Level;
import java.util.logging.Logger;
K
kohsuke 已提交
48

49
import jenkins.model.Jenkins;
K
Kohsuke Kawaguchi 已提交
50
import org.jenkinsci.Symbol;
K
kohsuke 已提交
51
import org.kohsuke.stapler.DataBoundConstructor;
52 53
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
K
kohsuke 已提交
54 55 56 57 58 59

/**
 * Information about JDK installation.
 *
 * @author Kohsuke Kawaguchi
 */
60
public final class JDK extends ToolInstallation implements NodeSpecific<JDK>, EnvironmentSpecific<JDK> {
61 62

    /**
63
     * Name of the “System JDK”, which is just the JDK on Jenkins' $PATH.
64 65
     * @since 1.577
     */
66
    public static final String DEFAULT_NAME = "(System)";
67
    private static final long serialVersionUID = -3318291200160313357L;
68

69 70 71 72 73 74
    @Restricted(NoExternalUse.class)
    public static boolean isDefaultName(String name) {
        if ("(Default)".equals(name)) {
            // DEFAULT_NAME took this value prior to 1.598.
            return true;
        }
75
        return DEFAULT_NAME.equals(name) || name == null;
76
    }
77

M
mindless 已提交
78 79 80
    /**
     * @deprecated since 2009-02-25
     */
81
    @Deprecated // kept for backward compatibility - use getHome() instead
82
    private transient String javaHome;
K
kohsuke 已提交
83 84

    public JDK(String name, String javaHome) {
J
Josh Soref 已提交
85
        super(name, javaHome, Collections.emptyList());
K
kohsuke 已提交
86 87 88 89 90
    }

    @DataBoundConstructor
    public JDK(String name, String home, List<? extends ToolProperty<?>> properties) {
        super(name, home, properties);
K
kohsuke 已提交
91 92 93 94
    }

    /**
     * install directory.
K
kohsuke 已提交
95 96 97
     *
     * @deprecated as of 1.304
     *      Use {@link #getHome()}
K
kohsuke 已提交
98
     */
99
    @Deprecated
K
kohsuke 已提交
100
    public String getJavaHome() {
101
        return getHome();
K
kohsuke 已提交
102 103 104 105 106 107
    }

    /**
     * Gets the path to the bin directory.
     */
    public File getBinDir() {
K
kohsuke 已提交
108
        return new File(getHome(),"bin");
K
kohsuke 已提交
109 110 111 112 113
    }
    /**
     * Gets the path to 'java'.
     */
    private File getExecutable() {
114
        String execName = (File.separatorChar == '\\') ? "java.exe" : "java";
K
kohsuke 已提交
115
        return new File(getHome(),"bin/"+execName);
K
kohsuke 已提交
116 117 118 119 120 121 122 123 124 125
    }

    /**
     * Returns true if the executable exists.
     */
    public boolean getExists() {
        return getExecutable().exists();
    }

    /**
126
     * @deprecated as of 1.460. Use {@link #buildEnvVars(EnvVars)}
K
kohsuke 已提交
127
     */
128
    @Deprecated
K
kohsuke 已提交
129
    public void buildEnvVars(Map<String,String> env) {
130 131 132 133
        String home = getHome();
        if (home == null) {
            return;
        }
134
        // see EnvVars javadoc for why this adds PATH.
135 136
        env.put("PATH+JDK",home+"/bin");
        env.put("JAVA_HOME", home);
K
kohsuke 已提交
137
    }
K
kohsuke 已提交
138

139 140 141 142 143 144 145 146
    /**
     * Sets PATH and JAVA_HOME from this JDK.
     */
    @Override
    public void buildEnvVars(EnvVars env) {
        buildEnvVars((Map)env);
    }

K
kohsuke 已提交
147 148
    public JDK forNode(Node node, TaskListener log) throws IOException, InterruptedException {
        return new JDK(getName(), translateFor(node, log));
149 150
    }

151 152 153 154
    public JDK forEnvironment(EnvVars environment) {
        return new JDK(getName(), environment.expand(getHome()));
    }

K
kohsuke 已提交
155 156 157 158 159 160 161 162 163 164 165
    /**
     * Checks if "java" is in PATH on the given node.
     *
     * <p>
     * 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);
166
            return launcher.launch().cmds("java","-fullversion").stdout(listener).join()==0;
167
        } catch (IOException | InterruptedException e) {
K
kohsuke 已提交
168 169 170
            return false;
        }
    }
171

K
Kohsuke Kawaguchi 已提交
172
    @Extension @Symbol("jdk")
173 174 175
    public static class DescriptorImpl extends ToolDescriptor<JDK> {

        public String getDisplayName() {
176
            return "JDK"; // TODO I18N
177 178
        }

K
kohsuke 已提交
179
        public @Override JDK[] getInstallations() {
180
            return Jenkins.get().getJDKs().toArray(new JDK[0]);
181 182
        }

183
        public @Override void setInstallations(JDK... jdks) {
184
            Jenkins.get().setJDKs(Arrays.asList(jdks));
K
kohsuke 已提交
185 186 187
        }

        @Override
188 189
        public List<? extends ToolInstaller> getDefaultInstallers() {
            try {
190
                Class<? extends ToolInstaller> jdkInstallerClass = Jenkins.get().getPluginManager()
191 192 193 194 195 196 197 198 199
                        .uberClassLoader.loadClass("hudson.tools.JDKInstaller").asSubclass(ToolInstaller.class);
                Constructor<? extends ToolInstaller> constructor = jdkInstallerClass.getConstructor(String.class, boolean.class);
                return Collections.singletonList(constructor.newInstance(null, false));
            } catch (ClassNotFoundException e) {
                return Collections.emptyList();
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, "Unable to get default installer", e);
                return Collections.emptyList();
            }
200 201
        }

K
kohsuke 已提交
202 203 204
        /**
         * Checks if the JAVA_HOME is a valid JAVA_HOME path.
         */
205
        @Override protected FormValidation checkHomeDirectory(File value) {
K
kohsuke 已提交
206 207
            File toolsJar = new File(value,"lib/tools.jar");
            File mac = new File(value,"lib/dt.jar");
208 209 210 211 212

            // JENKINS-25601: JDK 9+ no longer has tools.jar. Keep the existing dt.jar/tools.jar checks to be safe.
            File javac = new File(value, "bin/javac");
            File javacExe = new File(value, "bin/javac.exe");
            if(!toolsJar.exists() && !mac.exists() && !javac.exists() && !javacExe.exists())
K
kohsuke 已提交
213 214 215 216
                return FormValidation.error(Messages.Hudson_NotJDKDir(value));

            return FormValidation.ok();
        }
217

218
    }
219 220 221 222 223 224 225

    public static class ConverterImpl extends ToolConverter {
        public ConverterImpl(XStream2 xstream) { super(xstream); }
        @Override protected String oldHomeField(ToolInstallation obj) {
            return ((JDK)obj).javaHome;
        }
    }
226 227

    private static final Logger LOGGER = Logger.getLogger(JDK.class.getName());
K
kohsuke 已提交
228
}