JDK.java 6.2 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 46 47
import java.util.Arrays;
import java.util.Collections;

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

/**
 * Information about JDK installation.
 *
 * @author Kohsuke Kawaguchi
 */
54
public final class JDK extends ToolInstallation implements NodeSpecific<JDK>, EnvironmentSpecific<JDK> {
M
mindless 已提交
55 56 57
    /**
     * @deprecated since 2009-02-25
     */
58
    @Deprecated // kept for backward compatibility - use getHome() instead
59
    private transient String javaHome;
K
kohsuke 已提交
60 61

    public JDK(String name, String javaHome) {
K
kohsuke 已提交
62 63 64 65 66 67
        super(name, javaHome, Collections.<ToolProperty<?>>emptyList());
    }

    @DataBoundConstructor
    public JDK(String name, String home, List<? extends ToolProperty<?>> properties) {
        super(name, home, properties);
K
kohsuke 已提交
68 69 70 71
    }

    /**
     * install directory.
K
kohsuke 已提交
72 73 74
     *
     * @deprecated as of 1.304
     *      Use {@link #getHome()}
K
kohsuke 已提交
75 76
     */
    public String getJavaHome() {
77
        return getHome();
K
kohsuke 已提交
78 79 80 81 82 83
    }

    /**
     * Gets the path to the bin directory.
     */
    public File getBinDir() {
K
kohsuke 已提交
84
        return new File(getHome(),"bin");
K
kohsuke 已提交
85 86 87 88 89
    }
    /**
     * Gets the path to 'java'.
     */
    private File getExecutable() {
90
        String execName = (File.separatorChar == '\\') ? "java.exe" : "java";
K
kohsuke 已提交
91
        return new File(getHome(),"bin/"+execName);
K
kohsuke 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
    }

    /**
     * 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) {
105
        // see EnvVars javadoc for why this adss PATH.
K
kohsuke 已提交
106
        env.put("PATH+JDK",getHome()+"/bin");
K
kohsuke 已提交
107
        env.put("JAVA_HOME",getHome());
K
kohsuke 已提交
108
    }
K
kohsuke 已提交
109

K
kohsuke 已提交
110 111
    public JDK forNode(Node node, TaskListener log) throws IOException, InterruptedException {
        return new JDK(getName(), translateFor(node, log));
112 113
    }

114 115 116 117
    public JDK forEnvironment(EnvVars environment) {
        return new JDK(getName(), environment.expand(getHome()));
    }

K
kohsuke 已提交
118 119 120 121 122 123 124 125 126 127 128
    /**
     * 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);
129
            return launcher.launch().cmds("java","-fullversion").stdout(listener).join()==0;
K
kohsuke 已提交
130 131 132 133 134 135
        } catch (IOException e) {
            return false;
        } catch (InterruptedException e) {
            return false;
        }
    }
136 137 138 139 140

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

        public String getDisplayName() {
141
            return "JDK"; // XXX I18N
142 143
        }

K
kohsuke 已提交
144
        public @Override JDK[] getInstallations() {
145 146 147 148
            return Hudson.getInstance().getJDKs().toArray(new JDK[0]);
        }

        // this isn't really synchronized well since the list is Hudson.jdks :(
K
kohsuke 已提交
149
        public @Override synchronized void setInstallations(JDK... jdks) {
150 151
            List<JDK> list = Hudson.getInstance().getJDKs();
            list.clear();
K
kohsuke 已提交
152 153 154 155 156 157
            list.addAll(Arrays.asList(jdks));
        }

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

K
kohsuke 已提交
160 161 162 163 164 165 166
        /**
         * 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);

167
            if(value.getPath().equals(""))
168 169
                return FormValidation.ok();

170 171 172
            if(!value.isDirectory())
                return FormValidation.error(Messages.Hudson_NotADirectory(value));

K
kohsuke 已提交
173 174 175 176 177 178 179
            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();
        }
180 181 182 183

        public FormValidation doCheckName(@QueryParameter String value) {
            return FormValidation.validateRequired(value);
        }
184
    }
185 186 187 188 189 190 191

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