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