]> source.dussan.org Git - archiva.git/blob
5021384132ade60a1917943b16861dd595894a14
[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.metadata.repository.storage.RepositoryPathTranslator;
34 import org.apache.archiva.proxy.common.WagonFactory;
35 import org.apache.maven.artifact.Artifact;
36 import org.apache.maven.artifact.factory.ArtifactFactory;
37 import org.apache.maven.model.building.DefaultModelBuilderFactory;
38 import org.apache.maven.model.building.ModelBuilder;
39 import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
40 import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
41 import org.apache.maven.repository.internal.DefaultVersionResolver;
42 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.sonatype.aether.RepositorySystem;
46 import org.sonatype.aether.RepositorySystemSession;
47 import org.sonatype.aether.collection.CollectRequest;
48 import org.sonatype.aether.collection.CollectResult;
49 import org.sonatype.aether.collection.DependencyCollectionException;
50 import org.sonatype.aether.collection.DependencySelector;
51 import org.sonatype.aether.graph.Dependency;
52 import org.sonatype.aether.graph.DependencyVisitor;
53 import org.sonatype.aether.impl.ArtifactDescriptorReader;
54 import org.sonatype.aether.impl.VersionRangeResolver;
55 import org.sonatype.aether.impl.VersionResolver;
56 import org.sonatype.aether.impl.internal.DefaultServiceLocator;
57 import org.sonatype.aether.repository.LocalRepository;
58 import org.sonatype.aether.spi.connector.RepositoryConnectorFactory;
59 import org.sonatype.aether.util.artifact.DefaultArtifact;
60 import org.sonatype.aether.util.graph.selector.AndDependencySelector;
61 import org.sonatype.aether.util.graph.selector.ExclusionDependencySelector;
62 import org.sonatype.aether.util.graph.selector.OptionalDependencySelector;
63 import org.springframework.stereotype.Service;
64
65 import javax.annotation.PostConstruct;
66 import javax.inject.Inject;
67 import javax.inject.Named;
68 import java.io.File;
69 import java.util.ArrayList;
70 import java.util.HashMap;
71 import java.util.List;
72 import java.util.Map;
73
74 /**
75  * @author Olivier Lamy
76  * @since 1.4-M3
77  */
78 @Service( "dependencyTreeBuilder#maven3" )
79 public class Maven3DependencyTreeBuilder
80 {
81     private Logger log = LoggerFactory.getLogger( getClass() );
82
83     @Inject
84     private PlexusSisuBridge plexusSisuBridge;
85
86     @Inject
87     @Named( value = "repositoryPathTranslator#maven2" )
88     private RepositoryPathTranslator pathTranslator;
89
90     @Inject
91     private WagonFactory wagonFactory;
92
93     @Inject
94     private ManagedRepositoryAdmin managedRepositoryAdmin;
95
96     @Inject
97     private ProxyConnectorAdmin proxyConnectorAdmin;
98
99     @Inject
100     private NetworkProxyAdmin networkProxyAdmin;
101
102     @Inject
103     private RemoteRepositoryAdmin remoteRepositoryAdmin;
104
105     private ArtifactFactory factory;
106
107     private ModelBuilder builder;
108
109
110     private RepositorySystem repoSystem;
111
112     @PostConstruct
113     public void initialize()
114         throws PlexusSisuBridgeException
115     {
116         factory = plexusSisuBridge.lookup( ArtifactFactory.class, "default" );
117
118         repoSystem = plexusSisuBridge.lookup( RepositorySystem.class );
119         DefaultModelBuilderFactory defaultModelBuilderFactory = new DefaultModelBuilderFactory();
120         builder = defaultModelBuilderFactory.newInstance();
121     }
122
123     public void buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId, String version,
124                                      DependencyVisitor dependencyVisitor )
125         throws Exception
126     {
127         Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version );
128         ManagedRepository repository = null;
129         try
130         {
131             repository = findArtifactInRepositories( repositoryIds, projectArtifact );
132         }
133         catch ( RepositoryAdminException e )
134         {
135             // FIXME better exception
136             throw new Exception( "Cannot build project dependency tree " + e.getMessage(), e );
137         }
138
139         if ( repository == null )
140         {
141             // metadata could not be resolved
142             return;
143         }
144
145         // MRM-1411
146         // TODO: this is a workaround for a lack of proxy capability in the resolvers - replace when it can all be
147         //       handled there. It doesn't cache anything locally!
148         List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>();
149         Map<String, NetworkProxy> networkProxies = new HashMap<String, NetworkProxy>();
150
151         Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
152         List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get( repository.getId() );
153         if ( proxyConnectors != null )
154         {
155             for ( ProxyConnector proxyConnector : proxyConnectors )
156             {
157                 remoteRepositories.add( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) );
158
159                 NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy( proxyConnector.getProxyId() );
160
161                 if ( networkProxyConfig != null )
162                 {
163                     // key/value: remote repo ID/proxy info
164                     networkProxies.put( proxyConnector.getTargetRepoId(), networkProxyConfig );
165                 }
166             }
167         }
168
169         // FIXME take care of relative path
170         resolve( repository.getLocation(), groupId, artifactId, version, dependencyVisitor );
171
172     }
173
174
175     private void resolve( String localRepoDir, String groupId, String artifactId, String version,
176                           DependencyVisitor dependencyVisitor )
177     {
178
179         RepositorySystem system = newRepositorySystem();
180
181         RepositorySystemSession session = newRepositorySystemSession( system, localRepoDir );
182
183         org.sonatype.aether.artifact.Artifact artifact =
184             new DefaultArtifact( groupId + ":" + artifactId + ":" + version );
185
186         CollectRequest collectRequest = new CollectRequest();
187         collectRequest.setRoot( new Dependency( artifact, "" ) );
188
189         // add remote repositories ?
190         //collectRequest.addRepository(  )
191
192         collectRequest.setRequestContext( "project" );
193
194         //collectRequest.addRepository( repo );
195
196         try
197         {
198             CollectResult collectResult = system.collectDependencies( session, collectRequest );
199             collectResult.getRoot().accept( dependencyVisitor );
200             log.debug( "test" );
201         }
202         catch ( DependencyCollectionException e )
203         {
204             log.error( e.getMessage(), e );
205         }
206
207
208     }
209
210     public static RepositorySystem newRepositorySystem()
211     {
212         DefaultServiceLocator locator = new DefaultServiceLocator();
213         locator.addService( RepositoryConnectorFactory.class,
214                             ArchivaRepositoryConnectorFactory.class );// FileRepositoryConnectorFactory.class );
215         locator.addService( VersionResolver.class, DefaultVersionResolver.class );
216         locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
217         locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class );
218         //locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
219         //locator.setServices( WagonProvider.class,  );
220
221         return locator.getService( RepositorySystem.class );
222     }
223
224     public static RepositorySystemSession newRepositorySystemSession( RepositorySystem system, String localRepoDir )
225     {
226         MavenRepositorySystemSession session = new MavenRepositorySystemSession();
227
228         DependencySelector depFilter = new AndDependencySelector( new ExclusionDependencySelector() );
229         session.setDependencySelector( depFilter );
230
231         LocalRepository localRepo = new LocalRepository( localRepoDir );
232         session.setLocalRepositoryManager( system.newLocalRepositoryManager( localRepo ) );
233
234         //session.setTransferListener(  );
235         //session.setRepositoryListener( n );
236
237         return session;
238     }
239
240
241     private ManagedRepository findArtifactInRepositories( List<String> repositoryIds, Artifact projectArtifact )
242         throws RepositoryAdminException
243     {
244         for ( String repoId : repositoryIds )
245         {
246             ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId );
247
248             File repoDir = new File( managedRepository.getLocation() );
249             File file = pathTranslator.toFile( repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(),
250                                                projectArtifact.getBaseVersion(),
251                                                projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion()
252                                                    + ".pom" );
253
254             if ( file.exists() )
255             {
256                 return managedRepository;
257             }
258         }
259         return null;
260     }
261
262
263 }