All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.server.plugincontext.PluginSetEntryContext Maven / Gradle / Ivy

There is a newer version: 3.11.0-rc3
Show newest version
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.plugincontext;

import static java.util.Objects.requireNonNull;

import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.registration.Extension;
import com.google.gerrit.server.plugincontext.PluginContext.CheckedExtensionImplFunction;
import com.google.gerrit.server.plugincontext.PluginContext.ExtensionImplConsumer;
import com.google.gerrit.server.plugincontext.PluginContext.ExtensionImplFunction;
import com.google.gerrit.server.plugincontext.PluginContext.PluginMetrics;

/**
 * Context to invoke an extension from {@link DynamicSet}.
 *
 * 

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. * *

Example if all exceptions should be caught and logged: * *

{@code
 * fooPluginSetEntryContext.run(foo -> foo.doFoo());
 * }
* *

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

{@code
 * try {
 *   fooPluginSetEntryContext.run(foo -> foo.doFoo(), MyException.class);
 * } catch (MyException e) {
 *   // handle the exception
 * }
 * }
* *

Example if return values should be handled: * *

{@code
 * Object result = fooPluginSetEntryContext.call(foo -> foo.getFoo());
 * }
* *

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

{@code
 * Object result;
 * try {
 *   result = fooPluginSetEntryContext.call(foo -> foo.getFoo(), MyException.class);
 * } catch (MyException e) {
 *   // handle the exception
 * }
 * }
* *

Example if several exceptions should be handled: * *

{@code
 * for (Extension fooExtension : fooDynamicSet.entries()) {
 *   try (TraceContext traceContext = PluginContext.newTrace(fooExtension)) {
 *     fooExtension.get().doFoo();
 *   } catch (MyException1 | MyException2 | MyException3 e) {
 *     // handle the exception
 *   }
 * }
 * }
*/ public class PluginSetEntryContext { private final Extension extension; private final PluginMetrics pluginMetrics; PluginSetEntryContext(Extension extension, PluginMetrics pluginMetrics) { this.extension = requireNonNull(extension); this.pluginMetrics = pluginMetrics; } /** * Returns the name of the plugin that registered this extension. * * @return the plugin name */ public String getPluginName() { return extension.getPluginName(); } /** * Returns the implementation of this extension. * *

Should only be used in exceptional cases to get direct access to the extension * implementation. If possible the extension should be invoked through {@link * #run(PluginContext.ExtensionImplConsumer)}, {@link #run(PluginContext.ExtensionImplConsumer, * java.lang.Class)}, {@link #call(PluginContext.ExtensionImplFunction)} and {@link * #call(PluginContext.CheckedExtensionImplFunction, java.lang.Class)}. * * @return the implementation of this extension */ public T get() { return extension.get(); } /** * Invokes the plugin extension. All exceptions from the plugin extension are caught and logged. * *

The consumer gets the extension implementation provided that should be invoked. * * @param extensionImplConsumer consumer that invokes the extension */ public void run(ExtensionImplConsumer extensionImplConsumer) { PluginContext.runLogExceptions(pluginMetrics, extension, extensionImplConsumer); } /** * Invokes the plugin extension. All exceptions from the plugin extension are caught and logged. * *

The consumer gets the extension implementation provided that should be invoked. * * @param extensionImplConsumer consumer that invokes the extension * @param exceptionClass type of the exceptions that should be thrown * @throws X expected exception from the plugin extension */ public void run( ExtensionImplConsumer extensionImplConsumer, Class exceptionClass) throws X { PluginContext.runLogExceptions(pluginMetrics, extension, extensionImplConsumer, exceptionClass); } /** * Calls the plugin extension and returns the result from the plugin extension call. * *

The function gets the extension point provided that should be invoked. * * @param extensionImplFunction function that invokes the extension * @return the result from the plugin extension */ public R call(ExtensionImplFunction extensionImplFunction) { return PluginContext.call(pluginMetrics, extension, extensionImplFunction); } /** * 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 function gets the extension implementation provided that should be invoked. * * @param checkedExtensionImplFunction function that invokes the extension * @param exceptionClass type of the exceptions that should be thrown * @return the result from the plugin extension * @throws X expected exception from the plugin extension */ public R call( CheckedExtensionImplFunction checkedExtensionImplFunction, Class exceptionClass) throws X { return PluginContext.call( pluginMetrics, extension, checkedExtensionImplFunction, exceptionClass); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy