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

io.opentelemetry.maven.handler.MojoGoalExecutionHandlerConfiguration Maven / Gradle / Ivy

There is a newer version: 1.41.0-alpha
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.maven.handler;

import io.opentelemetry.maven.MavenGoal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.stream.Stream;

public class MojoGoalExecutionHandlerConfiguration {

  public static Map loadMojoGoalExecutionHandler(
      ClassLoader classLoader) {

    // built-in handlers
    List builtInHandlers =
        Arrays.asList(
            new GoogleJibBuildHandler(),
            new MavenDeployHandler(),
            new SnykMonitorHandler(),
            new SnykTestHandler(),
            new SpringBootBuildImageHandler());

    List spiHandlers = new ArrayList<>();
    // Must use the classloader of the class rather the default ThreadContextClassloader to prevent
    // java.util.ServiceConfigurationError:
    //    io.opentelemetry.maven.handler.MojoGoalExecutionHandler:
    //    io.opentelemetry.maven.handler.SpringBootBuildImageHandler not a subtype
    ServiceLoader.load(MojoGoalExecutionHandler.class, classLoader).forEach(spiHandlers::add);

    Map mojoGoalExecutionHandlers = new HashMap<>();

    Stream.concat(builtInHandlers.stream(), spiHandlers.stream())
        .forEach(
            handler ->
                handler
                    .getSupportedGoals()
                    .forEach(
                        goal -> {
                          MojoGoalExecutionHandler previousHandler =
                              mojoGoalExecutionHandlers.put(goal, handler);
                          if (previousHandler != null) {
                            throw new IllegalStateException(
                                "More than one handler found for maven goal "
                                    + goal
                                    + ": "
                                    + previousHandler
                                    + ", "
                                    + handler);
                          }
                        }));
    return mojoGoalExecutionHandlers;
  }

  private MojoGoalExecutionHandlerConfiguration() {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy