Coverage Report - org.apache.maven.archetype.source.CatalogArchetypeDataSource
 
Classes in this File Line Coverage Branch Coverage Complexity
CatalogArchetypeDataSource
22%
15/66
16%
3/18
7
 
 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  36
 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  2
     public static final File USER_HOME = new File( System.getProperty( "user.home" ) );
 55  
 
 56  2
     public static final File MAVEN_CONFIGURATION = new File( USER_HOME, ".m2" );
 57  
 
 58  2
     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  0
         String s = properties.getProperty( ARCHETYPE_CATALOG_PROPERTY );
 64  
 
 65  0
         s = StringUtils.replace( s, "${user.home}", System.getProperty( "user.home" ) );
 66  
 
 67  0
         getLogger().debug( "Using catalog " + s );
 68  
 
 69  0
         File catalogFile = new File( s );
 70  
 
 71  
         ArchetypeCatalog catalog;
 72  0
         if ( catalogFile.exists() )
 73  
         {
 74  
             try
 75  
             {
 76  0
                 getLogger().debug( "Reading the catalog " + catalogFile );
 77  0
                 catalog = readCatalog( ReaderFactory.newXmlReader( catalogFile ) );
 78  
             }
 79  0
             catch ( FileNotFoundException ex )
 80  
             {
 81  0
                 getLogger().debug( "Catalog file don't exist" );
 82  0
                 catalog = new ArchetypeCatalog();
 83  
             }
 84  0
             catch ( IOException e )
 85  
             {
 86  0
                 throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
 87  0
             }
 88  
         }
 89  
         else
 90  
         {
 91  0
             getLogger().debug( "Catalog file don't exist" );
 92  0
             catalog = new ArchetypeCatalog();
 93  
         }
 94  
 
 95  0
         Iterator<Archetype> archetypes = catalog.getArchetypes().iterator();
 96  0
         boolean found = false;
 97  0
         Archetype newArchetype = archetype;
 98  0
         while ( !found && archetypes.hasNext() )
 99  
         {
 100  0
             Archetype a = (Archetype) archetypes.next();
 101  0
             if ( a.getGroupId().equals( archetype.getGroupId() )
 102  
                 && a.getArtifactId().equals( archetype.getArtifactId() ) )
 103  
             {
 104  0
                 newArchetype = a;
 105  0
                 found = true;
 106  
             }
 107  0
         }
 108  0
         if ( !found )
 109  
         {
 110  0
             catalog.addArchetype( newArchetype );
 111  
         }
 112  
 
 113  0
         newArchetype.setVersion( archetype.getVersion() );
 114  0
         newArchetype.setRepository( archetype.getRepository() );
 115  0
         newArchetype.setDescription( archetype.getDescription() );
 116  0
         newArchetype.setProperties( archetype.getProperties() );
 117  0
         newArchetype.setGoals( archetype.getGoals() );
 118  
 
 119  0
         writeLocalCatalog( catalog, catalogFile );
 120  0
     }
 121  
 
 122  
     public ArchetypeCatalog getArchetypeCatalog( Properties properties )
 123  
         throws ArchetypeDataSourceException
 124  
     {
 125  4
         String s = properties.getProperty( ARCHETYPE_CATALOG_PROPERTY );
 126  
 
 127  4
         s = StringUtils.replace( s, "${user.home}", System.getProperty( "user.home" ) );
 128  
 
 129  4
         File catalogFile = new File( s );
 130  4
         if ( catalogFile.exists() && catalogFile.isDirectory() )
 131  
         {
 132  4
             catalogFile = new File( catalogFile, ARCHETYPE_CATALOG_FILENAME );
 133  
         }
 134  4
         getLogger().debug( "Using catalog " + catalogFile );
 135  
 
 136  4
         if ( catalogFile.exists() )
 137  
         {
 138  
             try
 139  
             {
 140  4
                 return readCatalog( ReaderFactory.newXmlReader( catalogFile ) );
 141  
             }
 142  0
             catch ( FileNotFoundException e )
 143  
             {
 144  0
                 throw new ArchetypeDataSourceException( "The specific archetype catalog does not exist.", e );
 145  
             }
 146  0
             catch ( IOException e )
 147  
             {
 148  0
                 throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
 149  
             }
 150  
         }
 151  
         else
 152  
         {
 153  0
             return new ArchetypeCatalog();
 154  
         }
 155  
     }
 156  
 
 157  
     protected void writeLocalCatalog( ArchetypeCatalog catalog, File catalogFile )
 158  
         throws ArchetypeDataSourceException
 159  
     {
 160  0
         Writer writer = null;
 161  
         try
 162  
         {
 163  0
             writer = WriterFactory.newXmlWriter( catalogFile );
 164  
 
 165  0
             ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
 166  
 
 167  0
             catalogWriter.write( writer, catalog );
 168  
         }
 169  0
         catch ( IOException e )
 170  
         {
 171  0
             throw new ArchetypeDataSourceException( "Error writing archetype catalog.", e );
 172  
         }
 173  
         finally
 174  
         {
 175  0
             IOUtil.close( writer );
 176  0
         }
 177  0
     }
 178  
 
 179  
     protected ArchetypeCatalog readCatalog( Reader reader )
 180  
         throws ArchetypeDataSourceException
 181  
     {
 182  
         try
 183  
         {
 184  6
             ArchetypeCatalogXpp3Reader catalogReader = new ArchetypeCatalogXpp3Reader();
 185  
 
 186  6
             return catalogReader.read( reader );
 187  
         }
 188  0
         catch ( IOException e )
 189  
         {
 190  0
             throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
 191  
         }
 192  0
         catch ( XmlPullParserException e )
 193  
         {
 194  0
             throw new ArchetypeDataSourceException( "Error parsing archetype catalog.", e );
 195  
         }
 196  
         finally
 197  
         {
 198  6
             IOUtil.close( reader );
 199  
         }
 200  
     }
 201  
 }