1 package org.apache.maven.extension.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
40
41
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
60
61 public ClassRealm getClassRealm()
62 {
63 return realm;
64 }
65
66
67
68
69 public Set<String> getExportedArtifacts()
70 {
71 return artifacts;
72 }
73
74
75
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
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
131 }
132
133 return new CoreExtensionEntry( loader, artifacts, packages );
134 }
135
136 }