Class PluginSetContext<T>

  • All Implemented Interfaces:
    Iterable<PluginSetEntryContext<T>>

    public class PluginSetContext<T>
    extends Object
    implements Iterable<PluginSetEntryContext<T>>
    Context to invoke extensions from a DynamicSet.

    When a plugin extension is invoked a logging tag with the plugin name is set. This way any errors that are triggered by the plugin extension (even if they happen in Gerrit code which is called by the plugin extension) can be easily attributed to the plugin.

    Example if all exceptions should be caught and logged:

     fooPluginSetContext.runEach(foo -> foo.doFoo());
     

    Example if all exceptions, but one, should be caught and logged:

     try {
       fooPluginSetContext.runEach(foo -> foo.doFoo(), MyException.class);
     } catch (MyException e) {
       // handle the exception
     }
     

    Example if return values should be handled:

     for (PluginSetEntryContext c : fooPluginSetContext) {
       if (c.call(foo -> foo.handles(x))) {
         c.run(foo -> foo.doFoo());
       }
     }
     

    Example if return values and a single exception should be handled:

     try {
       for (PluginSetEntryContext c : fooPluginSetContext) {
         if (c.call(foo -> foo.handles(x), MyException.class)) {
           c.run(foo -> foo.doFoo(), MyException.class);
         }
       }
     } catch (MyException e) {
       // handle the exception
     }
     

    Example if several exceptions should be handled:

     for (Extension fooExtension : fooDynamicSet.entries()) {
       try (TraceContext traceContext = PluginContext.newTrace(fooExtension)) {
         fooExtension.get().doFoo();
       } catch (MyException1 | MyException2 | MyException3 e) {
         // handle the exception
       }
     }
     
    • Method Detail

      • iterator

        public Iterator<PluginSetEntryContext<T>> iterator()
        Iterator that provides contexts for invoking the extensions in this set.

        This is useful if:

        • invoking of each extension returns a result that should be handled
        • a sequence of invocations should be done on each extension
        Specified by:
        iterator in interface Iterable<T>
      • isEmpty

        public boolean isEmpty()
        Checks if no implementations for this extension point have been registered.
        Returns:
        true if no implementations for this extension point have been registered, otherwise false
      • plugins

        public SortedSet<String> plugins()
        Returns a sorted list of the plugins that have registered implementations for this extension point.
        Returns:
        sorted list of the plugins that have registered implementations for this extension point
      • runEach

        public void runEach​(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer)
        Invokes each extension in the set. All exceptions from the plugin extensions are caught and logged.

        The consumer gets the extension implementation provided that should be invoked.

        All extension in the set are invoked, even if invoking some of the extensions failed.

        Parameters:
        extensionImplConsumer - consumer that invokes the extension
      • stream

        public Stream<T> stream()
      • runEach

        public <X extends Exception> void runEach​(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer,
                                                  Class<X> exceptionClass)
                                           throws X extends Exception
        Invokes each extension in the set. All exceptions from the plugin extensions except exceptions of the specified type are caught and logged.

        The consumer gets the extension implementation provided that should be invoked.

        All extension in the set are invoked, even if invoking some of the extensions failed.

        Parameters:
        extensionImplConsumer - consumer that invokes the extension
        exceptionClass - type of the exceptions that should be thrown
        Throws:
        X - expected exception from the plugin extension
        X extends Exception