public class PluginItemContext<T>
extends java.lang.Object
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 and Description |
---|
PluginItemContext(DynamicItem<T> dynamicItem,
PluginContext.PluginMetrics pluginMetrics) |
Modifier and Type | Method and Description |
---|---|
<R,X extends java.lang.Exception> |
call(PluginContext.CheckedExtensionImplFunction<T,R,X> checkedExtensionImplFunction,
java.lang.Class<X> exceptionClass)
Calls the plugin extension of the item and returns the result from the plugin extension call.
|
<R> R |
call(PluginContext.ExtensionImplFunction<T,R> extensionImplFunction)
Calls the plugin extension of the item and returns the result from the plugin extension call.
|
java.lang.String |
getPluginName()
Returns the name of the plugin that registered the extension.
|
boolean |
hasImplementation()
Checks if an implementation for this extension point has been registered.
|
void |
run(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer)
Invokes the plugin extension of the item.
|
<X extends java.lang.Exception> |
run(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer,
java.lang.Class<X> exceptionClass)
Invokes the plugin extension of the item.
|
@Inject public PluginItemContext(DynamicItem<T> dynamicItem, PluginContext.PluginMetrics pluginMetrics)
public boolean hasImplementation()
true
if an implementation for this extension point has been registered,
otherwise false
public java.lang.String getPluginName()
null
if no implementation is registered for this extension
pointpublic void run(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer)
The consumer gets the extension implementation provided that should be invoked.
No-op if no implementation is registered for this extension point.
extensionImplConsumer
- consumer that invokes the extensionpublic <X extends java.lang.Exception> void run(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.
No-op if no implementation is registered for this extension point.
extensionImplConsumer
- consumer that invokes the extensionexceptionClass
- type of the exceptions that should be thrownX
- expected exception from the plugin extensionX extends java.lang.Exception
public <R> R call(PluginContext.ExtensionImplFunction<T,R> extensionImplFunction)
The function gets the extension implementation provided that should be invoked.
Fails with IllegalStateException
if no implementation is registered for the item.
extensionImplFunction
- function that invokes the extensionjava.lang.IllegalStateException
- if no implementation is registered for the itempublic <R,X extends java.lang.Exception> R call(PluginContext.CheckedExtensionImplFunction<T,R,X> checkedExtensionImplFunction, java.lang.Class<X> exceptionClass) throws X extends java.lang.Exception
The function gets the extension implementation provided that should be invoked.
Fails with IllegalStateException
if no implementation is registered for the item.
checkedExtensionImplFunction
- function that invokes the extensionexceptionClass
- type of the exceptions that should be thrownX
- expected exception from the plugin extensionjava.lang.IllegalStateException
- if no implementation is registered for the itemX extends java.lang.Exception