View Javadoc
1   package org.apache.maven.archetype.source;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.util.Iterator;
26  
27  import org.apache.maven.archetype.catalog.Archetype;
28  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
29  import org.apache.maven.project.ProjectBuildingRequest;
30  import org.apache.maven.shared.transfer.repository.RepositoryManager;
31  
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.annotations.Requirement;
34  import org.codehaus.plexus.util.ReaderFactory;
35  
36  @Component( role = ArchetypeDataSource.class, hint = "catalog" )
37  public class LocalCatalogArchetypeDataSource
38      extends CatalogArchetypeDataSource
39  {
40      @Requirement
41      private RepositoryManager repositoryManager;
42  
43      @Override
44      public void updateCatalog( ProjectBuildingRequest buildingRequest, Archetype archetype )
45          throws ArchetypeDataSourceException
46      {
47          File localRepo = repositoryManager.getLocalRepositoryBasedir( buildingRequest );
48  
49          File catalogFile = new File( localRepo, ARCHETYPE_CATALOG_FILENAME );
50  
51          getLogger().debug( "Catalog to be used for update: " + catalogFile.getAbsolutePath() );
52  
53          ArchetypeCatalog catalog;
54          if ( catalogFile.exists() )
55          {
56              try
57              {
58                  getLogger().debug( "Reading catalog to be updated: " + catalogFile );
59                  catalog = readCatalog( ReaderFactory.newXmlReader( catalogFile ) );
60              }
61              catch ( FileNotFoundException ex )
62              {
63                  getLogger().debug( "Catalog file don't exist" );
64                  catalog = new ArchetypeCatalog();
65              }
66              catch ( IOException e )
67              {
68                  throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
69              }
70          }
71          else
72          {
73              getLogger().debug( "Catalog file don't exist" );
74              catalog = new ArchetypeCatalog();
75          }
76  
77          Iterator<Archetype> archetypes = catalog.getArchetypes().iterator();
78          boolean found = false;
79          Archetype newArchetype = archetype;
80          while ( !found && archetypes.hasNext() )
81          {
82              Archetype a = archetypes.next();
83              if ( a.getGroupId().equals( archetype.getGroupId() )
84                  && a.getArtifactId().equals( archetype.getArtifactId() ) )
85              {
86                  newArchetype = a;
87                  found = true;
88              }
89          }
90          if ( !found )
91          {
92              catalog.addArchetype( newArchetype );
93          }
94  
95          newArchetype.setVersion( archetype.getVersion() );
96          newArchetype.setRepository( archetype.getRepository() );
97          newArchetype.setDescription( archetype.getDescription() );
98          newArchetype.setProperties( archetype.getProperties() );
99          newArchetype.setGoals( archetype.getGoals() );
100 
101         writeLocalCatalog( catalog, catalogFile );
102     }
103 
104     @Override
105     public ArchetypeCatalog getArchetypeCatalog( ProjectBuildingRequest buildingRequest )
106         throws ArchetypeDataSourceException
107     {
108         File localRepo = repositoryManager.getLocalRepositoryBasedir( buildingRequest );
109 
110         File catalogFile = new File( localRepo, ARCHETYPE_CATALOG_FILENAME );
111 
112         if ( catalogFile.exists() && catalogFile.isDirectory() )
113         {
114             catalogFile = new File( catalogFile, ARCHETYPE_CATALOG_FILENAME );
115         }
116         getLogger().debug( "Getting archetypes from catalog: " + catalogFile );
117 
118         if ( catalogFile.exists() )
119         {
120             try
121             {
122                 return readCatalog( ReaderFactory.newXmlReader( catalogFile ) );
123             }
124             catch ( FileNotFoundException e )
125             {
126                 throw new ArchetypeDataSourceException( "The specific archetype catalog does not exist.", e );
127             }
128             catch ( IOException e )
129             {
130                 throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
131             }
132         }
133         else
134         {
135             return new ArchetypeCatalog();
136         }
137     }
138 }