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