Class PluginItemContext<T>
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 Summary
ConstructorsConstructorDescriptionPluginItemContext
(DynamicItem<T> dynamicItem, PluginContext.PluginMetrics pluginMetrics) -
Method Summary
Modifier and TypeMethodDescription<R,
X extends Exception>
Rcall
(PluginContext.CheckedExtensionImplFunction<T, R, X> checkedExtensionImplFunction, 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.Returns the name of the plugin that registered the extension.boolean
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 Exception>
voidrun
(PluginContext.ExtensionImplConsumer<T> extensionImplConsumer, Class<X> exceptionClass) Invokes the plugin extension of the item.
-
Constructor Details
-
PluginItemContext
@Inject public PluginItemContext(DynamicItem<T> dynamicItem, PluginContext.PluginMetrics pluginMetrics)
-
-
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, otherwisefalse
-
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
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 extensionexceptionClass
- type of the exceptions that should be thrown- Throws:
X
- expected exception from the plugin extension
-
call
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, throws XX> checkedExtensionImplFunction, Class<X> exceptionClass) 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 extensionexceptionClass
- type of the exceptions that should be thrown- Returns:
- the result from the plugin extension
- Throws:
X
- expected exception from the plugin extensionIllegalStateException
- if no implementation is registered for the item
-