JDK.java 1.7 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
package hudson.model;

import hudson.EnvVars;

import java.io.File;
import java.util.Map;

/**
 * Information about JDK installation.
 *
 * @author Kohsuke Kawaguchi
 */
public final class JDK {
    private final String name;
    private final String javaHome;

    public JDK(String name, String javaHome) {
        this.name = name;
        this.javaHome = javaHome;
    }

    /**
     * install directory.
     */
    public String getJavaHome() {
        return javaHome;
    }

    /**
     * Human readable display name.
     */
    public String getName() {
        return name;
    }

    /**
     * Gets the path to the bin directory.
     */
    public File getBinDir() {
        return new File(getJavaHome(),"bin");
    }
    /**
     * Gets the path to 'java'.
     */
    private File getExecutable() {
        String execName;
        if(File.separatorChar=='\\')
            execName = "java.exe";
        else
            execName = "java";

        return new File(getJavaHome(),"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<String,String> env) {
        String path = env.get("PATH");
        if(path==null)
            path = EnvVars.masterEnvVars.get("PATH");
        
        if(path==null)
            path = getBinDir().getPath();
        else
            path = getBinDir().getPath()+File.pathSeparator+path;
        env.put("PATH",path);
        env.put("JAVA_HOME",javaHome);
        if(!env.containsKey("HUDSON_HOME"))
            env.put("HUDSON_HOME", Hudson.getInstance().getRootDir().getPath() );
    }
}