You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ra...@apache.org on 2007/10/03 22:44:06 UTC

svn commit: r581720 - in /maven/sandbox/trunk/archetypeng: archetypeng-common/src/main/java/org/apache/maven/archetype/source/ archetypeng-plugin/src/main/java/org/apache/maven/archetype/mojos/

Author: rafale
Date: Wed Oct  3 13:44:05 2007
New Revision: 581720

URL: http://svn.apache.org/viewvc?rev=581720&view=rev
Log:
Refactoring the updateLocalCatalogMojo to the DataSources components

Modified:
    maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/ArchetypeDataSource.java
    maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/CatalogArchetypeDataSource.java
    maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java
    maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/WikiArchetypeDataSource.java
    maven/sandbox/trunk/archetypeng/archetypeng-plugin/src/main/java/org/apache/maven/archetype/mojos/updateLocalCatalogMojo.java

Modified: maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/ArchetypeDataSource.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/ArchetypeDataSource.java?rev=581720&r1=581719&r2=581720&view=diff
==============================================================================
--- maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/ArchetypeDataSource.java (original)
+++ maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/ArchetypeDataSource.java Wed Oct  3 13:44:05 2007
@@ -2,6 +2,7 @@
 
 import java.util.List;
 import java.util.Properties;
+import org.apache.maven.archetype.catalog.Archetype;
 
 /**
  * Sources we can get Archetypes from. This may be the local registry, a Wiki, or,
@@ -14,8 +15,12 @@
  */
 public interface ArchetypeDataSource
 {
-    String ROLE = ArchetypeDataSource.class.getName();
+
+    String ROLE = ArchetypeDataSource.class.getName(  );
 
     List getArchetypes( Properties properties )
         throws ArchetypeDataSourceException;
-}
+
+    public void updateCatalog( Properties properties, Archetype archetype )
+        throws ArchetypeDataSourceException;
+}
\ No newline at end of file

