File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\JDK.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import com.infradna.tool.bridge_method_injector.BridgeMethodsAdded;
27 import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
28 import hudson.util.StreamTaskListener;
29 import hudson.util.NullStream;
30 import hudson.util.FormValidation;
31 import hudson.Launcher;
32 import hudson.Extension;
33 import hudson.EnvVars;
34 import hudson.slaves.NodeSpecific;
35 import hudson.tools.ToolInstallation;
36 import hudson.tools.ToolDescriptor;
37 import hudson.tools.ToolProperty;
38 import hudson.tools.JDKInstaller;
39 import hudson.util.XStream2;
40
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.Map;
44 import java.util.List;
45 import java.util.Arrays;
46 import java.util.Collections;
47
48 import jenkins.model.Jenkins;
49 import org.kohsuke.stapler.DataBoundConstructor;
50 import org.kohsuke.stapler.QueryParameter;
51
52 /**
53 * Information about JDK installation.
54 *
55 * @author Kohsuke Kawaguchi
56 */
57 public final class JDK extends ToolInstallation implements NodeSpecific<JDK>, EnvironmentSpecific<JDK> {
58 /**
59 * @deprecated since 2009-02-25
60 */
61 @Deprecated // kept for backward compatibility - use getHome() instead
62 private transient String javaHome;
63
64 public JDK(String name, String javaHome) {
65 super(name, javaHome, Collections.<ToolProperty<?>>emptyList());
66 }
67
68 @DataBoundConstructor
69 public JDK(String name, String home, List<? extends ToolProperty<?>> properties) {
70 super(name, home, properties);
71 }
72
73 /**
74 * install directory.
75 *
76 * @deprecated as of 1.304
77 * Use {@link #getHome()}
78 */
79 public String getJavaHome() {
80 return getHome();
81 }
82
83 /**
84 * Gets the path to the bin directory.
85 */
86 public File getBinDir() {
87 return new File(getHome(),"bin");
88 }
89 /**
90 * Gets the path to 'java'.
91 */
92 private File getExecutable() {
93 String execName = (File.separatorChar == '\\') ? "java.exe" : "java";
94 return new File(getHome(),"bin/"+execName);
95 }
96
97 /**
98 * Returns true if the executable exists.
99 */
100 public boolean getExists() {
101 return getExecutable().exists();
102 }
103
104 /**
105 * @deprecated as of 1.460. Use {@link #buildEnvVars(EnvVars)}
106 */
107 public void buildEnvVars(Map<String,String> env) {
108 // see EnvVars javadoc for why this adds PATH.
109 env.put("PATH+JDK",getHome()+"/bin");
110 env.put("JAVA_HOME",getHome());
111 }
112
113 /**
114 * Sets PATH and JAVA_HOME from this JDK.
115 */
116 @Override
117 public void buildEnvVars(EnvVars env) {
118 buildEnvVars((Map)env);
119 }
120
121 public JDK forNode(Node node, TaskListener log) throws IOException, InterruptedException {
122 return new JDK(getName(), translateFor(node, log));
123 }
124
125 public JDK forEnvironment(EnvVars environment) {
126 return new JDK(getName(), environment.expand(getHome()));
127 }
128
129 /**
130 * Checks if "java" is in PATH on the given node.
131 *
132 * <p>
133 * If it's not, then the user must specify a configured JDK,
134 * so this is often useful for form field validation.
135 */
136 public static boolean isDefaultJDKValid(Node n) {
137 try {
138 TaskListener listener = new StreamTaskListener(new NullStream());
139 Launcher launcher = n.createLauncher(listener);
140 return launcher.launch().cmds("java","-fullversion").stdout(listener).join()==0;
141 } catch (IOException e) {
142 return false;
143 } catch (InterruptedException e) {
144 return false;
145 }
146 }
147
148 @Extension
149 public static class DescriptorImpl extends ToolDescriptor<JDK> {
150
151 public String getDisplayName() {
152 return "JDK"; // XXX I18N
153 }
154
155 public @Override JDK[] getInstallations() {
156 return Jenkins.getInstance().getJDKs().toArray(new JDK[0]);
157 }
158
159 // this isn't really synchronized well since the list is Hudson.jdks :(
160 public @Override synchronized void setInstallations(JDK... jdks) {
161 List<JDK> list = Jenkins.getInstance().getJDKs();
162 list.clear();
163 list.addAll(Arrays.asList(jdks));
164 }
165
166 @Override
167 public List<JDKInstaller> getDefaultInstallers() {
168 return Collections.singletonList(new JDKInstaller(null,false));
169 }
170
171 /**
172 * Checks if the JAVA_HOME is a valid JAVA_HOME path.
173 */
174 public FormValidation doCheckHome(@QueryParameter File value) {
175 // this can be used to check the existence of a file on the server, so needs to be protected
176 Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
177
178 if(value.getPath().equals(""))
179 return FormValidation.ok();
180
181 if(!value.isDirectory())
182 return FormValidation.error(Messages.Hudson_NotADirectory(value));
183
184 File toolsJar = new File(value,"lib/tools.jar");
185 File mac = new File(value,"lib/dt.jar");
186 if(!toolsJar.exists() && !mac.exists())
187 return FormValidation.error(Messages.Hudson_NotJDKDir(value));
188
189 return FormValidation.ok();
190 }
191
192 public FormValidation doCheckName(@QueryParameter String value) {
193 return FormValidation.validateRequired(value);
194 }
195 }
196
197 public static class ConverterImpl extends ToolConverter {
198 public ConverterImpl(XStream2 xstream) { super(xstream); }
199 @Override protected String oldHomeField(ToolInstallation obj) {
200 return ((JDK)obj).javaHome;
201 }
202 }
203 }
204