1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.extension.internal;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URL;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Enumeration;
28 import java.util.HashSet;
29 import java.util.LinkedHashSet;
30 import java.util.Set;
31
32 import org.apache.maven.api.xml.XmlNode;
33 import org.apache.maven.project.ExtensionDescriptor;
34 import org.apache.maven.project.ExtensionDescriptorBuilder;
35 import org.codehaus.plexus.classworlds.realm.ClassRealm;
36
37
38
39
40
41
42
43 public class CoreExtensionEntry {
44 private final ClassRealm realm;
45
46 private final Set<String> artifacts;
47
48 private final Set<String> packages;
49
50 private final String key;
51
52 private final XmlNode configuration;
53
54 public CoreExtensionEntry(
55 ClassRealm realm,
56 Collection<String> artifacts,
57 Collection<String> packages,
58 String key,
59 XmlNode configuration) {
60 this.realm = realm;
61 this.artifacts = Collections.unmodifiableSet(new HashSet<>(artifacts));
62 this.packages = Collections.unmodifiableSet(new HashSet<>(packages));
63 this.key = key;
64 this.configuration = configuration;
65 }
66
67
68
69
70 public ClassRealm getClassRealm() {
71 return realm;
72 }
73
74
75
76
77 public Set<String> getExportedArtifacts() {
78 return artifacts;
79 }
80
81
82
83
84 public Set<String> getExportedPackages() {
85 return packages;
86 }
87
88
89
90
91
92 public String getKey() {
93 return key;
94 }
95
96
97
98
99 public XmlNode getConfiguration() {
100 return configuration;
101 }
102
103 private static final ExtensionDescriptorBuilder BUILDER = new ExtensionDescriptorBuilder();
104
105 public static CoreExtensionEntry discoverFrom(ClassRealm loader) {
106 Set<String> artifacts = new LinkedHashSet<>();
107 Set<String> packages = new LinkedHashSet<>();
108
109 try {
110 Enumeration<URL> urls = loader.getResources(BUILDER.getExtensionDescriptorLocation());
111 while (urls.hasMoreElements()) {
112
113 try (InputStream is = urls.nextElement().openStream()) {
114 ExtensionDescriptor descriptor = BUILDER.build(is);
115 artifacts.addAll(descriptor.getExportedArtifacts());
116 packages.addAll(descriptor.getExportedPackages());
117 }
118 }
119 } catch (IOException ignored) {
120
121 }
122
123 return new CoreExtensionEntry(loader, artifacts, packages, null, null);
124 }
125
126 public static CoreExtensionEntry discoverFrom(
127 ClassRealm loader, Collection<File> classpath, String key, XmlNode configuration) {
128 Set<String> artifacts = new LinkedHashSet<>();
129 Set<String> packages = new LinkedHashSet<>();
130
131 try {
132 for (File entry : classpath) {
133 ExtensionDescriptor descriptor = BUILDER.build(entry);
134 if (descriptor != null) {
135 artifacts.addAll(descriptor.getExportedArtifacts());
136 packages.addAll(descriptor.getExportedPackages());
137 }
138 }
139 } catch (IOException ignored) {
140
141 }
142
143 return new CoreExtensionEntry(loader, artifacts, packages, key, configuration);
144 }
145 }