Class PluginMapContext<T>
- java.lang.Object
-
- com.google.gerrit.server.plugincontext.PluginMapContext<T>
-
- All Implemented Interfaces:
Iterable<PluginMapEntryContext<T>>
public class PluginMapContext<T> extends Object implements Iterable<PluginMapEntryContext<T>>
Context to invoke extensions from aDynamicMap
.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:
Map
results = new HashMap<>(); fooPluginMapContext.runEach( extension -> results.put(extension.getExportName(), extension.get().getFoo()); Example if all exceptions, but one, should be caught and logged:
Map
results = new HashMap<>(); try { fooPluginMapContext.runEach( extension -> results.put(extension.getExportName(), extension.get().getFoo(), MyException.class); } catch (MyException e) { // handle the exception } Example if return values should be handled:
Map
results = new HashMap<>(); for (PluginMapEntryContext c : fooPluginMapContext) { if (c.call(extension -> extension.get().handles(x))) { c.run(extension -> results.put(extension.getExportName(), extension.get().getFoo()); } } Example if return values and a single exception should be handled:
Map
results = new HashMap<>(); try { for (PluginMapEntryContext c : fooPluginMapContext) { if (c.call(extension -> extension.handles(x), MyException.class)) { c.run(extension -> results.put(extension.getExportName(), extension.get().getFoo(), MyException.class); } } } catch (MyException e) { // handle the exception } Example if several exceptions should be handled:
for (Extension
fooExtension : fooDynamicMap) { try (TraceContext traceContext = PluginContext.newTrace(fooExtension)) { fooExtension.get().doFoo(); } catch (MyException1 | MyException2 | MyException3 e) { // handle the exception } }
-
-
Constructor Summary
Constructors Constructor Description PluginMapContext(DynamicMap<T> dynamicMap, PluginContext.PluginMetrics pluginMetrics)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isEmpty()
Checks if no implementations for this extension point have been registered.Iterator<PluginMapEntryContext<T>>
iterator()
Iterator that provides contexts for invoking the extensions in this map.SortedSet<String>
plugins()
Returns a sorted list of the plugins that have registered implementations for this extension point.void
runEach(PluginContext.ExtensionConsumer<Extension<T>> extensionConsumer)
Invokes each extension in the map.<X extends Exception>
voidrunEach(PluginContext.ExtensionConsumer<Extension<T>> extensionConsumer, Class<X> exceptionClass)
Invokes each extension in the map.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
PluginMapContext
@Inject public PluginMapContext(DynamicMap<T> dynamicMap, PluginContext.PluginMetrics pluginMetrics)
-
-
Method Detail
-
iterator
public Iterator<PluginMapEntryContext<T>> iterator()
Iterator that provides contexts for invoking the extensions in this map.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
-
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, otherwisefalse
-
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.ExtensionConsumer<Extension<T>> extensionConsumer)
Invokes each extension in the map. All exceptions from the plugin extensions 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.All extension in the map are invoked, even if invoking some of the extensions failed.
- Parameters:
extensionConsumer
- consumer that invokes the extension
-
runEach
public <X extends Exception> void runEach(PluginContext.ExtensionConsumer<Extension<T>> extensionConsumer, Class<X> exceptionClass) throws X extends Exception
Invokes each extension in the map. All exceptions from the plugin extensions except exceptions of the specified type 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.All extension in the map are invoked, even if invoking some of the extensions failed.
- Parameters:
extensionConsumer
- consumer that invokes the extensionexceptionClass
- type of the exceptions that should be thrown- Throws:
X
- expected exception from the plugin extensionX extends Exception
-
-