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

com.google.gerrit.server.plugincontext.PluginSetContext 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 com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Iterators;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.registration.Extension;
import com.google.gerrit.server.plugincontext.PluginContext.ExtensionImplConsumer;
import com.google.gerrit.server.plugincontext.PluginContext.PluginMetrics;
import com.google.inject.Inject;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.stream.Stream;

/**
 * Context to invoke extensions from a {@link 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: * *

{@code
 * fooPluginSetContext.runEach(foo -> foo.doFoo());
 * }
* *

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

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

Example if return values should be handled: * *

{@code
 * for (PluginSetEntryContext c : fooPluginSetContext) {
 *   if (c.call(foo -> foo.handles(x))) {
 *     c.run(foo -> foo.doFoo());
 *   }
 * }
 * }
* *

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

{@code
 * try {
 *   for (PluginSetEntryContext c : 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: * *

{@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 PluginSetContext implements Iterable> { private final DynamicSet dynamicSet; private final PluginMetrics pluginMetrics; @VisibleForTesting @Inject public PluginSetContext(DynamicSet dynamicSet, PluginMetrics pluginMetrics) { this.dynamicSet = dynamicSet; this.pluginMetrics = pluginMetrics; } /** * Iterator that provides contexts for invoking the extensions in this set. * *

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 *
*/ @Override public Iterator> iterator() { return Iterators.transform( dynamicSet.entries().iterator(), e -> new PluginSetEntryContext<>(e, pluginMetrics)); } /** * Checks if no implementations for this extension point have been registered. * * @return {@code true} if no implementations for this extension point have been registered, * otherwise {@code false} */ public boolean isEmpty() { return !dynamicSet.iterator().hasNext(); } /** * Returns a sorted list of the plugins that have registered implementations for this extension * point. * * @return sorted list of the plugins that have registered implementations for this extension * point */ public SortedSet plugins() { return dynamicSet.plugins(); } /** * Invokes each extension in the set. All exceptions from the plugin extensions are caught and * logged. * *

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. * * @param extensionImplConsumer consumer that invokes the extension */ public void runEach(ExtensionImplConsumer extensionImplConsumer) { dynamicSet .entries() .forEach(p -> PluginContext.runLogExceptions(pluginMetrics, p, extensionImplConsumer)); } public Stream stream() { return dynamicSet.stream(); } /** * Invokes each extension in the set. All exceptions from the plugin extensions except exceptions * of the specified type are caught and logged. * *

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. * * @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 runEach( ExtensionImplConsumer extensionImplConsumer, Class exceptionClass) throws X { for (Extension extension : dynamicSet.entries()) { PluginContext.runLogExceptions( pluginMetrics, extension, extensionImplConsumer, exceptionClass); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy