public class PluginSetContext<T> extends java.lang.Object implements java.lang.Iterable<PluginSetEntryContext<T>>
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 (PluginSetEntryContextc : 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 (PluginSetEntryContextc : 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 (ExtensionfooExtension : fooDynamicSet.entries()) { try (TraceContext traceContext = PluginContext.newTrace(fooExtension)) { fooExtension.get().doFoo(); } catch (MyException1 | MyException2 | MyException3 e) { // handle the exception } }
Constructor and Description |
---|
PluginSetContext(DynamicSet<T> dynamicSet,
PluginContext.PluginMetrics pluginMetrics) |
Modifier and Type | Method and Description |
---|---|
boolean |
isEmpty()
Checks if no implementations for this extension point have been registered.
|
java.util.Iterator<PluginSetEntryContext<T>> |
iterator()
Iterator that provides contexts for invoking the extensions in this set.
|
java.util.SortedSet<java.lang.String> |
plugins()
Returns a sorted list of the plugins that have registered implementations for this extension
point.
|
void |
runEach(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer)
Invokes each extension in the set.
|
<X extends java.lang.Exception> |
runEach(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer,
java.lang.Class<X> exceptionClass)
Invokes each extension in the set.
|
java.util.stream.Stream<T> |
stream() |
@Inject public PluginSetContext(DynamicSet<T> dynamicSet, PluginContext.PluginMetrics pluginMetrics)
public java.util.Iterator<PluginSetEntryContext<T>> iterator()
This is useful if:
iterator
in interface java.lang.Iterable<PluginSetEntryContext<T>>
public boolean isEmpty()
true
if no implementations for this extension point have been registered,
otherwise false
public java.util.SortedSet<java.lang.String> plugins()
public void runEach(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer)
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.
extensionImplConsumer
- consumer that invokes the extensionpublic java.util.stream.Stream<T> stream()
public <X extends java.lang.Exception> void runEach(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer, java.lang.Class<X> exceptionClass) throws X extends java.lang.Exception
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.
extensionImplConsumer
- consumer that invokes the extensionexceptionClass
- type of the exceptions that should be thrownX
- expected exception from the plugin extensionX extends java.lang.Exception