]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1360] store artifact metadata in the repository and make maven facet's compliant
authorBrett Porter <brett@apache.org>
Thu, 11 Mar 2010 02:40:39 +0000 (02:40 +0000)
committerBrett Porter <brett@apache.org>
Thu, 11 Mar 2010 02:40:39 +0000 (02:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@921671 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacet.java
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacetFactory.java [new file with mode: 0644]
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenProjectFacet.java
archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java
archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java

index 1fc43012b3992b2346f82463193435346a999037..611861cd782ee91027972554bc962ecbb83362f4 100644 (file)
@@ -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 );
index f24ca0cc08df11c7826cf460243e5c426c1307b8..5dbb2657e26917b447dde80dead2e9195953e85c 100644 (file)
@@ -91,19 +91,28 @@ public class MavenArtifactFacet
     public Map<String, String> toProperties()
     {
         HashMap<String, String> properties = new HashMap<String, String>();
-        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<String, String> 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 (file)
index 0000000..002eaf7
--- /dev/null
@@ -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
index 93df5a1e117e22887dffa11a7db15941d757125d..83fe1693e710d9ce275b4d8c10fee880b24c3fd2 100644 (file)
@@ -91,30 +91,30 @@ public class MavenProjectFacet
     public Map<String, String> toProperties()
     {
         HashMap<String, String> properties = new HashMap<String, String>();
-        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<String, String> 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;
         }
     }
index 18cbdca20808838145152da52ff1bc03cadc39ef..e2dfbc662db4aad6244a664c6e165bdffe7aab85 100644 (file)
@@ -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<String,String> 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<String, String> map = new HashMap<String, String>();
+                                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<String, String> e : facet.toProperties().entrySet() )
+            {
+                String key = propertyPrefix + facet.getFacetId() + ":" + e.getKey();
+                properties.setProperty( key, e.getValue() );
+            }
+        }
+    }
+
     public Collection<String> 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<String> facetIds = new LinkedHashSet<String>( 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;
     }
index 8f48d484c9ebf764e08a391c463db511dc76444d..2e41424d27d5995527de27df375327e16aab18de 100644 (file)
@@ -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.<String>emptyList(), new ArrayList<String>( 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.<String>emptyList(), new ArrayList<String>( 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.<String>emptyList(), new ArrayList<String>( 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<String, String> properties )
         {
-            String value = properties.get( testFacetId + ":foo" );
+            String value = properties.get( "foo" );
             if ( value != null )
             {
                 this.value = value;