/[Apache-SVN]/maven/components/trunk/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java
ViewVC logotype

Diff of /maven/components/trunk/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- maven/components/trunk/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java	2005/05/07 05:42:39	169040
+++ maven/components/trunk/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java	2005/05/07 05:45:20	169041
@@ -15,21 +15,12 @@ package org.apache.maven.tools.repoclean
  * the License. ====================================================================
  */
 
-import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.metadata.ArtifactMetadata;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
-import org.apache.maven.tools.repoclean.artifact.metadata.ProjectMetadata;
-import org.apache.maven.tools.repoclean.digest.DigestVerifier;
-import org.apache.maven.tools.repoclean.discover.ArtifactDiscoverer;
 import org.apache.maven.tools.repoclean.index.ArtifactIndexer;
+import org.apache.maven.tools.repoclean.phase.DiscoveryPhase;
+import org.apache.maven.tools.repoclean.phase.RewritePhase;
 import org.apache.maven.tools.repoclean.report.FileReporter;
-import org.apache.maven.tools.repoclean.report.PathLister;
-import org.apache.maven.tools.repoclean.report.ReportWriteException;
-import org.apache.maven.tools.repoclean.report.Reporter;
-import org.apache.maven.tools.repoclean.rewrite.ArtifactPomRewriter;
-import org.apache.maven.tools.repoclean.transaction.RewriteTransaction;
-import org.apache.maven.tools.repoclean.transaction.RollbackException;
 import org.codehaus.plexus.PlexusConstants;
 import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.context.Context;
@@ -40,39 +31,32 @@ import org.codehaus.plexus.mailsender.Ma
 import org.codehaus.plexus.mailsender.MailSender;
 import org.codehaus.plexus.mailsender.MailSenderException;
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
-import org.codehaus.plexus.util.IOUtil;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.text.SimpleDateFormat;
 import java.util.Date;
-import java.util.Iterator;
 import java.util.List;
 
 /**
  * @author jdcasey
  */
-public class RepositoryCleaner extends AbstractLogEnabled implements Contextualizable
+public class RepositoryCleaner
+    extends AbstractLogEnabled
+    implements Contextualizable
 {
 
     public static final String ROLE = RepositoryCleaner.class.getName();
 
     private static final String REPORTS_DIR_DATE_FORMAT = "dd-MMM-yyyy_hh.mm.ss";
 
-    private DigestVerifier digestVerifier;
-
-    private ArtifactRepositoryLayout bridgingLayout;
-
     private MailSender mailSender;
 
     private ArtifactIndexer artifactIndexer;
 
+    private DiscoveryPhase discoveryPhase;
+
+    private RewritePhase rewritePhase;
+
     private PlexusContainer container;
 
     private String dateSubdir;
@@ -97,90 +81,41 @@ public class RepositoryCleaner extends A
             {
                 repoReporter = new FileReporter( reportsBase, "repository.report.txt" );
 
-                ArtifactDiscoverer artifactDiscoverer = null;
-
-                List artifacts = null;
-
-                PathLister kickoutLister = null;
-                PathLister excludeLister = null;
-
-                try
-                {
-                    artifactDiscoverer = (ArtifactDiscoverer) container.lookup( ArtifactDiscoverer.ROLE,
-                                                                                configuration.getSourceRepositoryLayout() );
-
-                    if ( logger.isDebugEnabled() )
-                    {
-                        logger.debug( "Discovering artifacts." );
-                    }
-
-                    try
-                    {
-                        File kickoutsList = new File( reportsBase, "kickouts.txt" );
-                        File excludesList = new File( reportsBase, "excludes.txt" );
+                List artifacts;
 
-                        kickoutLister = new PathLister( kickoutsList );
-                        excludeLister = new PathLister( excludesList );
+                artifacts = discoveryPhase.execute( reportsBase, sourceRepositoryBase, configuration, repoReporter );
 
-                        artifacts = artifactDiscoverer.discoverArtifacts( sourceRepositoryBase, repoReporter,
-                                                                          configuration.getBlacklistedPatterns(),
-                                                                          excludeLister, kickoutLister );
-                    }
-                    catch ( Exception e )
-                    {
-                        repoReporter.error( "Error discovering artifacts in source repository.", e );
-                        
-                        throw e;
-                    }
-
-                }
-                finally
-                {
-                    if ( artifactDiscoverer != null )
-                    {
-                        container.release( artifactDiscoverer );
-                    }
-
-                    if ( excludeLister != null )
-                    {
-                        excludeLister.close();
-                    }
-
-                    if ( kickoutLister != null )
-                    {
-                        kickoutLister.close();
-                    }
-                }
-
-                if ( artifacts != null )
+                if ( !artifacts.isEmpty() )
                 {
                     ArtifactRepositoryLayout sourceLayout = null;
                     ArtifactRepositoryLayout targetLayout = null;
                     try
                     {
                         sourceLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE,
-                                                                                    configuration.getSourceRepositoryLayout() );
+                                                                                    configuration
+                                                                                        .getSourceRepositoryLayout() );
 
-                        ArtifactRepository sourceRepo = new ArtifactRepository( "source", "file://" +
-                                                                                          sourceRepositoryBase.getAbsolutePath(),
-                                                                                sourceLayout );
+                        ArtifactRepository sourceRepo = new ArtifactRepository( "source", "file://"
+                            + sourceRepositoryBase.getAbsolutePath(), sourceLayout );
 
                         targetLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE,
-                                                                                    configuration.getTargetRepositoryLayout() );
+                                                                                    configuration
+                                                                                        .getTargetRepositoryLayout() );
 
