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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
5 * Erik Ramfelt, Koichi Fujikawa, Red Hat, Inc., Seiji Sogabe,
6 * Stephen Connolly, Tom Huybrechts, Yahoo! Inc., Alan Harder, CloudBees, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26 package hudson.model;
27
28 import hudson.ExtensionListView;
29 import hudson.Functions;
30 import hudson.Platform;
31 import hudson.PluginManager;
32 import hudson.cli.declarative.CLIResolver;
33 import hudson.model.listeners.ItemListener;
34 import hudson.slaves.ComputerListener;
35 import hudson.util.CopyOnWriteList;
36 import hudson.util.FormValidation;
37 import jenkins.model.Jenkins;
38 import org.jvnet.hudson.reactor.ReactorException;
39 import org.kohsuke.stapler.QueryParameter;
40 import org.kohsuke.stapler.Stapler;
41 import org.kohsuke.stapler.StaplerRequest;
42 import org.kohsuke.stapler.StaplerResponse;
43
44 import javax.servlet.ServletContext;
45 import javax.servlet.ServletException;
46 import java.io.File;
47 import java.io.IOException;
48 import java.text.NumberFormat;
49 import java.text.ParseException;
50 import java.util.Collections;
51 import java.util.List;
52 import java.util.Map;
53
54 import static hudson.Util.fixEmpty;
55
56 public class Hudson extends Jenkins {
57
58 /**
59 * List of registered {@link hudson.model.listeners.ItemListener}s.
60 * @deprecated as of 1.286
61 */
62 private transient final CopyOnWriteList<ItemListener> itemListeners = ExtensionListView.createCopyOnWriteList(ItemListener.class);
63
64 /**
65 * List of registered {@link hudson.slaves.ComputerListener}s.
66 * @deprecated as of 1.286
67 */
68 private transient final CopyOnWriteList<ComputerListener> computerListeners = ExtensionListView.createCopyOnWriteList(ComputerListener.class);
69
70
71 @CLIResolver
72 public static Hudson getInstance() {
73 return (Hudson)Jenkins.getInstance();
74 }
75
76 public Hudson(File root, ServletContext context) throws IOException, InterruptedException, ReactorException {
77 this(root,context,null);
78 }
79
80 public Hudson(File root, ServletContext context, PluginManager pluginManager) throws IOException, InterruptedException, ReactorException {
81 super(root, context, pluginManager);
82 }
83
84 /**
85 * Gets all the installed {@link ItemListener}s.
86 *
87 * @deprecated as of 1.286.
88 * Use {@link ItemListener#all()}.
89 */
90 public CopyOnWriteList<ItemListener> getJobListeners() {
91 return itemListeners;
92 }
93
94 /**
95 * Gets all the installed {@link ComputerListener}s.
96 *
97 * @deprecated as of 1.286.
98 * Use {@link ComputerListener#all()}.
99 */
100 public CopyOnWriteList<ComputerListener> getComputerListeners() {
101 return computerListeners;
102 }
103
104 /**
105 * Gets the slave node of the give name, hooked under this Hudson.
106 *
107 * @deprecated
108 * Use {@link #getNode(String)}. Since 1.252.
109 */
110 public Slave getSlave(String name) {
111 Node n = getNode(name);
112 if (n instanceof Slave)
113 return (Slave)n;
114 return null;
115 }
116
117 /**
118 * @deprecated
119 * Use {@link #getNodes()}. Since 1.252.
120 */
121 public List<Slave> getSlaves() {
122 return (List)slaves;
123 }
124
125 /**
126 * Updates the slave list.
127 *
128 * @deprecated
129 * Use {@link #setNodes(List)}. Since 1.252.
130 */
131 public void setSlaves(List<Slave> slaves) throws IOException {
132 setNodes(slaves);
133 }
134
135 /**
136 * @deprecated
137 * Left only for the compatibility of URLs.
138 * Should not be invoked for any other purpose.
139 */
140 public TopLevelItem getJob(String name) {
141 return getItem(name);
142 }
143
144 /**
145 * @deprecated
146 * Used only for mapping jobs to URL in a case-insensitive fashion.
147 */
148 public TopLevelItem getJobCaseInsensitive(String name) {
149 String match = Functions.toEmailSafeString(name);
150 for(TopLevelItem item : getItems()) {
151 if(Functions.toEmailSafeString(item.getName()).equalsIgnoreCase(match)) {
152 return item;
153 }
154 }
155 return null;
156 }
157
158 /**
159 * @deprecated as of 1.317
160 * Use {@link #doQuietDown()} instead.
161 */
162 public synchronized void doQuietDown(StaplerResponse rsp) throws IOException, ServletException {
163 doQuietDown().generateResponse(null, rsp, this);
164 }
165
166 /**
167 * RSS feed for log entries.
168 *
169 * @deprecated
170 * As on 1.267, moved to "/log/rss..."
171 */
172 public void doLogRss( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
173 String qs = req.getQueryString();
174 rsp.sendRedirect2("./log/rss"+(qs==null?"":'?'+qs));
175 }
176
177 /**
178 * @deprecated as of 1.294
179 * Define your own check method, instead of relying on this generic one.
180 */
181 public void doFieldCheck(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
182 doFieldCheck(
183 fixEmpty(req.getParameter("value")),
184 fixEmpty(req.getParameter("type")),
185 fixEmpty(req.getParameter("errorText")),
186 fixEmpty(req.getParameter("warningText"))).generateResponse(req,rsp,this);
187 }
188
189 /**
190 * Checks if the value for a field is set; if not an error or warning text is displayed.
191 * If the parameter "value" is not set then the parameter "errorText" is displayed
192 * as an error text. If the parameter "errorText" is not set, then the parameter "warningText"
193 * is displayed as a warning text.
194 * <p>
195 * If the text is set and the parameter "type" is set, it will validate that the value is of the
196 * correct type. Supported types are "number, "number-positive" and "number-negative".
197 *
198 * @deprecated as of 1.324
199 * Either use client-side validation (e.g. class="required number")
200 * or define your own check method, instead of relying on this generic one.
201 */
202 public FormValidation doFieldCheck(@QueryParameter(fixEmpty=true) String value,
203 @QueryParameter(fixEmpty=true) String type,
204 @QueryParameter(fixEmpty=true) String errorText,
205 @QueryParameter(fixEmpty=true) String warningText) {
206 if (value == null) {
207 if (errorText != null)
208 return FormValidation.error(errorText);
209 if (warningText != null)
210 return FormValidation.warning(warningText);
211 return FormValidation.error("No error or warning text was set for fieldCheck().");
212 }
213
214 if (type != null) {
215 try {
216 if (type.equalsIgnoreCase("number")) {
217 NumberFormat.getInstance().parse(value);
218 } else if (type.equalsIgnoreCase("number-positive")) {
219 if (NumberFormat.getInstance().parse(value).floatValue() <= 0)
220 return FormValidation.error(Messages.Hudson_NotAPositiveNumber());
221 } else if (type.equalsIgnoreCase("number-negative")) {
222 if (NumberFormat.getInstance().parse(value).floatValue() >= 0)
223 return FormValidation.error(Messages.Hudson_NotANegativeNumber());
224 }
225 } catch (ParseException e) {
226 return FormValidation.error(Messages.Hudson_NotANumber());
227 }
228 }
229
230 return FormValidation.ok();
231 }
232
233 /**
234 * @deprecated
235 * Use {@link Functions#isWindows()}.
236 */
237 public static boolean isWindows() {
238 return File.pathSeparatorChar==';';
239 }
240
241 /**
242 * @deprecated
243 * Use {@link hudson.Platform#isDarwin()}
244 */
245 public static boolean isDarwin() {
246 return Platform.isDarwin();
247 }
248
249 /**
250 * @deprecated since 2007-12-18.
251 * Use {@link #checkPermission(hudson.security.Permission)}
252 */
253 public static boolean adminCheck() throws IOException {
254 return adminCheck(Stapler.getCurrentRequest(), Stapler.getCurrentResponse());
255 }
256
257 /**
258 * @deprecated since 2007-12-18.
259 * Use {@link #checkPermission(hudson.security.Permission)}
260 */
261 public static boolean adminCheck(StaplerRequest req,StaplerResponse rsp) throws IOException {
262 if (isAdmin(req)) return true;
263
264 rsp.sendError(StaplerResponse.SC_FORBIDDEN);
265 return false;
266 }
267
268 /**
269 * Checks if the current user (for which we are processing the current request)
270 * has the admin access.
271 *
272 * @deprecated since 2007-12-18.
273 * This method is deprecated when Hudson moved from simple Unix root-like model
274 * of "admin gets to do everything, and others don't have any privilege" to more
275 * complex {@link hudson.security.ACL} and {@link hudson.security.Permission} based scheme.
276 *
277 * <p>
278 * For a quick migration, use {@code Hudson.getInstance().getACL().hasPermission(Hudson.ADMINISTER)}
279 * To check if the user has the 'administer' role in Hudson.
280 *
281 * <p>
282 * But ideally, your plugin should first identify a suitable {@link hudson.security.Permission} (or create one,
283 * if appropriate), then identify a suitable {@link hudson.security.AccessControlled} object to check its permission
284 * against.
285 */
286 public static boolean isAdmin() {
287 return Jenkins.getInstance().getACL().hasPermission(ADMINISTER);
288 }
289
290 /**
291 * @deprecated since 2007-12-18.
292 * Define a custom {@link hudson.security.Permission} and check against ACL.
293 * See {@link #isAdmin()} for more instructions.
294 */
295 public static boolean isAdmin(StaplerRequest req) {
296 return isAdmin();
297 }
298
299 static {
300 XSTREAM.alias("hudson",Hudson.class);
301 }
302
303 /**
304 * @deprecated only here for backward comp
305 */
306 public static final class MasterComputer extends Jenkins.MasterComputer {
307 // no op
308 }
309
310 /**
311 * @deprecated only here for backward comp
312 */
313 public static class CloudList extends Jenkins.CloudList {
314 public CloudList(Jenkins h) {
315 super(h);
316 }
317
318 public CloudList() {// needed for XStream deserialization
319 super();
320 }
321 }
322 }
323