View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.extension.internal;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.URL;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Enumeration;
28  import java.util.HashSet;
29  import java.util.LinkedHashSet;
30  import java.util.Set;
31  
32  import org.apache.maven.project.ExtensionDescriptor;
33  import org.apache.maven.project.ExtensionDescriptorBuilder;
34  import org.codehaus.plexus.classworlds.realm.ClassRealm;
35  
36  /**
37   * Provides information about artifacts (identified by groupId:artifactId string key) and classpath elements exported by
38   * Maven core itself or a Maven core extension.
39   *
40   * @since 3.3.0
41   */
42  public class CoreExtensionEntry {
43      private final ClassRealm realm;
44  
45      private final Set<String> artifacts;
46  
47      private final Set<String> packages;
48  
49      public CoreExtensionEntry(ClassRealm realm, Collection<String> artifacts, Collection<String> packages) {
50          this.realm = realm;
51          this.artifacts = Collections.unmodifiableSet(new HashSet<>(artifacts));
52          this.packages = Collections.unmodifiableSet(new HashSet<>(packages));
53      }
54  
55      /**
56       * Returns ClassLoader used to load extension classes.
57       */
58      public ClassRealm getClassRealm() {
59          return realm;
60      }
61  
62      /**
63       * Returns artifacts exported by the extension, identified by groupId:artifactId string key.
64       */
65      public Set<String> getExportedArtifacts() {
66          return artifacts;
67      }
68  
69      /**
70       * Returns classpath elements exported by the extension.
71       */
72      public Set<String> getExportedPackages() {
73          return packages;
74      }
75  
76      private static final ExtensionDescriptorBuilder BUILDER = new ExtensionDescriptorBuilder();
77  
78      public static CoreExtensionEntry discoverFrom(ClassRealm loader) {
79          Set<String> artifacts = new LinkedHashSet<>();
80          Set<String> packages = new LinkedHashSet<>();
81  
82          try {
83              Enumeration<URL> urls = loader.getResources(BUILDER.getExtensionDescriptorLocation());
84              while (urls.hasMoreElements()) {
85  
86                  try (InputStream is = urls.nextElement().openStream()) {
87                      ExtensionDescriptor descriptor = BUILDER.build(is);
88                      artifacts.addAll(descriptor.getExportedArtifacts());
89                      packages.addAll(descriptor.getExportedPackages());
90                  }
91              }
92          } catch (IOException ignored) {
93              // exports descriptors are entirely optional
94          }
95  
96          return new CoreExtensionEntry(loader, artifacts, packages);
97      }
98  
99      public static CoreExtensionEntry discoverFrom(ClassRealm loader, Collection<File> classpath) {
100         Set<String> artifacts = new LinkedHashSet<>();
101         Set<String> packages = new LinkedHashSet<>();
102 
103         try {
104             for (File entry : classpath) {
105                 ExtensionDescriptor descriptor = BUILDER.build(entry);
106                 if (descriptor != null) {
107                     artifacts.addAll(descriptor.getExportedArtifacts());
108                     packages.addAll(descriptor.getExportedPackages());
109                 }
110             }
111         } catch (IOException ignored) {
112             // exports descriptors are entirely optional
113         }
114 
115         return new CoreExtensionEntry(loader, artifacts, packages);
116     }
117 }