-                        ArtifactRepository targetRepo = new ArtifactRepository( "target", "file://" +
-                                                                                          targetRepositoryBase.getAbsolutePath(),
-                                                                                targetLayout );
+                        ArtifactRepository targetRepo = new ArtifactRepository( "target", "file://"
+                            + targetRepositoryBase.getAbsolutePath(), targetLayout );
 
                         if ( logger.isDebugEnabled() )
                         {
                             logger.debug( "Rewriting POMs and artifact files." );
                         }
 
-                        artifactIndexer.writeAritfactIndex( artifacts, targetRepositoryBase );
+                        //                        List originalArtifacts = new ArrayList( artifacts );
+
+                        List rewritten = rewritePhase.execute( artifacts, sourceRepo, targetRepo, configuration,
+                                                               reportsBase, repoReporter );
 
-                        rewriteArtifactsAndPoms( artifacts, sourceRepo, targetRepo, configuration, reportsBase,
-                                                 sourceRepositoryBase, targetRepositoryBase, repoReporter );
+                        artifactIndexer.writeAritfactIndex( rewritten, targetRepositoryBase );
                     }
                     finally
                     {
@@ -203,8 +138,8 @@ public class RepositoryCleaner extends A
 
                 if ( repoReporter.hasWarning() && logger.isDebugEnabled() )
                 {
-                    logger.debug(
-                        "Warning encountered while rewriting one or more artifacts from source repository to target repository." );
+                    logger
+                        .warn( "Warning encountered while rewriting one or more artifacts from source repository to target repository." );
                 }
             }
             finally
@@ -215,7 +150,8 @@ public class RepositoryCleaner extends A
                 }
             }
 
