Added factory for ArtifactRepositoryIndex

Removed usage of src/test/index createTestIndex (renamed from testIndex() which gave the wrong impression) will create the index needed

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@365590 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Edwin L. Punzalan 2006-01-03 09:34:00 +00:00
parent aadae96321
commit c4a2df22e5
12 changed files with 128 additions and 50 deletions

View File

@ -18,7 +18,7 @@ package org.apache.maven.repository.indexing;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.apache.maven.artifact.repository.ArtifactRepository;
import java.io.File;
import java.io.IOException;
@ -30,17 +30,18 @@ import java.util.Collection;
* @author Edwin Punzalan
*/
public abstract class AbstractRepositoryIndex
extends AbstractLogEnabled
implements RepositoryIndex
{
private String indexPath;
private boolean indexOpen;
private IndexReader indexReader;
private IndexWriter indexWriter;
protected ArtifactRepository repository;
/**
* method to encapsulate the optimize() method for lucene
*/
@ -103,7 +104,7 @@ public abstract class AbstractRepositoryIndex
/**
* method for opening the index directory for indexing operations
*/
public void open( String indexPath )
protected void open( String indexPath )
throws RepositoryIndexException
{
try
@ -169,13 +170,13 @@ public abstract class AbstractRepositoryIndex
}
else
{
getLogger().info( "Skipping index field validations for empty index." );
//getLogger().info( "Skipping index field validations for empty index." );
}
}
else if ( !indexDir.exists() )
{
indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
getLogger().info( "New index directory created in: " + indexDir.getAbsolutePath() );
//getLogger().info( "New index directory created in: " + indexDir.getAbsolutePath() );
}
else if ( indexDir.isDirectory() )
{

View File

@ -30,13 +30,13 @@ import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.apache.maven.artifact.repository.ArtifactRepository;
/**
* Class used to index Artifact objects in a specified repository
*
* @author Edwin Punzalan
* @plexus.component role="org.apache.maven.repository.indexing.RepositoryIndex" role-hint="artifact" instantiation-strategy="per-lookup"
* @todo I think we should merge with Abstract*. Don't see that there'd be multiple implementations based on this
* @todo I think we should instantiate this based on a repository from a factory instead of making it a component of its own
*/
@ -65,9 +65,17 @@ public class ArtifactRepositoryIndex
private Analyzer analyzer;
/** @plexus.requirement */
private Digester digester;
public ArtifactRepositoryIndex( String indexPath, ArtifactRepository repository, Digester digester )
throws RepositoryIndexException
{
this.repository = repository;
this.digester = digester;
open( indexPath );
}
/**
* method to get the Analyzer used to create indices
*

View File

@ -84,6 +84,8 @@ public class ArtifactRepositoryIndexSearcher
artifactList.add( artifact );
}
searcher.close();
}
catch ( IOException e )
{

View File

@ -0,0 +1,44 @@
package org.apache.maven.repository.indexing;
/*
* 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 org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.digest.Digester;
/**
*
* @author Edwin Punzalan
* @plexus.component role="org.apache.maven.repository.indexing.RepositoryIndexingFactory"
*/
public class DefaultRepositoryIndexingFactory
implements RepositoryIndexingFactory
{
/** @plexus.requirement */
private Digester digester;
public ArtifactRepositoryIndexSearcher createArtifactRepositoryIndexSearcher(ArtifactRepositoryIndex index)
{
return null;
}
public ArtifactRepositoryIndex createArtifactRepositoryIndex( String indexPath, ArtifactRepository repository )
throws RepositoryIndexException
{
return new ArtifactRepositoryIndex( indexPath, repository, digester );
}
}

View File

@ -35,9 +35,9 @@ public interface RepositoryIndex
void close()
throws RepositoryIndexException;
void open( String indexPath )
/* void open( String indexPath )
throws RepositoryIndexException;
*/
void optimize()
throws RepositoryIndexException;

View File

@ -32,24 +32,7 @@ public interface RepositoryIndexSearcher
* @param index
* @param queryString
* @param searchField
*/
List search( RepositoryIndex index, String queryString, String searchField )
throws RepositoryIndexSearchException;
*/
/**
*
*/
void addQuery( String queryField, String queryText );
/**
*
*/
void addQuery( String queryField, String queryText, boolean required );
/**
*
*/
List search( RepositoryIndex index )
throws RepositoryIndexSearchException;
}