Modified: maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/CatalogArchetypeDataSource.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/CatalogArchetypeDataSource.java?rev=581720&r1=581719&r2=581720&view=diff
==============================================================================
--- maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/CatalogArchetypeDataSource.java (original)
+++ maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/CatalogArchetypeDataSource.java Wed Oct  3 13:44:05 2007
@@ -6,16 +6,17 @@
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
-
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
+import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 
 /**
@@ -26,11 +27,11 @@
     extends AbstractLogEnabled
     implements ArchetypeDataSource
 {
-    public static String ARCHETYPE_CATALOG_PROPERTY = "file";
 
+    public static String ARCHETYPE_CATALOG_PROPERTY = "file";
     public static String ARCHETYPE_CATALOG_FILENAME = "archetype-catalog.xml";
-
-    private ArchetypeCatalogXpp3Reader catalogReader = new ArchetypeCatalogXpp3Reader();
+    private ArchetypeCatalogXpp3Reader catalogReader = new ArchetypeCatalogXpp3Reader(  );
+    private ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer(  );
 
     public List getArchetypes( Properties properties )
         throws ArchetypeDataSourceException
@@ -39,11 +40,11 @@
 
         s = StringUtils.replace( s, "${user.home}", System.getProperty( "user.home" ) );
 
-        getLogger().debug( "Using catalog " + s );
+        getLogger(  ).debug( "Using catalog " + s );
 
         File catalogFile = new File( s );
 
-        if ( catalogFile.exists() )
+        if ( catalogFile.exists(  ) )
         {
 
             try
@@ -59,16 +60,93 @@
         }
         else
         {
-            return new ArrayList();
+            return new ArrayList(  );
+        }
+    }
+
+    public void updateCatalog( Properties properties, Archetype archetype )
+        throws ArchetypeDataSourceException
+    {
+        String s = properties.getProperty( ARCHETYPE_CATALOG_PROPERTY );
+
+        s = StringUtils.replace( s, "${user.home}", System.getProperty( "user.home" ) );
+
+        getLogger(  ).debug( "Using catalog " + s );
+
+        File catalogFile = new File( s );
+
+        ArchetypeCatalog catalog;
+        if ( catalogFile.exists(  ) )
+        {
+            try
+            {
+                getLogger(  ).debug( "Reading the catalog " + catalogFile );
+                catalog = readCatalog( new FileReader( catalogFile ) );
+            }
+            catch ( FileNotFoundException ex )
+            {
+                getLogger(  ).debug( "Catalog file don't exist" );
+                catalog = new ArchetypeCatalog(  );
+            }
+        }
+        else
+        {
+            getLogger(  ).debug( "Catalog file don't exist" );
+            catalog = new ArchetypeCatalog(  );
+        }
+
+        Iterator archetypes = catalog.getArchetypes(  ).iterator(  );
+        boolean found = false;
+        Archetype newArchetype = null;
+        while ( !found && archetypes.hasNext(  ) )
+        {
+            Archetype a = (Archetype) archetypes.next();
+            if ( a.getGroupId(  ).equals( archetype.getGroupId(  ) ) && a.getArtifactId(  ).
+                equals( archetype.getArtifactId(  ) ) )
+            {
+                newArchetype = a;
+                found = true;
+            }
+        }
+        if ( !found )
+        {
+            catalog.addArchetype( newArchetype );
+        }
+
+        newArchetype.setVersion( archetype.getVersion(  ) );
+        newArchetype.setRepository( archetype.getRepository(  ) );
+        newArchetype.setDescription( archetype.getDescription(  ) );
+        newArchetype.setProperties( archetype.getProperties(  ) );
+        newArchetype.setGoals( archetype.getGoals(  ) );
+
+        writeLocalCatalog( catalog, catalogFile );
+    }
+
+    protected void writeLocalCatalog( ArchetypeCatalog catalog, File catalogFile )
+        throws ArchetypeDataSourceException
+    {
+        FileWriter writer = null;
+        try
+        {
+            writer = new FileWriter( catalogFile );
+            catalogWriter.write( writer, catalog );
+        }
+        catch ( IOException e )
+        {
+            throw new ArchetypeDataSourceException( "Error writing archetype catalog.", e );
+        }
+        finally
+        {
+            IOUtil.close( writer );
         }
     }
 
     protected List createArchetypeMap( ArchetypeCatalog archetypeCatalog )
         throws ArchetypeDataSourceException
     {
-        List archetypes = new ArrayList();
+        List archetypes = new ArrayList(  );
 
-        for ( Iterator i = archetypeCatalog.getArchetypes().iterator(); i.hasNext(); )
+        for ( Iterator i = archetypeCatalog.getArchetypes(  ).iterator(  ); i.hasNext(  ); )
         {
             Archetype archetype = (Archetype) i.next();
 

Modified: maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java?rev=581720&r1=581719&r2=581720&view=diff
==============================================================================
--- maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java (original)
+++ maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java Wed Oct  3 13:44:05 2007
@@ -6,6 +6,7 @@
 import java.net.URL;
 import java.util.List;
 import java.util.Properties;
+import org.apache.maven.archetype.catalog.Archetype;
 
 /**
  * @plexus.component role-hint="remote-catalog"
@@ -25,12 +26,12 @@
 
             if ( repository.endsWith( "/" ) )
             {
-                repository = repository.substring( 0, repository.length() - 1 );
+                repository = repository.substring( 0, repository.length(  ) - 1 );
             }
 
             URL url = new URL( repository + "/" + "archetype-catalog.xml" );
 
-            return createArchetypeMap( readCatalog( new InputStreamReader( url.openStream() ) ) );
+            return createArchetypeMap( readCatalog( new InputStreamReader( url.openStream(  ) ) ) );
         }
         catch ( MalformedURLException e )
         {
@@ -40,5 +41,11 @@
         {
             throw new ArchetypeDataSourceException( "Error reading archetype registry.", e );
         }
+    }
+
+    public void updateCatalog( Properties properties, Archetype archetype )
+        throws ArchetypeDataSourceException
+    {
+        throw new UnsupportedOperationException( "Not supported yet." );
     }
 }

Modified: maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/WikiArchetypeDataSource.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/WikiArchetypeDataSource.java?rev=581720&r1=581719&r2=581720&view=diff
==============================================================================
--- maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/WikiArchetypeDataSource.java (original)
+++ maven/sandbox/trunk/archetypeng/archetypeng-common/src/main/java/org/apache/maven/archetype/source/WikiArchetypeDataSource.java Wed Oct  3 13:44:05 2007
@@ -97,5 +97,11 @@
     static String cleanupUrl( String val )
     {
         return val.replaceAll( "\\r|\\n|\\s{2,}|\\[|\\]|\\ ", "" );
-    }    
+    }
+
+    public void updateCatalog( Properties properties, Archetype archetype )
+        throws ArchetypeDataSourceException
+    {
+        throw new UnsupportedOperationException( "Not supported yet." );
+    }
 }

Modified: maven/sandbox/trunk/archetypeng/archetypeng-plugin/src/main/java/org/apache/maven/archetype/mojos/updateLocalCatalogMojo.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/archetypeng/archetypeng-plugin/src/main/java/org/apache/maven/archetype/mojos/updateLocalCatalogMojo.java?rev=581720&r1=581719&r2=581720&view=diff
==============================================================================
--- maven/sandbox/trunk/archetypeng/archetypeng-plugin/src/main/java/org/apache/maven/archetype/mojos/updateLocalCatalogMojo.java (original)
+++ maven/sandbox/trunk/archetypeng/archetypeng-plugin/src/main/java/org/apache/maven/archetype/mojos/updateLocalCatalogMojo.java Wed Oct  3 13:44:05 2007
@@ -1,27 +1,18 @@
 package org.apache.maven.archetype.mojos;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.Reader;
 import java.util.Iterator;
+import java.util.Map;
 import java.util.Properties;
 import org.apache.maven.archetype.catalog.Archetype;
-import org.apache.maven.archetype.catalog.ArchetypeCatalog;
-import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader;
-import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer;
+import org.apache.maven.archetype.source.ArchetypeDataSource;
 import org.apache.maven.archetype.source.ArchetypeDataSourceException;
-import org.apache.maven.archetype.source.CatalogArchetypeDataSource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.ContextEnabled;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.PropertyUtils;
 import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
 
 /**
@@ -36,6 +27,9 @@
     extends AbstractMojo
     implements ContextEnabled
 {
+    /** @plexus.requirement role="org.apache.maven.archetype.source.ArchetypeDataSource" */
+    private Map archetypeSources;
+
     /**
      * The project artifact, which should have the LATEST metadata added to it.
      *
@@ -54,165 +48,67 @@
      */
     private File localRepository;
 
