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  import org.apache.maven.project.ExtensionDescriptor;
32  import org.apache.maven.project.ExtensionDescriptorBuilder;
33  import org.codehaus.plexus.classworlds.realm.ClassRealm;
34  
35  /**
36   * Provides information about artifacts (identified by groupId:artifactId string key) and classpath elements exported by
37   * Maven core itself or a Maven core extension.
38   *
39   * @since 3.3.0
40   */
41  public class CoreExtensionEntry {
42      private final ClassRealm realm;
43  
44      private final Set<String> artifacts;
45  
46      private final Set<String> packages;
47  
48      public CoreExtensionEntry(ClassRealm realm, Collection<String> artifacts, Collection<String> packages) {
49          this.realm = realm;
50          this.artifacts = Collections.unmodifiableSet(new HashSet<>(artifacts));
51          this.packages = Collections.unmodifiableSet(new HashSet<>(packages));
52      }
53  
54      /**
55       * Returns ClassLoader used to load extension classes.
56       */
57      public ClassRealm getClassRealm() {
58          return realm;
59      }
60  
61      /**
62       * Returns artifacts exported by the extension, identified by groupId:artifactId string key.
63       */
64      public Set<String> getExportedArtifacts() {
65          return artifacts;
66      }
67  
68      /**
69       * Returns classpath elements exported by the extension.
70       */
71      public Set<String> getExportedPackages() {
72          return packages;
73      }
74  
75      private static final ExtensionDescriptorBuilder BUILDER = new ExtensionDescriptorBuilder();
76  
77      public static CoreExtensionEntry discoverFrom(ClassRealm loader) {
78          Set<String> artifacts = new LinkedHashSet<>();
79          Set<String> packages = new LinkedHashSet<>();
80  
81          try {
82              Enumeration<URL> urls = loader.getResources(BUILDER.getExtensionDescriptorLocation());
83              while (urls.hasMoreElements()) {
84  
85                  try (InputStream is = urls.nextElement().openStream()) {
86                      ExtensionDescriptor descriptor = BUILDER.build(is);
87                      artifacts.addAll(descriptor.getExportedArtifacts());
88                      packages.addAll(descriptor.getExportedPackages());
89                  }
90              }
91          } catch (IOException ignored) {
92              // exports descriptors are entirely optional
93          }
94  
95          return new CoreExtensionEntry(loader, artifacts, packages);
96      }
97  
98      public static CoreExtensionEntry discoverFrom(ClassRealm loader, Collection<File> classpath) {
99          Set<String> artifacts = new LinkedHashSet<>();
100         Set<String> packages = new LinkedHashSet<>();
101 
102         try {
103             for (File entry : classpath) {
104                 ExtensionDescriptor descriptor = BUILDER.build(entry);
105                 if (descriptor != null) {
106                     artifacts.addAll(descriptor.getExportedArtifacts());
107                     packages.addAll(descriptor.getExportedPackages());
108                 }
109             }
110         } catch (IOException ignored) {
111             // exports descriptors are entirely optional
112         }
113 
114         return new CoreExtensionEntry(loader, artifacts, packages);
115     }
116 }