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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, Inc.
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.diagnosis;
25
26 import hudson.Extension;
27 import hudson.PluginWrapper;
28 import hudson.init.Initializer;
29 import hudson.model.AdministrativeMonitor;
30 import hudson.model.Descriptor;
31 import jenkins.model.Jenkins;
32
33 import java.text.MessageFormat;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39
40 import static hudson.init.InitMilestone.EXTENSIONS_AUGMENTED;
41
42 /**
43 * Some old descriptors apparently has the getId() method that's used in different ways
44 * and that's causing errors like JENKINS-8866, so detect and report that.
45 *
46 * @author Kohsuke Kawaguchi
47 * @since 1.402
48 */
49 @Extension
50 public class NullIdDescriptorMonitor extends AdministrativeMonitor {
51
52 private final List<Descriptor> problems = new ArrayList<Descriptor>();
53
54 @Override
55 public boolean isActivated() {
56 return !problems.isEmpty();
57 }
58
59 public List<Descriptor> getProblems() {
60 return Collections.unmodifiableList(problems);
61 }
62
63 private void verify() {
64 Jenkins h = Jenkins.getInstance();
65 for (Descriptor d : h.getExtensionList(Descriptor.class)) {
66 PluginWrapper p = h.getPluginManager().whichPlugin(d.getClass());
67 String id;
68 try {
69 id = d.getId();
70 } catch (Throwable t) {
71 LOGGER.log(Level.SEVERE,MessageFormat.format("Descriptor {0} from plugin {1} with display name {2} reported an exception for ID",
72 d, p == null ? "???" : p.getLongName(), d.getDisplayName()),t);
73 problems.add(d);
74 continue;
75 }
76 if (id==null) {
77 LOGGER.severe(MessageFormat.format("Descriptor {0} from plugin {1} with display name {2} has null ID",
78 d, p==null?"???":p.getLongName(), d.getDisplayName()));
79 problems.add(d);
80 }
81 }
82 }
83
84 @Initializer(after=EXTENSIONS_AUGMENTED)
85 public static void verifyId() {
86 AdministrativeMonitor.all().get(NullIdDescriptorMonitor.class).verify();
87 }
88
89 private static final Logger LOGGER = Logger.getLogger(NullIdDescriptorMonitor.class.getName());
90 }
91