/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

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