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