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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, 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 hudson.ExtensionPoint;
27 import hudson.ExtensionList;
28 import hudson.Extension;
29 import hudson.ExtensionPoint.LegacyInstancesAreScopedToHudson;
30 import hudson.triggers.SCMTrigger;
31 import hudson.triggers.TimerTrigger;
32
33 import java.util.Set;
34 import java.io.IOException;
35
36 import jenkins.model.Jenkins;
37 import org.kohsuke.stapler.StaplerRequest;
38 import org.kohsuke.stapler.StaplerResponse;
39
40 /**
41 * Checks the health of a subsystem of Jenkins and if there's something
42 * that requires administrator's attention, notify the administrator.
43 *
44 * <h2>How to implement?</h2>
45 * <p>
46 * Plugins who wish to contribute such notifications can implement this
47 * class and put {@link Extension} on it to register it to Jenkins.
48 *
49 * <p>
50 * Once installed, it's the implementer's responsibility to perform
51 * monitoring and activate/deactivate the monitor accordingly. Sometimes
52 * this can be done by updating a flag from code (see {@link SCMTrigger}
53 * for one such example), while other times it's more convenient to do
54 * so by running some code periodically (for this, use {@link TimerTrigger#timer})
55 *
56 * <p>
57 * {@link AdministrativeMonitor}s are bound to URL by {@link Jenkins#getAdministrativeMonitor(String)}.
58 * See {@link #getUrl()}.
59 *
60 * <h3>Views</h3>
61 * <dl>
62 * <dt>message.jelly</dt>
63 * <dd>
64 * If {@link #isActivated()} returns true, Hudson will use the <tt>message.jelly</tt>
65 * view of this object to render the warning text. This happens in the
66 * <tt>http://SERVER/jenkins/manage</tt> page. This view should typically render
67 * a DIV box with class='error' or class='warning' with a human-readable text
68 * inside it. It often also contains a link to a page that provides more details
69 * about the problem.
70 * </dd>
71 * </dl>
72 *
73 * @author Kohsuke Kawaguchi
74 * @since 1.273
75 * @see Jenkins#administrativeMonitors
76 */
77 @LegacyInstancesAreScopedToHudson
78 public abstract class AdministrativeMonitor extends AbstractModelObject implements ExtensionPoint {
79 /**
80 * Human-readable ID of this monitor, which needs to be unique within the system.
81 *
82 * <p>
83 * This ID is used to remember persisted setting for this monitor,
84 * so the ID should remain consistent beyond the Hudson JVM lifespan.
85 */
86 public final String id;
87
88 protected AdministrativeMonitor(String id) {
89 this.id = id;
90 }
91
92 protected AdministrativeMonitor() {
93 this.id = this.getClass().getName();
94 }
95
96 /**
97 * Returns the URL of this monitor, relative to the context path, like "administrativeMonitor/foobar".
98 */
99 public String getUrl() {
100 return "administrativeMonitor/"+id;
101 }
102
103 public String getDisplayName() {
104 return id;
105 }
106
107 public final String getSearchUrl() {
108 return getUrl();
109 }
110
111 /**
112 * Mark this monitor as disabled, to prevent this from showing up in the UI.
113 */
114 public void disable(boolean value) throws IOException {
115 AbstractCIBase hudson = Jenkins.getInstance();
116 Set<String> set = hudson.disabledAdministrativeMonitors;
117 if(value) set.add(id);
118 else set.remove(id);
119 hudson.save();
120 }
121
122 /**
123 * Returns true if this monitor {@link #disable(boolean) isn't disabled} earlier.
124 *
125 * <p>
126 * This flag implements the ability for the admin to say "no thank you" to the monitor that
127 * he wants to ignore.
128 */
129 public boolean isEnabled() {
130 return !((AbstractCIBase)Jenkins.getInstance()).disabledAdministrativeMonitors.contains(id);
131 }
132
133 /**
134 * Returns true if this monitor is activated and
135 * wants to produce a warning message.
136 *
137 * <p>
138 * This method is called from the HTML rendering thread,
139 * so it should run efficiently.
140 */
141 public abstract boolean isActivated();
142
143 /**
144 * URL binding to disable this monitor.
145 */
146 public void doDisable(StaplerRequest req, StaplerResponse rsp) throws IOException {
147 Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
148 disable(true);
149 rsp.sendRedirect2(req.getContextPath()+"/manage");
150 }
151
152 /**
153 * All registered {@link AdministrativeMonitor} instances.
154 */
155 public static ExtensionList<AdministrativeMonitor> all() {
156 return Jenkins.getInstance().getExtensionList(AdministrativeMonitor.class);
157 }
158 }
159