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 org.apache.maven.project.ExtensionDescriptor;
23  import org.apache.maven.project.ExtensionDescriptorBuilder;
24  import org.codehaus.plexus.classworlds.realm.ClassRealm;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.net.URL;
30  import java.util.Collection;
31  import java.util.Collections;
32  import java.util.Enumeration;
33  import java.util.HashSet;
34  import java.util.LinkedHashSet;
35  import java.util.Set;
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  {
45      private final ClassRealm realm;
46  
47      private final Set<String> artifacts;
48  
49      private final Set<String> packages;
50  
51      public CoreExtensionEntry( ClassRealm realm, Collection<String> artifacts, Collection<String> packages )
52      {
53          this.realm = realm;
54          this.artifacts = Collections.unmodifiableSet( new HashSet<>( artifacts ) );
55          this.packages = Collections.unmodifiableSet( new HashSet<>( packages ) );
56      }
57  
58      /**
59       * Returns ClassLoader used to load extension classes.
60       */
61      public ClassRealm getClassRealm()
62      {
63          return realm;
64      }
65  
66      /**
67       * Returns artifacts exported by the extension, identified by groupId:artifactId string key.
68       */
69      public Set<String> getExportedArtifacts()
70      {
71          return artifacts;
72      }
73  
74      /**
75       * Returns classpath elements exported by the extension.
76       */
77      public Set<String> getExportedPackages()
78      {
79          return packages;
80      }
81  
82      private static final ExtensionDescriptorBuilder BUILDER = new ExtensionDescriptorBuilder();
83  
84      public static CoreExtensionEntry discoverFrom( ClassRealm loader )
85      {
86          Set<String> artifacts = new LinkedHashSet<>();
87          Set<String> packages = new LinkedHashSet<>();
88  
89          try
90          {
91              Enumeration<URL> urls = loader.getResources( BUILDER.getExtensionDescriptorLocation() );
92              while ( urls.hasMoreElements() )
93              {
94  
95                  try ( InputStream is = urls.nextElement().openStream() )
96                  {
97                      ExtensionDescriptor descriptor = BUILDER.build( is );
98                      artifacts.addAll( descriptor.getExportedArtifacts() );
99                      packages.addAll( descriptor.getExportedPackages() );
100                 }
101             }
102         }
103         catch ( IOException ignored )
104         {
105             // exports descriptors are entirely optional
106         }
107 
108         return new CoreExtensionEntry( loader, artifacts, packages );
109     }
110 
111     public static CoreExtensionEntry discoverFrom( ClassRealm loader, Collection<File> classpath )
112     {
113         Set<String> artifacts = new LinkedHashSet<>();
114         Set<String> packages = new LinkedHashSet<>();
115 
116         try
117         {
118             for ( File entry : classpath )
119             {
120                 ExtensionDescriptor descriptor = BUILDER.build( entry );
121                 if ( descriptor != null )
122                 {
123                     artifacts.addAll( descriptor.getExportedArtifacts() );
124                     packages.addAll( descriptor.getExportedPackages() );
125                 }
126             }
127         }
128         catch ( IOException ignored )
129         {
130             // exports descriptors are entirely optional
131         }
132 
133         return new CoreExtensionEntry( loader, artifacts, packages );
134     }
135 
136 }