1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cling.extensions;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23
24 import java.nio.file.Path;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.NoSuchElementException;
29 import java.util.Set;
30 import java.util.function.Function;
31 import java.util.stream.Collectors;
32
33 import org.apache.maven.RepositoryUtils;
34 import org.apache.maven.api.Service;
35 import org.apache.maven.api.Session;
36 import org.apache.maven.api.cli.extensions.CoreExtension;
37 import org.apache.maven.api.model.Plugin;
38 import org.apache.maven.api.services.ArtifactCoordinatesFactory;
39 import org.apache.maven.api.services.ArtifactManager;
40 import org.apache.maven.api.services.ArtifactResolver;
41 import org.apache.maven.api.services.Interpolator;
42 import org.apache.maven.api.services.InterpolatorException;
43 import org.apache.maven.api.services.RepositoryFactory;
44 import org.apache.maven.api.services.VersionParser;
45 import org.apache.maven.api.services.VersionRangeResolver;
46 import org.apache.maven.execution.DefaultMavenExecutionResult;
47 import org.apache.maven.execution.MavenExecutionRequest;
48 import org.apache.maven.execution.MavenSession;
49 import org.apache.maven.extension.internal.CoreExports;
50 import org.apache.maven.extension.internal.CoreExtensionEntry;
51 import org.apache.maven.internal.impl.DefaultArtifactCoordinatesFactory;
52 import org.apache.maven.internal.impl.DefaultArtifactManager;
53 import org.apache.maven.internal.impl.DefaultArtifactResolver;
54 import org.apache.maven.internal.impl.DefaultModelVersionParser;
55 import org.apache.maven.internal.impl.DefaultRepositoryFactory;
56 import org.apache.maven.internal.impl.DefaultSession;
57 import org.apache.maven.internal.impl.DefaultVersionParser;
58 import org.apache.maven.internal.impl.DefaultVersionRangeResolver;
59 import org.apache.maven.internal.impl.InternalSession;
60 import org.apache.maven.internal.impl.model.DefaultInterpolator;
61 import org.apache.maven.plugin.PluginResolutionException;
62 import org.apache.maven.plugin.internal.DefaultPluginDependenciesResolver;
63 import org.apache.maven.resolver.MavenChainedWorkspaceReader;
64 import org.apache.maven.resolver.RepositorySystemSessionFactory;
65 import org.codehaus.plexus.DefaultPlexusContainer;
66 import org.codehaus.plexus.PlexusContainer;
67 import org.codehaus.plexus.classworlds.ClassWorld;
68 import org.codehaus.plexus.classworlds.realm.ClassRealm;
69 import org.eclipse.aether.RepositorySystem;
70 import org.eclipse.aether.RepositorySystemSession;
71 import org.eclipse.aether.RepositorySystemSession.CloseableSession;
72 import org.eclipse.aether.artifact.Artifact;
73 import org.eclipse.aether.graph.DependencyFilter;
74 import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
75 import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
76 import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
77 import org.eclipse.aether.repository.RemoteRepository;
78 import org.eclipse.aether.repository.WorkspaceReader;
79 import org.eclipse.aether.resolution.ArtifactResult;
80 import org.eclipse.aether.resolution.DependencyResult;
81 import org.eclipse.aether.util.filter.ExclusionsDependencyFilter;
82 import org.eclipse.aether.util.version.GenericVersionScheme;
83 import org.eclipse.sisu.Nullable;
84 import org.slf4j.Logger;
85 import org.slf4j.LoggerFactory;
86
87
88
89
90 @Named
91 public class BootstrapCoreExtensionManager {
92 public static final String STRATEGY_PARENT_FIRST = "parent-first";
93 public static final String STRATEGY_PLUGIN = "plugin";
94 public static final String STRATEGY_SELF_FIRST = "self-first";
95
96 private final Logger log = LoggerFactory.getLogger(getClass());
97
98 private final DefaultPluginDependenciesResolver pluginDependenciesResolver;
99
100 private final RepositorySystemSessionFactory repositorySystemSessionFactory;
101
102 private final CoreExports coreExports;
103
104 private final ClassWorld classWorld;
105
106 private final ClassRealm parentRealm;
107
108 private final WorkspaceReader ideWorkspaceReader;
109
110 private final RepositorySystem repoSystem;
111
112 @Inject
113 public BootstrapCoreExtensionManager(
114 DefaultPluginDependenciesResolver pluginDependenciesResolver,
115 RepositorySystemSessionFactory repositorySystemSessionFactory,
116 CoreExports coreExports,
117 PlexusContainer container,
118 @Nullable @Named("ide") WorkspaceReader ideWorkspaceReader,
119 RepositorySystem repoSystem) {
120 this.pluginDependenciesResolver = pluginDependenciesResolver;
121 this.repositorySystemSessionFactory = repositorySystemSessionFactory;
122 this.coreExports = coreExports;
123 this.classWorld = ((DefaultPlexusContainer) container).getClassWorld();
124 this.parentRealm = container.getContainerRealm();
125 this.ideWorkspaceReader = ideWorkspaceReader;
126 this.repoSystem = repoSystem;
127 }
128
129 public List<CoreExtensionEntry> loadCoreExtensions(
130 MavenExecutionRequest request, Set<String> providedArtifacts, List<CoreExtension> extensions)
131 throws Exception {
132 try (CloseableSession repoSession = repositorySystemSessionFactory
133 .newRepositorySessionBuilder(request)
134 .setWorkspaceReader(new MavenChainedWorkspaceReader(request.getWorkspaceReader(), ideWorkspaceReader))
135 .build()) {
136 MavenSession mSession = new MavenSession(repoSession, request, new DefaultMavenExecutionResult());
137 InternalSession iSession = new SimpleSession(mSession, repoSystem, null);
138 InternalSession.associate(repoSession, iSession);
139
140 List<RemoteRepository> repositories = RepositoryUtils.toRepos(request.getPluginArtifactRepositories());
141 Function<String, String> interpolator = createInterpolator(request);
142
143 return resolveCoreExtensions(repoSession, repositories, providedArtifacts, extensions, interpolator);
144 }
145 }
146
147 private List<CoreExtensionEntry> resolveCoreExtensions(
148 RepositorySystemSession repoSession,
149 List<RemoteRepository> repositories,
150 Set<String> providedArtifacts,
151 List<CoreExtension> configuration,
152 Function<String, String> interpolator)
153 throws Exception {
154 List<CoreExtensionEntry> extensions = new ArrayList<>();
155
156 DependencyFilter dependencyFilter = new ExclusionsDependencyFilter(providedArtifacts);
157
158 for (CoreExtension extension : configuration) {
159 List<Artifact> artifacts =
160 resolveExtension(extension, repoSession, repositories, dependencyFilter, interpolator);
161 if (!artifacts.isEmpty()) {
162 extensions.add(createExtension(extension, artifacts));
163 }
164 }
165
166 return Collections.unmodifiableList(extensions);
167 }
168
169 private CoreExtensionEntry createExtension(CoreExtension extension, List<Artifact> artifacts) throws Exception {
170 String realmId = "coreExtension>" + extension.getGroupId() + ":" + extension.getArtifactId() + ":"
171 + extension.getVersion();
172 final ClassRealm realm = classWorld.newRealm(realmId, null);
173 Set<String> providedArtifacts = Collections.emptySet();
174 String classLoadingStrategy = extension.getClassLoadingStrategy();
175 if (STRATEGY_PARENT_FIRST.equals(classLoadingStrategy)) {
176 realm.importFrom(parentRealm, "");
177 } else if (STRATEGY_PLUGIN.equals(classLoadingStrategy)) {
178 coreExports.getExportedPackages().forEach((p, cl) -> realm.importFrom(cl, p));
179 providedArtifacts = coreExports.getExportedArtifacts();
180 } else if (STRATEGY_SELF_FIRST.equals(classLoadingStrategy)) {
181 realm.setParentRealm(parentRealm);
182 } else {
183 throw new IllegalArgumentException("Unsupported class-loading strategy '"
184 + classLoadingStrategy + "'. Supported values are: " + STRATEGY_PARENT_FIRST
185 + ", " + STRATEGY_PLUGIN + " and " + STRATEGY_SELF_FIRST);
186 }
187 log.debug("Populating class realm {}", realm.getId());
188 for (Artifact artifact : artifacts) {
189 String id = artifact.getGroupId() + ":" + artifact.getArtifactId();
190 if (providedArtifacts.contains(id)) {
191 log.debug(" Excluded {}", id);
192 } else {
193 Path file = artifact.getPath();
194 log.debug(" Included {} located at {}", id, file);
195 realm.addURL(file.toUri().toURL());
196 }
197 }
198 return CoreExtensionEntry.discoverFrom(
199 realm,
200 Collections.singleton(artifacts.get(0).getPath().toFile()),
201 extension.getGroupId() + ":" + extension.getArtifactId(),
202 extension.getConfiguration());
203 }
204
205 private List<Artifact> resolveExtension(
206 CoreExtension extension,
207 RepositorySystemSession repoSession,
208 List<RemoteRepository> repositories,
209 DependencyFilter dependencyFilter,
210 Function<String, String> interpolator)
211 throws ExtensionResolutionException {
212 try {
213
214
215
216
217 Plugin plugin = Plugin.newBuilder()
218 .groupId(interpolator.apply(extension.getGroupId()))
219 .artifactId(interpolator.apply(extension.getArtifactId()))
220 .version(interpolator.apply(extension.getVersion()))
221 .build();
222
223 DependencyResult result = pluginDependenciesResolver.resolveCoreExtension(
224 new org.apache.maven.model.Plugin(plugin), dependencyFilter, repositories, repoSession);
225 return result.getArtifactResults().stream()
226 .filter(ArtifactResult::isResolved)
227 .map(ArtifactResult::getArtifact)
228 .collect(Collectors.toList());
229 } catch (PluginResolutionException | InterpolatorException e) {
230 throw new ExtensionResolutionException(extension, e);
231 }
232 }
233
234 private static Function<String, String> createInterpolator(MavenExecutionRequest request) {
235 Interpolator interpolator = new DefaultInterpolator();
236 Function<String, String> callback = v -> {
237 String r = request.getUserProperties().getProperty(v);
238 if (r == null) {
239 r = request.getSystemProperties().getProperty(v);
240 }
241 return r != null ? r : v;
242 };
243 return v -> interpolator.interpolate(v, callback);
244 }
245
246 static class SimpleSession extends DefaultSession {
247 SimpleSession(
248 MavenSession session,
249 RepositorySystem repositorySystem,
250 List<org.apache.maven.api.RemoteRepository> repositories) {
251 super(session, repositorySystem, repositories, null, null, null);
252 }
253
254 @Override
255 protected Session newSession(
256 MavenSession mavenSession, List<org.apache.maven.api.RemoteRepository> repositories) {
257 return new SimpleSession(mavenSession, getRepositorySystem(), repositories);
258 }
259
260 @Override
261 public <T extends Service> T getService(Class<T> clazz) throws NoSuchElementException {
262 if (clazz == ArtifactCoordinatesFactory.class) {
263 return (T) new DefaultArtifactCoordinatesFactory();
264 } else if (clazz == VersionParser.class) {
265 return (T) new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme()));
266 } else if (clazz == VersionRangeResolver.class) {
267 return (T) new DefaultVersionRangeResolver(repositorySystem);
268 } else if (clazz == ArtifactResolver.class) {
269 return (T) new DefaultArtifactResolver();
270 } else if (clazz == ArtifactManager.class) {
271 return (T) new DefaultArtifactManager(this);
272 } else if (clazz == RepositoryFactory.class) {
273 return (T) new DefaultRepositoryFactory(new DefaultRemoteRepositoryManager(
274 new DefaultUpdatePolicyAnalyzer(), new DefaultChecksumPolicyProvider()));
275 } else if (clazz == Interpolator.class) {
276 return (T) new DefaultInterpolator();
277
278
279 }
280 throw new NoSuchElementException("No service for " + clazz.getName());
281 }
282 }
283 }