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  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  /**
44   * @author Jason van Zyl
45   */
46  @Named("remote-catalog")
47  @Singleton
48  public class RemoteCatalogArchetypeDataSource extends CatalogArchetypeDataSource implements ArchetypeDataSource {
49      private static final Logger LOGGER = LoggerFactory.getLogger(RemoteCatalogArchetypeDataSource.class);
50  
51      @Inject
52      private RepositorySystem repositorySystem;
53  
54      /**
55       * Id of the repository used to download catalog file. Proxy or authentication info can
56       * be setup in settings.xml.
57       */
58      public static final String ARCHETYPE_REPOSITORY_ID = "archetype";
59  
60      public static final String CENTRAL_REPOSITORY_ID = "central";
61  
62      @Override
63      public ArchetypeCatalog getArchetypeCatalog(
64              RepositorySystemSession repositorySession, List<RemoteRepository> remoteRepositories)
65              throws ArchetypeDataSourceException {
66  
67          MetadataRequest request = new MetadataRequest();
68          request.setRepository(getRemoteRepo(remoteRepositories));
69          request.setMetadata(new DefaultMetadata(ARCHETYPE_CATALOG_FILENAME, Metadata.Nature.RELEASE));
70  
71          MetadataResult metadataResult = repositorySystem
72                  .resolveMetadata(repositorySession, Collections.singletonList(request))
73                  .get(0);
74  
75          if (metadataResult.isResolved()) {
76              try {
77                  return readCatalog(
78                          new XmlStreamReader(metadataResult.getMetadata().getFile()));
79              } catch (IOException e) {
80                  throw new ArchetypeDataSourceException(e);
81              }
82          } else {
83              throw new ArchetypeDataSourceException(metadataResult.getException());
84          }
85      }
86  
87      private RemoteRepository getRemoteRepo(List<RemoteRepository> remoteRepositories) {
88  
89          if (remoteRepositories == null || remoteRepositories.isEmpty()) {
90              return null;
91          }
92  
93          for (RemoteRepository remoteRepository : remoteRepositories) {
94              if (ARCHETYPE_REPOSITORY_ID.equals(remoteRepository.getId())) {
95                  return remoteRepository;
96              }
97  
98              if (CENTRAL_REPOSITORY_ID.equals(remoteRepository.getId())) {
99                  return remoteRepository;
100             }
101 
102             if (getRemoteRepo(remoteRepository.getMirroredRepositories()) != null) {
103                 return remoteRepository;
104             }
105         }
106 
107         return null;
108     }
109 
110     @Override
111     public File updateCatalog(RepositorySystemSession repositorySession, Archetype archetype)
112             throws ArchetypeDataSourceException {
113         throw new ArchetypeDataSourceException("Not supported yet.");
114     }
115 }