Class PluginSetEntryContext<T>

java.lang.Object
com.google.gerrit.server.plugincontext.PluginSetEntryContext<T>

public class PluginSetEntryContext<T> extends Object
Context to invoke an extension from DynamicSet.

When the 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.

The run* methods execute the extension but don't deliver a result back to the caller. Exceptions can be caught and logged.

The call* methods execute the extension and deliver a result back to the caller.

Example if all exceptions should be caught and logged:


 fooPluginSetEntryContext.run(foo -> foo.doFoo());
 

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


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

Example if return values should be handled:


 Object result = fooPluginSetEntryContext.call(foo -> foo.getFoo());
 

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


 Object result;
 try {
   result = fooPluginSetEntryContext.call(foo -> foo.getFoo(), MyException.class);
 } catch (MyException e) {
   // handle the exception
 }
 

Example if several exceptions should be handled:


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

    • getPluginName

      public String getPluginName()
      Returns the name of the plugin that registered this extension.
      Returns:
      the plugin name
    • get

      public T get()
      Returns the implementation of this extension.

      Should only be used in exceptional cases to get direct access to the extension implementation. If possible the extension should be invoked through run(PluginContext.ExtensionImplConsumer), run(PluginContext.ExtensionImplConsumer, java.lang.Class), call(PluginContext.ExtensionImplFunction) and call(PluginContext.CheckedExtensionImplFunction, java.lang.Class).

      Returns:
      the implementation of this extension
    • run

      public void run(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer)
      Invokes the plugin extension. All exceptions from the plugin extension are caught and logged.

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

      Parameters:
      extensionImplConsumer - consumer that invokes the extension
    • run

      public <X extends Exception> void run(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer, Class<X> exceptionClass) throws X
      Invokes the plugin extension. All exceptions from the plugin extension are caught and logged.

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

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

      public <R> R call(PluginContext.ExtensionImplFunction<T,R> extensionImplFunction)
      Calls the plugin extension and returns the result from the plugin extension call.

      The function gets the extension point provided that should be invoked.

      Parameters:
      extensionImplFunction - function that invokes the extension
      Returns:
      the result from the plugin extension
    • call

      public <R, X extends Exception> R call(PluginContext.CheckedExtensionImplFunction<T,R,X> checkedExtensionImplFunction, Class<X> exceptionClass) throws X
      Calls the plugin extension and returns the result from the plugin extension call. Exceptions of the specified type are thrown and must be handled by the caller.

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

      Parameters:
      checkedExtensionImplFunction - function that invokes the extension
      exceptionClass - type of the exceptions that should be thrown
      Returns:
      the result from the plugin extension
      Throws:
      X - expected exception from the plugin extension