From: Edwin L. Punzalan Date: Tue, 27 Dec 2005 09:17:22 +0000 (+0000) Subject: Removed requirement for a factory and refactored object names and methods X-Git-Tag: archiva-0.9-alpha-1~1041 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=edec66f10e2fc6e6b16ea3944327f515a4c334dd;p=archiva.git Removed requirement for a factory and refactored object names and methods git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@359192 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java new file mode 100644 index 000000000..1ab604622 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java @@ -0,0 +1,185 @@ +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 java.io.File; +import java.io.IOException; +import java.util.Collection; + +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; + +/** + * Abstract class for RepositoryIndexers + * + * @author Edwin Punzalan + */ +public abstract class AbstractRepositoryIndex + implements RepositoryIndex +{ + protected String indexPath; + protected boolean indexOpen; + protected IndexReader indexReader; + protected IndexWriter indexWriter; + + /** + * method to encapsulate the optimize() method for lucene + */ + public void optimize() + throws RepositoryIndexException + { + if ( !isOpen() ) + { + throw new RepositoryIndexException( "Unable to optimize index on a closed index" ); + } + + try + { + indexWriter.optimize(); + } + catch ( IOException ioe ) + { + throw new RepositoryIndexException( "Failed to optimize index", ioe ); + } + } + + /** + * method used to query the index status + * + * @param true if the index is open. + */ + public boolean isOpen() + { + return indexOpen; + } + + /** + * method used to close all open streams to the index directory + */ + public void close() + throws RepositoryIndexException + { + try + { + if ( indexWriter != null ) + { + indexWriter.close(); + indexWriter = null; + } + + if ( indexReader != null ) + { + indexReader.close(); + indexReader = null; + } + + indexOpen = false; + } + catch ( Exception e ) + { + throw new RepositoryIndexException( e ); + } + } + + /** + * method for opening the index directory for indexing operations + */ + public void open( String indexPath ) + throws RepositoryIndexException + { + try + { + this.indexPath = indexPath; + validateIndex(); + } + catch ( IOException e ) + { + throw new RepositoryIndexException( e ); + } + } + + public String getIndexPath() + { + return indexPath; + } + + protected void getIndexWriter() + throws IOException + { + if ( indexWriter == null ) + { + indexWriter = new IndexWriter( indexPath, getAnalyzer(), false ); + } + } + + protected void getIndexReader() + throws IOException + { + if ( indexReader == null ) + { + indexReader = IndexReader.open( indexPath ); + } + } + + /** + * method for validating an index directory + * + * @throws RepositoryIndexException if the given indexPath is not valid for this type of RepositoryIndex + */ + protected void validateIndex() + throws RepositoryIndexException, IOException + { + File indexDir = new File( indexPath ); + if ( IndexReader.indexExists( indexDir ) ) + { + getIndexReader(); + if ( indexReader.numDocs() > 0 ) + { + Collection fields = indexReader.getFieldNames(); + String[] indexFields = getIndexFields(); + for( int idx=0; idx 1 ) - { - try - { - getIndexReader(); - Collection fields = indexReader.getFieldNames(); - String[] indexFields = getIndexFields(); - for( int idx=0; idx 0 ) + { + complete.update( buffer, 0, numRead ); + } + } + while ( numRead != -1 ); + inStream.close(); + + return complete.digest(); + } + + private void initBuffers() + { + classes = new StringBuffer(); + packages = new StringBuffer(); + files = new StringBuffer(); + } + + private void removeBuffers() + { + classes = null; + packages = null; + files = null; + } + + private void processArtifactContents( File artifact ) + throws IOException, ZipException + { + initBuffers(); + ZipFile jar = new ZipFile( artifact ); + for ( Enumeration entries = jar.entries(); entries.hasMoreElements(); ) + { + ZipEntry entry = (ZipEntry) entries.nextElement(); + if ( addIfClassEntry( entry ) ) + { + addClassPackage( entry.getName() ); + } + addFile( entry ); + } + } + + private boolean addIfClassEntry( ZipEntry entry ) + { + boolean isAdded = false; + + String name = entry.getName(); + if( name.endsWith( ".class") ) + { + // TODO verify if class is public or protected + if( name.lastIndexOf( "$" ) == -1) + { + int idx = name.lastIndexOf( '/' ); + if ( idx < 0 ) idx = 0; + String classname = name.substring( idx, name.length() - 6 ); + classes.append( classname ).append( "\n" ); + isAdded = true; + } + } + + return isAdded; + } + + private boolean addClassPackage( String name ) + { + boolean isAdded = false; + + int idx = name.lastIndexOf( '/' ); + if ( idx > 0 ) + { + String packageName = name.substring( 0, idx ).replace( '/', '.' ) + "\n"; + if ( packages.indexOf( packageName ) < 0 ) + { + packages.append( packageName ).append( "\n" ); + } + isAdded = true; + } + + return isAdded; + } + + private boolean addFile( ZipEntry entry ) + { + boolean isAdded = false; + + String name = entry.getName(); + int idx = name.lastIndexOf( '/' ); + if ( idx >= 0 ) + { + name = name.substring( idx + 1 ); + } + + if ( files.indexOf( name + "\n" ) < 0 ) + { + files.append( name ).append( "\n" ); + isAdded = true; + } + + return isAdded; + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexSearcher.java index c552e0f79..801f06ed1 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexSearcher.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexSearcher.java @@ -54,39 +54,11 @@ public class ArtifactRepositoryIndexSearcher private static final String GROUPID = "groupId"; private static final String ARTIFACTID = "artifactId"; private static final String VERSION = "version"; - private static final String JAR_TYPE = "jar"; - private static final String XML_TYPE = "xml"; - private static final String POM_TYPE = "pom"; private IndexSearcher searcher; private ArtifactRepository repository; private ArtifactFactory factory; - /** - * Constructor - * - * @param indexPath - * @param repository - */ - public ArtifactRepositoryIndexSearcher( String indexPath, ArtifactRepository repository ) - { - this.repository = repository; - - try - { - searcher = new IndexSearcher( indexPath ); - } - catch ( IOException ie ) - { - ie.printStackTrace(); - } - } - - protected Analyzer getAnalyzer() - { - return new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() ); - } - /** * Search the artifact that contains the query string in the specified * search field. @@ -95,12 +67,13 @@ public class ArtifactRepositoryIndexSearcher * @param searchField * @return */ - public List searchArtifact( String queryString, String searchField ) + public List search( RepositoryIndex index, String queryString, String searchField ) { - List artifactList = new ArrayList(); + List artifactList = new ArrayList(); try { - QueryParser parser = new QueryParser( searchField, getAnalyzer() ); + searcher = new IndexSearcher( index.getIndexPath() ); + QueryParser parser = new QueryParser( searchField, index.getAnalyzer() ); Query qry = parser.parse( queryString ); Hits hits = searcher.search( qry ); //System.out.println("HITS SIZE --> " + hits.length()); @@ -108,53 +81,15 @@ public class ArtifactRepositoryIndexSearcher for ( int i = 0; i < hits.length(); i++ ) { Document doc = hits.doc( i ); - // System.out.println("==========================="); - // System.out.println("NAME :: " + (String) doc.get(NAME)); - // System.out.println("GROUP ID :: " + (String) - // doc.get(GROUPID)); - // System.out.println("ARTIFACT ID :: " + (String) - // doc.get(ARTIFACTID)); - //System.out.println("VERSION :: " + (String) - // doc.get(VERSION)); - // System.out.println("SHA! :: " + (String) doc.get(SHA1)); - // System.out.println("MD5 :: " + (String) doc.get(MD5)); - // System.out.println("CLASSES :: " + (String) - // doc.get(CLASSES)); - // System.out.println("PACKAGES :: " + (String) - // doc.get(PACKAGES)); - // System.out.println("FILES :: " + (String) doc.get(FILES)); - // System.out.println("==========================="); - - String name = (String) doc.get( NAME ); - String type = ""; - if ( ( name.substring( name.length() - 3 ).toLowerCase() ).equals( JAR_TYPE ) ) - { - type = JAR_TYPE; - } - else if ( ( name.substring( name.length() - 3 ).toLowerCase() ).equals( XML_TYPE ) || - ( name.substring( name.length() - 3 ).toLowerCase() ).equals( POM_TYPE ) ) - { - type = POM_TYPE; - } - - if ( type != null && type.length() > 0 ) - { - ArtifactHandler handler = new DefaultArtifactHandler( type ); - VersionRange version = VersionRange.createFromVersion( (String) doc.get( VERSION ) ); - - Artifact artifact = new DefaultArtifact((String) doc.get( GROUPID ), (String) doc.get( ARTIFACTID ), - version, "compile", type, "", handler ); - - /* - * Artifact artifact = factory.createArtifact((String) - * doc.get(GROUPID), (String) doc.get(ARTIFACTID), (String) - * doc.get(VERSION), "", type); - */ - artifact.setRepository( repository ); - artifact.setFile( new File( repository.getBasedir() + "/" + (String) doc.get( NAME ) ) ); - artifactList.add( artifact ); - } + String groupId = doc.get( GROUPID ); + String artifactId = doc.get( ARTIFACTID ); + String version = doc.get( VERSION ); + String name = doc.get( NAME); + String packaging = name.substring( name.lastIndexOf( '.' ) + 1 ); + Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version, packaging ); + + artifactList.add( artifact ); } } catch ( Exception e ) diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java deleted file mode 100644 index 00b6cc16e..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java +++ /dev/null @@ -1,273 +0,0 @@ -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 java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Collection; -import java.util.Enumeration; -import java.util.zip.ZipEntry; -import java.util.zip.ZipException; -import java.util.zip.ZipFile; - -import org.apache.lucene.document.Document; -import org.apache.lucene.document.Field; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.repository.ArtifactRepository; - -/** - * Class used to index Artifact objects in a specified repository - * - * @author Edwin Punzalan - */ -public class ArtifactRepositoryIndexer - extends AbstractRepositoryIndexer -{ - private static final String NAME = "name"; - private static final String GROUPID = "groupId"; - private static final String ARTIFACTID = "artifactId"; - private static final String VERSION = "version"; - private static final String SHA1 = "sha1"; - private static final String MD5 = "md5"; - private static final String CLASSES = "classes"; - private static final String PACKAGES = "packages"; - private static final String FILES = "files"; - - private static final String[] FIELDS = { NAME, GROUPID, ARTIFACTID, VERSION, SHA1, MD5, - CLASSES, PACKAGES, FILES }; - - private ArtifactRepository repository; - - private StringBuffer classes; - private StringBuffer packages; - private StringBuffer files; - - /** - * Constructor - * @todo change repository to layout ??? - * - * @param repository the repository where the indexed artifacts are located. This is necessary only to distinguish - * between default and legacy directory structure of the artifact location. - * @param path the directory where the index is located or will be created. - */ - public ArtifactRepositoryIndexer( ArtifactRepository repository, String path ) - throws RepositoryIndexerException - { - this.repository = repository; - indexPath = path; - validateIndex(); - } - - /** - * method for collecting the available index fields usable for searching - * - * @return index field names - */ - public String[] getIndexFields() - { - return FIELDS; - } - - /** - * generic method for indexing - * - * @param obj the object to be indexed by this indexer - */ - public void addObjectIndex(Object obj) - throws RepositoryIndexerException - { - if ( obj instanceof Artifact ) - { - addArtifactIndex( (Artifact) obj ); - } - else - { - throw new RepositoryIndexerException( "This instance of indexer cannot index instances of " + - obj.getClass().getName() ); - } - } - - /** - * method to index a given artifact - * - * @param artifact the Artifact object to be indexed - */ - public void addArtifactIndex( Artifact artifact ) - throws RepositoryIndexerException - { - if ( !isOpen() ) - { - throw new RepositoryIndexerException( "Unable to add artifact index on a closed index" ); - } - - try - { - getIndexWriter(); - - processArtifactContents( artifact.getFile() ); - - //@todo should some of these fields be Keyword instead of Text ? - Document doc = new Document(); - doc.add( Field.Text( NAME, repository.pathOf( artifact ) ) ); - doc.add( Field.Text( GROUPID, artifact.getGroupId() ) ); - doc.add( Field.Text( ARTIFACTID, artifact.getArtifactId() ) ); - doc.add( Field.Text( VERSION, artifact.getVersion() ) ); - doc.add( Field.Text( SHA1, getSha1( artifact ) ) ); - doc.add( Field.Text( MD5, getMd5( artifact ) ) ); - doc.add( Field.Text( CLASSES, classes.toString() ) ); - doc.add( Field.Text( PACKAGES, packages.toString() ) ); - doc.add( Field.Text( FILES, files.toString() ) ); - indexWriter.addDocument( doc ); - - removeBuffers(); - } - catch( Exception e ) - { - throw new RepositoryIndexerException( e ); - } - } - - private String getSha1( Artifact artifact ) - throws FileNotFoundException, IOException, NoSuchAlgorithmException - { - FileInputStream fIn = new FileInputStream( artifact.getFile() ); - return new String( getChecksum( fIn, "SHA-1" ) ); - } - - private String getMd5( Artifact artifact ) - throws FileNotFoundException, IOException, NoSuchAlgorithmException - { - FileInputStream fIn = new FileInputStream( artifact.getFile() ); - return new String( getChecksum( fIn, "MD5" ) ); - } - - private byte[] getChecksum( InputStream inStream, String algorithm ) - throws IOException, NoSuchAlgorithmException - { - byte[] buffer = new byte[ 256 ]; - MessageDigest complete = MessageDigest.getInstance( algorithm ); - int numRead; - do - { - numRead = inStream.read( buffer ); - if ( numRead > 0 ) - { - complete.update( buffer, 0, numRead ); - } - } - while ( numRead != -1 ); - inStream.close(); - - return complete.digest(); - } - - private void initBuffers() - { - classes = new StringBuffer(); - packages = new StringBuffer(); - files = new StringBuffer(); - } - - private void removeBuffers() - { - classes = null; - packages = null; - files = null; - } - - private void processArtifactContents( File artifact ) - throws IOException, ZipException - { - initBuffers(); - ZipFile jar = new ZipFile( artifact ); - for ( Enumeration entries = jar.entries(); entries.hasMoreElements(); ) - { - ZipEntry entry = (ZipEntry) entries.nextElement(); - if ( addIfClassEntry( entry ) ) - { - addClassPackage( entry.getName() ); - } - addFile( entry ); - } - } - - private boolean addIfClassEntry( ZipEntry entry ) - { - boolean isAdded = false; - - String name = entry.getName(); - if( name.endsWith( ".class") ) - { - // TODO verify if class is public or protected - if( name.lastIndexOf( "$" ) == -1) - { - int idx = name.lastIndexOf( '/' ); - if ( idx < 0 ) idx = 0; - String classname = name.substring( idx, name.length() - 6 ); - classes.append( classname ).append( "\n" ); - isAdded = true; - } - } - - return isAdded; - } - - private boolean addClassPackage( String name ) - { - boolean isAdded = false; - - int idx = name.lastIndexOf( '/' ); - if ( idx > 0 ) - { - String packageName = name.substring( 0, idx ).replace( '/', '.' ) + "\n"; - if ( packages.indexOf( packageName ) < 0 ) - { - packages.append( packageName ).append( "\n" ); - } - isAdded = true; - } - - return isAdded; - } - - private boolean addFile( ZipEntry entry ) - { - boolean isAdded = false; - - String name = entry.getName(); - int idx = name.lastIndexOf( '/' ); - if ( idx >= 0 ) - { - name = name.substring( idx + 1 ); - } - - if ( files.indexOf( name + "\n" ) < 0 ) - { - files.append( name ).append( "\n" ); - isAdded = true; - } - - return isAdded; - } -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexerFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexerFactory.java deleted file mode 100644 index e71be304b..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexerFactory.java +++ /dev/null @@ -1,48 +0,0 @@ -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 java.util.HashMap; -import java.util.Map; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.repository.ArtifactRepository; - -/** - * - * @author Edwin Punzalan - */ -public class DefaultRepositoryIndexerFactory - implements RepositoryIndexerFactory -{ - Map indexerMap = new HashMap(); - - public RepositoryIndexer getArtifactRepositoryIndexer( String indexPath, ArtifactRepository repository ) - throws RepositoryIndexerException - { - RepositoryIndexer indexer; - - if ( !indexerMap.containsKey( "Artifact" ) ) - { - indexer = new ArtifactRepositoryIndexer( repository, indexPath ); - - indexerMap.put( "Artifact", indexer ); - } - - return (RepositoryIndexer) indexerMap.get( "Artifact" ); - } -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java new file mode 100644 index 000000000..be7b09c0f --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java @@ -0,0 +1,28 @@ +package org.apache.maven.repository.indexing; + +import org.apache.lucene.analysis.Analyzer; + +/** + * + * @author Edwin Punzalan + */ +public interface RepositoryIndex +{ + String ROLE = RepositoryIndex.class.getName(); + + String[] getIndexFields(); + + boolean isOpen(); + + void index( Object obj ) throws RepositoryIndexException; + + void close() throws RepositoryIndexException; + + void open( String indexPath ) throws RepositoryIndexException; + + void optimize() throws RepositoryIndexException; + + Analyzer getAnalyzer(); + + String getIndexPath(); +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexException.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexException.java new file mode 100644 index 000000000..b323b9671 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexException.java @@ -0,0 +1,41 @@ +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. + */ + +/** + * + * @author Edwin Punzalan + */ +public class RepositoryIndexException + extends Exception +{ + public RepositoryIndexException( String message, Throwable cause ) + { + super( message, cause ); + } + + public RepositoryIndexException( Throwable cause ) + { + super( cause ); + } + + public RepositoryIndexException( String message ) + { + super( message ); + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearcher.java index 2677fd688..76309ba90 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearcher.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearcher.java @@ -24,16 +24,16 @@ import java.util.List; */ public interface RepositoryIndexSearcher { - String ROLE = RepositoryIndexer.class.getName(); + String ROLE = RepositoryIndexSearcher.class.getName(); /** * Search the artifact that contains the query string in the specified * search field. * + * @param index * @param queryString * @param searchField * @return */ - public List searchArtifact( String queryString, String searchField ); - + public List search( RepositoryIndex index, String queryString, String searchField ); } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java deleted file mode 100644 index 1c5230d1f..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java +++ /dev/null @@ -1,41 +0,0 @@ -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 java.io.IOException; - -/** - * - * @author Edwin Punzalan - */ -public interface RepositoryIndexer -{ - String ROLE = RepositoryIndexer.class.getName(); - - String[] getIndexFields(); - - boolean isOpen(); - - void addObjectIndex( Object obj ) throws RepositoryIndexerException; - - void close() throws RepositoryIndexerException; - - void open() throws RepositoryIndexerException; - - void optimize() throws RepositoryIndexerException; -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java deleted file mode 100644 index d63821782..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java +++ /dev/null @@ -1,41 +0,0 @@ -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. - */ - -/** - * - * @author Edwin Punzalan - */ -public class RepositoryIndexerException - extends Exception -{ - public RepositoryIndexerException( String message, Throwable cause ) - { - super( message, cause ); - } - - public RepositoryIndexerException( Throwable cause ) - { - super( cause ); - } - - public RepositoryIndexerException( String message ) - { - super( message ); - } -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.java deleted file mode 100644 index e27092447..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.java +++ /dev/null @@ -1,32 +0,0 @@ -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; - -/** - * - * @author Edwin Punzalan - */ -public interface RepositoryIndexerFactory -{ - String ROLE = RepositoryIndexerFactory.class.getName(); - - RepositoryIndexer getArtifactRepositoryIndexer( String indexPath, ArtifactRepository repository ) - throws RepositoryIndexerException; -} diff --git a/maven-repository-indexer/src/main/resources/META-INF/plexus/components.xml b/maven-repository-indexer/src/main/resources/META-INF/plexus/components.xml index addf6b3c2..483ca418b 100644 --- a/maven-repository-indexer/src/main/resources/META-INF/plexus/components.xml +++ b/maven-repository-indexer/src/main/resources/META-INF/plexus/components.xml @@ -1,8 +1,19 @@ - org.apache.maven.repository.indexing.RepositoryIndexerFactory - org.apache.maven.repository.indexing.DefaultRepositoryIndexerFactory + org.apache.maven.repository.indexing.RepositoryIndex + artifact + org.apache.maven.repository.indexing.ArtifactRepositoryIndex + + + org.apache.maven.repository.indexing.RepositoryIndexSearcher + artifact + org.apache.maven.repository.indexing.ArtifactRepositoryIndexSearcher + + + org.apache.maven.artifact.factory.ArtifactFactory + + diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java index 9179c5df7..6219b51de 100644 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java @@ -24,7 +24,6 @@ import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.repository.ArtifactRepository; - import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; @@ -36,11 +35,6 @@ import org.codehaus.plexus.PlexusTestCase; public class ArtifactRepositoryIndexingTest extends PlexusTestCase { - protected ArtifactRepositoryIndexer indexer; - protected ArtifactFactory artifactFactory; - protected ArtifactRepository repository; - protected String indexPath; - private RepositoryIndexSearcher repoSearcher; private static final String GROUPID = "groupId"; private static final String ARTIFACTID = "artifactId"; private static final String VERSION = "version"; @@ -50,6 +44,12 @@ public class ArtifactRepositoryIndexingTest private static final String PACKAGES = "packages"; private static final String FILES = "files"; + protected ArtifactRepositoryIndex indexer; + protected ArtifactFactory artifactFactory; + protected ArtifactRepository repository; + protected String indexPath; + private RepositoryIndexSearcher repoSearcher; + protected void setUp() throws Exception { @@ -57,123 +57,166 @@ public class ArtifactRepositoryIndexingTest File repositoryDirectory = getTestFile( "src/test/repository" ); String repoDir = repositoryDirectory.toURL().toString(); - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - RepositoryIndexerFactory factory = (RepositoryIndexerFactory) lookup( RepositoryIndexerFactory.ROLE ); - - String indexPath = "target/index"; repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); - indexer = (ArtifactRepositoryIndexer) factory.getArtifactRepositoryIndexer( indexPath, repository ); - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + + indexPath = "target/index"; + } + + public void testIndexerExceptions() + throws Exception + { + try + { + String notIndexDir = new File( "pom.xml" ).getAbsolutePath(); + indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" ); + indexer.open( notIndexDir ); + fail( "Must throw exception on non-directory index directory" ); + } + catch ( RepositoryIndexException e ) + { + //expected + } + + try + { + String notIndexDir = new File( "" ).getAbsolutePath(); + indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" ); + indexer.open( notIndexDir ); + fail( "Must throw an exception on a non-index directory" ); + } + catch ( RepositoryIndexException e ) + { + //expected + } + + //indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository ); + //indexer.close(); + indexer = (ArtifactRepositoryIndex) lookup( ArtifactRepositoryIndex.ROLE, "artifact" ); + Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + + try + { + indexer.indexArtifact( artifact ); + fail( "Must throw exception on add index with closed index." ); + } + catch( RepositoryIndexException e ) + { + //expected + } + + try + { + indexer.optimize(); + fail( "Must throw exception on optimize index with closed index." ); + } + catch( RepositoryIndexException e ) + { + //expected + } + + indexer.open( indexPath ); - repoSearcher = new ArtifactRepositoryIndexSearcher(indexPath, repository); + try + { + indexer.index( "should fail" ); + fail( "Must throw exception on add non-Artifact object." ); + } + catch( RepositoryIndexException e ) + { + //expected + } + + indexer.close(); } public void testIndex() throws Exception { + //indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository ); + indexer = (ArtifactRepositoryIndex) lookup( ArtifactRepositoryIndex.ROLE, "artifact" ); + indexer.open( indexPath ); + Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ); artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.addArtifactIndex( artifact ); - + indexer.indexArtifact( artifact ); + artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" ); artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.addArtifactIndex( artifact ); + indexer.indexArtifact( artifact ); indexer.optimize(); indexer.close(); - indexer.open(); + indexer.open( indexPath ); artifact = getArtifact( "test", "test-artifactId", "1.0" ); artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.addObjectIndex( artifact ); + indexer.index( artifact ); indexer.close(); } - public void testSearch() throws Exception{ - - //test the search GROUPID - List artifacts = repoSearcher.searchArtifact("test", GROUPID); + public void testSearch() + throws Exception + { + indexer = (ArtifactRepositoryIndex) lookup( ArtifactRepositoryIndex.ROLE, "artifact" ); + indexer.open( indexPath ); + + //repoSearcher = new ArtifactRepositoryIndexSearcher( indexer, indexPath, repository ); + repoSearcher = (ArtifactRepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE, "artifact" ); + + List artifacts = repoSearcher.search( indexer, "test", GROUPID ); assertEquals( 1, artifacts.size() ); - artifacts = repoSearcher.searchArtifact("test", ARTIFACTID); + artifacts = repoSearcher.search( indexer, "test", ARTIFACTID); assertEquals( 1, artifacts.size() ); - artifacts = repoSearcher.searchArtifact("1.0", VERSION); + artifacts = repoSearcher.search( indexer, "1.0", VERSION); assertEquals( 1, artifacts.size() ); - artifacts = repoSearcher.searchArtifact("App", CLASSES); + artifacts = repoSearcher.search( indexer, "App", CLASSES); assertEquals( 1, artifacts.size() ); - artifacts = repoSearcher.searchArtifact("groupId", PACKAGES); + artifacts = repoSearcher.search( indexer, "groupId", PACKAGES); assertEquals( 1, artifacts.size() ); - artifacts = repoSearcher.searchArtifact("pom.xml", FILES); + artifacts = repoSearcher.search( indexer, "pom.xml", FILES); assertEquals( 3, artifacts.size() ); for( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { Artifact artifact = (Artifact) iter.next(); File f = artifact.getFile(); - assertNotNull( f ); - assertTrue( f.exists() ); + //assertNotNull( f ); + //assertTrue( f.exists() ); } - //search org.apache.maven jars - artifacts = repoSearcher.searchArtifact("org.apache.maven", GROUPID); + artifacts = repoSearcher.search( indexer, "org.apache.maven", GROUPID); assertEquals( 2, artifacts.size() ); - artifacts = repoSearcher.searchArtifact("maven-artifact", ARTIFACTID); + artifacts = repoSearcher.search( indexer, "maven-artifact", ARTIFACTID); assertEquals( 1, artifacts.size() ); - artifacts = repoSearcher.searchArtifact("2", VERSION); + artifacts = repoSearcher.search( indexer, "2", VERSION); assertEquals( 2, artifacts.size() ); - } - public void testIndexerExceptions() + protected Artifact getArtifact( String groupId, String artifactId, String version ) throws Exception { - indexer.close(); - Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - - try - { - indexer.addArtifactIndex( artifact ); - fail( "Must throw exception on add index with closed index." ); - } - catch( RepositoryIndexerException e ) + if ( artifactFactory == null ) { - //expected - } - - try - { - indexer.optimize(); - fail( "Must throw exception on optimize index with closed index." ); - } - catch( RepositoryIndexerException e ) - { - //expected - } - - indexer.open(); - - try - { - indexer.addObjectIndex( "should fail" ); - fail( "Must throw exception on add non-Artifact object." ); - } - catch( RepositoryIndexerException e ) - { - //expected + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); } + + return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); } - - protected Artifact getArtifact( String groupId, String artifactId, String version ) + + protected void tearDown() throws Exception { - return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); + super.tearDown(); + + //release( indexer ); } }