View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2015 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.devops.conga.tooling.maven.plugin.util;
21  
22  import java.io.File;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.net.URLClassLoader;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.maven.artifact.DependencyResolutionRequiredException;
31  import org.apache.maven.model.Resource;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.project.MavenProject;
34  
35  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
36  import io.wcm.devops.conga.generator.spi.context.PluginContextOptions;
37  import io.wcm.devops.conga.generator.spi.context.ValueProviderGlobalContext;
38  import io.wcm.devops.conga.generator.util.VariableMapResolver;
39  import io.wcm.devops.conga.generator.util.VariableStringResolver;
40  
41  /**
42   * Utility methods for managing classpath and class loaders.
43   */
44  public final class ClassLoaderUtil {
45  
46    private ClassLoaderUtil() {
47      // static methods only
48    }
49  
50    /**
51     * Build {@link ClassLoader} based on given list of dependency URLs.
52     * @param classpathUrls Classpath urls
53     * @return Resource loader
54     */
55    @SuppressFBWarnings("DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED")
56    public static ClassLoader buildClassLoader(List<URL> classpathUrls) {
57      return new URLClassLoader(classpathUrls.toArray(new URL[0]));
58    }
59  
60    /**
61     * Build class loader from dependency of a maven project.
62     * @param project Maven project
63     * @return Class loader
64     * @throws MojoExecutionException Mojo execution exception
65     */
66    public static List<URL> getMavenProjectClasspathUrls(MavenProject project) throws MojoExecutionException {
67      try {
68        List<URL> classpathUrls = new ArrayList<>();
69        for (String path : project.getCompileClasspathElements()) {
70          classpathUrls.add(new File(path).toURI().toURL());
71        }
72        for (Resource resource : project.getResources()) {
73          classpathUrls.add(new File(resource.getDirectory()).toURI().toURL());
74        }
75        return classpathUrls;
76      }
77      catch (MalformedURLException | DependencyResolutionRequiredException ex) {
78        throw new MojoExecutionException("Unable to get classpath elements for class loader.", ex);
79      }
80    }
81  
82    /**
83     * Resolves an environment dependency URL.
84     * @param dependencyUrl Dependeny URL
85     * @param pluginContextOptions Plugin context options
86     * @return Resolved dependency URL.
87     */
88    public static String resolveDependencyUrl(String dependencyUrl, PluginContextOptions pluginContextOptions) {
89  
90      ValueProviderGlobalContext valueProviderGlobalContext = new ValueProviderGlobalContext()
91          .pluginContextOptions(pluginContextOptions);
92      VariableMapResolver variableMapResolver = new VariableMapResolver(valueProviderGlobalContext);
93      VariableStringResolver variableStringResolver = new VariableStringResolver(valueProviderGlobalContext, variableMapResolver);
94  
95      // resolver variables without config map - thus supporting only value providers with external values
96      return variableStringResolver.resolveString(dependencyUrl, Map.of());
97    }
98  
99  }