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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2012, 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 jenkins;
25
26 import hudson.ExtensionComponent;
27 import hudson.ExtensionFinder;
28 import hudson.ExtensionList;
29 import hudson.ExtensionPoint;
30 import hudson.model.AdministrativeMonitor;
31 import hudson.model.Describable;
32 import hudson.model.Descriptor;
33 import hudson.model.DescriptorVisibilityFilter;
34 import jenkins.model.Jenkins;
35
36 /**
37 * Filters out {@link ExtensionComponent}s discovered by {@link ExtensionFinder}s,
38 * as if they were never discovered.
39 *
40 * <p>
41 * This is useful for those who are deploying restricted/simplified version of Jenkins
42 * by reducing the functionality.
43 *
44 * <p>
45 * Because of the way {@link ExtensionFinder} works, even when an extension component
46 * is rejected by a filter, its instance still gets created first.
47 *
48 * @author Kohsuke Kawaguchi
49 * @since 1.472
50 * @see DescriptorVisibilityFilter
51 * @see ExtensionComponentSet#filtered()
52 */
53 public abstract class ExtensionFilter implements ExtensionPoint {
54 /**
55 * Checks if a newly discovered extension is allowed to participate into Jenkins.
56 *
57 * <p>
58 * To filter {@link Descriptor}s based on the {@link Describable} subtypes, do as follows:
59 *
60 * <pre>
61 * return !component.isDescriptorOf(Builder.class);
62 * </pre>
63 *
64 * @param type
65 * The type of the extension that we are discovering. This is not the actual instance
66 * type, but the contract type, such as {@link Descriptor}, {@link AdministrativeMonitor}, etc.
67 * @return
68 * true to let the component into Jenkins. false to drop it and pretend
69 * as if it didn't exist. When any one of {@link ExtensionFilter}s veto
70 * a component, it gets dropped.
71 */
72 public abstract <T> boolean allows(Class<T> type, ExtensionComponent<T> component);
73
74 public static <T> boolean isAllowed(Class<T> type, ExtensionComponent<T> component) {
75 // to avoid infinite recursion, those extension points are handled differently.
76 if (type==ExtensionFilter.class || type==ExtensionFinder.class)
77 return true;
78
79 for (ExtensionFilter f : all())
80 if (!f.allows(type, component))
81 return false;
82 return true;
83 }
84
85 /**
86 * All registered {@link ExtensionFilter} instances.
87 */
88 public static ExtensionList<ExtensionFilter> all() {
89 return Jenkins.getInstance().getExtensionList(ExtensionFilter.class);
90 }
91 }
92