You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ep...@apache.org on 2005/12/10 03:00:32 UTC

svn commit: r355691 - in /maven/repository-manager/trunk/maven-repository-reports-standard/src: main/java/org/apache/maven/repository/reporting/ test/java/org/apache/maven/repository/reporting/

Author: epunzalan
Date: Fri Dec  9 18:00:13 2005
New Revision: 355691

URL: http://svn.apache.org/viewcvs?rev=355691&view=rev
Log:
PR: MRM-39

Work in progress for the caching query layer

Added:
    maven/repository-manager/trunk/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java   (with props)
    maven/repository-manager/trunk/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java   (with props)

Added: maven/repository-manager/trunk/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java?rev=355691&view=auto
==============================================================================
--- maven/repository-manager/trunk/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java (added)
+++ maven/repository-manager/trunk/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java Fri Dec  9 18:00:13 2005
@@ -0,0 +1,140 @@
+package org.apache.maven.repository.reporting;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ 
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.File;
+import java.io.FileReader;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.Metadata;
+import org.apache.maven.artifact.repository.metadata.Snapshot;
+import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
+
+
+/**
+ *
+ */
+public class CachedRepositoryQueryLayer
+    implements RepositoryQueryLayer
+{
+    // plexus components
+    private ArtifactRepository repository;
+
+    
+    //cache for metadata
+    private Map cacheMetadata;
+
+    //cache for repository files, all types
+    //@todo should also cache missing files???
+    private Map cacheFile;
+    
+    public CachedRepositoryQueryLayer( ArtifactRepository repository )
+    {
+        this.repository = repository;
+        
+        cacheMetadata = new HashMap();
+        
+        cacheFile = new HashMap();
+    }
+
+    public boolean containsArtifact( Artifact artifact )
+    {
+        // @todo should check for snapshot artifacts
+        File artifactFile = new File( repository.pathOf( artifact ) );
+        
+        return fileExists( artifactFile );
+    }
+
+    public boolean containsArtifact( Artifact artifact, Snapshot snapshot )
+    {
+        return false;
+    }
+
+    private List getArtifactVersions( Artifact artifact )
+    {
+        Metadata metadata = getMetadata( artifact );
+        
+        return metadata.getVersioning().getVersions();
+    }
+
+    /**
+     * Method to utilize the cache
+     */
+    private boolean fileExists( File file )
+    {
+        boolean existing = true;
+        
+        String path = file.getAbsolutePath();
+        if ( !cacheFile.containsKey( path ) )
+        {
+            if ( file.exists() )
+            {
+                cacheFile.put( path, file );
+            }
+            else
+            {
+                existing = false;
+            }
+        }
+        
+        return existing;
+    }
+
+    private boolean fileExists( String repositoryPath )
+    {
+        return fileExists(  new File( repository.getBasedir(), repositoryPath ) );
+    }
+
+    /**
+     * Method to utilize the cache
+     */
+    private Metadata getMetadata( Artifact artifact )
+    {
+        Metadata metadata = null;
+        
+        if ( cacheMetadata.containsKey( artifact.getId() ) )
+        {
+            metadata = (Metadata) cacheMetadata.get( artifact.getId() );
+        }
+        else
+        {
+            ArtifactRepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
+            String path = repository.pathOfRemoteRepositoryMetadata( repositoryMetadata );
+            if ( fileExists( new File( path ) ) )
+            {
+                MetadataXpp3Reader reader = new MetadataXpp3Reader();
+                try
+                {
+                    metadata = reader.read( new FileReader( path ) );
+                    cacheMetadata.put( path, metadata );
+                }
+                catch ( Exception e )
+                {
+                    //@todo should throw something
+                }
+            }
+        }
+        
+        return metadata;
+    }
+}

Propchange: maven/repository-manager/trunk/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/repository-manager/trunk/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/repository-manager/trunk/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java?rev=355691&view=auto
==============================================================================
--- maven/repository-manager/trunk/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java (added)
+++ maven/repository-manager/trunk/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java Fri Dec  9 18:00:13 2005
@@ -0,0 +1,80 @@
+package org.apache.maven.repository.reporting;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ 
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.File;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+
+import org.codehaus.plexus.PlexusTestCase;
+
+/**
+ *
+ */
+public abstract class AbstractRepositoryQueryLayerTest
+    extends PlexusTestCase
+{
+    protected ArtifactFactory artifactFactory;
+    
+    protected ArtifactRepository repository;
+    
+    protected CachedRepositoryQueryLayer queryLayer;
+    
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        File repositoryDirectory = getTestFile( "src/test/repository" );
+
+        ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
+        ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
+        ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
+
+        repository =
+            factory.createArtifactRepository( "test", repositoryDirectory.toURL().toString(), layout, null, null );
+
+        queryLayer = new CachedRepositoryQueryLayer( repository );
+    }
+
+    public void testContainsArtifactTrue()
+    {
+        Artifact artifact = getArtifact( "groupId", "artifactId", "1.0-alpha-1" );
+        
+        assertTrue( "check artifact exists", queryLayer.containsArtifact( artifact ) );
+    }
+
+    public void testContainsArtifactFalse()
+    {
+        Artifact artifact = getArtifact( "groupId", "artifactId", "1.0-beta-1" );
+        
+        assertFalse( "check artifact exists", queryLayer.containsArtifact( artifact ) );
+    }
+
+    public void testContainsSnapshotArtifact()
+    {
+        
+    }
+    
+    protected Artifact getArtifact( String groupId, String artifactId, String version )
+    {
+        return artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
+    }
+}

Propchange: maven/repository-manager/trunk/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/repository-manager/trunk/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"