PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/java/hudson/model/Hudson.java

https://github.com/jpederzolli/jenkins-1
Java | 345 lines | 168 code | 33 blank | 144 comment | 19 complexity | 37ce8bd26870b2462ae7a5188daf234c MD5 | raw file
  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. import hudson.ExtensionListView;
  28. import hudson.Functions;
  29. import hudson.Platform;
  30. import hudson.PluginManager;
  31. import hudson.cli.declarative.CLIResolver;
  32. import hudson.model.listeners.ItemListener;
  33. import hudson.slaves.ComputerListener;
  34. import hudson.util.CopyOnWriteList;
  35. import hudson.util.FormValidation;
  36. import jenkins.model.Jenkins;
  37. import org.jvnet.hudson.reactor.ReactorException;
  38. import org.kohsuke.stapler.QueryParameter;
  39. import org.kohsuke.stapler.Stapler;
  40. import org.kohsuke.stapler.StaplerRequest;
  41. import org.kohsuke.stapler.StaplerResponse;
  42. import javax.servlet.ServletContext;
  43. import javax.servlet.ServletException;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.text.NumberFormat;
  47. import java.text.ParseException;
  48. import java.util.Collections;
  49. import java.util.List;
  50. import java.util.Map;
  51. import static hudson.Util.fixEmpty;
  52. import javax.annotation.CheckForNull;
  53. public class Hudson extends Jenkins {
  54. /**
  55. * List of registered {@link hudson.model.listeners.ItemListener}s.
  56. * @deprecated as of 1.286
  57. */
  58. @Deprecated
  59. private transient final CopyOnWriteList<ItemListener> itemListeners = ExtensionListView.createCopyOnWriteList(ItemListener.class);
  60. /**
  61. * List of registered {@link hudson.slaves.ComputerListener}s.
  62. * @deprecated as of 1.286
  63. */
  64. @Deprecated
  65. private transient final CopyOnWriteList<ComputerListener> computerListeners = ExtensionListView.createCopyOnWriteList(ComputerListener.class);
  66. /** @deprecated Here only for compatibility. Use {@link Jenkins#getInstance} instead. */
  67. @Deprecated
  68. @CLIResolver
  69. public static @CheckForNull Hudson getInstance() {
  70. return (Hudson)Jenkins.getInstance();
  71. }
  72. public Hudson(File root, ServletContext context) throws IOException, InterruptedException, ReactorException {
  73. this(root,context,null);
  74. }
  75. public Hudson(File root, ServletContext context, PluginManager pluginManager) throws IOException, InterruptedException, ReactorException {
  76. super(root, context, pluginManager);
  77. }
  78. /**
  79. * Gets all the installed {@link ItemListener}s.
  80. *
  81. * @deprecated as of 1.286.
  82. * Use {@link ItemListener#all()}.
  83. */
  84. @Deprecated
  85. public CopyOnWriteList<ItemListener> getJobListeners() {
  86. return itemListeners;
  87. }
  88. /**
  89. * Gets all the installed {@link ComputerListener}s.
  90. *
  91. * @deprecated as of 1.286.
  92. * Use {@link ComputerListener#all()}.
  93. */
  94. @Deprecated
  95. public CopyOnWriteList<ComputerListener> getComputerListeners() {
  96. return computerListeners;
  97. }
  98. /**
  99. * Gets the slave node of the give name, hooked under this Hudson.
  100. *
  101. * @deprecated
  102. * Use {@link #getNode(String)}. Since 1.252.
  103. */
  104. @Deprecated
  105. public Slave getSlave(String name) {
  106. Node n = getNode(name);
  107. if (n instanceof Slave)
  108. return (Slave)n;
  109. return null;
  110. }
  111. /**
  112. * @deprecated
  113. * Use {@link #getNodes()}. Since 1.252.
  114. */
  115. @Deprecated
  116. public List<Slave> getSlaves() {
  117. return (List)getNodes();
  118. }
  119. /**
  120. * Updates the slave list.
  121. *
  122. * @deprecated
  123. * Use {@link #setNodes(List)}. Since 1.252.
  124. */
  125. @Deprecated
  126. public void setSlaves(List<Slave> slaves) throws IOException {
  127. setNodes(slaves);
  128. }
  129. /**
  130. * @deprecated
  131. * Left only for the compatibility of URLs.
  132. * Should not be invoked for any other purpose.
  133. */
  134. @Deprecated
  135. public TopLevelItem getJob(String name) {
  136. return getItem(name);
  137. }
  138. /**
  139. * @deprecated
  140. * Used only for mapping jobs to URL in a case-insensitive fashion.
  141. */
  142. @Deprecated
  143. public TopLevelItem getJobCaseInsensitive(String name) {
  144. String match = Functions.toEmailSafeString(name);
  145. for(TopLevelItem item : getItems()) {
  146. if(Functions.toEmailSafeString(item.getName()).equalsIgnoreCase(match)) {
  147. return item;
  148. }
  149. }
  150. return null;
  151. }
  152. /**
  153. * @deprecated as of 1.317
  154. * Use {@link #doQuietDown()} instead.
  155. */
  156. @Deprecated
  157. public synchronized void doQuietDown(StaplerResponse rsp) throws IOException, ServletException {
  158. doQuietDown().generateResponse(null, rsp, this);
  159. }
  160. /**
  161. * RSS feed for log entries.
  162. *
  163. * @deprecated
  164. * As on 1.267, moved to "/log/rss..."
  165. */
  166. @Deprecated
  167. public void doLogRss( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
  168. String qs = req.getQueryString();
  169. rsp.sendRedirect2("./log/rss"+(qs==null?"":'?'+qs));
  170. }
  171. /**
  172. * @deprecated as of 1.294
  173. * Define your own check method, instead of relying on this generic one.
  174. */
  175. @Deprecated
  176. public void doFieldCheck(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
  177. doFieldCheck(
  178. fixEmpty(req.getParameter("value")),
  179. fixEmpty(req.getParameter("type")),
  180. fixEmpty(req.getParameter("errorText")),
  181. fixEmpty(req.getParameter("warningText"))).generateResponse(req,rsp,this);
  182. }
  183. /**
  184. * Checks if the value for a field is set; if not an error or warning text is displayed.
  185. * If the parameter "value" is not set then the parameter "errorText" is displayed
  186. * as an error text. If the parameter "errorText" is not set, then the parameter "warningText"
  187. * is displayed as a warning text.
  188. * <p>
  189. * If the text is set and the parameter "type" is set, it will validate that the value is of the
  190. * correct type. Supported types are "number, "number-positive" and "number-negative".
  191. *
  192. * @deprecated as of 1.324
  193. * Either use client-side validation (e.g. class="required number")
  194. * or define your own check method, instead of relying on this generic one.
  195. */
  196. @Deprecated
  197. public FormValidation doFieldCheck(@QueryParameter(fixEmpty=true) String value,
  198. @QueryParameter(fixEmpty=true) String type,
  199. @QueryParameter(fixEmpty=true) String errorText,
  200. @QueryParameter(fixEmpty=true) String warningText) {
  201. if (value == null) {
  202. if (errorText != null)
  203. return FormValidation.error(errorText);
  204. if (warningText != null)
  205. return FormValidation.warning(warningText);
  206. return FormValidation.error("No error or warning text was set for fieldCheck().");
  207. }
  208. if (type != null) {
  209. try {
  210. if (type.equalsIgnoreCase("number")) {
  211. NumberFormat.getInstance().parse(value);
  212. } else if (type.equalsIgnoreCase("number-positive")) {
  213. if (NumberFormat.getInstance().parse(value).floatValue() <= 0)
  214. return FormValidation.error(Messages.Hudson_NotAPositiveNumber());
  215. } else if (type.equalsIgnoreCase("number-negative")) {
  216. if (NumberFormat.getInstance().parse(value).floatValue() >= 0)
  217. return FormValidation.error(Messages.Hudson_NotANegativeNumber());
  218. }
  219. } catch (ParseException e) {
  220. return FormValidation.error(Messages.Hudson_NotANumber());
  221. }
  222. }
  223. return FormValidation.ok();
  224. }
  225. /**
  226. * @deprecated
  227. * Use {@link Functions#isWindows()}.
  228. */
  229. @Deprecated
  230. public static boolean isWindows() {
  231. return File.pathSeparatorChar==';';
  232. }
  233. /**
  234. * @deprecated
  235. * Use {@link hudson.Platform#isDarwin()}
  236. */
  237. @Deprecated
  238. public static boolean isDarwin() {
  239. return Platform.isDarwin();
  240. }
  241. /**
  242. * @deprecated since 2007-12-18.
  243. * Use {@link #checkPermission(hudson.security.Permission)}
  244. */
  245. @Deprecated
  246. public static boolean adminCheck() throws IOException {
  247. return adminCheck(Stapler.getCurrentRequest(), Stapler.getCurrentResponse());
  248. }
  249. /**
  250. * @deprecated since 2007-12-18.
  251. * Use {@link #checkPermission(hudson.security.Permission)}
  252. */
  253. @Deprecated
  254. public static boolean adminCheck(StaplerRequest req,StaplerResponse rsp) throws IOException {
  255. if (isAdmin(req)) return true;
  256. rsp.sendError(StaplerResponse.SC_FORBIDDEN);
  257. return false;
  258. }
  259. /**
  260. * Checks if the current user (for which we are processing the current request)
  261. * has the admin access.
  262. *
  263. * @deprecated since 2007-12-18.
  264. * This method is deprecated when Hudson moved from simple Unix root-like model
  265. * of "admin gets to do everything, and others don't have any privilege" to more
  266. * complex {@link hudson.security.ACL} and {@link hudson.security.Permission} based scheme.
  267. *
  268. * <p>
  269. * For a quick migration, use {@code Hudson.getInstance().getACL().hasPermission(Hudson.ADMINISTER)}
  270. * To check if the user has the 'administer' role in Hudson.
  271. *
  272. * <p>
  273. * But ideally, your plugin should first identify a suitable {@link hudson.security.Permission} (or create one,
  274. * if appropriate), then identify a suitable {@link hudson.security.AccessControlled} object to check its permission
  275. * against.
  276. */
  277. @Deprecated
  278. public static boolean isAdmin() {
  279. return Jenkins.getInstance().getACL().hasPermission(ADMINISTER);
  280. }
  281. /**
  282. * @deprecated since 2007-12-18.
  283. * Define a custom {@link hudson.security.Permission} and check against ACL.
  284. * See {@link #isAdmin()} for more instructions.
  285. */
  286. @Deprecated
  287. public static boolean isAdmin(StaplerRequest req) {
  288. return isAdmin();
  289. }
  290. static {
  291. XSTREAM.alias("hudson",Hudson.class);
  292. }
  293. /**
  294. * @deprecated only here for backward comp
  295. */
  296. @Deprecated
  297. public static final class MasterComputer extends Jenkins.MasterComputer {
  298. // no op
  299. }
  300. /**
  301. * @deprecated only here for backward comp
  302. */
  303. @Deprecated
  304. public static class CloudList extends Jenkins.CloudList {
  305. public CloudList(Jenkins h) {
  306. super(h);
  307. }
  308. public CloudList() {// needed for XStream deserialization
  309. super();
  310. }
  311. }
  312. }