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