JDK.java 6.7 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;
K
kohsuke 已提交
35 36
import hudson.tools.ToolProperty;
import hudson.tools.JDKInstaller;
37
import hudson.util.XStream2;
K
kohsuke 已提交
38

K
kohsuke 已提交
39
import java.io.File;
K
kohsuke 已提交
40
import java.io.IOException;
K
kohsuke 已提交
41
import java.util.Map;
42
import java.util.List;
K
kohsuke 已提交
43 44 45
import java.util.Arrays;
import java.util.Collections;

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

/**
 * Information about JDK installation.
 *
 * @author Kohsuke Kawaguchi
 */
57
public final class JDK extends ToolInstallation implements NodeSpecific<JDK>, EnvironmentSpecific<JDK> {
58 59

    /**
60
     * Name of the “System JDK”, which is just the JDK on Jenkins' $PATH.
61 62
     * @since 1.577
     */
63
    public static final String DEFAULT_NAME = "(System)";
64

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

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

    public JDK(String name, String javaHome) {
K
kohsuke 已提交
81 82 83 84 85 86
        super(name, javaHome, Collections.<ToolProperty<?>>emptyList());
    }

    @DataBoundConstructor
    public JDK(String name, String home, List<? extends ToolProperty<?>> properties) {
        super(name, home, properties);
K
kohsuke 已提交
87 88 89 90
    }

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

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

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

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

135 136 137 138 139 140 141 142
    /**
     * Sets PATH and JAVA_HOME from this JDK.
     */
    @Override
    public void buildEnvVars(EnvVars env) {
        buildEnvVars((Map)env);
    }

K
kohsuke 已提交
143 144
    public JDK forNode(Node node, TaskListener log) throws IOException, InterruptedException {
        return new JDK(getName(), translateFor(node, log));
145 146
    }

147 148 149 150
    public JDK forEnvironment(EnvVars environment) {
        return new JDK(getName(), environment.expand(getHome()));
    }

K
kohsuke 已提交
151 152 153 154 155 156 157 158 159 160 161
    /**
     * 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);
162
            return launcher.launch().cmds("java","-fullversion").stdout(listener).join()==0;
K
kohsuke 已提交
163 164 165 166 167 168
        } catch (IOException e) {
            return false;
        } catch (InterruptedException e) {
            return false;
        }
    }
169

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

        public String getDisplayName() {
174
            return "JDK"; // TODO I18N
175 176
        }

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

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

        @Override
        public List<JDKInstaller> getDefaultInstallers() {
            return Collections.singletonList(new JDKInstaller(null,false));
188 189
        }

K
kohsuke 已提交
190 191 192
        /**
         * Checks if the JAVA_HOME is a valid JAVA_HOME path.
         */
193
        @Override protected FormValidation checkHomeDirectory(File value) {
K
kohsuke 已提交
194 195
            File toolsJar = new File(value,"lib/tools.jar");
            File mac = new File(value,"lib/dt.jar");
196 197 198 199 200

            // 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 已提交
201 202 203 204
                return FormValidation.error(Messages.Hudson_NotJDKDir(value));

            return FormValidation.ok();
        }
205

206
    }
207 208 209 210 211 212 213

    public static class ConverterImpl extends ToolConverter {
        public ConverterImpl(XStream2 xstream) { super(xstream); }
        @Override protected String oldHomeField(ToolInstallation obj) {
            return ((JDK)obj).javaHome;
        }
    }
K
kohsuke 已提交
214
}