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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2011, Sun Microsystems, Inc., Tom Huybrechts, Seiji Sogabe
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.tools;
25
26 import hudson.DescriptorExtensionList;
27 import hudson.Extension;
28 import hudson.model.Descriptor;
29 import jenkins.model.Jenkins;
30 import hudson.model.Node;
31 import hudson.model.TaskListener;
32 import hudson.slaves.NodeProperty;
33 import hudson.slaves.NodePropertyDescriptor;
34 import hudson.slaves.NodeSpecific;
35 import java.io.IOException;
36 import java.util.ArrayList;
37
38 import org.kohsuke.stapler.DataBoundConstructor;
39
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.List;
43
44 /**
45 * {@link NodeProperty} that allows users to specify different locations for {@link ToolInstallation}s.
46 *
47 * @since 1.286
48 */
49 public class ToolLocationNodeProperty extends NodeProperty<Node> {
50
51 /**
52 * Override locations. Never null.
53 */
54 private final List<ToolLocation> locations;
55
56 @DataBoundConstructor
57 public ToolLocationNodeProperty(List<ToolLocation> locations) {
58 if (locations == null) {
59 locations = new ArrayList<ToolLocation>();
60 }
61 this.locations = locations;
62 }
63
64 public ToolLocationNodeProperty(ToolLocation... locations) {
65 this(Arrays.asList(locations));
66 }
67
68 public List<ToolLocation> getLocations() {
69 return Collections.unmodifiableList(locations);
70 }
71
72 public String getHome(ToolInstallation installation) {
73 for (ToolLocation location : locations) {
74 if (location.getName().equals(installation.getName()) && location.getType() == installation.getDescriptor()) {
75 return location.getHome();
76 }
77 }
78 return null;
79 }
80
81 /**
82 * Checks if the location of the tool is overridden for the given node, and if so,
83 * return the node-specific home directory. Otherwise return {@code installation.getHome()}
84 *
85 * <p>
86 * This is the core logic behind {@link NodeSpecific#forNode(Node)} for {@link ToolInstallation}.
87 *
88 * @return
89 * never null.
90 * @deprecated since 2009-04-09.
91 * Use {@link ToolInstallation#translateFor(Node,TaskListener)}
92 */
93 public static String getToolHome(Node node, ToolInstallation installation, TaskListener log) throws IOException, InterruptedException {
94 String result = null;
95
96 // node-specific configuration takes precedence
97 ToolLocationNodeProperty property = node.getNodeProperties().get(ToolLocationNodeProperty.class);
98 if (property != null) {
99 result = property.getHome(installation);
100 }
101 if (result != null) {
102 return result;
103 }
104
105 // consult translators
106 for (ToolLocationTranslator t : ToolLocationTranslator.all()) {
107 result = t.getToolHome(node, installation, log);
108 if (result != null) {
109 return result;
110 }
111 }
112
113 // fall back is no-op
114 return installation.getHome();
115 }
116
117 @Extension
118 public static class DescriptorImpl extends NodePropertyDescriptor {
119
120 public String getDisplayName() {
121 return Messages.ToolLocationNodeProperty_displayName();
122 }
123
124 public DescriptorExtensionList<ToolInstallation, ToolDescriptor<?>> getToolDescriptors() {
125 return ToolInstallation.all();
126 }
127
128 public String getKey(ToolInstallation installation) {
129 return installation.getDescriptor().getClass().getName() + "@" + installation.getName();
130 }
131
132 @Override
133 public boolean isApplicable(Class<? extends Node> nodeType) {
134 return nodeType != Jenkins.class;
135 }
136 }
137
138 public static final class ToolLocation {
139
140 private final String type;
141
142 private final String name;
143
144 private final String home;
145
146 private transient volatile ToolDescriptor descriptor;
147
148 public ToolLocation(ToolDescriptor type, String name, String home) {
149 this.descriptor = type;
150 this.type = type.getClass().getName();
151 this.name = name;
152 this.home = home;
153 }
154
155 @DataBoundConstructor
156 public ToolLocation(String key, String home) {
157 this.type = key.substring(0, key.indexOf('@'));
158 this.name = key.substring(key.indexOf('@') + 1);
159 this.home = home;
160 }
161
162 public String getName() {
163 return name;
164 }
165
166 public String getHome() {
167 return home;
168 }
169
170 public ToolDescriptor getType() {
171 if (descriptor == null) {
172 descriptor = (ToolDescriptor) Descriptor.find(type);
173 }
174 return descriptor;
175 }
176
177 public String getKey() {
178 return type + "@" + name;
179 }
180 }
181 }
182