]> source.dussan.org Git - archiva.git/commitdiff
refactor discoverer to remove large section of duplicated code
authorBrett Porter <brett@apache.org>
Tue, 6 Jun 2006 07:06:05 +0000 (07:06 +0000)
committerBrett Porter <brett@apache.org>
Tue, 6 Jun 2006 07:06:05 +0000 (07:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@412022 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractArtifactDiscoverer.java
maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractDiscoverer.java [new file with mode: 0644]
maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java
maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultMetadataDiscoverer.java
maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java
maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java
maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/LegacyArtifactDiscovererTest.java
maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java

index fcd3937a5181d39349fc34ca8b6d05d746be972b..ea8a01354b599b3eb809caa3bd830521f9b15cce 100644 (file)
@@ -16,14 +16,14 @@ package org.apache.maven.repository.discovery;
  * limitations under the License.
  */
 
-import org.codehaus.plexus.logging.AbstractLogEnabled;
-import org.codehaus.plexus.util.DirectoryScanner;
-import org.codehaus.plexus.util.FileUtils;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 
 import java.io.File;
+import java.io.FileReader;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
 import java.util.List;
 
 /**
@@ -33,7 +33,7 @@ import java.util.List;
  * @author Brett Porter
  */
 public abstract class AbstractArtifactDiscoverer
-    extends AbstractLogEnabled
+    extends AbstractDiscoverer
 {
     /**
      * Standard patterns to exclude from discovery as they are not artifacts.
@@ -42,11 +42,7 @@ public abstract class AbstractArtifactDiscoverer
         "**/*.MD5", "**/*.sha1", "**/*.SHA1", "**/*snapshot-version", "*/website/**", "*/licenses/**", "*/licences/**",
         "**/.htaccess", "**/*.html", "**/*.asc", "**/*.txt", "**/*.xml", "**/README*", "**/CHANGELOG*", "**/KEYS*"};
 
-    private static final String[] EMPTY_STRING_ARRAY = new String[0];
-
-    private List excludedPaths = new ArrayList();
-
-    private List kickedOutPaths = new ArrayList();
+    protected static final String POM = ".pom";
 
     /**
      * Scan the repository for artifact paths.
@@ -56,57 +52,84 @@ public abstract class AbstractArtifactDiscoverer
         return scanForArtifactPaths( repositoryBase, blacklistedPatterns, null, STANDARD_DISCOVERY_EXCLUDES );
     }
 
-    /**
-     * Scan the repository for artifact paths.
-     */
-    protected String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns, String[] includes,
-                                             String[] excludes )
+    protected abstract Artifact buildArtifactFromPath( String path, ArtifactRepository repository );
+
+    public List discoverArtifacts( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots )
     {
-        List allExcludes = new ArrayList();
-        allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
-        if ( excludes != null )
+        if ( !"file".equals( repository.getProtocol() ) )
         {
-            allExcludes.addAll( Arrays.asList( excludes ) );
+            throw new UnsupportedOperationException( "Only filesystem repositories are supported" );
         }
 
-        if ( blacklistedPatterns != null && blacklistedPatterns.length() > 0 )
-        {
-            allExcludes.addAll( Arrays.asList( blacklistedPatterns.split( "," ) ) );
-        }
+        File repositoryBase = new File( repository.getBasedir() );
 
-        DirectoryScanner scanner = new DirectoryScanner();
-        scanner.setBasedir( repositoryBase );
-        if ( includes != null )
-        {
-            scanner.setIncludes( includes );
-        }
-        scanner.setExcludes( (String[]) allExcludes.toArray( EMPTY_STRING_ARRAY ) );
+        List artifacts = new ArrayList();
 
-        scanner.scan();
+        String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
 
-        excludedPaths.addAll( Arrays.asList( scanner.getExcludedFiles() ) );
+        for ( int i = 0; i < artifactPaths.length; i++ )
+        {
+            String path = artifactPaths[i];
+
+            Artifact artifact = buildArtifactFromPath( path, repository );
+            if ( artifact != null )
+            {
+                if ( includeSnapshots || !artifact.isSnapshot() )
+                {
+                    artifacts.add( artifact );
+                }
+            }
+            else
+            {
+                addKickedOutPath( path );
+            }
+        }
 
-        return scanner.getIncludedFiles();
+        return artifacts;
     }
 
-    /**
-     * Add a path to the list of files that were kicked out due to being invalid.
-     *
-     * @param path the path to add
-     * @todo add a reason
-     */
-    protected void addKickedOutPath( String path )
+    public List discoverStandalonePoms( ArtifactRepository repository, String blacklistedPatterns,
+                                        boolean includeSnapshots )
     {
-        kickedOutPaths.add( path );
-    }
+        List artifacts = new ArrayList();
 
-    public Iterator getExcludedPathsIterator()
-    {
-        return excludedPaths.iterator();
-    }
+        File repositoryBase = new File( repository.getBasedir() );
 
-    public Iterator getKickedOutPathsIterator()
-    {
-        return kickedOutPaths.iterator();
+        String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
+
+        for ( int i = 0; i < artifactPaths.length; i++ )
+        {
+            String path = artifactPaths[i];
+
+            if ( path.toLowerCase().endsWith( POM ) )
+            {
+                Artifact pomArtifact = buildArtifactFromPath( path, repository );
+                if ( pomArtifact != null )
+                {
+                    pomArtifact.setFile( new File( repositoryBase, path ) );
+                }
+
+                MavenXpp3Reader mavenReader = new MavenXpp3Reader();
+                String filename = repositoryBase.getAbsolutePath() + "/" + path;
+                try
+                {
+                    Model model = mavenReader.read( new FileReader( filename ) );
+                    if ( ( pomArtifact != null ) && ( "pom".equals( model.getPackaging() ) ) )
+                    {
+                        if ( includeSnapshots || !pomArtifact.isSnapshot() )
+                        {
+                            artifacts.add( model );
+                        }
+                    }
+                }
+                catch ( Exception e )
+                {
+                    getLogger().info( "error reading file: " + filename );
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        return artifacts;
     }
 }
diff --git a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractDiscoverer.java
new file mode 100644 (file)
index 0000000..9b62af1
--- /dev/null
@@ -0,0 +1,86 @@
+package org.apache.maven.repository.discovery;
+
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.DirectoryScanner;
+import org.codehaus.plexus.util.FileUtils;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * TODO [!]: Description.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ */
+public class AbstractDiscoverer
+    extends AbstractLogEnabled
+{
+    private List kickedOutPaths = new ArrayList();
+
+    /**
+     * @plexus.requirement
+     */
+    protected ArtifactFactory artifactFactory;
+
+    private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+    private List excludedPaths = new ArrayList();
+
+    /**
+     * Add a path to the list of files that were kicked out due to being invalid.
+     *
+     * @param path the path to add
+     * @todo add a reason
+     */
+    protected void addKickedOutPath( String path )
+    {
+        kickedOutPaths.add( path );
+    }
+
+    public Iterator getKickedOutPathsIterator()
+    {
+        return kickedOutPaths.iterator();
+    }
+
+    /**
+     * Scan the repository for artifact paths.
+     */
+    protected String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns, String[] includes,
+                                             String[] excludes )
+    {
+        List allExcludes = new ArrayList();
+        allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
+        if ( excludes != null )
+        {
+            allExcludes.addAll( Arrays.asList( excludes ) );
+        }
+
+        if ( blacklistedPatterns != null && blacklistedPatterns.length() > 0 )
+        {
+            allExcludes.addAll( Arrays.asList( blacklistedPatterns.split( "," ) ) );
+        }
+
+        DirectoryScanner scanner = new DirectoryScanner();
+        scanner.setBasedir( repositoryBase );
+        if ( includes != null )
+        {
+            scanner.setIncludes( includes );
+        }
+        scanner.setExcludes( (String[]) allExcludes.toArray( EMPTY_STRING_ARRAY ) );
+
+        scanner.scan();
+
+        excludedPaths.addAll( Arrays.asList( scanner.getExcludedFiles() ) );
+
+        return scanner.getIncludedFiles();
+    }
+
+    public Iterator getExcludedPathsIterator()
+    {
+        return excludedPaths.iterator();
+    }
+}
index e5eb6a6383d4c2dad4669574764f8428db76de6f..62345323ec44905529e70a2763402822b67cc793 100644 (file)
@@ -17,117 +17,22 @@ package org.apache.maven.repository.discovery;
  */
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.apache.maven.model.Model;
-import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 import org.apache.maven.repository.ArtifactUtils;
 
-import java.io.File;
-import java.io.FileReader;
-import java.util.ArrayList;
-import java.util.List;
-
 /**
  * Artifact discoverer for the new repository layout (Maven 2.0+).
  *
  * @author John Casey
  * @author Brett Porter
- * @plexus.component role="org.apache.maven.repository.discovery.ArtifactDiscoverer" role-hint="org.apache.maven.repository.discovery.DefaultArtifactDiscoverer"
+ * @plexus.component role="org.apache.maven.repository.discovery.ArtifactDiscoverer" role-hint="default"
  */
 public class DefaultArtifactDiscoverer
     extends AbstractArtifactDiscoverer
     implements ArtifactDiscoverer
 {
-    private final static String POM = ".pom";
-
-    /**
-     * @plexus.requirement
-     */
-    private ArtifactFactory artifactFactory;
-
-    public List discoverArtifacts( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots )
+    protected Artifact buildArtifactFromPath( String path, ArtifactRepository repository )
     {
-        if ( !"file".equals( repository.getProtocol() ) )
-        {
-            throw new UnsupportedOperationException( "Only filesystem repositories are supported" );
-        }
-
-        File repositoryBase = new File( repository.getBasedir() );
-
-        List artifacts = new ArrayList();
-
-        String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
-
-        for ( int i = 0; i < artifactPaths.length; i++ )
-        {
-            String path = artifactPaths[i];
-
-            Artifact artifact = ArtifactUtils.buildArtifact( repositoryBase, path, repository, artifactFactory );
-
-            if ( artifact != null )
-            {
-                if ( includeSnapshots || !artifact.isSnapshot() )
-                {
-                    artifacts.add( artifact );
-                }
-            }
-            else
-            {
-                addKickedOutPath( path );
-            }
-        }
-
-        return artifacts;
-    }
-
-    public List discoverStandalonePoms( ArtifactRepository repository, String blacklistedPatterns,
-                                        boolean convertSnapshots )
-    {
-        List artifacts = new ArrayList();
-
-        File repositoryBase = new File( repository.getBasedir() );
-
-        String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
-
-        for ( int i = 0; i < artifactPaths.length; i++ )
-        {
-            String path = artifactPaths[i];
-
-            if ( path.toLowerCase().endsWith( POM ) )
-            {
-                Artifact pomArtifact = ArtifactUtils.buildArtifact( path, artifactFactory );
-                if ( pomArtifact != null )
-                {
-                    pomArtifact.setFile( new File( repositoryBase, path ) );
-                }
-
-                MavenXpp3Reader mavenReader = new MavenXpp3Reader();
-                String filename = repositoryBase.getAbsolutePath() + "/" + path;
-                try
-                {
-                    Model model = mavenReader.read( new FileReader( filename ) );
-                    if ( ( model != null ) && ( "pom".equals( model.getPackaging() ) ) )
-                    {
-                        artifacts.add( model );
-                    }
-                    /*if ( ( pomArtifact != null ) && ( "pom".equals( model.getPackaging() ) ) )
-                    {
-                        if ( convertSnapshots || !pomArtifact.isSnapshot() )
-                        {
-                            artifacts.add( pomArtifact );
-                        }
-                    }
-                    */
-                }
-                catch ( Exception e )
-                {
-                    getLogger().info( "error reading file: " + filename );
-                    e.printStackTrace();
-                }
-            }
-        }
-
-        return artifacts;
+        return ArtifactUtils.buildArtifact( path, repository, artifactFactory );
     }
 }
index a2f0119171b425efdb9a8161398c96d72c5fceb6..21d56acf14d55ada2c17563439b0028333125da0 100644 (file)
@@ -17,7 +17,6 @@ package org.apache.maven.repository.discovery;
  */
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.Metadata;
@@ -46,14 +45,9 @@ import java.util.StringTokenizer;
  * @plexus.component role="org.apache.maven.repository.discovery.MetadataDiscoverer" role-hint="org.apache.maven.repository.discovery.DefaultMetadataDiscoverer"
  */
 public class DefaultMetadataDiscoverer
-    extends AbstractArtifactDiscoverer
+    extends AbstractDiscoverer
     implements MetadataDiscoverer
 {
-    /**
-     * @plexus.requirement
-     */
-    private ArtifactFactory artifactFactory;
-
     /**
      * Standard patterns to include in discovery of metadata files.
      */
index c4db5f2da278854cea0fd8dc7f5b97329bcf6fca..dbf065af05e7e8fa6569f8f934bf374878e6320c 100644 (file)
@@ -17,110 +17,32 @@ package org.apache.maven.repository.discovery;
  */
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.apache.maven.model.Model;
-import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 import org.apache.maven.repository.ArtifactUtils;
 
 import java.io.File;
-import java.io.FileReader;
-import java.util.ArrayList;
-import java.util.List;
 
 /**
  * Artifact discoverer for the legacy repository layout (Maven 1.x).
  *
  * @author John Casey
  * @author Brett Porter
- * @plexus.component role="org.apache.maven.repository.discovery.ArtifactDiscoverer" role-hint="org.apache.maven.repository.discovery.LegacyArtifactDiscoverer"
+ * @plexus.component role="org.apache.maven.repository.discovery.ArtifactDiscoverer" role-hint="legacy"
  */
 public class LegacyArtifactDiscoverer
     extends AbstractArtifactDiscoverer
     implements ArtifactDiscoverer
 {
-    private final static String POM = ".pom";
-
-    private final static String DELIM = "\\";
-
-    /**
-     * @plexus.requirement
-     */
-    private ArtifactFactory artifactFactory;
-
-    public List discoverArtifacts( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots )
+    protected Artifact buildArtifactFromPath( String path, ArtifactRepository repository )
     {
-        List artifacts = new ArrayList();
+        Artifact artifact = ArtifactUtils.buildArtifactFromLegacyPath( path, artifactFactory );
 
-        File repositoryBase = new File( repository.getBasedir() );
-        String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
-
-        for ( int i = 0; i < artifactPaths.length; i++ )
+        if ( artifact != null )
         {
-            String path = artifactPaths[i];
-
-            Artifact artifact = ArtifactUtils.buildArtifactFromLegacyPath( path, artifactFactory );
-            if ( artifact != null )
-            {
-                artifact.setRepository( repository );
-                artifact.setFile( new File( repositoryBase, path ) );
-
-                if ( includeSnapshots || !artifact.isSnapshot() )
-                {
-                    artifacts.add( artifact );
-                }
-            }
-            else
-            {
-                addKickedOutPath( path );
-            }
-        }
-
-        return artifacts;
-    }
-
-    public List discoverStandalonePoms( ArtifactRepository repository, String blacklistedPatterns,
-                                        boolean convertSnapshots )
-    {
-        List artifacts = new ArrayList();
-
-        File repositoryBase = new File( repository.getBasedir() );
-
-        String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
-
-        for ( int i = 0; i < artifactPaths.length; i++ )
-        {
-            String path = artifactPaths[i];
-
-            if ( path.toLowerCase().endsWith( POM ) )
-            {
-                Artifact pomArtifact = ArtifactUtils.buildArtifactFromLegacyPath( path, artifactFactory );
-                if ( pomArtifact != null )
-                {
-                    pomArtifact.setFile( new File( repositoryBase, path ) );
-                }
-
-                MavenXpp3Reader mavenReader = new MavenXpp3Reader();
-                String filename = repositoryBase.getAbsolutePath() + DELIM + path;
-                try
-                {
-                    Model model = mavenReader.read( new FileReader( filename ) );
-                    if ( ( pomArtifact != null ) && ( "pom".equals( model.getPackaging() ) ) )
-                    {
-                        if ( convertSnapshots || !pomArtifact.isSnapshot() )
-                        {
-                            artifacts.add( pomArtifact );
-                        }
-                    }
-                }
-                catch ( Exception e )
-                {
-                    getLogger().info( "error reading file: " + filename );
-                    e.printStackTrace();
-                }
-            }
+            artifact.setRepository( repository );
+            artifact.setFile( new File( repository.getBasedir(), path ) );
         }
 
-        return artifacts;
+        return artifact;
     }
 }
index 0b4919f9a33be8fc8d318f986eb253d30c58e47f..e8ade7ebc9077a6c3a1c3840d7e98eb479a793c3 100644 (file)
@@ -52,8 +52,7 @@ public class DefaultArtifactDiscovererTest
     {
         super.setUp();
 
-        discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE,
-                                                  "org.apache.maven.repository.discovery.DefaultArtifactDiscoverer" );
+        discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, "default" );
 
         factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
 
index 6e699ccb17542e20186de67c24cfebad07d6ebb3..d2c92e70f8f4ff3fd17553c9b3668690f9c01454 100644 (file)
@@ -49,8 +49,7 @@ public class LegacyArtifactDiscovererTest
     {
         super.setUp();
 
-        discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE,
-                                                  "org.apache.maven.repository.discovery.LegacyArtifactDiscoverer" );
+        discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, "legacy" );
 
         factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
 
index 12ef61cd969b080cfad57993d889bfa5d0ec0d54..41e5bf74b10be8fd62f24c4f93eb224de1e563da 100644 (file)
@@ -39,21 +39,19 @@ public class ArtifactUtils
     /**
      * Method used to build an artifact and then set its repository and file fields with the proper values
      *
-     * @param repositoryBase  the base directory of the repository
      * @param path            the path of the artifact relative from the repository base directory
      * @param repository      the repository where the artifact can be found
      * @param artifactFactory the artifactFactory to build the Artifact object when the given path is a valid artifact path
      * @return Artifact object if the given path represents an artifact path, otherwise, returns null
      */
-    public static Artifact buildArtifact( File repositoryBase, String path, ArtifactRepository repository,
-                                          ArtifactFactory artifactFactory )
+    public static Artifact buildArtifact( String path, ArtifactRepository repository, ArtifactFactory artifactFactory )
     {
         Artifact artifact = buildArtifact( path, artifactFactory );
 
         if ( artifact != null )
         {
             artifact.setRepository( repository );
-            artifact.setFile( new File( repositoryBase, path ) );
+            artifact.setFile( new File( repository.getBasedir(), path ) );
         }
 
         return artifact;