View File

@ -0,0 +1,34 @@
package org.apache.maven.repository.indexing;
/*
* 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 org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.digest.Digester;
/**
*
* @author Edwin Punzalan
*/
public interface RepositoryIndexingFactory
{
String ROLE = RepositoryIndexingFactory.class.getName();
ArtifactRepositoryIndexSearcher createArtifactRepositoryIndexSearcher( ArtifactRepositoryIndex index );
ArtifactRepositoryIndex createArtifactRepositoryIndex( String indexPath, ArtifactRepository repository )
throws RepositoryIndexException;
}

View File

@ -63,19 +63,18 @@ public class ArtifactRepositoryIndexingTest
repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
indexPath = "target/index";
FileUtils.deleteDirectory( indexPath );
}
public void testIndexerExceptions()
throws Exception
{
ArtifactRepositoryIndex indexer;
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
try
{
String notIndexDir = new File( "pom.xml" ).getAbsolutePath();
indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
indexer.open( notIndexDir );
indexer = factory.createArtifactRepositoryIndex( notIndexDir, repository );
fail( "Must throw exception on non-directory index directory" );
}
catch ( RepositoryIndexException e )
@ -86,8 +85,7 @@ public class ArtifactRepositoryIndexingTest
try
{
String notIndexDir = new File( "" ).getAbsolutePath();
indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
indexer.open( notIndexDir );
indexer = factory.createArtifactRepositoryIndex( notIndexDir, repository );
fail( "Must throw an exception on a non-index directory" );
}
catch ( RepositoryIndexException e )
@ -95,12 +93,12 @@ public class ArtifactRepositoryIndexingTest
//expected
}
//indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository );
//indexer.close();
indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
indexer.close();
try
{
indexer.indexArtifact( artifact );
@ -121,7 +119,7 @@ public class ArtifactRepositoryIndexingTest
//expected
}
indexer.open( indexPath );
indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
try
{
@ -136,12 +134,11 @@ public class ArtifactRepositoryIndexingTest
indexer.close();
}
public void testIndex()
public void createTestIndex()
throws Exception
{
//indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository );
ArtifactRepositoryIndex indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
indexer.open( indexPath );
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
@ -151,23 +148,21 @@ public class ArtifactRepositoryIndexingTest
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.indexArtifact( artifact );
indexer.optimize();
indexer.close();
indexer.open( indexPath );
artifact = getArtifact( "test", "test-artifactId", "1.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.index( artifact );
indexer.close();
// TODO: assert something!
indexer.optimize();
indexer.close();
}
public void testSearch()
throws Exception
{
ArtifactRepositoryIndex indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
indexer.open( getTestPath( "src/test/index" ) );
createTestIndex();
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
ArtifactRepositoryIndex indexer = indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
RepositoryIndexSearcher repoSearcher =
(RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE, "artifact" );
@ -198,6 +193,8 @@ public class ArtifactRepositoryIndexingTest
artifacts = repoSearcher.search( indexer, "2", VERSION );
assertEquals( 2, artifacts.size() );
indexer.close();
}
private Artifact getArtifact( String groupId, String artifactId, String version )
@ -210,4 +207,13 @@ public class ArtifactRepositoryIndexingTest
return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
}
protected void tearDown()
throws Exception
{
repository = null;
FileUtils.deleteDirectory( indexPath );
super.tearDown();
}
}