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.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.File;
25  import java.io.FileNotFoundException;
26  import java.io.IOException;
27  import java.io.Reader;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import org.apache.maven.archetype.catalog.Archetype;
32  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
33  import org.codehaus.plexus.util.xml.XmlStreamReader;
34  import org.eclipse.aether.RepositorySystemSession;
35  import org.eclipse.aether.repository.RemoteRepository;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  @Named("catalog")
40  @Singleton
41  public class LocalCatalogArchetypeDataSource extends CatalogArchetypeDataSource {
42      private static final Logger LOGGER = LoggerFactory.getLogger(LocalCatalogArchetypeDataSource.class);
43  
44      @Override
45      public File updateCatalog(RepositorySystemSession repositorySession, Archetype archetype)
46              throws ArchetypeDataSourceException {
47          File localRepo = repositorySession.getLocalRepository().getBasedir();
48  
49          File catalogFile = new File(localRepo, ARCHETYPE_CATALOG_FILENAME);
50  
51          LOGGER.debug("Catalog to be used for update: " + catalogFile.getAbsolutePath());
52  
53          ArchetypeCatalog catalog;
54          if (catalogFile.exists()) {
55              try (Reader reader = new XmlStreamReader(catalogFile)) {
56                  LOGGER.debug("Reading catalog to be updated: " + catalogFile);
57                  catalog = readCatalog(reader);
58              } catch (FileNotFoundException ex) {
59                  LOGGER.debug("Catalog file don't exist");
60                  catalog = new ArchetypeCatalog();
61              } catch (IOException e) {
62                  throw new ArchetypeDataSourceException("Error reading archetype catalog.", e);
63              }
64          } else {
65              LOGGER.debug("Catalog file don't exist");
66              catalog = new ArchetypeCatalog();
67          }
68  
69          Iterator<Archetype> archetypes = catalog.getArchetypes().iterator();
70          boolean found = false;
71          Archetype newArchetype = archetype;
72          while (!found && archetypes.hasNext()) {
73              Archetype a = archetypes.next();
74              if (a.getGroupId().equals(archetype.getGroupId())
75                      && a.getArtifactId().equals(archetype.getArtifactId())) {
76                  newArchetype = a;
77                  found = true;
78              }
79          }
80          if (!found) {
81              catalog.addArchetype(newArchetype);
82          }
83  
84          newArchetype.setVersion(archetype.getVersion());
85          newArchetype.setRepository(archetype.getRepository());
86          newArchetype.setDescription(archetype.getDescription());
87          newArchetype.setProperties(archetype.getProperties());
88          newArchetype.setGoals(archetype.getGoals());
89  
90          writeLocalCatalog(catalog, catalogFile);
91          return catalogFile;
92      }
93  
94      @Override
95      public ArchetypeCatalog getArchetypeCatalog(
96              RepositorySystemSession repositorySession, List<RemoteRepository> remoteRepositories)
97              throws ArchetypeDataSourceException {
98  
99          File localRepo = repositorySession.getLocalRepository().getBasedir();
100 
101         File catalogFile = new File(localRepo, ARCHETYPE_CATALOG_FILENAME);
102 
103         if (catalogFile.exists() && catalogFile.isDirectory()) {
104             catalogFile = new File(catalogFile, ARCHETYPE_CATALOG_FILENAME);
105         }
106         LOGGER.debug("Getting archetypes from catalog: " + catalogFile);
107 
108         if (catalogFile.exists()) {
109             try {
110                 return readCatalog(new XmlStreamReader(catalogFile));
111             } catch (FileNotFoundException e) {
112                 throw new ArchetypeDataSourceException("The specific archetype catalog does not exist.", e);
113             } catch (IOException e) {
114                 throw new ArchetypeDataSourceException("Error reading archetype catalog.", e);
115             }
116         } else {
117             return new ArchetypeCatalog();
118         }
119     }
120 }