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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, 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
25 package hudson;
26
27 import hudson.model.Describable;
28 import hudson.model.Descriptor;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import jenkins.ExtensionFilter;
32
33 /**
34 * Discovered {@link Extension} object with a bit of metadata for Hudson.
35 * This is a plain value object.
36 *
37 * @author Kohsuke Kawaguchi
38 * @since 1.356
39 * @see ExtensionFinder
40 * @see ExtensionFilter
41 */
42 public class ExtensionComponent<T> implements Comparable<ExtensionComponent<T>> {
43 private static final Logger LOG = Logger.getLogger(ExtensionComponent.class.getName());
44 private final T instance;
45 private final double ordinal;
46
47 public ExtensionComponent(T instance, double ordinal) {
48 this.instance = instance;
49 this.ordinal = ordinal;
50 }
51
52 public ExtensionComponent(T instance, Extension annotation) {
53 this(instance,annotation.ordinal());
54 }
55
56 public ExtensionComponent(T instance) {
57 this(instance,0);
58 }
59
60 /**
61 * See {@link Extension#ordinal()}. Used to sort extensions.
62 */
63 public double ordinal() {
64 return ordinal;
65 }
66
67 /**
68 * The instance of the discovered extension.
69 *
70 * @return never null.
71 */
72 public T getInstance() {
73 return instance;
74 }
75
76 /**
77 * Checks if this component is a {@link Descriptor} describing the given type
78 *
79 * For example, {@code component.isDescriptorOf(Builder.class)}
80 */
81 public boolean isDescriptorOf(Class<? extends Describable> c) {
82 return instance instanceof Descriptor && ((Descriptor)instance).isSubTypeOf(c);
83 }
84
85 /**
86 * Sort {@link ExtensionComponent}s in the descending order of {@link #ordinal()}.
87 */
88 public int compareTo(ExtensionComponent<T> that) {
89 double a = this.ordinal();
90 double b = that.ordinal();
91 if (a>b) return -1;
92 if (a<b) return 1;
93
94 // make the order bit more deterministic among extensions of the same ordinal
95 if (this.instance instanceof Descriptor && that.instance instanceof Descriptor) {
96 try {
97 return Util.fixNull(((Descriptor)this.instance).getDisplayName()).compareTo(Util.fixNull(((Descriptor)that.instance).getDisplayName()));
98 } catch (RuntimeException x) {
99 LOG.log(Level.WARNING, null, x);
100 } catch (LinkageError x) {
101 LOG.log(Level.WARNING, null, x);
102 }
103 }
104 return this.instance.getClass().getName().compareTo(that.instance.getClass().getName());
105 }
106 }
107