Drop odl-guava-18
[odlparent.git] / karaf4-plugin / src / main / java / org / opendaylight / odlparent / AetherUtil.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.odlparent;
10
11 import java.io.File;
12 import java.util.ArrayList;
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import java.util.Set;
16 import org.eclipse.aether.DefaultRepositorySystemSession;
17 import org.eclipse.aether.RepositorySystem;
18 import org.eclipse.aether.RepositorySystemSession;
19 import org.eclipse.aether.artifact.Artifact;
20 import org.eclipse.aether.artifact.DefaultArtifact;
21 import org.eclipse.aether.collection.CollectRequest;
22 import org.eclipse.aether.graph.Dependency;
23 import org.eclipse.aether.graph.DependencyFilter;
24 import org.eclipse.aether.installation.InstallationException;
25 import org.eclipse.aether.installation.InstallRequest;
26 import org.eclipse.aether.repository.LocalRepository;
27 import org.eclipse.aether.repository.LocalRepositoryManager;
28 import org.eclipse.aether.repository.RemoteRepository;
29 import org.eclipse.aether.resolution.ArtifactRequest;
30 import org.eclipse.aether.resolution.ArtifactResolutionException;
31 import org.eclipse.aether.resolution.ArtifactResult;
32 import org.eclipse.aether.resolution.DependencyRequest;
33 import org.eclipse.aether.resolution.DependencyResolutionException;
34 import org.eclipse.aether.resolution.DependencyResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class AetherUtil {
39     private static final Logger LOG = LoggerFactory.getLogger(AetherUtil.class);
40     private RepositorySystem repoSystem;
41
42     private RepositorySystemSession repoSession;
43
44     private List<RemoteRepository> remoteRepos;
45
46     protected File localRepository;
47
48     /**
49      * Create an instance for the given repositories.
50      *
51      * @param repoSystem The repository system.
52      * @param repoSession The repository session.
53      * @param remoteRepos The remote repositories.
54      * @param localRepository The local repository.
55      */
56     public AetherUtil(
57             RepositorySystem repoSystem, RepositorySystemSession repoSession, List<RemoteRepository> remoteRepos,
58             File localRepository) {
59         this.repoSystem = repoSystem;
60         this.repoSession = repoSession;
61         this.remoteRepos = remoteRepos;
62         this.localRepository = localRepository;
63     }
64
65     /**
66      * Resolves the given dependencies.
67      *
68      * @param dependencies The dependencies.
69      * @param filter The dependency filter.
70      * @return The corresponding artifacts.
71      * @throws DependencyResolutionException if an error occurs.
72      */
73     public Set<Artifact> resolveDependencies(List<Dependency> dependencies, DependencyFilter filter)
74             throws DependencyResolutionException {
75         Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
76         CollectRequest collectRequest = new CollectRequest();
77         collectRequest.setDependencies(dependencies);
78         collectRequest.setRepositories(remoteRepos);
79         DependencyRequest request = new DependencyRequest(collectRequest, filter);
80         DependencyResult results = repoSystem.resolveDependencies(repoSession, request);
81         for (ArtifactResult artifactResult : results.getArtifactResults()) {
82             artifacts.add(artifactResult.getArtifact());
83         }
84         LOG.trace("resolveDependencies({}) returns {}", dependencies, artifacts);
85         return artifacts;
86     }
87
88     /**
89      * Resolves the given artifact.
90      *
91      * @param artifact The artifact.
92      * @return The resolved artifact, or {@code null} if it can't be resolved.
93      */
94     public Artifact resolveArtifact(Artifact artifact) {
95         ArtifactRequest request = new ArtifactRequest(artifact, remoteRepos, null);
96         ArtifactResult result;
97         try {
98             result = repoSystem.resolveArtifact(repoSession, request);
99         } catch (ArtifactResolutionException e) {
100             LOG.warn("Unable to resolve artifact: {}", e.getMessage(), e);
101             return null;
102         }
103         LOG.trace("resolveArtifacts({}) returns {}", artifact, result.getArtifact());
104         return result.getArtifact();
105     }
106
107     /**
108      * Resolves the given coordinates.
109      *
110      * @param coord The coordinates to resolve.
111      * @return The resolved artifact, or {@code null} if the coordinates can't be resolved.
112      */
113     public Artifact resolveArtifact(String coord) {
114         DefaultArtifact artifact = new DefaultArtifact(coord);
115         return resolveArtifact(artifact);
116     }
117
118     /**
119      * Resolves the given coordinates.
120      *
121      * @param coords The set of coordinates to resolve.
122      * @return The resolved artifacts. Unresolvable coordinates are skipped without error.
123      */
124     public Set<Artifact> resolveArtifacts(Set<String> coords) {
125         Set<Artifact> result = new LinkedHashSet<Artifact>();
126         for (String coord : coords) {
127             Artifact artifact = resolveArtifact(coord);
128             if (artifact != null) {
129                 result.add(artifact);
130             }
131         }
132         LOG.trace("resolveArtifacts({}) returns {}", coords, result);
133         return result;
134     }
135
136     /**
137      * Converts the given artifact coordinates to a {@link Dependency} instance.
138      *
139      * @param coord The coordinates.
140      * @return The dependency.
141      */
142     public Dependency toDependency(String coord) {
143         Artifact artifact = new DefaultArtifact(coord);
144         return new Dependency(artifact, null);
145     }
146
147     /**
148      * Converts the given list of artifact coordinates to dependencies.
149      *
150      * @param coords The list of coordinates.
151      * @return The corresponding dependencies.
152      */
153     public List<Dependency> toDependencies(List<String> coords) {
154         List<Dependency> result = new ArrayList<Dependency>();
155         for (String coord : coords) {
156             result.add(toDependency(coord));
157         }
158         LOG.trace("toDependencies({}) returns {}", coords, result);
159         return result;
160     }
161
162     /**
163      * Installs the given artifacts.
164      *
165      * @param artifacts The artifacts to install.
166      * @throws InstallationException if an error occurs.
167      */
168     public void installArtifacts(Set<Artifact> artifacts) throws InstallationException {
169         LocalRepository localRepo = new LocalRepository(localRepository);
170         LocalRepositoryManager localManager = repoSystem.newLocalRepositoryManager(repoSession, localRepo);
171         DefaultRepositorySystemSession localSession = new DefaultRepositorySystemSession();
172         localSession.setLocalRepositoryManager(localManager);
173         InstallRequest installRequest = new InstallRequest();
174         for (Artifact featureArtifact : artifacts) {
175             installRequest.addArtifact(featureArtifact);
176         }
177         repoSystem.install(localSession, installRequest);
178     }
179 }