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