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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Tom Huybrechts
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
25 package hudson.tools;
26
27 import hudson.model.Descriptor;
28 import hudson.util.DescribableList;
29
30 import java.lang.reflect.ParameterizedType;
31 import java.lang.reflect.Type;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import java.io.IOException;
36 import java.lang.reflect.Array;
37 import net.sf.json.JSONObject;
38 import org.jvnet.tiger_types.Types;
39 import org.kohsuke.stapler.StaplerRequest;
40
41 /**
42 * {@link Descriptor} for {@link ToolInstallation}.
43 *
44 * @author huybrechts
45 * @since 1.286
46 */
47 public abstract class ToolDescriptor<T extends ToolInstallation> extends Descriptor<ToolInstallation> {
48
49 private T[] installations;
50
51 /**
52 * Configured instances of {@link ToolInstallation}s.
53 *
54 * @return read-only list of installations;
55 * can be empty but never null.
56 */
57 public T[] getInstallations() {
58 if (installations != null)
59 return installations.clone();
60
61 Type bt = Types.getBaseClass(getClass(), ToolDescriptor.class);
62 if (bt instanceof ParameterizedType) {
63 ParameterizedType pt = (ParameterizedType) bt;
64 // this 't' is the closest approximation of T of Descriptor<T>.
65 Class t = Types.erasure(pt.getActualTypeArguments()[0]);
66 return (T[])Array.newInstance(t,0);
67 } else {
68 // can't infer the type. fallacbk
69 return (T[])new Object[0];
70 }
71 }
72
73 /**
74 * Overwrites {@link ToolInstallation}s.
75 *
76 * @param installations list of installations;
77 * can be empty but never null.
78 */
79 public void setInstallations(T... installations) {
80 this.installations = installations.clone();
81 }
82
83 /**
84 * Lists up {@link ToolPropertyDescriptor}s that are applicable to this {@link ToolInstallation}.
85 */
86 public List<ToolPropertyDescriptor> getPropertyDescriptors() {
87 return PropertyDescriptor.for_(ToolProperty.all(),clazz);
88 }
89
90 /**
91 * Optional list of installers to be configured by default for new tools of this type.
92 * If there are popular versions of the tool available using generic installation techniques,
93 * they can be returned here for the user's convenience.
94 * @since 1.305
95 */
96 public List<? extends ToolInstaller> getDefaultInstallers() {
97 return Collections.emptyList();
98 }
99
100 /**
101 * Default value for {@link ToolInstallation#getProperties()} used in the form binding.
102 * @since 1.305
103 */
104 public DescribableList<ToolProperty<?>,ToolPropertyDescriptor> getDefaultProperties() throws IOException {
105 DescribableList<ToolProperty<?>,ToolPropertyDescriptor> r
106 = new DescribableList<ToolProperty<?>, ToolPropertyDescriptor>(NOOP);
107
108 List<? extends ToolInstaller> installers = getDefaultInstallers();
109 if(!installers.isEmpty())
110 r.add(new InstallSourceProperty(installers));
111
112 return r;
113 }
114
115 @Override
116 @SuppressWarnings("unchecked") // cast to T[]
117 public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
118 setInstallations(req.bindJSONToList(clazz, json.get("tool")).toArray((T[]) Array.newInstance(clazz, 0)));
119 return true;
120 }
121
122 }
123