PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/model/DescriptorVisibilityFilter.java

http://github.com/hudson/hudson
Java | 54 lines | 25 code | 7 blank | 22 comment | 3 complexity | 068dd771d205bd2a51bac4fc48e8b95d MD5 | raw file
 1package hudson.model;
 2
 3import hudson.ExtensionList;
 4import hudson.ExtensionPoint;
 5import hudson.scm.SCMDescriptor;
 6
 7import java.util.ArrayList;
 8import java.util.List;
 9
10/**
11 * Hides {@link Descriptor}s from users.
12 *
13 * @author Kohsuke Kawaguchi
14 * @since 1.393
15 */
16public abstract class DescriptorVisibilityFilter implements ExtensionPoint {
17
18    /**
19     * Decides if the given descriptor should be visible to the user.
20     *
21     * @param context
22     *      The object that indicates where the visibility of a descriptor is evaluated.
23     *      For example, if Hudson is deciding whether a {@link FreeStyleProject} should gets a
24     *      {@link SCMDescriptor}, the context object will be the {@link FreeStyleProject}.
25     *      The caller can pass in null if there's no context.
26     * @param descriptor
27     *      Descriptor whose visibility is evaluated. Never null.
28     *
29     * @return
30     *      true to allow the descriptor to be visible. false to hide it.
31     *      If any of the installed {@link DescriptorVisibilityFilter} returns false,
32     *      the descriptor is not shown.
33     */
34    public abstract boolean filter(Object context, Descriptor descriptor);
35
36    public static ExtensionList<DescriptorVisibilityFilter> all() {
37        return Hudson.getInstance().getExtensionList(DescriptorVisibilityFilter.class);
38    }
39
40    public static <T extends Descriptor> List<T> apply(Object context, Iterable<T> source) {
41        ExtensionList<DescriptorVisibilityFilter> filters = all();
42        List<T> r = new ArrayList<T>();
43        
44        OUTER:
45        for (T d : source) {
46            for (DescriptorVisibilityFilter f : filters) {
47                if (!f.filter(context,d))
48                    continue OUTER; // veto-ed. not shown
49            }
50            r.add(d);
51        }
52        return r;
53    }
54}