JDK.java 6.0 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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.
 */
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;
K
kohsuke 已提交
37

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

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
K
kohsuke 已提交
47 48 49 50 51 52

/**
 * Information about JDK installation.
 *
 * @author Kohsuke Kawaguchi
 */
53
public final class JDK extends ToolInstallation implements NodeSpecific<JDK>, EnvironmentSpecific<JDK> {
54 55
    @Deprecated // kept for backward compatibility - use getHome() instead
    private String javaHome;
K
kohsuke 已提交
56 57

    public JDK(String name, String javaHome) {
K
kohsuke 已提交
58 59 60 61 62 63
        super(name, javaHome, Collections.<ToolProperty<?>>emptyList());
    }

    @DataBoundConstructor
    public JDK(String name, String home, List<? extends ToolProperty<?>> properties) {
        super(name, home, properties);
K
kohsuke 已提交
64 65 66 67
    }

    /**
     * install directory.
K
kohsuke 已提交
68 69 70
     *
     * @deprecated as of 1.304
     *      Use {@link #getHome()}
K
kohsuke 已提交
71 72
     */
    public String getJavaHome() {
73
        return getHome();
K
kohsuke 已提交
74 75
    }

K
kohsuke 已提交
76 77
    @SuppressWarnings({"deprecation"})
    public @Override String getHome() {
78 79
        if (javaHome != null) return javaHome;
        return super.getHome();
K
kohsuke 已提交
80 81 82 83 84 85
    }

    /**
     * Gets the path to the bin directory.
     */
    public File getBinDir() {
K
kohsuke 已提交
86
        return new File(getHome(),"bin");
K
kohsuke 已提交
87 88 89 90 91 92 93 94 95 96 97
    }
    /**
     * Gets the path to 'java'.
     */
    private File getExecutable() {
        String execName;
        if(File.separatorChar=='\\')
            execName = "java.exe";
        else
            execName = "java";

K
kohsuke 已提交
98
        return new File(getHome(),"bin/"+execName);
K
kohsuke 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111
    }

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

    /**
     * Sets PATH and JAVA_HOME from this JDK.
     */
    public void buildEnvVars(Map<String,String> env) {
112 113
        // see EnvVars javadoc for why this adss PATH.
        env.put("PATH+JDK",getBinDir().getPath());
K
kohsuke 已提交
114
        env.put("JAVA_HOME",getHome());
K
kohsuke 已提交
115
    }
K
kohsuke 已提交
116

K
kohsuke 已提交
117 118
    public JDK forNode(Node node, TaskListener log) throws IOException, InterruptedException {
        return new JDK(getName(), translateFor(node, log));
119 120
    }

121 122 123 124
    public JDK forEnvironment(EnvVars environment) {
        return new JDK(getName(), environment.expand(getHome()));
    }

K
kohsuke 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    /**
     * 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);
            return launcher.launch("java -fullversion",new String[0],listener.getLogger(),null).join()==0;
        } catch (IOException e) {
            return false;
        } catch (InterruptedException e) {
            return false;
        }
    }
143 144 145 146 147

    @Extension
    public static class DescriptorImpl extends ToolDescriptor<JDK> {

        public String getDisplayName() {
148
            return "JDK"; // XXX I18N
149 150
        }

K
kohsuke 已提交
151
        public @Override JDK[] getInstallations() {
152 153 154 155
            return Hudson.getInstance().getJDKs().toArray(new JDK[0]);
        }

        // this isn't really synchronized well since the list is Hudson.jdks :(
K
kohsuke 已提交
156
        public @Override synchronized void setInstallations(JDK... jdks) {
157 158
            List<JDK> list = Hudson.getInstance().getJDKs();
            list.clear();
K
kohsuke 已提交
159 160 161 162 163 164
            list.addAll(Arrays.asList(jdks));
        }

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

K
kohsuke 已提交
167 168 169 170 171 172 173
        /**
         * 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);

174
            if(value.getPath().equals(""))
175 176
                return FormValidation.ok();

177 178 179
            if(!value.isDirectory())
                return FormValidation.error(Messages.Hudson_NotADirectory(value));

K
kohsuke 已提交
180 181 182 183 184 185 186
            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();
        }
187
    }
K
kohsuke 已提交
188
}