Class PluginMapEntryContext<T>

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

public class PluginMapEntryContext<T> extends Object
Context to invoke an extension from DynamicMap.

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.


 Map<String, Object> results = new HashMap<>();
 fooPluginMapEntryContext.run(
     extension -> results.put(extension.getExportName(), extension.get().getFoo());
 

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


 Map<String, Object> results = new HashMap<>();
 try {
   fooPluginMapEntryContext.run(
     extension -> results.put(extension.getExportName(), extension.get().getFoo(),
     MyException.class);
 } catch (MyException e) {
   // handle the exception
 }
 

Example if return values should be handled:


 Object result = fooPluginMapEntryContext.call(extension -> extension.get().getFoo());
 

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


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

Example if several exceptions should be handled:


 for (Extension<Foo> fooExtension : fooDynamicMap) {
   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 map entry.
      Returns:
      the plugin name
    • getExportName

      public String getExportName()
      Returns the export name for which this map entry was registered.
      Returns:
      the export name
    • run

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

      The consumer get the Extension provided that should be invoked. The extension provides access to the plugin name and the export name.

      Parameters:
      extensionConsumer - consumer that invokes the extension
    • run

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

      The consumer get the Extension provided that should be invoked. The extension provides access to the plugin name and the export name.

      Parameters:
      extensionConsumer - 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.ExtensionFunction<Extension<T>,R> extensionFunction)
      Calls the plugin extension and returns the result from the plugin extension call.

      The consumer get the Extension provided that should be invoked. The extension provides access to the plugin name and the export name.

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

      public <R, X extends Exception> R call(PluginContext.CheckedExtensionFunction<Extension<T>,R,X> checkedExtensionFunction, 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 consumer get the Extension provided that should be invoked. The extension provides access to the plugin name and the export name.

      Parameters:
      checkedExtensionFunction - 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