Skip to content

Commit

Permalink
add extraClasspath
Browse files Browse the repository at this point in the history
  • Loading branch information
jetoile committed Nov 27, 2019
1 parent 307e268 commit 70568de
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -87,6 +84,44 @@ public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable except
return locator.getService(RepositorySystem.class);
}

private List<ArtifactResult> loadExtraClasspath(ComponentArtifact currentArtifact, RepositorySystem system, DefaultRepositorySystemSession session, DependencyFilter classpathFilter) {
Map<String, String> properties = currentArtifact.getProperties();
if (properties == null) {
return Collections.EMPTY_LIST;
}
String extraClasspathProperty = properties.get(currentArtifact.getComponentName().toLowerCase() + ".extraClasspath");
if (extraClasspathProperty != null) {
log.info("Found extraClasspath for component " + currentArtifact.getComponentName() + " configuration. Is going to add them to classpath");
String[] artifacts = extraClasspathProperty.split(",");
return Arrays.asList(artifacts).stream()
.map(a -> {
DefaultArtifact artifact = new DefaultArtifact(a.trim());

CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(new Dependency(artifact, JavaScopes.RUNTIME));
collectRequest.setRepositories(remoteRepos);

log.info("Resolving artifact for extra Classpath" + artifact + " from " + remoteRepos.stream().map(r -> r.getId() + "-" + r.getUrl()).collect(Collectors.joining(", ")));

DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter);

List<ArtifactResult> artifactResults = null;
try {
artifactResults = system.resolveDependencies(session, dependencyRequest).getArtifactResults();
} catch (DependencyResolutionException e) {
log.error("an error occured during the dependencies phase: " + e.getMessage());
e.printStackTrace();
}

return artifactResults;
})
.flatMap(x -> x.stream())
.collect(Collectors.toList());
} else {
return Collections.EMPTY_LIST;
}
}

@Override
public void run() {
log.info("is going to start hadoop unit");
Expand Down Expand Up @@ -116,6 +151,8 @@ public void run() {
}

List<File> artifacts = new ArrayList<>();
artifactResults.addAll(loadExtraClasspath(c, system, session, classpathFilter));

artifactResults.stream().forEach(a ->
artifacts.add(a.getArtifact().getFile())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,39 @@ private static void addModuleJarToCurrentClassloader(Map<String, ComponentDepend
});
}

private static List<ArtifactResult> loadExtraClasspath(String currentArtifact, RepositorySystem system, DefaultRepositorySystemSession session, DependencyFilter classpathFilter) {
String extraClasspathProperty = hadoopUnitConfiguration.getString(currentArtifact.toLowerCase() + ".extraClasspath");
if (extraClasspathProperty != null) {
LOGGER.info("Found extraClasspath for component {} configuration. Is going to add them to classpath", currentArtifact);
String[] artifacts = extraClasspathProperty.split(",");
return Arrays.asList(artifacts).stream()
.map(a -> {
DefaultArtifact artifact = new DefaultArtifact(a.trim());

CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(new Dependency(artifact, JavaScopes.RUNTIME));
collectRequest.setRepositories(getRemoteRepositories());

LOGGER.info("Resolving artifact for extra Classpath {} from {}", artifact, getRemoteRepositories().stream().map(r -> r.getId() + "-" + r.getUrl()).collect(Collectors.joining(", ")));

DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter);

List<ArtifactResult> artifactResults = null;
try {
artifactResults = system.resolveDependencies(session, dependencyRequest).getArtifactResults();
} catch (DependencyResolutionException e) {
LOGGER.error("an error occured during the dependencies phase: " + e.getMessage());
}

return artifactResults;
})
.flatMap(x -> x.stream())
.collect(Collectors.toList());
} else {
return Collections.EMPTY_LIST;
}
}

private static Map<String, ComponentDependencies> loadMavenDependencies(List<String> componentsToStart) throws BootstrapException {
Map<String, ComponentDependencies> componentsToStartWithDependencies = new HashMap<>();

Expand Down Expand Up @@ -357,8 +390,9 @@ private static Map<String, ComponentDependencies> loadMavenDependencies(List<Str
throw new BootstrapException("failed to resolve dependency artifact " + artifact, e);
}


List<File> artifacts = new ArrayList<>();
artifactResults.addAll(loadExtraClasspath(c, repositorySystem, session, classpathFilter));

artifactResults.stream().forEach(a ->
artifacts.add(a.getArtifact().getFile())
);
Expand Down

0 comments on commit 70568de

Please sign in to comment.