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 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
40
41
42
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
61
62 public ClassRealm getClassRealm()
63 {
64 return realm;
65 }
66
67
68
69
70 public Set<String> getExportedArtifacts()
71 {
72 return artifacts;
73 }
74
75
76
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
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
136 }
137
138 return new CoreExtensionEntry( loader, artifacts, packages );
139 }
140
141 }