]> source.dussan.org Git - archiva.git/blob
d632c4250654f68fb3c0151c2e2e96521323d0e9
[archiva.git] /
1 package org.apache.archiva.dependency.tree.maven2;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.beans.NetworkProxy;
25 import org.apache.archiva.admin.model.beans.ProxyConnector;
26 import org.apache.archiva.admin.model.beans.RemoteRepository;
27 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
28 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
29 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
30 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
31 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
32 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
33 import org.apache.archiva.maven2.model.TreeEntry;
34 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
35 import org.apache.archiva.proxy.common.WagonFactory;
36 import org.apache.maven.artifact.Artifact;
37 import org.apache.maven.artifact.factory.ArtifactFactory;
38 import org.apache.maven.model.building.DefaultModelBuilderFactory;
39 import org.apache.maven.model.building.ModelBuilder;
40 import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
41 import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
42 import org.apache.maven.repository.internal.DefaultVersionResolver;
43 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.sonatype.aether.RepositorySystem;
47 import org.sonatype.aether.RepositorySystemSession;
48 import org.sonatype.aether.collection.CollectRequest;
49 import org.sonatype.aether.collection.CollectResult;
50 import org.sonatype.aether.collection.DependencyCollectionException;
51 import org.sonatype.aether.collection.DependencySelector;
52 import org.sonatype.aether.graph.Dependency;
53 import org.sonatype.aether.graph.DependencyVisitor;
54 import org.sonatype.aether.impl.ArtifactDescriptorReader;
55 import org.sonatype.aether.impl.VersionRangeResolver;
56 import org.sonatype.aether.impl.VersionResolver;
57 import org.sonatype.aether.impl.internal.DefaultServiceLocator;
58 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
59 import org.sonatype.aether.repository.LocalRepository;
60 import org.sonatype.aether.spi.connector.RepositoryConnectorFactory;
61 import org.sonatype.aether.util.artifact.DefaultArtifact;
62 import org.sonatype.aether.util.graph.selector.AndDependencySelector;
63 import org.sonatype.aether.util.graph.selector.ExclusionDependencySelector;
64 import org.springframework.stereotype.Service;
65
66 import javax.annotation.PostConstruct;
67 import javax.inject.Inject;
68 import javax.inject.Named;
69 import java.io.File;
70 import java.util.ArrayList;
71 import java.util.HashMap;
72 import java.util.List;
73 import java.util.Map;
74
75 /**
76  * @author Olivier Lamy
77  * @since 1.4-M3
78  */
79 @Service( "dependencyTreeBuilder#maven3" )
80 public class Maven3DependencyTreeBuilder
81     implements DependencyTreeBuilder
82 {
83     private Logger log = LoggerFactory.getLogger( getClass() );
84
85     @Inject
86     private PlexusSisuBridge plexusSisuBridge;
87
88     @Inject
89     @Named( value = "repositoryPathTranslator#maven2" )
90     private RepositoryPathTranslator pathTranslator;
91
92     @Inject
93     private WagonFactory wagonFactory;
94
95     @Inject
96     private ManagedRepositoryAdmin managedRepositoryAdmin;
97
98     @Inject
99     private ProxyConnectorAdmin proxyConnectorAdmin;
100
101     @Inject
102     private NetworkProxyAdmin networkProxyAdmin;
103
104     @Inject
105     private RemoteRepositoryAdmin remoteRepositoryAdmin;
106
107     private ArtifactFactory factory;
108
109     private ModelBuilder builder;
110
111
112     private RepositorySystem repoSystem;
113
114     @PostConstruct
115     public void initialize()
116         throws PlexusSisuBridgeException
117     {
118         factory = plexusSisuBridge.lookup( ArtifactFactory.class, "default" );
119
120         repoSystem = plexusSisuBridge.lookup( RepositorySystem.class );
121         DefaultModelBuilderFactory defaultModelBuilderFactory = new DefaultModelBuilderFactory();
122         builder = defaultModelBuilderFactory.newInstance();
123     }
124
125     public void buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId, String version,
126                                      DependencyVisitor dependencyVisitor )
127         throws DependencyTreeBuilderException
128     {
129         Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version );
130         ManagedRepository repository = null;
131         try
132         {
133             repository = findArtifactInRepositories( repositoryIds, projectArtifact );
134         }
135         catch ( RepositoryAdminException e )
136         {
137             // FIXME better exception
138             throw new DependencyTreeBuilderException( "Cannot build project dependency tree " + e.getMessage(), e );
139         }
140
141         if ( repository == null )
142         {
143             // metadata could not be resolved
144             return;
145         }
146
147         try
148         {
149             // MRM-1411
150             // TODO: this is a workaround for a lack of proxy capability in the resolvers - replace when it can all be
151             //       handled there. It doesn't cache anything locally!
152             List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>();
153             Map<String, NetworkProxy> networkProxies = new HashMap<String, NetworkProxy>();
154
155             Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
156             List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get( repository.getId() );
157             if ( proxyConnectors != null )
158             {
159                 for ( ProxyConnector proxyConnector : proxyConnectors )
160                 {
161                     remoteRepositories.add( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) );
162
163                     NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy( proxyConnector.getProxyId() );
164
165                     if ( networkProxyConfig != null )
166                     {
167                         // key/value: remote repo ID/proxy info
168                         networkProxies.put( proxyConnector.getTargetRepoId(), networkProxyConfig );
169                     }
170                 }
171             }
172         }
173         catch ( RepositoryAdminException e )
174         {
175             throw new DependencyTreeBuilderException( e.getMessage(), e );
176         }
177
178         // FIXME take care of relative path
179         resolve( repository.getLocation(), groupId, artifactId, version, dependencyVisitor );
180     }
181
182
183     public List<TreeEntry> buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId,
184                                                 String version )
185         throws DependencyTreeBuilderException
186     {
187
188         List<TreeEntry> treeEntries = new ArrayList<TreeEntry>();
189         TreeDependencyNodeVisitor treeDependencyNodeVisitor = new TreeDependencyNodeVisitor( treeEntries );
190
191         buildDependencyTree( repositoryIds, groupId, artifactId, version, treeDependencyNodeVisitor );
192
193         log.debug( "treeEntrie: {}", treeEntries );
194         return treeEntries;
195     }
196
197
198     private void resolve( String localRepoDir, String groupId, String artifactId, String version,
199                           DependencyVisitor dependencyVisitor )
200     {
201
202         RepositorySystem system = newRepositorySystem();
203
204         RepositorySystemSession session = newRepositorySystemSession( system, localRepoDir );
205
206         org.sonatype.aether.artifact.Artifact artifact =
207             new DefaultArtifact( groupId + ":" + artifactId + ":" + version );
208
209         CollectRequest collectRequest = new CollectRequest();
210         collectRequest.setRoot( new Dependency( artifact, "" ) );
211
212         // add remote repositories ?
213         //collectRequest.addRepository(  )
214
215         collectRequest.setRequestContext( "project" );
216
217         //collectRequest.addRepository( repo );
218
219         try
220         {
221             CollectResult collectResult = system.collectDependencies( session, collectRequest );
222             collectResult.getRoot().accept( dependencyVisitor );
223             log.debug( "test" );
224         }
225         catch ( DependencyCollectionException e )
226         {
227             log.error( e.getMessage(), e );
228         }
229
230
231     }
232
233     public static RepositorySystem newRepositorySystem()
234     {
235         DefaultServiceLocator locator = new DefaultServiceLocator();
236         locator.addService( RepositoryConnectorFactory.class,
237                             ArchivaRepositoryConnectorFactory.class );// FileRepositoryConnectorFactory.class );
238         locator.addService( VersionResolver.class, DefaultVersionResolver.class );
239         locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
240         locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class );
241         //locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
242         //locator.setServices( WagonProvider.class,  );
243
244         return locator.getService( RepositorySystem.class );
245     }
246
247     public static RepositorySystemSession newRepositorySystemSession( RepositorySystem system, String localRepoDir )
248     {
249         MavenRepositorySystemSession session = new MavenRepositorySystemSession();
250
251         DependencySelector depFilter = new AndDependencySelector( new ExclusionDependencySelector() );
252         session.setDependencySelector( depFilter );
253
254         LocalRepository localRepo = new LocalRepository( localRepoDir );
255         session.setLocalRepositoryManager(
256             new SimpleLocalRepositoryManager( localRepoDir ) );// system.newLocalRepositoryManager( localRepo ) );
257
258         //session.setTransferListener(  );
259         //session.setRepositoryListener( n );
260
261         return session;
262     }
263
264
265     private ManagedRepository findArtifactInRepositories( List<String> repositoryIds, Artifact projectArtifact )
266         throws RepositoryAdminException
267     {
268         for ( String repoId : repositoryIds )
269         {
270             ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId );
271
272             File repoDir = new File( managedRepository.getLocation() );
273             File file = pathTranslator.toFile( repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(),
274                                                projectArtifact.getBaseVersion(),
275                                                projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion()
276                                                    + ".pom" );
277
278             if ( file.exists() )
279             {
280                 return managedRepository;
281             }
282         }
283         return null;
284     }
285
286
287 }