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 org.apache.maven.archetype.catalog.Archetype;
23  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
24  import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader;
25  import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer;
26  import org.codehaus.plexus.component.annotations.Component;
27  import org.codehaus.plexus.logging.AbstractLogEnabled;
28  import org.codehaus.plexus.util.IOUtil;
29  import org.codehaus.plexus.util.ReaderFactory;
30  import org.codehaus.plexus.util.StringUtils;
31  import org.codehaus.plexus.util.WriterFactory;
32  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33  
34  import java.io.File;
35  import java.io.FileNotFoundException;
36  import java.io.IOException;
37  import java.io.Reader;
38  import java.io.Writer;
39  import java.util.Iterator;
40  import java.util.Properties;
41  
42  /**
43   * @author Jason van Zyl
44   */
45  @Component( role = ArchetypeDataSource.class, hint = "catalog" )
46  public class CatalogArchetypeDataSource
47      extends AbstractLogEnabled
48      implements ArchetypeDataSource
49  {
50      public static final String ARCHETYPE_CATALOG_PROPERTY = "file";
51  
52      public static final String ARCHETYPE_CATALOG_FILENAME = "archetype-catalog.xml";
53  
54      public static final File USER_HOME = new File( System.getProperty( "user.home" ) );
55  
56      public static final File MAVEN_CONFIGURATION = new File( USER_HOME, ".m2" );
57  
58      public static final File DEFAULT_ARCHETYPE_CATALOG = new File( MAVEN_CONFIGURATION, ARCHETYPE_CATALOG_FILENAME );
59  
60      public void updateCatalog( Properties properties, Archetype archetype )
61          throws ArchetypeDataSourceException
62      {
63          String s = properties.getProperty( ARCHETYPE_CATALOG_PROPERTY );
64  
65          s = StringUtils.replace( s, "${user.home}", System.getProperty( "user.home" ) );
66  
67          getLogger().debug( "Using catalog " + s );
68  
69          File catalogFile = new File( s );
70  
71          ArchetypeCatalog catalog;
72          if ( catalogFile.exists() )
73          {
74              try
75              {
76                  getLogger().debug( "Reading the catalog " + catalogFile );
77                  catalog = readCatalog( ReaderFactory.newXmlReader( catalogFile ) );
78              }
79              catch ( FileNotFoundException ex )
80              {
81                  getLogger().debug( "Catalog file don't exist" );
82                  catalog = new ArchetypeCatalog();
83              }
84              catch ( IOException e )
85              {
86                  throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
87              }
88          }
89          else
90          {
91              getLogger().debug( "Catalog file don't exist" );
92              catalog = new ArchetypeCatalog();
93          }
94  
95          Iterator<Archetype> archetypes = catalog.getArchetypes().iterator();
96          boolean found = false;
97          Archetype newArchetype = archetype;
98          while ( !found && archetypes.hasNext() )
99          {
100             Archetype a = (Archetype) archetypes.next();
101             if ( a.getGroupId().equals( archetype.getGroupId() )
102                 && a.getArtifactId().equals( archetype.getArtifactId() ) )
103             {
104                 newArchetype = a;
105                 found = true;
106             }
107         }
108         if ( !found )
109         {
110             catalog.addArchetype( newArchetype );
111         }
112 
113         newArchetype.setVersion( archetype.getVersion() );
114         newArchetype.setRepository( archetype.getRepository() );
115         newArchetype.setDescription( archetype.getDescription() );
116         newArchetype.setProperties( archetype.getProperties() );
117         newArchetype.setGoals( archetype.getGoals() );
118 
119         writeLocalCatalog( catalog, catalogFile );
120     }
121 
122     public ArchetypeCatalog getArchetypeCatalog( Properties properties )
123         throws ArchetypeDataSourceException
124     {
125         String s = properties.getProperty( ARCHETYPE_CATALOG_PROPERTY );
126 
127         s = StringUtils.replace( s, "${user.home}", System.getProperty( "user.home" ) );
128 
129         File catalogFile = new File( s );
130         if ( catalogFile.exists() && catalogFile.isDirectory() )
131         {
132             catalogFile = new File( catalogFile, ARCHETYPE_CATALOG_FILENAME );
133         }
134         getLogger().debug( "Using catalog " + catalogFile );
135 
136         if ( catalogFile.exists() )
137         {
138             try
139             {
140                 return readCatalog( ReaderFactory.newXmlReader( catalogFile ) );
141             }
142             catch ( FileNotFoundException e )
143             {
144                 throw new ArchetypeDataSourceException( "The specific archetype catalog does not exist.", e );
145             }
146             catch ( IOException e )
147             {
148                 throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
149             }
150         }
151         else
152         {
153             return new ArchetypeCatalog();
154         }
155     }
156 
157     protected void writeLocalCatalog( ArchetypeCatalog catalog, File catalogFile )
158         throws ArchetypeDataSourceException
159     {
160         Writer writer = null;
161         try
162         {
163             writer = WriterFactory.newXmlWriter( catalogFile );
164 
165             ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
166 
167             catalogWriter.write( writer, catalog );
168         }
169         catch ( IOException e )
170         {
171             throw new ArchetypeDataSourceException( "Error writing archetype catalog.", e );
172         }
173         finally
174         {
175             IOUtil.close( writer );
176         }
177     }
178 
179     protected ArchetypeCatalog readCatalog( Reader reader )
180         throws ArchetypeDataSourceException
181     {
182         try
183         {
184             ArchetypeCatalogXpp3Reader catalogReader = new ArchetypeCatalogXpp3Reader();
185 
186             return catalogReader.read( reader );
187         }
188         catch ( IOException e )
189         {
190             throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
191         }
192         catch ( XmlPullParserException e )
193         {
194             throw new ArchetypeDataSourceException( "Error parsing archetype catalog.", e );
195         }
196         finally
197         {
198             IOUtil.close( reader );
199         }
200     }
201 }