From 27ecedd93d388aaa2882cd182366e082bce5580a Mon Sep 17 00:00:00 2001 From: "Edwin L. Punzalan" Date: Sat, 10 Dec 2005 02:00:13 +0000 Subject: [PATCH] PR: MRM-39 Work in progress for the caching query layer git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@355691 13f79535-47bb-0310-9956-ffa450edef68 --- .../reporting/CachedRepositoryQueryLayer.java | 140 ++++++++++++++++++ .../AbstractRepositoryQueryLayerTest.java | 80 ++++++++++ 2 files changed, 220 insertions(+) create mode 100644 maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java create mode 100644 maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java diff --git a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java new file mode 100644 index 000000000..da56ccd75 --- /dev/null +++ b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/CachedRepositoryQueryLayer.java @@ -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; + } +} diff --git a/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java b/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java new file mode 100644 index 000000000..00586a8e0 --- /dev/null +++ b/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractRepositoryQueryLayerTest.java @@ -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" ); + } +} -- 2.39.5