Class PluginItemContext<T>

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

public class PluginItemContext<T> extends Object
Context to invoke an extension from a DynamicItem.

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 an extension but don't deliver a result back to the caller. Exceptions can be caught and logged.

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

Example if all exceptions should be caught and logged:


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

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


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

Example if return values should be handled:


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

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


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

Example if several exceptions should be handled:


 try (TraceContext traceContext = PluginContext.newTrace(fooDynamicItem.getEntry())) {
   fooDynamicItem.get().doFoo();
 } catch (MyException1 | MyException2 | MyException3 e) {
   // handle the exception
 }
 
  • Constructor Details

  • Method Details

    • hasImplementation

      public boolean hasImplementation()
      Checks if an implementation for this extension point has been registered.
      Returns:
      true if an implementation for this extension point has been registered, otherwise false
    • getPluginName

      public String getPluginName()
      Returns the name of the plugin that registered the extension.
      Returns:
      the plugin name, null if no implementation is registered for this extension point
    • run

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

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

      No-op if no implementation is registered for this extension point.

      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 of the item. All exceptions from the plugin extension are caught and logged.

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

      No-op if no implementation is registered for this extension point.

      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 of the item and returns the result from the plugin extension call.

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

      Fails with IllegalStateException if no implementation is registered for the item.

      Parameters:
      extensionImplFunction - function that invokes the extension
      Returns:
      the result from the plugin extension
      Throws:
      IllegalStateException - if no implementation is registered for the item
    • call

      public <R, X extends Exception> R call(PluginContext.CheckedExtensionImplFunction<T,R,X> checkedExtensionImplFunction, Class<X> exceptionClass) throws X
      Calls the plugin extension of the item 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.

      Fails with IllegalStateException if no implementation is registered for the item.

      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
      IllegalStateException - if no implementation is registered for the item