From: Brett Porter Date: Thu, 11 Mar 2010 02:40:39 +0000 (+0000) Subject: [MRM-1360] store artifact metadata in the repository and make maven facet's compliant X-Git-Tag: archiva-1.4-M1~982 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4330002cef27fa2e08653ad96d46f7ad850c765f;p=archiva.git [MRM-1360] store artifact metadata in the repository and make maven facet's compliant git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@921671 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java index 1fc43012b..611861cd7 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java @@ -241,7 +241,8 @@ public class Maven2RepositoryMetadataResolver private void addProblemReport( String repoId, String namespace, String projectId, String projectVersion, String problemId, String message ) { - // TODO: an event mechanism would remove coupling to the problem reporting plugin + // TODO: an event mechanism would remove coupling to the problem reporting plugin and allow other plugins to + // generate metadata on the fly if appropriately checked for missing facets in the resolver RepositoryProblemFacet problem = new RepositoryProblemFacet(); problem.setProblem( problemId ); problem.setMessage( message ); diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacet.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacet.java index f24ca0cc0..5dbb2657e 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacet.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacet.java @@ -91,19 +91,28 @@ public class MavenArtifactFacet public Map toProperties() { HashMap properties = new HashMap(); - properties.put( FACET_ID + ":type", type ); - properties.put( FACET_ID + ":classifier", classifier ); - properties.put( FACET_ID + ":timestamp", timestamp ); - properties.put( FACET_ID + ":buildNumber", Integer.toString( buildNumber )); + properties.put( "type", type ); + if ( classifier != null ) + { + properties.put( "classifier", classifier ); + } + if ( timestamp != null ) + { + properties.put( "timestamp", timestamp ); + } + if ( buildNumber > 0 ) + { + properties.put( "buildNumber", Integer.toString( buildNumber ) ); + } return properties; } public void fromProperties( Map properties ) { - type = properties.get( FACET_ID + ":type" ); - classifier = properties.get( FACET_ID + ":classifier" ); - timestamp = properties.get( FACET_ID + ":timestamp" ); - String buildNumber = properties.get( FACET_ID + ":buildNumber" ); + type = properties.get( "type" ); + classifier = properties.get( "classifier" ); + timestamp = properties.get( "timestamp" ); + String buildNumber = properties.get( "buildNumber" ); if ( buildNumber != null ) { this.buildNumber = Integer.valueOf( buildNumber ); diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacetFactory.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacetFactory.java new file mode 100644 index 000000000..002eaf755 --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacetFactory.java @@ -0,0 +1,40 @@ +package org.apache.archiva.metadata.repository.storage.maven2; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.archiva.metadata.model.MetadataFacet; +import org.apache.archiva.metadata.model.MetadataFacetFactory; + +/** + * @plexus.component role="org.apache.archiva.metadata.model.MetadataFacetFactory" role-hint="org.apache.archiva.metadata.repository.storage.maven2.artifact" + */ +public class MavenArtifactFacetFactory + implements MetadataFacetFactory +{ + public MetadataFacet createMetadataFacet() + { + return new MavenArtifactFacet(); + } + + public MetadataFacet createMetadataFacet( String repositoryId, String name ) + { + throw new UnsupportedOperationException( "There is no valid name for artifact facets" ); + } +} \ No newline at end of file diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenProjectFacet.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenProjectFacet.java index 93df5a1e1..83fe1693e 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenProjectFacet.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenProjectFacet.java @@ -91,30 +91,30 @@ public class MavenProjectFacet public Map toProperties() { HashMap properties = new HashMap(); - properties.put( FACET_ID + ":groupId", groupId ); - properties.put( FACET_ID + ":artifactId", artifactId ); - properties.put( FACET_ID + ":packaging", packaging ); + properties.put( "groupId", groupId ); + properties.put( "artifactId", artifactId ); + properties.put( "packaging", packaging ); if ( parent != null ) { - properties.put( FACET_ID + ":parent.groupId", parent.getGroupId() ); - properties.put( FACET_ID + ":parent.artifactId", parent.getArtifactId() ); - properties.put( FACET_ID + ":parent.version", parent.getVersion() ); + properties.put( "parent.groupId", parent.getGroupId() ); + properties.put( "parent.artifactId", parent.getArtifactId() ); + properties.put( "parent.version", parent.getVersion() ); } return properties; } public void fromProperties( Map properties ) { - groupId = properties.get( FACET_ID + ":groupId" ); - artifactId = properties.get( FACET_ID + ":artifactId" ); - packaging = properties.get( FACET_ID + ":packaging" ); - String parentArtifactId = properties.get( FACET_ID + ":parent.artifactId" ); + groupId = properties.get( "groupId" ); + artifactId = properties.get( "artifactId" ); + packaging = properties.get( "packaging" ); + String parentArtifactId = properties.get( "parent.artifactId" ); if ( parentArtifactId != null ) { MavenProjectParent parent = new MavenProjectParent(); - parent.setGroupId( properties.get( FACET_ID + ":parent.groupId" ) ); + parent.setGroupId( properties.get( "parent.groupId" ) ); parent.setArtifactId( parentArtifactId ); - parent.setVersion( properties.get( FACET_ID + ":parent.version" ) ); + parent.setVersion( properties.get( "parent.version" ) ); this.parent = parent; } } diff --git a/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java b/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java index 18cbdca20..e2dfbc662 100644 --- a/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java +++ b/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java @@ -201,10 +201,7 @@ public class FileMetadataRepository facetIds.addAll( Arrays.asList( properties.getProperty( "facetIds", "" ).split( "," ) ) ); properties.setProperty( "facetIds", join( facetIds ) ); - for ( MetadataFacet facet : versionMetadata.getFacetList() ) - { - properties.putAll( facet.toProperties() ); - } + updateProjectVersionFacets( versionMetadata, properties ); try { @@ -217,6 +214,17 @@ public class FileMetadataRepository } } + private void updateProjectVersionFacets( ProjectVersionMetadata versionMetadata, Properties properties ) + { + for ( MetadataFacet facet : versionMetadata.getFacetList() ) + { + for ( Map.Entry entry : facet.toProperties().entrySet() ) + { + properties.setProperty( facet.getFacetId() + ":" + entry.getKey(), entry.getValue() ); + } + } + } + public void updateProjectReference( String repoId, String namespace, String projectId, String projectVersion, ProjectVersionReference reference ) { @@ -423,7 +431,6 @@ public class FileMetadataRepository String id = tok.nextToken(); ArtifactMetadata artifact = artifacts.get( id ); - // TODO: facets (&test) if ( artifact == null ) { artifact = new ArtifactMetadata(); @@ -460,11 +467,58 @@ public class FileMetadataRepository { artifact.setSha1( value ); } + else if ( "facetIds".equals( field ) ) + { + if ( value.length() > 0 ) + { + String propertyPrefix = "artifact:facet:" + id + ":"; + for ( String facetId : value.split( "," ) ) + { + MetadataFacetFactory factory = metadataFacetFactories.get( facetId ); + if ( factory == null ) + { + log.error( "Attempted to load unknown artifact metadata facet: " + facetId ); + } + else + { + MetadataFacet facet = factory.createMetadataFacet(); + String prefix = propertyPrefix + facet.getFacetId(); + Map map = new HashMap(); + for ( Object key : new ArrayList( properties.keySet() ) ) + { + String property = (String) key; + if ( property.startsWith( prefix ) ) + { + map.put( property.substring( prefix.length() + 1 ), properties.getProperty( + property ) ); + } + } + facet.fromProperties( map ); + artifact.addFacet( facet ); + } + } + } + + updateArtifactFacets( artifact, properties ); + } } } return artifacts.values(); } + private void updateArtifactFacets( ArtifactMetadata artifact, Properties properties ) + { + String propertyPrefix = "artifact:facet:" + artifact.getId() + ":"; + for ( MetadataFacet facet : artifact.getFacetList() ) + { + for ( Map.Entry e : facet.toProperties().entrySet() ) + { + String key = propertyPrefix + facet.getFacetId() + ":" + e.getKey(); + properties.setProperty( key, e.getValue() ); + } + } + } + public Collection getRepositories() { return configuration.getConfiguration().getManagedRepositoriesAsMap().keySet(); @@ -497,6 +551,17 @@ public class FileMetadataRepository properties.remove( "artifact:md5:" + id ); properties.remove( "artifact:sha1:" + id ); properties.remove( "artifact:version:" + id ); + properties.remove( "artifact:facetIds:" + id ); + + String prefix = "artifact:facet:" + id + ":"; + for ( Object key : new ArrayList( properties.keySet() ) ) + { + String property = (String) key; + if ( property.startsWith( prefix ) ) + { + properties.remove( property ); + } + } try { @@ -580,20 +645,26 @@ public class FileMetadataRepository Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - properties.setProperty( "artifact:updated:" + artifact.getId(), Long.toString( - artifact.getFileLastModified().getTime() ) ); - properties.setProperty( "artifact:whenGathered:" + artifact.getId(), Long.toString( - artifact.getWhenGathered().getTime() ) ); - properties.setProperty( "artifact:size:" + artifact.getId(), Long.toString( artifact.getSize() ) ); + String id = artifact.getId(); + properties.setProperty( "artifact:updated:" + id, Long.toString( artifact.getFileLastModified().getTime() ) ); + properties.setProperty( "artifact:whenGathered:" + id, Long.toString( artifact.getWhenGathered().getTime() ) ); + properties.setProperty( "artifact:size:" + id, Long.toString( artifact.getSize() ) ); if ( artifact.getMd5() != null ) { - properties.setProperty( "artifact:md5:" + artifact.getId(), artifact.getMd5() ); + properties.setProperty( "artifact:md5:" + id, artifact.getMd5() ); } if ( artifact.getSha1() != null ) { - properties.setProperty( "artifact:sha1:" + artifact.getId(), artifact.getSha1() ); + properties.setProperty( "artifact:sha1:" + id, artifact.getSha1() ); } - properties.setProperty( "artifact:version:" + artifact.getId(), artifact.getVersion() ); + properties.setProperty( "artifact:version:" + id, artifact.getVersion() ); + + Set facetIds = new LinkedHashSet( artifact.getFacetIds() ); + String property = "artifact:facetIds:" + id; + facetIds.addAll( Arrays.asList( properties.getProperty( property, "" ).split( "," ) ) ); + properties.setProperty( property, join( facetIds ) ); + + updateArtifactFacets( artifact, properties ); try { @@ -790,7 +861,7 @@ public class FileMetadataRepository MetadataFacetFactory factory = metadataFacetFactories.get( facetId ); if ( factory == null ) { - log.error( "Attempted to load unknown metadata facet: " + facetId ); + log.error( "Attempted to load unknown project version metadata facet: " + facetId ); } else { @@ -801,7 +872,8 @@ public class FileMetadataRepository String property = (String) key; if ( property.startsWith( facet.getFacetId() ) ) { - map.put( property, properties.getProperty( property ) ); + map.put( property.substring( facet.getFacetId().length() + 1 ), properties.getProperty( + property ) ); } } facet.fromProperties( map ); @@ -810,10 +882,7 @@ public class FileMetadataRepository } } - for ( MetadataFacet facet : versionMetadata.getFacetList() ) - { - properties.putAll( facet.toProperties() ); - } + updateProjectVersionFacets( versionMetadata, properties ); } return versionMetadata; } diff --git a/archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java b/archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java index 8f48d484c..2e41424d2 100644 --- a/archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java +++ b/archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java @@ -19,18 +19,6 @@ package org.apache.archiva.metadata.repository.file; * under the License. */ -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; - import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.model.MailingList; import org.apache.archiva.metadata.model.MetadataFacet; @@ -44,6 +32,18 @@ import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.codehaus.plexus.spring.PlexusInSpringTestCase; import org.easymock.MockControl; +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; + /** * @todo should this be a generic MetadataRepository implementation test? */ @@ -198,6 +198,40 @@ public class FileMetadataRepositoryTest assertEquals( Collections.emptyList(), new ArrayList( metadata.getFacetIds() ) ); } + public void testUpdateArtifactMetadataWithExistingFacets() + { + ArtifactMetadata metadata = createArtifact(); + MetadataFacet facet = new TestMetadataFacet( "baz" ); + metadata.addFacet( facet ); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); + + metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).iterator().next(); + assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() ); + + metadata = createArtifact(); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); + + metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).iterator().next(); + assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() ); + TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID ); + assertEquals( "baz", testFacet.getValue() ); + } + + public void testUpdateArtifactMetadataWithNoExistingFacets() + { + ArtifactMetadata metadata = createArtifact(); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); + + metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).iterator().next(); + assertEquals( Collections.emptyList(), new ArrayList( metadata.getFacetIds() ) ); + + metadata = createArtifact(); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata ); + + metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).iterator().next(); + assertEquals( Collections.emptyList(), new ArrayList( metadata.getFacetIds() ) ); + } + public void testGetMetadataFacet() { repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) ); @@ -583,7 +617,7 @@ public class FileMetadataRepositoryTest { public final int compare ( ArtifactMetadata a, ArtifactMetadata b) { - return ( (String) a.getProject() ).compareTo( (String) b.getProject() ); + return a.getProject().compareTo( b.getProject() ); } } @@ -620,7 +654,7 @@ public class FileMetadataRepositoryTest { if ( value != null ) { - return Collections.singletonMap( testFacetId + ":foo", value ); + return Collections.singletonMap( "foo", value ); } else { @@ -630,7 +664,7 @@ public class FileMetadataRepositoryTest public void fromProperties( Map properties ) { - String value = properties.get( testFacetId + ":foo" ); + String value = properties.get( "foo" ); if ( value != null ) { this.value = value;