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 );
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 );
--- /dev/null
+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
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;
}
}
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
{
}
}
+ 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 )
{
String id = tok.nextToken();
ArtifactMetadata artifact = artifacts.get( id );
- // TODO: facets (&test)
if ( artifact == null )
{
artifact = new ArtifactMetadata();
{
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();
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
{
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
{
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
{
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 );
}
}
- for ( MetadataFacet facet : versionMetadata.getFacetList() )
- {
- properties.putAll( facet.toProperties() );
- }
+ updateProjectVersionFacets( versionMetadata, properties );
}
return versionMetadata;
}
* 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;
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?
*/
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 ) );
{
public final int compare ( ArtifactMetadata a, ArtifactMetadata b)
{
- return ( (String) a.getProject() ).compareTo( (String) b.getProject() );
+ return a.getProject().compareTo( b.getProject() );
}
}
{
if ( value != null )
{
- return Collections.singletonMap( testFacetId + ":foo", value );
+ return Collections.singletonMap( "foo", value );
}
else
{
public void fromProperties( Map<String, String> properties )
{
- String value = properties.get( testFacetId + ":foo" );
+ String value = properties.get( "foo" );
if ( value != null )
{
this.value = value;