View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.archetype.source;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import org.apache.maven.archetype.catalog.Archetype;
31  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
32  import org.codehaus.plexus.util.xml.XmlStreamReader;
33  import org.eclipse.aether.RepositorySystem;
34  import org.eclipse.aether.RepositorySystemSession;
35  import org.eclipse.aether.metadata.DefaultMetadata;
36  import org.eclipse.aether.metadata.Metadata;
37  import org.eclipse.aether.repository.RemoteRepository;
38  import org.eclipse.aether.resolution.MetadataRequest;
39  import org.eclipse.aether.resolution.MetadataResult;
40  
41  /**
42   * @author Jason van Zyl
43   */
44  @Named("remote-catalog")
45  @Singleton
46  public class RemoteCatalogArchetypeDataSource extends CatalogArchetypeDataSource implements ArchetypeDataSource {
47  
48      @Inject
49      private RepositorySystem repositorySystem;
50  
51      /**
52       * Id of the repository used to download catalog file. Proxy or authentication info can
53       * be setup in settings.xml.
54       */
55      public static final String ARCHETYPE_REPOSITORY_ID = "archetype";
56  
57      public static final String CENTRAL_REPOSITORY_ID = "central";
58  
59      @Override
60      public ArchetypeCatalog getArchetypeCatalog(
61              RepositorySystemSession repositorySession, List<RemoteRepository> remoteRepositories)
62              throws ArchetypeDataSourceException {
63  
64          MetadataRequest request = new MetadataRequest();
65          request.setRepository(getRemoteRepo(remoteRepositories));
66          request.setMetadata(new DefaultMetadata(ARCHETYPE_CATALOG_FILENAME, Metadata.Nature.RELEASE));
67  
68          MetadataResult metadataResult = repositorySystem
69                  .resolveMetadata(repositorySession, Collections.singletonList(request))
70                  .get(0);
71  
72          if (metadataResult.isResolved()) {
73              try {
74                  return readCatalog(
75                          new XmlStreamReader(metadataResult.getMetadata().getFile()));
76              } catch (IOException e) {
77                  throw new ArchetypeDataSourceException(e);
78              }
79          } else {
80              throw new ArchetypeDataSourceException(metadataResult.getException());
81          }
82      }
83  
84      private RemoteRepository getRemoteRepo(List<RemoteRepository> remoteRepositories) {
85  
86          if (remoteRepositories == null || remoteRepositories.isEmpty()) {
87              return null;
88          }
89  
90          for (RemoteRepository remoteRepository : remoteRepositories) {
91              if (ARCHETYPE_REPOSITORY_ID.equals(remoteRepository.getId())) {
92                  return remoteRepository;
93              }
94  
95              if (CENTRAL_REPOSITORY_ID.equals(remoteRepository.getId())) {
96                  return remoteRepository;
97              }
98  
99              if (getRemoteRepo(remoteRepository.getMirroredRepositories()) != null) {
100                 return remoteRepository;
101             }
102         }
103 
104         return null;
105     }
106 
107     @Override
108     public File updateCatalog(RepositorySystemSession repositorySession, Archetype archetype)
109             throws ArchetypeDataSourceException {
110         throw new ArchetypeDataSourceException("Not supported yet.");
111     }
112 }