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