-    private ArchetypeCatalogXpp3Reader catalogReader =
-        new ArchetypeCatalogXpp3Reader (  );
-
-    private ArchetypeCatalogXpp3Writer catalogWriter =
-        new ArchetypeCatalogXpp3Writer (  );
-
-    public void execute ( )
+    public void execute( )
         throws MojoExecutionException
     {
         try
         {
-            File catalogFile = getCatalogFile (  );
-            ArchetypeCatalog catalog = readLocalCatalog ( catalogFile );
-            updateCatalog ( project, catalog );
-            writeLocalCatalog ( catalog, catalogFile );
-        }
-        catch ( ArchetypeDataSourceException ex )
-        {
-            throw new MojoExecutionException ( ex.getMessage (  ), ex );
-        }
-    }
-
-    private File getCatalogFile ( )
-    {
-        File archetypeCatalogPropertiesFile =
-            new File ( System.getProperty ( "user.home" ),
-            ".m2/archetype-catalog.properties" );
-
-        getLog (  ).
-            debug ( "Using catalog properties " + archetypeCatalogPropertiesFile );
-
-        Properties archetypeCatalogProperties;
+            Archetype archetype = new Archetype(  );
+            archetype.setGroupId( project.getGroupId(  ) );
+            archetype.setArtifactId( project.getArtifactId(  ) );
+            archetype.setVersion( project.getVersion(  ) );
+            archetype.setDescription( project.getName(  ) );
+            archetype.setRepository( localRepository.toString(  ) );
+//            archetype.setGoals(project.get);
+//            archetype.setProperties(project.get);
+            File archetypeCatalogPropertiesFile = new File( System.getProperty( "user.home" ), ".m2/archetype-catalog.properties" );
 
-        if ( archetypeCatalogPropertiesFile.exists (  ) )
-        {
-            archetypeCatalogProperties =
-                PropertyUtils.loadProperties ( archetypeCatalogPropertiesFile );
-        }
-        else
-        {
-            archetypeCatalogProperties = new Properties (  );
-            archetypeCatalogProperties.setProperty ( "catalog." +
-                CatalogArchetypeDataSource.ARCHETYPE_CATALOG_PROPERTY,
-                CatalogArchetypeDataSource.ARCHETYPE_CATALOG_FILENAME );
-        }
+            if ( archetypeCatalogPropertiesFile.exists(  ) )
+            {
+                Properties archetypeCatalogProperties = PropertyUtils.loadProperties( archetypeCatalogPropertiesFile );
 
+                getLog(  ).debug( "Updating catalogs " + archetypeCatalogProperties );
 
-        String s =
-            archetypeCatalogProperties.getProperty ( "catalog." +
-            CatalogArchetypeDataSource.ARCHETYPE_CATALOG_PROPERTY );
+                String[] sources = StringUtils.split( archetypeCatalogProperties.getProperty( "sources" ), "," );
 
-        s = StringUtils.replace ( s, "${user.home}",
-            System.getProperty ( "user.home" ) );
+                for ( int i = 0; i < sources.length; i++ )
+                {
+                    String sourceRoleHint = sources[i];
 
-        getLog (  ).debug ( "Using catalog file " + s );
+                    getLog(  ).debug( "Updating catalog " + sourceRoleHint );
 
-        return new File ( s );
-    }
+                    ArchetypeDataSource source = (ArchetypeDataSource) archetypeSources.get( sourceRoleHint );
 
-    private ArchetypeCatalog readLocalCatalog ( File catalogFile )
-        throws ArchetypeDataSourceException
-    {
-        ArchetypeCatalog catalog;
-        if ( catalogFile.exists (  ) )
-        {
-            try
-            {
-                getLog (  ).debug ( "Reading the catalog " + catalogFile );
-                catalog = readCatalog ( new FileReader ( catalogFile ) );
+                    source.updateCatalog( getArchetypeSourceProperties( sourceRoleHint, archetypeCatalogProperties ), archetype );
+                }
             }
-            catch ( FileNotFoundException ex )
+            else
             {
-                getLog (  ).debug ( "Catalog file don't exist" );
-                catalog =
-                    new ArchetypeCatalog (  );
+                getLog(  ).debug( "Not updating wiki catalog" );
             }
         }
-        else
+        catch ( ArchetypeDataSourceException ex )
         {
-            getLog (  ).debug ( "Catalog file don't exist" );
-            catalog = new ArchetypeCatalog (  );
+            throw new MojoExecutionException( ex.getMessage(  ), ex );
         }
-
-        return catalog;
     }
 
-    protected ArchetypeCatalog readCatalog ( Reader reader )
-        throws ArchetypeDataSourceException
+    private Properties getArchetypeSourceProperties( String sourceRoleHint, Properties archetypeCatalogProperties )
     {
-        try
-        {
-            return catalogReader.read ( reader );
-        }
-        catch ( IOException e )
-        {
-            throw new ArchetypeDataSourceException ( "Error reading archetype catalog.",
-                e );
-        }
-        catch ( XmlPullParserException e )
-        {
-            throw new ArchetypeDataSourceException ( "Error parsing archetype catalog.",
-                e );
-        }
-        finally
-        {
-            IOUtil.close ( reader );
-        }
-    }
+        Properties p = new Properties(  );
 
-    private void updateCatalog ( MavenProject project,
-        ArchetypeCatalog catalog )
-    {
-        Iterator archetypes = catalog.getArchetypes (  ).iterator (  );
-        boolean found = false;
-        Archetype archetype = null;
-        while ( !found && archetypes.hasNext (  ) )
+        for ( Iterator i = archetypeCatalogProperties.keySet(  ).iterator(  ); i.hasNext(  ); )
         {
-            Archetype a =
-                (Archetype) archetypes.next();
-            if ( a.getGroupId (  ).equals ( project.getGroupId (  ) ) &&
-                a.getArtifactId (  ).equals ( project.getArtifactId (  ) ) )
+            String key = (String) i.next();
+
+            if ( key.startsWith( sourceRoleHint ) )
             {
-                archetype = a;
-                found = true;
+                String k = key.substring( sourceRoleHint.length(  ) + 1 );
+
+                p.setProperty( k, archetypeCatalogProperties.getProperty( key ) );
             }
         }
-        if ( !found )
-        {
-            archetype = new Archetype (  );
-            archetype.setGroupId ( project.getGroupId (  ) );
-            archetype.setArtifactId ( project.getArtifactId (  ) );
-            catalog.addArchetype ( archetype );
-        }
-        //TODO: find correct values
-        archetype.setVersion ( project.getVersion (  ) );
-        archetype.setRepository ( "file://" + localRepository.getAbsolutePath (  ) );
-        archetype.setDescription ( project.getDescription (  ) );
-//        archetype.setProperties(null);
-//        archetype.setGoals(null);
-    }
 
-    private void writeLocalCatalog ( ArchetypeCatalog catalog,
-        File catalogFile )
-        throws ArchetypeDataSourceException
-    {
-        FileWriter writer = null;
-        try
-        {
-            writer = new FileWriter ( catalogFile );
-            catalogWriter.write ( writer, catalog );
-        }
-        catch ( IOException e )
-        {
-            throw new ArchetypeDataSourceException ( "Error writing archetype catalog.",
-                e );
-        }
-        finally
-        {
-            IOUtil.close ( writer );
-        }
+        return p;
     }
 }