[MRM-1300] move search and find artifact action over to metadata repository for XMLRPC

git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@889203 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2009-12-10 11:36:51 +00:00
parent a08431ea1a
commit 9b2c646f75
2 changed files with 91 additions and 117 deletions

View File

@ -28,43 +28,36 @@ import org.apache.archiva.indexer.search.RepositorySearch;
import org.apache.archiva.indexer.search.SearchResultHit; import org.apache.archiva.indexer.search.SearchResultHit;
import org.apache.archiva.indexer.search.SearchResultLimits; import org.apache.archiva.indexer.search.SearchResultLimits;
import org.apache.archiva.indexer.search.SearchResults; import org.apache.archiva.indexer.search.SearchResults;
import org.apache.archiva.metadata.model.ArtifactMetadata;
import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.archiva.metadata.model.ProjectVersionReference; import org.apache.archiva.metadata.model.ProjectVersionReference;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataResolver; import org.apache.archiva.metadata.repository.MetadataResolver;
import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet; import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
import org.apache.archiva.web.xmlrpc.api.SearchService; import org.apache.archiva.web.xmlrpc.api.SearchService;
import org.apache.archiva.web.xmlrpc.api.beans.Artifact; import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
import org.apache.archiva.web.xmlrpc.api.beans.Dependency; import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories; import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
import org.apache.maven.archiva.common.utils.VersionUtil; import org.apache.maven.archiva.repository.content.ArtifactExtensionMapping;
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
import org.apache.maven.archiva.model.ArchivaArtifact;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SearchServiceImpl public class SearchServiceImpl
implements SearchService implements SearchService
{ {
private Logger log = LoggerFactory.getLogger( SearchServiceImpl.class );
private RepositorySearch search; private RepositorySearch search;
private XmlRpcUserRepositories xmlRpcUserRepositories; private XmlRpcUserRepositories xmlRpcUserRepositories;
private ArchivaDAO archivaDAO;
private MetadataResolver metadataResolver; private MetadataResolver metadataResolver;
public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO, private MetadataRepository metadataRepository;
MetadataResolver metadataResolver, RepositorySearch search )
public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, MetadataResolver metadataResolver,
MetadataRepository metadataRepository, RepositorySearch search )
{ {
this.xmlRpcUserRepositories = xmlRpcUserRepositories; this.xmlRpcUserRepositories = xmlRpcUserRepositories;
this.archivaDAO = archivaDAO;
this.search = search; this.search = search;
this.metadataResolver = metadataResolver; this.metadataResolver = metadataResolver;
this.metadataRepository = metadataRepository;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -74,21 +67,12 @@ public class SearchServiceImpl
List<Artifact> artifacts = new ArrayList<Artifact>(); List<Artifact> artifacts = new ArrayList<Artifact>();
List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories(); List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES ); SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
SearchResults results = null; SearchResults results;
results = search.search( "", observableRepos, queryString, limits, null ); results = search.search( "", observableRepos, queryString, limits, null );
for ( SearchResultHit resultHit : results.getHits() ) for ( SearchResultHit resultHit : results.getHits() )
{ {
// double-check all versions as done in SearchAction
final List<String> versions = (List<String>) archivaDAO.query(
new UniqueVersionConstraint( observableRepos, resultHit.getGroupId(), resultHit.getArtifactId() ) );
if ( versions != null && !versions.isEmpty() )
{
resultHit.setVersion( null );
resultHit.setVersions( filterTimestampedSnapshots( versions ) );
}
List<String> resultHitVersions = resultHit.getVersions(); List<String> resultHitVersions = resultHit.getVersions();
if ( resultHitVersions != null ) if ( resultHitVersions != null )
{ {
@ -130,47 +114,40 @@ public class SearchServiceImpl
return artifacts; return artifacts;
} }
/**
* Remove timestamped snapshots from versions
*/
private static List<String> filterTimestampedSnapshots( List<String> versions )
{
final List<String> filtered = new ArrayList<String>();
for ( final String version : versions )
{
final String baseVersion = VersionUtil.getBaseVersion( version );
if ( !filtered.contains( baseVersion ) )
{
filtered.add( baseVersion );
}
}
return filtered;
}
public List<Artifact> getArtifactByChecksum( String checksum ) public List<Artifact> getArtifactByChecksum( String checksum )
throws Exception throws Exception
{ {
// 1. get ArtifactDAO from ArchivaDAO List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
// 2. create ArtifactsByChecksumConstraint( "queryTerm" )
// 3. query artifacts using constraint
// 4. convert results to list of Artifact objects
List<Artifact> results = new ArrayList<Artifact>(); List<Artifact> results = new ArrayList<Artifact>();
ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO(); for ( String repoId : observableRepos )
ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum );
List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint );
for ( ArchivaArtifact archivaArtifact : artifacts )
{ {
Artifact artifact = for ( ArtifactMetadata artifact : metadataRepository.getArtifactsByChecksum( repoId, checksum ) )
new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(), {
archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(), // TODO: use a maven facet instead, for now just using the extension and classifier
archivaArtifact.getType() ); String type = null;
//archivaArtifact.getModel().getWhenGathered() ); String key = artifact.getProject() + "-" + artifact.getVersion();
results.add( artifact ); String filename = artifact.getId();
int extIndex = filename.lastIndexOf( "." );
if ( filename.startsWith( key ) )
{
int i = key.length();
char nextToken = filename.charAt( i );
if ( nextToken == '-' )
{
String classifier = filename.substring( i + 1, extIndex );
String extension = filename.substring( extIndex + 1 );
type = ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension );
}
else if ( nextToken == '.' )
{
type = ArtifactExtensionMapping.mapExtensionToType( filename.substring( i + 1 ) );
}
}
results.add( new Artifact( artifact.getRepositoryId(), artifact.getNamespace(), artifact.getProject(),
artifact.getVersion(), type ) );
}
} }
return results; return results;
} }
@ -198,11 +175,11 @@ public class SearchServiceImpl
public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since ) public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
throws Exception throws Exception
{ {
List<Artifact> artifacts = new ArrayList<Artifact>(); // List<Artifact> artifacts = new ArrayList<Artifact>();
// 1. get observable repositories // 1. get observable repositories
// 2. use RepositoryBrowsing method to query uniqueVersions? (but with date) // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
throw new UnsupportedOperationException( "getArtifactVersionsByDate not yet implemented" ); throw new UnsupportedOperationException( "getArtifactVersionsByDate not yet implemented" );
// return artifacts; // return artifacts;
@ -236,8 +213,8 @@ public class SearchServiceImpl
public List<Artifact> getDependencyTree( String groupId, String artifactId, String version ) public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
throws Exception throws Exception
{ {
List<Artifact> a = new ArrayList<Artifact>(); // List<Artifact> a = new ArrayList<Artifact>();
throw new UnsupportedOperationException( "getDependencyTree not yet implemented" ); throw new UnsupportedOperationException( "getDependencyTree not yet implemented" );
// return a; // return a;
} }

View File

@ -31,19 +31,16 @@ import org.apache.archiva.indexer.search.SearchResultHit;
import org.apache.archiva.indexer.search.SearchResultLimits; import org.apache.archiva.indexer.search.SearchResultLimits;
import org.apache.archiva.indexer.search.SearchResults; import org.apache.archiva.indexer.search.SearchResults;
import org.apache.archiva.indexer.util.SearchUtil; import org.apache.archiva.indexer.util.SearchUtil;
import org.apache.archiva.metadata.model.ArtifactMetadata;
import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.archiva.metadata.model.ProjectVersionReference; import org.apache.archiva.metadata.model.ProjectVersionReference;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataResolver; import org.apache.archiva.metadata.repository.MetadataResolver;
import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet; import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
import org.apache.archiva.web.xmlrpc.api.SearchService; import org.apache.archiva.web.xmlrpc.api.SearchService;
import org.apache.archiva.web.xmlrpc.api.beans.Artifact; import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
import org.apache.archiva.web.xmlrpc.api.beans.Dependency; import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories; import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
import org.apache.maven.archiva.model.ArchivaArtifact;
import org.codehaus.plexus.spring.PlexusInSpringTestCase; import org.codehaus.plexus.spring.PlexusInSpringTestCase;
import org.easymock.MockControl; import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl; import org.easymock.classextension.MockClassControl;
@ -66,14 +63,6 @@ public class SearchServiceImplTest
private RepositorySearch search; private RepositorySearch search;
private MockControl archivaDAOControl;
private ArchivaDAO archivaDAO;
private MockControl artifactDAOControl;
private ArtifactDAO artifactDAO;
private static final String ARCHIVA_TEST_ARTIFACT_ID = "archiva-xmlrpc-test"; private static final String ARCHIVA_TEST_ARTIFACT_ID = "archiva-xmlrpc-test";
private static final String ARCHIVA_TEST_GROUP_ID = "org.apache.archiva"; private static final String ARCHIVA_TEST_GROUP_ID = "org.apache.archiva";
@ -82,6 +71,14 @@ public class SearchServiceImplTest
private MetadataResolver metadataResolver; private MetadataResolver metadataResolver;
private MockControl metadataRepositoryControl;
private MetadataRepository metadataRepository;
private static final String CHECKSUM = "a1b2c3aksjhdasfkdasasd";
private static final String TEST_REPO = "test-repo";
@Override @Override
public void setUp() public void setUp()
throws Exception throws Exception
@ -89,10 +86,6 @@ public class SearchServiceImplTest
userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class ); userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
userRepos = (XmlRpcUserRepositories) userReposControl.getMock(); userRepos = (XmlRpcUserRepositories) userReposControl.getMock();
archivaDAOControl = MockControl.createControl( ArchivaDAO.class );
archivaDAOControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
archivaDAO = (ArchivaDAO) archivaDAOControl.getMock();
searchControl = MockControl.createControl( RepositorySearch.class ); searchControl = MockControl.createControl( RepositorySearch.class );
searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER ); searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
search = (RepositorySearch) searchControl.getMock(); search = (RepositorySearch) searchControl.getMock();
@ -100,13 +93,14 @@ public class SearchServiceImplTest
metadataResolverControl = MockControl.createControl( MetadataResolver.class ); metadataResolverControl = MockControl.createControl( MetadataResolver.class );
metadataResolver = (MetadataResolver) metadataResolverControl.getMock(); metadataResolver = (MetadataResolver) metadataResolverControl.getMock();
artifactDAOControl = MockControl.createControl( ArtifactDAO.class ); metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
artifactDAO = (ArtifactDAO) artifactDAOControl.getMock(); metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
searchService = new SearchServiceImpl( userRepos, archivaDAO, metadataResolver, search ); searchService = new SearchServiceImpl( userRepos, metadataResolver, metadataRepository, search );
} }
// MRM-1230 // MRM-1230
public void testQuickSearchModelPackagingIsUsed() public void testQuickSearchModelPackagingIsUsed()
throws Exception throws Exception
{ {
@ -133,10 +127,6 @@ public class SearchServiceImplTest
searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
results ); results );
archivaDAOControl.expectAndReturn( archivaDAO.query(
new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
null );
ProjectVersionMetadata model = new ProjectVersionMetadata(); ProjectVersionMetadata model = new ProjectVersionMetadata();
model.setId( "1.0" ); model.setId( "1.0" );
MavenProjectFacet facet = new MavenProjectFacet(); MavenProjectFacet facet = new MavenProjectFacet();
@ -150,14 +140,14 @@ public class SearchServiceImplTest
userReposControl.replay(); userReposControl.replay();
searchControl.replay(); searchControl.replay();
metadataResolverControl.replay(); metadataResolverControl.replay();
archivaDAOControl.replay(); metadataRepositoryControl.replay();
List<Artifact> artifacts = searchService.quickSearch( "archiva" ); List<Artifact> artifacts = searchService.quickSearch( "archiva" );
userReposControl.verify(); userReposControl.verify();
searchControl.verify(); searchControl.verify();
metadataResolverControl.verify(); metadataResolverControl.verify();
archivaDAOControl.verify(); metadataRepositoryControl.verify();
assertNotNull( artifacts ); assertNotNull( artifacts );
assertEquals( 1, artifacts.size() ); assertEquals( 1, artifacts.size() );
@ -197,10 +187,6 @@ public class SearchServiceImplTest
searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
results ); results );
archivaDAOControl.expectAndReturn( archivaDAO.query(
new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
null );
metadataResolverControl.expectAndReturn( metadataResolverControl.expectAndReturn(
metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
"1.0" ), null ); "1.0" ), null );
@ -214,14 +200,14 @@ public class SearchServiceImplTest
userReposControl.replay(); userReposControl.replay();
searchControl.replay(); searchControl.replay();
metadataResolverControl.replay(); metadataResolverControl.replay();
archivaDAOControl.replay(); metadataRepositoryControl.replay();
List<Artifact> artifacts = searchService.quickSearch( "archiva" ); List<Artifact> artifacts = searchService.quickSearch( "archiva" );
userReposControl.verify(); userReposControl.verify();
searchControl.verify(); searchControl.verify();
metadataResolverControl.verify(); metadataResolverControl.verify();
archivaDAOControl.verify(); metadataRepositoryControl.verify();
assertNotNull( artifacts ); assertNotNull( artifacts );
assertEquals( 1, artifacts.size() ); assertEquals( 1, artifacts.size() );
@ -255,10 +241,6 @@ public class SearchServiceImplTest
results.addHit( SearchUtil.getHitId( resultHit.getGroupId(), resultHit.getArtifactId() ), resultHit ); results.addHit( SearchUtil.getHitId( resultHit.getGroupId(), resultHit.getArtifactId() ), resultHit );
archivaDAOControl.expectAndReturn( archivaDAO.query(
new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
null );
SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES ); SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
@ -276,14 +258,14 @@ public class SearchServiceImplTest
userReposControl.replay(); userReposControl.replay();
searchControl.replay(); searchControl.replay();
archivaDAOControl.replay(); metadataRepositoryControl.replay();
metadataResolverControl.replay(); metadataResolverControl.replay();
List<Artifact> artifacts = searchService.quickSearch( "archiva" ); List<Artifact> artifacts = searchService.quickSearch( "archiva" );
userReposControl.verify(); userReposControl.verify();
searchControl.verify(); searchControl.verify();
archivaDAOControl.verify(); metadataRepositoryControl.verify();
metadataResolverControl.verify(); metadataResolverControl.verify();
assertNotNull( artifacts ); assertNotNull( artifacts );
@ -327,30 +309,31 @@ public class SearchServiceImplTest
public void testGetArtifactByChecksum() public void testGetArtifactByChecksum()
throws Exception throws Exception
{ {
userReposControl.expectAndReturn( userRepos.getObservableRepositories(),
Collections.singletonList( TEST_REPO ) );
Date whenGathered = new Date(); Date whenGathered = new Date();
ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( "a1b2c3aksjhdasfkdasasd" ); ArtifactMetadata artifact = createArtifact( whenGathered );
List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>(); metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, CHECKSUM ),
ArchivaArtifact artifact = Collections.singletonList( artifact ) );
new ArchivaArtifact( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "", "jar", "test-repo" );
artifact.getModel().setWhenGathered( whenGathered );
artifacts.add( artifact );
archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO ); metadataRepositoryControl.replay();
artifactDAO.queryArtifacts( constraint ); userReposControl.replay();
artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER );
artifactDAOControl.setReturnValue( artifacts );
archivaDAOControl.replay(); List<Artifact> results = searchService.getArtifactByChecksum( CHECKSUM );
artifactDAOControl.replay();
List<Artifact> results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" ); metadataRepositoryControl.verify();
userReposControl.verify();
archivaDAOControl.verify();
artifactDAOControl.verify();
assertNotNull( results ); assertNotNull( results );
assertEquals( 1, results.size() ); assertEquals( 1, results.size() );
Artifact result = results.get( 0 );
assertEquals( ARCHIVA_TEST_GROUP_ID, result.getGroupId() );
assertEquals( ARCHIVA_TEST_ARTIFACT_ID, result.getArtifactId() );
assertEquals( "1.0", result.getVersion() );
assertEquals( "jar", result.getType() );
assertEquals( TEST_REPO, result.getRepositoryId() );
} }
public void testGetArtifactVersionsArtifactExists() public void testGetArtifactVersionsArtifactExists()
@ -553,4 +536,18 @@ public class SearchServiceImplTest
assertTrue( searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ).isEmpty() ); assertTrue( searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ).isEmpty() );
} }
private ArtifactMetadata createArtifact( Date whenGathered )
{
String version = "1.0";
ArtifactMetadata artifactMetadata = new ArtifactMetadata();
artifactMetadata.setVersion( version );
artifactMetadata.setId( ARCHIVA_TEST_ARTIFACT_ID + "-" + version + ".jar" );
artifactMetadata.setProject( ARCHIVA_TEST_ARTIFACT_ID );
artifactMetadata.setNamespace( ARCHIVA_TEST_GROUP_ID );
artifactMetadata.setMd5( CHECKSUM );
artifactMetadata.setWhenGathered( whenGathered );
artifactMetadata.setRepositoryId( TEST_REPO );
return artifactMetadata;
}
} }