-            // if we wrote a repository report, and the configuration says to email the report, then do it.
+            // if we wrote a repository report with an error in it, and the configuration says to email the report, 
+            // then do it.
             if ( repoReporter.hasError() && configuration.mailErrorReport() )
             {
                 logger.debug( "Sending error report to " + configuration.getErrorReportToName() + " via email." );
@@ -224,10 +160,10 @@ public class RepositoryCleaner extends A
 
                 StringBuffer contentBuffer = new StringBuffer();
 
-                contentBuffer.append( "Errors occurred while performing maven-1 to maven-2 repository conversion.\n\n" +
-                                      "For more details, see:\n\n" );
+                contentBuffer.append( "Errors occurred while performing maven-1 to maven-2 repository conversion.\n\n"
+                    + "For more details, see:\n\n" );
 
-                contentBuffer.append( configuration.getErrorReportLink().replaceAll("#date", dateSubdir) );
+                contentBuffer.append( configuration.getErrorReportLink().replaceAll( "#date", dateSubdir ) );
 
                 message.setContent( contentBuffer.toString() );
                 message.setSubject( configuration.getErrorReportSubject() );
@@ -242,292 +178,12 @@ public class RepositoryCleaner extends A
                 catch ( MailSenderException e )
                 {
                     logger.error( "An error occurred while trying to email repoclean report.", e );
-                    
-                    throw e;
-                }
-            }
-        }
-
-    }
-
-    private void rewriteArtifactsAndPoms( List artifacts, ArtifactRepository sourceRepo, ArtifactRepository targetRepo,
-                                          RepositoryCleanerConfiguration configuration, File reportsBase,
-                                          File sourceRepositoryBase, File targetRepositoryBase,
-                                          FileReporter repoReporter )
-        throws Exception
-    {
-        Logger logger = getLogger();
-
-        ArtifactPomRewriter artifactPomRewriter = null;
-
-        try
-        {
-            logger.debug( "Rewriting up to " + artifacts.size() + " artifacts (Should be " + ( artifacts.size() * 2 ) +
-                         " rewrites including POMs)." );
-
-            int actualRewriteCount = 0;
-            for ( Iterator it = artifacts.iterator(); it.hasNext(); )
-            {
-                Artifact artifact = (Artifact) it.next();
-
-                RewriteTransaction transaction = new RewriteTransaction( artifact );
-
-                String artifactReportPath = buildArtifactReportPath( artifact );
-
-                FileReporter artifactReporter = null;
-                try
-                {
-                    artifactReporter = new FileReporter( reportsBase, artifactReportPath );
-
-                    boolean errorOccurred = false;
-
-                    File artifactSource = new File( sourceRepo.getBasedir(), sourceRepo.pathOf( artifact ) );
-                    File artifactTarget = new File( targetRepo.getBasedir(), targetRepo.pathOf( artifact ).replace(
-                        '+', '-' ) );
-
-                    transaction.addFile( artifactTarget );
-
-                    artifact.setFile( artifactSource );
-
-                    boolean targetMissingOrOlder = !artifactTarget.exists() ||
-                        artifactTarget.lastModified() < artifactSource.lastModified();
-
-                    if ( artifactSource.exists() && ( configuration.force() || targetMissingOrOlder ) )
-                    {
-                        actualRewriteCount++;
-
-                        transaction.addFile( artifactTarget );
-
-                        try
-                        {
-                            if ( !configuration.reportOnly() )
-                            {
-                                if ( logger.isDebugEnabled() )
-                                {
-                                    logger.debug( "sourceRepo basedir is: \'" + sourceRepo.getBasedir() + "\'" );
-                                    logger.debug( "targetRepo basedir is: \'" + targetRepo.getBasedir() + "\'" );
-                                }
-
-                                File targetParent = artifactTarget.getParentFile();
-                                if ( !targetParent.exists() )
-                                {
-                                    targetParent.mkdirs();
-                                }
-
-                                if ( logger.isDebugEnabled() )
-                                {
-                                    logger.debug( "Copying artifact[" + artifact.getId() + "] from \'" +
-                                                  artifactSource + "\' to \'" + artifactTarget + "\'." );
-                                }
-
-                                copyArtifact( artifact, artifactTarget, artifactReporter );
-                            }
-                        }
-                        catch ( Exception e )
-                        {
-                            repoReporter.error( "Error transferring artifact[" + artifact.getId() +
-                                                "] to the target repository.", e );
-
-                            throw e;
-                        }
-
-                        if ( logger.isDebugEnabled() )
-                        {
-                            logger.debug( "working on digest for artifact[" + artifact.getId() + "] with groupId: \'" +
-                                          artifact.getGroupId() + "\'" );
-                        }
-
-                        try
-                        {
-                            digestVerifier.verifyDigest( artifactSource, artifactTarget, transaction, artifactReporter,
-                                                         configuration.reportOnly() );
-                        }
-                        catch ( Exception e )
-                        {
-                            repoReporter.error( "Error verifying digest for artifact[" + artifact.getId() + "]", e );
-
-                            throw e;
-                        }
-
-                        ArtifactMetadata pom = new ProjectMetadata( artifact );
 
-                        artifactPomRewriter = (ArtifactPomRewriter) container.lookup( ArtifactPomRewriter.ROLE,
-                                                                                      configuration.getSourcePomVersion() );
-
-                        File sourcePom = new File( sourceRepositoryBase, sourceRepo.pathOfMetadata( pom ) );
-
-                        File targetPom = new File( targetRepositoryBase,
-                                                   targetRepo.pathOfMetadata( pom ).replace( '+', '-' ) );
-
-                        transaction.addFile( targetPom );
-
-                        File bridgedTargetPom = new File( targetRepositoryBase, bridgingLayout.pathOfMetadata( pom ).replace(
-                            '+', '-' ) );
-
-                        transaction.addFile( bridgedTargetPom );
-
-                        try
-                        {
-                            artifactPomRewriter.rewrite( artifact, sourcePom, targetPom, artifactReporter,
-                                                         configuration.reportOnly() );
-
-                            boolean wroteBridge = bridgePomLocations( targetPom, bridgedTargetPom, artifactReporter );
-
-                            digestVerifier.verifyDigest( sourcePom, targetPom, transaction, artifactReporter,
-                                                         configuration.reportOnly() );
-
-                            if ( wroteBridge )
-                            {
-                                digestVerifier.verifyDigest( sourcePom, bridgedTargetPom, transaction,
-                                                             artifactReporter, configuration.reportOnly() );
-                            }
-
-                        }
-                        catch ( Exception e )
-                        {
-                            repoReporter.error( "Error rewriting POM for artifact[" + artifact.getId() +
-                                                "] into the target repository.\n Error message: " + e.getMessage() );
-
-                            throw e;
-                        }
-
-                    }
-                    else if ( !targetMissingOrOlder )
-                    {
-                        artifactReporter.warn( "Target file for artifact is present and not stale. (Artifact: \'" +
-                                               artifact.getId() + "\' in path: \'" + artifactSource +
-                                               "\' with target path: " + artifactTarget + ")." );
-                    }
-                    else
-                    {
-                        artifactReporter.error( "Cannot find source file for artifact: \'" + artifact.getId() +
-                                                "\' under path: \'" + artifactSource + "\'" );
-                    }
-
-                    if ( artifactReporter.hasError() )
-                    {
-                        repoReporter.warn( "Error(s) occurred while rewriting artifact: \'" + artifact.getId() +
-                                           "\' or its POM." );
-                    }
-                }
-                catch ( Exception e )
-                {
-                    if ( !configuration.force() )
-                    {
-                        repoReporter.warn( "Rolling back conversion for: " + artifact );
-                        try
-                        {
-                            transaction.rollback();
-                        }
-                        catch ( RollbackException re )
-                        {
-                            repoReporter.error( "Error rolling back conversion transaction.", re );
-                        }
-                    }
-                    else
-                    {
-                        repoReporter.warn( "NOT Rolling back conversion for: " + artifact + "; we are in --force mode." );
-                    }
-
-                    artifactReporter.error( "Error while rewriting file or POM for artifact: \'" + artifact.getId() +
-                                            "\'. See report at: \'" + artifactReportPath + "\'.", e );
-                }
-                finally
-                {
-                    if ( artifactReporter != null )
-                    {
-                        artifactReporter.close();
-                    }
+                    throw e;
                 }
             }
-
-            logger.info( "Actual number of artifacts rewritten: " + actualRewriteCount + " (" +
-                         ( actualRewriteCount * 2 ) + " including POMs)." );
-        }
-        finally
-        {
-            if ( artifactPomRewriter != null )
-            {
-                container.release( artifactPomRewriter );
-            }
-        }
-    }
-
-    private boolean bridgePomLocations( File targetPom, File bridgedTargetPom, Reporter reporter )
-        throws IOException, ReportWriteException
-    {
-        if ( targetPom.equals( bridgedTargetPom ) )
-        {
-            reporter.warn( "Cannot create legacy-compatible copy of POM at: " + targetPom +
-                           "; legacy-compatible path is the same as the converted POM itself." );
-
-            return false;
-        }
-
-        FileInputStream in = null;
-        FileOutputStream out = null;
-
-        try
-        {
-            in = new FileInputStream( targetPom );
-            out = new FileOutputStream( bridgedTargetPom );
-
-            IOUtil.copy( in, out );
-        }
-        finally
-        {
-            IOUtil.close( in );
-            IOUtil.close( out );
         }
 
-        return true;
-    }
-
-    private String buildArtifactReportPath( Artifact artifact )
-    {
-        String classifier = artifact.getClassifier();
-        String groupId = artifact.getGroupId().replace( '.', '/' );
-        String artifactId = artifact.getArtifactId();
-        String type = artifact.getType();
-        String version = artifact.getVersion();
-
-        return groupId + "/" + artifactId + "/" + type + "/" +
-            ( ( classifier != null ) ? ( classifier + "-" ) : ( "" ) ) + version + ".report.txt";
-    }
-
-    private void copyArtifact( Artifact artifact, File artifactTarget, FileReporter reporter )
-        throws IOException
-    {
-        File artifactSource = artifact.getFile();
-
-        InputStream inStream = null;
-        OutputStream outStream = null;
-        try
-        {
-            File targetParent = artifactTarget.getParentFile();
-            if ( !targetParent.exists() )
-            {
-                targetParent.mkdirs();
-            }
-
-            inStream = new BufferedInputStream( new FileInputStream( artifactSource ) );
-            outStream = new BufferedOutputStream( new FileOutputStream( artifactTarget ) );
-
-            byte[] buffer = new byte[16];
-            int read = -1;
-
-            while ( ( read = inStream.read( buffer ) ) > -1 )
-            {
-                outStream.write( buffer, 0, read );
-            }
-
-            outStream.flush();
-        }
-        finally
-        {
-            IOUtil.close( inStream );
-            IOUtil.close( outStream );
-        }
     }
 
     private File normalizeTargetRepositoryBase( String targetRepositoryPath )
@@ -546,8 +202,8 @@ public class RepositoryCleaner extends A
         }
         else if ( !targetRepositoryBase.isDirectory() )
         {
-            logger.error( "Cannot write to target repository \'" + targetRepositoryBase +
-                          "\' because it is not a directory." );
+            logger.error( "Cannot write to target repository \'" + targetRepositoryBase
+                + "\' because it is not a directory." );
 
             targetRepositoryBase = null;
         }

 

infrastructure at apache.org
ViewVC Help
Powered by ViewVC 1.1.26