* limitations under the License.
*/
+import java.io.File;
import java.io.IOException;
import java.util.Collection;
{
if ( indexWriter == null )
{
- indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
+ indexWriter = new IndexWriter( indexPath, getAnalyzer(), false );
}
}
protected void validateIndex()
throws RepositoryIndexerException
{
- indexOpen = true;
- if ( true ) return;
- try
+ File indexDir = new File( indexPath );
+ if ( indexDir.exists() )
{
- getIndexReader();
- Collection fields = indexReader.getFieldNames();
- String[] indexFields = getIndexFields();
- for( int idx=0; idx<indexFields.length; idx++ )
+ if ( indexDir.isDirectory() )
{
- if ( !fields.contains( indexFields[ idx ] ) )
+ if ( indexDir.listFiles().length > 1 )
+ {
+ try
+ {
+ getIndexReader();
+ Collection fields = indexReader.getFieldNames();
+ String[] indexFields = getIndexFields();
+ for( int idx=0; idx<indexFields.length; idx++ )
+ {
+ if ( !fields.contains( indexFields[ idx ] ) )
+ {
+ throw new RepositoryIndexerException( "The Field " + indexFields[ idx ] + " does not exist in " +
+ "index path " + indexPath + "." );
+ }
+ }
+ }
+ catch ( IOException e )
+ {
+ throw new RepositoryIndexerException( e );
+ }
+ }
+ else
{
- throw new RepositoryIndexerException( "The Field " + indexFields[ idx ] + " does not exist in " +
- "index path " + indexPath + "." );
+ System.out.println( "Skipping validation of an empty index in: " + indexDir.getAbsolutePath() );
}
}
- indexOpen = true;
+ else
+ {
+ throw new RepositoryIndexerException( "Specified index path is not a directory: " +
+ indexDir.getAbsolutePath() );
+ }
}
- catch ( IOException e )
+ else
{
- throw new RepositoryIndexerException( e );
+ try
+ {
+ indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
+ System.out.println( "New index directory created in: " + indexDir.getAbsolutePath() );
+ }
+ catch( Exception e )
+ {
+ throw new RepositoryIndexerException( e );
+ }
}
+
+ indexOpen = true;
}
}
indexer.close();
}
+ public void testIndexerExceptions()
+ throws Exception
+ {
+ //test closed index
+ try
+ {
+ indexer.close();
+ Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
+ artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
+ indexer.addArtifactIndex( artifact );
+ fail( "Must throw exception on closed index." );
+ }
+ catch( RepositoryIndexerException e )
+ {
+ //expected
+ }
+ }
+
protected Artifact getArtifact( String groupId, String artifactId, String version )
{
return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );