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