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.api.xml.XmlNode;
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      private final String key;
51  
52      private final XmlNode configuration;
53  
54      public CoreExtensionEntry(
55              ClassRealm realm,
56              Collection<String> artifacts,
57              Collection<String> packages,
58              String key,
59              XmlNode configuration) {
60          this.realm = realm;
61          this.artifacts = Collections.unmodifiableSet(new HashSet<>(artifacts));
62          this.packages = Collections.unmodifiableSet(new HashSet<>(packages));
63          this.key = key;
64          this.configuration = configuration;
65      }
66  
67      /**
68       * Returns ClassLoader used to load extension classes.
69       */
70      public ClassRealm getClassRealm() {
71          return realm;
72      }
73  
74      /**
75       * Returns artifacts exported by the extension, identified by groupId:artifactId string key.
76       */
77      public Set<String> getExportedArtifacts() {
78          return artifacts;
79      }
80  
81      /**
82       * Returns classpath elements exported by the extension.
83       */
84      public Set<String> getExportedPackages() {
85          return packages;
86      }
87  
88      /**
89       * The key that can must used to identify the configuration using the
90       * {@link javax.inject.Named} annotation.
91       */
92      public String getKey() {
93          return key;
94      }
95  
96      /**
97       * Returns the configuration for this extension.
98       */
99      public XmlNode getConfiguration() {
100         return configuration;
101     }
102 
103     private static final ExtensionDescriptorBuilder BUILDER = new ExtensionDescriptorBuilder();
104 
105     public static CoreExtensionEntry discoverFrom(ClassRealm loader) {
106         Set<String> artifacts = new LinkedHashSet<>();
107         Set<String> packages = new LinkedHashSet<>();
108 
109         try {
110             Enumeration<URL> urls = loader.getResources(BUILDER.getExtensionDescriptorLocation());
111             while (urls.hasMoreElements()) {
112 
113                 try (InputStream is = urls.nextElement().openStream()) {
114                     ExtensionDescriptor descriptor = BUILDER.build(is);
115                     artifacts.addAll(descriptor.getExportedArtifacts());
116                     packages.addAll(descriptor.getExportedPackages());
117                 }
118             }
119         } catch (IOException ignored) {
120             // exports descriptors are entirely optional
121         }
122 
123         return new CoreExtensionEntry(loader, artifacts, packages, null, null);
124     }
125 
126     public static CoreExtensionEntry discoverFrom(
127             ClassRealm loader, Collection<File> classpath, String key, XmlNode configuration) {
128         Set<String> artifacts = new LinkedHashSet<>();
129         Set<String> packages = new LinkedHashSet<>();
130 
131         try {
132             for (File entry : classpath) {
133                 ExtensionDescriptor descriptor = BUILDER.build(entry);
134                 if (descriptor != null) {
135                     artifacts.addAll(descriptor.getExportedArtifacts());
136                     packages.addAll(descriptor.getExportedPackages());
137                 }
138             }
139         } catch (IOException ignored) {
140             // exports descriptors are entirely optional
141         }
142 
143         return new CoreExtensionEntry(loader, artifacts, packages, key, configuration);
144     }
145 }