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