X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.support%2Fsrc%2Forg%2Fargeo%2Fslc%2Faether%2FAetherTemplate.java;fp=org.argeo.slc.support%2Fsrc%2Forg%2Fargeo%2Fslc%2Faether%2FAetherTemplate.java;h=a83caa1e08d01674ca08363b6f02922a8b4b671d;hb=40fb326ca1e1e45f95e3a89c23fc3d07177da20d;hp=0000000000000000000000000000000000000000;hpb=b9505fef5ba8186433e903e9de3c73c17bdf6562;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.support/src/org/argeo/slc/aether/AetherTemplate.java b/org.argeo.slc.support/src/org/argeo/slc/aether/AetherTemplate.java new file mode 100644 index 000000000..a83caa1e0 --- /dev/null +++ b/org.argeo.slc.support/src/org/argeo/slc/aether/AetherTemplate.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.argeo.slc.aether; + +import java.io.File; +import java.util.List; + +import org.argeo.slc.SlcException; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.graph.Dependency; +import org.eclipse.aether.graph.DependencyNode; +import org.eclipse.aether.repository.RemoteRepository; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; + +/** Simplifies access to Aether. */ +public class AetherTemplate { + private RepositorySystem repositorySystem; + private RepositorySystemSession repositorySystemSession; + private List remoteRepositories; + + /** Resolves the artifact in order to give access to its file. */ + public File getResolvedFile(Artifact artifact) { + try { + ArtifactRequest artifactRequest = new ArtifactRequest(artifact, + remoteRepositories, null); + ArtifactResult result = repositorySystem.resolveArtifact( + repositorySystemSession, artifactRequest); + return result.getArtifact().getFile(); + } catch (ArtifactResolutionException e) { + throw new SlcException("Cannot resolve " + artifact, e); + } + } + + /** + * Transitively resolves the dependencies of this artifact (with scope + * 'compile') + * + * @param artifact + * the artifact to resolve + */ + public DependencyNode resolveDependencies(Artifact artifact) { + return resolveDependencies(artifact, "compile"); + } + + /** + * Transitively resolves the dependencies of this artifact. + * + * @param artifact + * the artifact to resolve + * @param scope + * the scope + */ + public DependencyNode resolveDependencies(Artifact artifact, String scope) { + try { + Dependency dependency = new Dependency(artifact, scope); + CollectRequest collectRequest = new CollectRequest(); + collectRequest.setRoot(dependency); + for (RemoteRepository remoteRepository : remoteRepositories) + collectRequest.addRepository(remoteRepository); + DependencyNode node = repositorySystem.collectDependencies( + repositorySystemSession, collectRequest).getRoot(); + + // FIXME adapt to changes in Aether + // repositorySystem.resolveDependencies(repositorySystemSession, + // node, + // null); + return node; + } catch (Exception e) { + throw new SlcException("Cannot resolve dependencies of " + artifact + + " (scope: " + scope + ")", e); + } + } + + public RepositorySystem getRepositorySystem() { + return repositorySystem; + } + + public void setRepositorySystem(RepositorySystem repositorySystem) { + this.repositorySystem = repositorySystem; + } + + public RepositorySystemSession getRepositorySystemSession() { + return repositorySystemSession; + } + + public void setRepositorySystemSession( + RepositorySystemSession repositorySystemSession) { + this.repositorySystemSession = repositorySystemSession; + } + + public List getRemoteRepositories() { + return remoteRepositories; + } + + public void setRemoteRepositories(List remoteRepositories) { + this.remoteRepositories = remoteRepositories; + } + +}