From 4c632578886dfa4baf4239e2ec5cce0972a9d7ad Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Fri, 28 Jul 2006 06:42:52 +0000 Subject: [PATCH] [MRM-127] remove old classes git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@426405 13f79535-47bb-0310-9956-ffa450edef68 --- .../indexing/AbstractRepositoryIndex.java | 415 ----------- .../indexing/ArtifactRepositoryIndex.java | 608 ---------------- .../DefaultRepositoryIndexSearchLayer.java | 427 ----------- .../DefaultRepositoryIndexSearcher.java | 327 --------- .../DefaultRepositoryIndexingFactory.java | 54 -- .../indexing/EclipseRepositoryIndex.java | 343 --------- .../indexing/MetadataRepositoryIndex.java | 218 ------ .../repository/indexing/RepositoryIndex.java | 154 ---- .../indexing/RepositoryIndexSearchHit.java | 96 --- .../indexing/RepositoryIndexSearchLayer.java | 60 -- .../indexing/RepositoryIndexSearcher.java | 44 -- .../indexing/RepositoryIndexingFactory.java | 53 -- .../repository/indexing/SearchResult.java | 92 --- .../ArtifactRepositoryIndexingTest.java | 679 ------------------ .../indexing/EclipseRepositoryIndexTest.java | 238 ------ .../MetadataRepositoryIndexingTest.java | 286 -------- .../RepositoryIndexSearchLayerTest.java | 414 ----------- 17 files changed, 4508 deletions(-) delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearchLayer.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/EclipseRepositoryIndex.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchHit.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayer.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearcher.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/SearchResult.java delete mode 100644 maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java delete mode 100644 maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/EclipseRepositoryIndexTest.java delete mode 100644 maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java delete mode 100644 maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java 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 deleted file mode 100644 index c2613c59f..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java +++ /dev/null @@ -1,415 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.lucene.analysis.Analyzer; -import org.apache.lucene.analysis.CharTokenizer; -import org.apache.lucene.analysis.SimpleAnalyzer; -import org.apache.lucene.analysis.TokenStream; -import org.apache.lucene.document.Document; -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.index.Term; -import org.apache.maven.artifact.repository.ArtifactRepository; - -import java.io.File; -import java.io.IOException; -import java.io.Reader; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.zip.ZipEntry; - -/** - * Abstract class for RepositoryIndexers. - * - * @author Edwin Punzalan - * @todo [BP] overall am not happy with the design of this class and subclasses, but will refactor over time based on how it is used and by assessing how this affects Lucene's performance - * @todo in particular - we should remove the reference to documents and instead just index artifacts, models and metadata. - * @todo these should be individual tasks, with the grouping and atomicity (closing of index after a whole unit) done separately - */ -public abstract class AbstractRepositoryIndex - implements RepositoryIndex -{ - // TODO: can this be derived from the repository? -- probably a sensible default, but still should be configurable - private File indexPath; - - private IndexWriter indexWriter; - - protected ArtifactRepository repository; - - private Analyzer analyzer; - - /** - * Class constructor - * - * @param indexPath - * @param repository - */ - protected AbstractRepositoryIndex( File indexPath, ArtifactRepository repository ) - throws RepositoryIndexException - { - this.repository = repository; - this.indexPath = indexPath; - - try - { - validate(); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Failed to validate index path: " + indexPath.getAbsolutePath(), e ); - } - } - - /** - * @see org.apache.maven.repository.indexing.RepositoryIndex#optimize() - */ - public void optimize() - throws RepositoryIndexException - { - try - { - getIndexWriter().optimize(); - close(); - } - catch ( IOException ioe ) - { - throw new RepositoryIndexException( "Failed to optimize index", ioe ); - } - } - - /** - * closes the current index from writing thus removing lock files - */ - private void close() - throws RepositoryIndexException - { - try - { - if ( indexWriter != null ) - { - indexWriter.close(); - indexWriter = null; - } - } - catch ( IOException e ) - { - throw new RepositoryIndexException( e.getMessage(), e ); - } - } - - /** - * @see org.apache.maven.repository.indexing.RepositoryIndex#getIndexPath() - */ - public File getIndexPath() - { - return indexPath; - } - - /** - * Method to retrieve the lucene IndexWriter used in creating/updating the index - * - * @return the lucene IndexWriter object used to update the index - * @throws IOException - */ - private IndexWriter getIndexWriter() - throws IOException, RepositoryIndexException - { - if ( indexWriter == null ) - { - if ( indexExists() ) - { - indexWriter = new IndexWriter( indexPath, getAnalyzer(), false ); - } - else - { - indexWriter = new IndexWriter( indexPath, getAnalyzer(), true ); - } - } - - return indexWriter; - } - - /** - * @see RepositoryIndex#validate() - */ - public final void validate() - throws RepositoryIndexException, IOException - { - if ( indexExists() ) - { - IndexReader indexReader = IndexReader.open( indexPath ); - try - { - if ( indexReader.numDocs() > 0 ) - { - Collection fields = indexReader.getFieldNames( IndexReader.FieldOption.ALL ); - for ( int idx = 0; idx < FIELDS.length; idx++ ) - { - if ( !fields.contains( FIELDS[idx] ) ) - { - throw new RepositoryIndexException( - "The Field " + FIELDS[idx] + " does not exist in index " + indexPath + "." ); - } - } - } - } - finally - { - indexReader.close(); - } - } - } - - /** - * @see org.apache.maven.repository.indexing.RepositoryIndex#getRepository() - */ - public ArtifactRepository getRepository() - { - return repository; - } - - /** - * Delete the document(s) that contains the specified value on the specified field. - * - * @param field - * @param value - * @throws RepositoryIndexException - * @throws IOException - */ - protected void deleteDocument( String field, String value ) - throws RepositoryIndexException, IOException - { - Term term = new Term( field, value ); - - deleteDocuments( Collections.singletonList( term ) ); - } - - /** - * @see RepositoryIndex#deleteDocuments(java.util.List) - */ - public void deleteDocuments( List termList ) - throws RepositoryIndexException, IOException - { - if ( indexExists() ) - { - IndexReader indexReader = null; - try - { - indexReader = IndexReader.open( indexPath ); - - for ( Iterator terms = termList.iterator(); terms.hasNext(); ) - { - Term term = (Term) terms.next(); - - indexReader.deleteDocuments( term ); - } - } - finally - { - if ( indexReader != null ) - { - indexReader.close(); - } - } - } - } - - /** - * @see RepositoryIndex#addDocuments(java.util.List) - */ - public void addDocuments( List docList ) - throws RepositoryIndexException - { - try - { - IndexWriter indexWriter = getIndexWriter(); - - for ( Iterator docs = docList.iterator(); docs.hasNext(); ) - { - Document doc = (Document) docs.next(); - indexWriter.addDocument( doc ); - } - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Failed to add an index document", e ); - } - finally - { - close(); - } - } - - /** - * Check if the index already exists. - * - * @return true if the index already exists - * @throws RepositoryIndexException - */ - public boolean indexExists() - throws RepositoryIndexException - { - if ( IndexReader.indexExists( indexPath ) ) - { - return true; - } - else if ( !indexPath.exists() ) - { - return false; - } - else if ( indexPath.isDirectory() ) - { - if ( indexPath.listFiles().length > 1 ) - { - throw new RepositoryIndexException( indexPath + " is not a valid index directory." ); - } - else - { - return false; - } - } - else - { - throw new RepositoryIndexException( indexPath + " is not a directory." ); - } - } - - /** - * @see org.apache.maven.repository.indexing.RepositoryIndex#getAnalyzer() - */ - public Analyzer getAnalyzer() - { - if ( analyzer == null ) - { - analyzer = new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() ); - } - - return analyzer; - } - - /** - * @see RepositoryIndex#isKeywordField(String) - */ - public boolean isKeywordField( String field ) - { - return KEYWORD_FIELDS.contains( field ); - } - - /** - * Method to test a zip entry if it is a java class, and adds it to the classes buffer - * - * @param entry the zip entry to test for java class - * @param classes the String buffer to add the java class if the test result as true - * @return true if the zip entry is a java class and was successfully added to the buffer - */ - protected boolean addIfClassEntry( ZipEntry entry, StringBuffer classes ) - { - 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 + 1, name.length() - 6 ); - classes.append( classname ).append( "\n" ); - isAdded = true; - } - } - - return isAdded; - } - - /** - * Inner class used as the default IndexAnalyzer - */ - private static class ArtifactRepositoryIndexAnalyzer - extends Analyzer - { - private Analyzer defaultAnalyzer; - - /** - * constructor to for this analyzer - * - * @param defaultAnalyzer the analyzer to use as default for the general fields of the artifact indeces - */ - ArtifactRepositoryIndexAnalyzer( Analyzer defaultAnalyzer ) - { - this.defaultAnalyzer = defaultAnalyzer; - } - - /** - * Method called by lucence during indexing operations - * - * @param fieldName the field name that the lucene object is currently processing - * @param reader a Reader object to the index stream - * @return an analyzer to specific to the field name or the default analyzer if none is present - */ - public TokenStream tokenStream( String fieldName, Reader reader ) - { - TokenStream tokenStream; - - if ( RepositoryIndex.FLD_VERSION.equals( fieldName ) || RepositoryIndex.FLD_LASTUPDATE.equals( fieldName ) ) - { - tokenStream = new VersionTokenizer( reader ); - } - else - { - tokenStream = defaultAnalyzer.tokenStream( fieldName, reader ); - } - - return tokenStream; - } - } - - /** - * Inner class used to tokenize an artifact's version. - */ - private static class VersionTokenizer - extends CharTokenizer - { - /** - * Constructor with the required reader to the index stream - * - * @param reader the Reader object of the index stream - */ - VersionTokenizer( Reader reader ) - { - super( reader ); - } - - /** - * method that lucene calls to check tokenization of a stream character - * - * @param character char currently being processed - * @return true if the char is a token, false if the char is a stop char - */ - protected boolean isTokenChar( char character ) - { - return character != '.' && character != '-'; - } - } -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java deleted file mode 100644 index 74e83bbcc..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java +++ /dev/null @@ -1,608 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.lucene.document.Document; -import org.apache.lucene.document.Field; -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.Term; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; -import org.apache.maven.model.Dependency; -import org.apache.maven.model.License; -import org.apache.maven.model.Model; -import org.apache.maven.model.Plugin; -import org.apache.maven.model.ReportPlugin; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.repository.digest.Digester; -import org.apache.maven.repository.digest.DigesterException; -import org.codehaus.plexus.util.StringUtils; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipException; -import java.util.zip.ZipFile; - - -/** - * Class used to index Artifact objects in a specific repository - * - * @author Edwin Punzalan - */ -public class ArtifactRepositoryIndex - extends AbstractRepositoryIndex -{ - private Digester digester; - - /** - * Class constructor - * - * @param indexPath the path where the lucene index will be created/updated. - * @param repository the repository where the indexed artifacts are located - * @param digester the digester object to generate the checksum strings - */ - public ArtifactRepositoryIndex( File indexPath, ArtifactRepository repository, Digester digester ) - throws RepositoryIndexException - { - super( indexPath, repository ); - this.digester = digester; - } - - /** - * Indexes the artifacts found within the specified list. Deletes existing indices for the same artifacts first, - * before proceeding on adding them into the index. - * - * @param artifactList - * @throws RepositoryIndexException - */ - public void indexArtifacts( List artifactList ) - throws RepositoryIndexException - { - try - { - deleteDocuments( getTermList( artifactList ) ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Failed to delete an index document", e ); - } - - addDocuments( getDocumentList( artifactList ) ); - } - - /** - * Creates a list of Lucene Term object used in index deletion - * - * @param artifactList - * @return List of Term object - */ - private List getTermList( List artifactList ) - { - List list = new ArrayList(); - - for ( Iterator artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - Artifact artifact = (Artifact) artifacts.next(); - - list.add( new Term( FLD_ID, ARTIFACT + ":" + artifact.getId() ) ); - - if ( "pom".equals( artifact.getType() ) ) - { - list.add( new Term( FLD_ID, POM + ":" + artifact.getId() ) ); - } - } - - return list; - } - - /** - * Creates a list of Lucene documents, used for index additions - * - * @param artifactList - * @return - */ - private List getDocumentList( List artifactList ) - { - List list = new ArrayList(); - - for ( Iterator artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - Artifact artifact = (Artifact) artifacts.next(); - - try - { - list.add( createDocument( artifact ) ); - } - catch ( RepositoryIndexException e ) - { - // TODO: log the problem and record it as a repository error - // We log the problem, but do not add the document to the list to be added to the index - } - - if ( "pom".equals( artifact.getType() ) ) - { - try - { - Model model = new MavenXpp3Reader().read( new FileReader( artifact.getFile() ) ); - - list.add( createDocument( artifact, model ) ); - } - catch ( IOException e ) - { - // TODO: log the problem and record it as a repository error - // We log the problem, but do not add the document to the list to be added to the index - } - catch ( XmlPullParserException e ) - { - // TODO: log the problem and record it as a repository error - // We log the problem, but do not add the document to the list to be added to the index - } - catch ( RepositoryIndexException e ) - { - // TODO: log the problem and record it as a repository error - // We log the problem, but do not add the document to the list to be added to the index - } - } - } - - return list; - } - - /** - * Method to index a given artifact - * - * @param artifact the Artifact object to be indexed - * @throws RepositoryIndexException - */ - public void indexArtifact( Artifact artifact ) - throws RepositoryIndexException - { - indexArtifacts( Collections.singletonList( artifact ) ); - } - - /** - * Creates a Lucene Document from an artifact; used for index additions - * - * @param artifact - * @return - * @throws RepositoryIndexException - */ - private Document createDocument( Artifact artifact ) - throws RepositoryIndexException - { - StringBuffer classes = new StringBuffer(); - StringBuffer packages = new StringBuffer(); - StringBuffer files = new StringBuffer(); - - String sha1sum; - String md5sum; - try - { - sha1sum = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); - md5sum = digester.createChecksum( artifact.getFile(), Digester.MD5 ); - } - catch ( DigesterException e ) - { - throw new RepositoryIndexException( "Unable to create a checksum", e ); - } - - try - { - // TODO: improve - if ( "jar".equals( artifact.getType() ) ) - { - ZipFile jar = new ZipFile( artifact.getFile() ); - - for ( Enumeration entries = jar.entries(); entries.hasMoreElements(); ) - { - ZipEntry entry = (ZipEntry) entries.nextElement(); - if ( addIfClassEntry( entry, classes ) ) - { - addClassPackage( entry.getName(), packages ); - } - addFile( entry, files ); - } - } - } - catch ( ZipException e ) - { - throw new RepositoryIndexException( "Error reading from artifact file: " + artifact.getFile(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Error reading from artifact file", e ); - } - - Document doc = new Document(); - doc.add( createKeywordField( FLD_ID, ARTIFACT + ":" + artifact.getId() ) ); - doc.add( createTextField( FLD_NAME, artifact.getFile().getName() ) ); - doc.add( createTextField( FLD_GROUPID, artifact.getGroupId() ) ); - doc.add( createTextField( FLD_ARTIFACTID, artifact.getArtifactId() ) ); - doc.add( createTextField( FLD_VERSION, artifact.getVersion() ) ); - doc.add( createTextField( FLD_SHA1, sha1sum ) ); - doc.add( createTextField( FLD_MD5, md5sum ) ); - doc.add( createTextField( FLD_CLASSES, classes.toString() ) ); - doc.add( createTextField( FLD_PACKAGES, packages.toString() ) ); - doc.add( createTextField( FLD_FILES, files.toString() ) ); - doc.add( createUnindexedField( FLD_DOCTYPE, ARTIFACT ) ); - doc.add( createTextField( FLD_LASTUPDATE, "" ) ); - doc.add( createTextField( FLD_PLUGINPREFIX, "" ) ); - doc.add( createKeywordField( FLD_LICENSE_URLS, "" ) ); - doc.add( createKeywordField( FLD_DEPENDENCIES, "" ) ); - doc.add( createKeywordField( FLD_PLUGINS_REPORT, "" ) ); - doc.add( createKeywordField( FLD_PLUGINS_BUILD, "" ) ); - doc.add( createKeywordField( FLD_PLUGINS_ALL, "" ) ); - int i = artifact.getFile().getName().lastIndexOf( '.' ); - doc.add( createTextField( FLD_PACKAGING, artifact.getFile().getName().substring( i + 1 ) ) ); - - return doc; - } - - private static Field createUnindexedField( String name, String value ) - { - return new Field( name, value, Field.Store.YES, Field.Index.NO ); - } - - private static Field createTextField( String name, String value ) - { - return new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ); - } - - private static Field createKeywordField( String name, String value ) - { - return new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ); - } - - /** - * Method to add a class package to the buffer of packages - * - * @param name the complete path name of the class - * @param packages the packages buffer - */ - private void addClassPackage( String name, StringBuffer packages ) - { - 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" ); - } - } - } - - /** - * Method to add the zip entry as a file list - * - * @param entry the zip entry to be added - * @param files the buffer of files to update - */ - private void addFile( ZipEntry entry, StringBuffer files ) - { - 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" ); - } - } - - public List enumerateGroupIds() - throws IOException - { - IndexReader indexReader = IndexReader.open( getIndexPath() ); - - Set groups = new HashSet(); - - try - { - for ( int i = 0; i < indexReader.numDocs(); i++ ) - { - Document doc = indexReader.document( i ); - groups.add( doc.getField( FLD_GROUPID ).stringValue() ); - } - } - finally - { - indexReader.close(); - } - - List sortedGroups = new ArrayList( groups ); - Collections.sort( sortedGroups ); - return sortedGroups; - } - - public List getArtifacts( String groupId ) - throws IOException - { - IndexReader indexReader = IndexReader.open( getIndexPath() ); - - Set artifactIds = new HashSet(); - - try - { - for ( int i = 0; i < indexReader.numDocs(); i++ ) - { - Document doc = indexReader.document( i ); - if ( doc.getField( FLD_GROUPID ).stringValue().equals( groupId ) ) - { - artifactIds.add( doc.getField( FLD_ARTIFACTID ).stringValue() ); - } - } - } - finally - { - indexReader.close(); - } - - List sortedArtifactIds = new ArrayList( artifactIds ); - Collections.sort( sortedArtifactIds ); - return sortedArtifactIds; - } - - public List getVersions( String groupId, String artifactId ) - throws IOException - { - IndexReader indexReader = IndexReader.open( getIndexPath() ); - - Set versions = new HashSet(); - - try - { - for ( int i = 0; i < indexReader.numDocs(); i++ ) - { - Document doc = indexReader.document( i ); - if ( doc.getField( FLD_GROUPID ).stringValue().equals( groupId ) && - doc.getField( FLD_ARTIFACTID ).stringValue().equals( artifactId ) ) - { - // DefaultArtifactVersion is used for correct ordering - versions.add( new DefaultArtifactVersion( doc.getField( FLD_VERSION ).stringValue() ) ); - } - } - } - finally - { - indexReader.close(); - } - - List sortedVersions = new ArrayList( versions ); - Collections.sort( sortedVersions ); - return sortedVersions; - } - - /** - * Creates a Lucene Document from a Model; used for index additions - * - * @param pom - * @return - * @throws RepositoryIndexException - */ - private Document createDocument( Artifact artifact, Model pom ) - throws RepositoryIndexException - { - String version = pom.getVersion(); - if ( version == null ) - { - // It was inherited - version = pom.getParent().getVersion(); - // TODO: do we need to use the general inheritence mechanism or do we only want to search within those defined in this pom itself? - // I think searching just this one is adequate, and it is only necessary to inherit the version and group ID [BP] - } - - String groupId = pom.getGroupId(); - if ( groupId == null ) - { - groupId = pom.getParent().getGroupId(); - } - - Document doc = new Document(); - doc.add( createKeywordField( FLD_ID, POM + ":" + artifact.getId() ) ); - doc.add( createTextField( FLD_GROUPID, groupId ) ); - doc.add( createTextField( FLD_ARTIFACTID, pom.getArtifactId() ) ); - doc.add( createTextField( FLD_VERSION, version ) ); - doc.add( createKeywordField( FLD_PACKAGING, pom.getPackaging() ) ); - - File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) ); - doc.add( createTextField( FLD_SHA1, getChecksum( Digester.SHA1, pomFile.getAbsolutePath() ) ) ); - doc.add( createTextField( FLD_MD5, getChecksum( Digester.MD5, pomFile.getAbsolutePath() ) ) ); - - indexLicenseUrls( doc, pom ); - indexDependencies( doc, pom ); - - boolean hasPlugins = false; - if ( pom.getBuild() != null && pom.getBuild().getPlugins() != null && pom.getBuild().getPlugins().size() > 0 ) - { - hasPlugins = true; - indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); - indexPlugins( doc, FLD_PLUGINS_ALL, pom.getBuild().getPlugins().iterator() ); - } - else - { - doc.add( createTextField( FLD_PLUGINS_BUILD, "" ) ); - } - - if ( pom.getReporting() != null && pom.getReporting().getPlugins() != null && - pom.getReporting().getPlugins().size() > 0 ) - { - hasPlugins = true; - indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); - indexReportPlugins( doc, FLD_PLUGINS_ALL, pom.getReporting().getPlugins().iterator() ); - } - else - { - doc.add( createTextField( FLD_PLUGINS_REPORT, "" ) ); - } - - if ( !hasPlugins ) - { - doc.add( createTextField( FLD_PLUGINS_ALL, "" ) ); - } - doc.add( createUnindexedField( FLD_DOCTYPE, POM ) ); - // TODO: do we need to add all these empty fields? - doc.add( createTextField( FLD_PLUGINPREFIX, "" ) ); - doc.add( createTextField( FLD_LASTUPDATE, "" ) ); - doc.add( createTextField( FLD_NAME, "" ) ); - doc.add( createTextField( FLD_CLASSES, "" ) ); - doc.add( createKeywordField( FLD_PACKAGES, "" ) ); - doc.add( createTextField( FLD_FILES, "" ) ); - return doc; - } - - /** - * Method to index license urls found inside the passed pom - * - * @param doc the index object to create the fields for the license urls - * @param pom the Model object to be indexed - */ - private void indexLicenseUrls( Document doc, Model pom ) - { - List licenseList = pom.getLicenses(); - if ( licenseList != null && licenseList.size() > 0 ) - { - Iterator licenses = licenseList.iterator(); - while ( licenses.hasNext() ) - { - License license = (License) licenses.next(); - String url = license.getUrl(); - if ( StringUtils.isNotEmpty( url ) ) - { - doc.add( createKeywordField( FLD_LICENSE_URLS, url ) ); - } - } - } - else - { - doc.add( createKeywordField( FLD_LICENSE_URLS, "" ) ); - } - } - - /** - * Method to index declared dependencies found inside the passed pom - * - * @param doc the index object to create the fields for the dependencies - * @param pom the Model object to be indexed - */ - private void indexDependencies( Document doc, Model pom ) - { - List dependencyList = pom.getDependencies(); - if ( dependencyList != null && dependencyList.size() > 0 ) - { - Iterator dependencies = dependencyList.iterator(); - while ( dependencies.hasNext() ) - { - Dependency dep = (Dependency) dependencies.next(); - String id = getId( dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ); - doc.add( createKeywordField( FLD_DEPENDENCIES, id ) ); - } - } - else - { - doc.add( createKeywordField( FLD_DEPENDENCIES, "" ) ); - } - } - - /** - * Method to index plugins to a specified index field - * - * @param doc the index object to create the fields for the plugins - * @param field the index field to store the passed plugin - * @param plugins the iterator to the list of plugins to be indexed - */ - private void indexPlugins( Document doc, String field, Iterator plugins ) - { - while ( plugins.hasNext() ) - { - Plugin plugin = (Plugin) plugins.next(); - String id = getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ); - doc.add( createKeywordField( field, id ) ); - } - } - - /** - * Method to index report plugins to a specified index field - * - * @param doc the index object to create the fields for the report plugins - * @param field the index field to store the passed report plugin - * @param plugins the iterator to the list of report plugins to be indexed - */ - private void indexReportPlugins( Document doc, String field, Iterator plugins ) - { - while ( plugins.hasNext() ) - { - ReportPlugin plugin = (ReportPlugin) plugins.next(); - String id = getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ); - doc.add( createKeywordField( field, id ) ); - } - } - - /** - * Method to generate the computed checksum of an existing file using the specified algorithm. - * - * @param algorithm the algorithm to be used to generate the checksum - * @param file the file to match the generated checksum - * @return a string representing the checksum - * @throws RepositoryIndexException - */ - private String getChecksum( String algorithm, String file ) - throws RepositoryIndexException - { - try - { - return digester.createChecksum( new File( file ), algorithm ); - } - catch ( DigesterException e ) - { - throw new RepositoryIndexException( "Failed to create checksum", e ); - } - } - - /** - * Method to create the unique artifact id to represent the artifact in the repository - * - * @param groupId the artifact groupId - * @param artifactId the artifact artifactId - * @param version the artifact version - * @return the String id to uniquely represent the artifact - */ - private String getId( String groupId, String artifactId, String version ) - { - return groupId + ":" + artifactId + ":" + version; - } - - public void deleteArtifact( Artifact artifact ) - throws IOException, RepositoryIndexException - { - deleteDocuments( getTermList( Collections.singletonList( artifact ) ) ); - } -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearchLayer.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearchLayer.java deleted file mode 100644 index bd0eac447..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearchLayer.java +++ /dev/null @@ -1,427 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.model.Dependency; -import org.apache.maven.model.License; -import org.apache.maven.model.Model; -import org.apache.maven.model.Plugin; -import org.apache.maven.model.ReportPlugin; -import org.apache.maven.repository.indexing.query.Query; -import org.apache.maven.repository.indexing.query.QueryTerm; -import org.apache.maven.repository.indexing.query.SingleTermQuery; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.StringTokenizer; - -/** - * This class is to be invoked or called by the action class for - * general and advanced searching. It uses the DefaultRepositoryIndexSearcher - * to perform the search and constructs the search result objects to be - * returned to tha webapp action class. - * - * @plexus.component role="org.apache.maven.repository.indexing.RepositoryIndexSearchLayer" - */ -public class DefaultRepositoryIndexSearchLayer - implements RepositoryIndexSearchLayer -{ - /** - * @plexus.requirement - */ - private ArtifactFactory factory; - - /** - * @plexus.requirement - */ - private RepositoryIndexSearcher searcher; - - public List searchGeneral( String keyword, RepositoryIndex index ) - throws RepositoryIndexSearchException - { - List generalSearchResults = new ArrayList(); - for ( int i = 0; i < RepositoryIndex.FIELDS.length; i++ ) - { - // TODO! does simply iterating the fields and searching each perform well enough and yield correct rankings? - // look into: http://wiki.apache.org/jakarta-lucene/LuceneFAQ#head-300f0756fdaa71f522c96a868351f716573f2d77 - // ie: http://lucene.apache.org/java/docs/api/org/apache/lucene/queryParser/MultiFieldQueryParser.html - QueryTerm term = new QueryTerm( RepositoryIndex.FIELDS[i], keyword ); - List results = searchAdvanced( new SingleTermQuery( term ), index ); - for ( Iterator iter = results.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - Set entrySet = map.entrySet(); - for ( Iterator it = entrySet.iterator(); it.hasNext(); ) - { - Map.Entry entry = (Map.Entry) it.next(); - SearchResult result2 = createSearchResult( result.getArtifact(), map, keyword, - (String) entry.getKey(), generalSearchResults ); - generalSearchResults.add( result2 ); - } - } - } - - return generalSearchResults; - } - - public List searchAdvanced( Query qry, RepositoryIndex index ) - throws RepositoryIndexSearchException - { - List searchResults = new ArrayList(); - - List hits = searcher.search( qry, index ); - for ( Iterator it = hits.iterator(); it.hasNext(); ) - { - RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) it.next(); - SearchResult result = new SearchResult(); - if ( hit.isHashMap() ) - { - Map map = (Map) hit.getObject(); - result.setArtifact( (Artifact) map.get( RepositoryIndex.ARTIFACT ) ); - - Map fields = new HashMap(); - fields.put( RepositoryIndex.FLD_CLASSES, map.get( RepositoryIndex.FLD_CLASSES ) ); - fields.put( RepositoryIndex.FLD_PACKAGES, map.get( RepositoryIndex.FLD_PACKAGES ) ); - fields.put( RepositoryIndex.FLD_FILES, map.get( RepositoryIndex.FLD_FILES ) ); - fields.put( RepositoryIndex.FLD_PACKAGING, map.get( RepositoryIndex.FLD_PACKAGING ) ); - fields.put( RepositoryIndex.FLD_SHA1, map.get( RepositoryIndex.FLD_SHA1 ) ); - fields.put( RepositoryIndex.FLD_MD5, map.get( RepositoryIndex.FLD_MD5 ) ); - - // TODO! this doesn't seem like the correct way to determine what matched - result.setFieldMatches( fields ); - searchResults.add( result ); - } - else if ( hit.isModel() ) - { - Model model = (Model) hit.getObject(); - for ( int i = 0; i < RepositoryIndex.MODEL_FIELDS.length; i++ ) - { - result = createSearchResult( model, RepositoryIndex.MODEL_FIELDS[i], searchResults ); - searchResults.add( result ); - } - } - else if ( hit.isMetadata() ) - { - //@todo what about metadata objects? -// RepositoryMetadata metadata = (RepositoryMetadata) hit.getObject(); - } - } - - return searchResults; - } - - /** - * Method for checking if the artifact already exists in the search result list. - * - * @param groupId the group id of the artifact - * @param artifactId the artifact id of the artifact - * @param version the version of the artifact - * @return the int index number of the artifact in the search result - */ - private int getListIndex( String groupId, String artifactId, String version, List list ) - { - int index = 0; - for ( Iterator iter = list.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - if ( artifact.getGroupId().equals( groupId ) && artifact.getArtifactId().equals( artifactId ) && - artifact.getVersion().equals( version ) ) - { - return index; - } - index++; - } - return -1; - } - - /** - * Method to create the unique artifact id to represent the artifact in the repository - * - * @param groupId the artifact groupId - * @param artifactId the artifact artifactId - * @param version the artifact version - * @return the String id to uniquely represent the artifact - */ - private String getId( String groupId, String artifactId, String version ) - { - return groupId + ":" + artifactId + ":" + version; - } - - /** - * Method to get the matching values (packages, classes and files) in the - * given string to be tokenized. - * - * @param tokenizeStr the string to be tokenized - * @param key the map key - * @param resultMap the map to be populated - * @param keyword the value to be matched - * @return the map that contains the matched values - */ - private Map getArtifactHits( String tokenizeStr, String key, Map resultMap, String keyword ) - { - List values = new ArrayList(); - StringTokenizer st = new StringTokenizer( tokenizeStr, "\n" ); - while ( st.hasMoreTokens() ) - { - String str = st.nextToken(); - if ( str.toLowerCase().indexOf( keyword.toLowerCase() ) != -1 ) - { - values.add( str ); - } - } - - if ( !values.isEmpty() ) - { - resultMap.put( key, values ); - } - - return resultMap; - } - - /** - * Method to create SearchResult object from a given HashMap. Used for general search results - * - * @param artifact the retrieved artifact from the index - * @param map the HashMap object that contains the values for the search result - * @param keyword the query term - * @return the SearchResult object - */ - private SearchResult createSearchResult( Artifact artifact, Map map, String keyword, String field, - List generalSearchResults ) - { - int index = getListIndex( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), - generalSearchResults ); - SearchResult result; - Map resultMap; - - if ( index > -1 ) - { - result = (SearchResult) generalSearchResults.remove( index ); - resultMap = result.getFieldMatches(); - } - else - { - result = new SearchResult(); - result.setArtifact( artifact ); - resultMap = new HashMap(); - } - - // the searched field is either the class, package or file field - if ( field.equals( RepositoryIndex.FLD_CLASSES ) || field.equals( RepositoryIndex.FLD_PACKAGES ) || - field.equals( RepositoryIndex.FLD_FILES ) ) - { - resultMap = getArtifactHits( (String) map.get( field ), field, resultMap, keyword ); - } - else if ( field.equals( RepositoryIndex.FLD_SHA1 ) || - ( field.equals( RepositoryIndex.FLD_MD5 ) || field.equals( RepositoryIndex.FLD_PACKAGING ) ) ) - { - if ( map.get( field ) != null ) - { - // the searched field is either the md5, sha1 or packaging field - if ( ( (String) map.get( field ) ).toLowerCase().equals( keyword.toLowerCase() ) || - ( (String) map.get( field ) ).toLowerCase().indexOf( keyword.toLowerCase() ) != -1 ) - { - resultMap.put( field, map.get( field ) ); - } - } - } - else if ( field.equals( RepositoryIndex.FLD_DEPENDENCIES ) || - field.equals( RepositoryIndex.FLD_PLUGINS_BUILD ) || field.equals( RepositoryIndex.FLD_PLUGINS_REPORT ) || - field.equals( RepositoryIndex.FLD_LICENSE_URLS ) ) - { - List contents = (List) map.get( field ); - List values = new ArrayList(); - for ( Iterator it = contents.iterator(); it.hasNext(); ) - { - String str = (String) it.next(); - if ( str.toLowerCase().equals( keyword.toLowerCase() ) ) - { - values.add( str ); - } - } - if ( values.size() > 0 ) - { - resultMap.put( field, values ); - } - } - result.setFieldMatches( resultMap ); - - return result; - } - - /** - * Method to create a SearchResult object from the given model. Used for advanced search results - * - * @param model the Model object that contains the values for the search result - * @param field the field whose value is to be retrieved - * @return a SearchResult object - */ - private SearchResult createSearchResult( Model model, String field, List searchResults ) - { - String groupId = model.getGroupId(); - if ( groupId == null ) - { - groupId = model.getParent().getGroupId(); - } - String version = model.getVersion(); - if ( version == null ) - { - version = model.getParent().getVersion(); - } - int index = getListIndex( groupId, model.getArtifactId(), version, searchResults ); - SearchResult result; - Map map; - - // the object already exists in the search result list - if ( index > -1 ) - { - result = (SearchResult) searchResults.remove( index ); - map = result.getFieldMatches(); - } - else - { - result = new SearchResult(); - result.setArtifact( - factory.createBuildArtifact( groupId, model.getArtifactId(), version, model.getPackaging() ) ); - map = new HashMap(); - } - - // get the matched value with the query term - List values = new ArrayList(); - if ( field.equals( RepositoryIndex.FLD_LICENSE_URLS ) ) - { - values = getLicenseUrls( model ); - } - else if ( field.equals( RepositoryIndex.FLD_DEPENDENCIES ) ) - { - values = getDependencies( model ); - } - else if ( field.equals( RepositoryIndex.FLD_PLUGINS_BUILD ) ) - { - if ( model.getBuild() != null && model.getBuild().getPlugins() != null ) - { - values = getBuildPlugins( model ); - } - } - else if ( field.equals( RepositoryIndex.FLD_PLUGINS_REPORT ) ) - { - if ( model.getReporting() != null && model.getReporting().getPlugins() != null ) - { - values = getReportPlugins( model ); - } - } - else if ( field.equals( RepositoryIndex.FLD_PACKAGING ) ) - { - if ( model.getPackaging() != null ) - { - map.put( RepositoryIndex.FLD_PACKAGING, model.getPackaging() ); - } - } - - if ( !values.isEmpty() ) - { - map.put( field, values ); - } - result.setFieldMatches( map ); - - return result; - } - - /** - * Method for getting the query term hits or matches in the pom's license urls. - * - * @param model the Model object that contains the pom values - * @return a List of matched license urls - */ - private List getLicenseUrls( Model model ) - { - List licenseUrls = new ArrayList(); - List licenseList = model.getLicenses(); - for ( Iterator it = licenseList.iterator(); it.hasNext(); ) - { - License license = (License) it.next(); - licenseUrls.add( license.getUrl() ); - } - return licenseUrls; - } - - /** - * Method for getting the hits or matches in the dependencies specified in the pom - * - * @param model the Model object that contains the pom values - * @return a List of matched dependencies - */ - private List getDependencies( Model model ) - { - List dependencies = new ArrayList(); - List dependencyList = model.getDependencies(); - for ( Iterator it = dependencyList.iterator(); it.hasNext(); ) - { - Dependency dep = (Dependency) it.next(); - dependencies.add( getId( dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ) ); - } - - return dependencies; - } - - /** - * Method for getting the hits or matches in the build plugins specified in the pom - * - * @param model the Model object that contains the pom values - * @return a List of matched build plugins - */ - private List getBuildPlugins( Model model ) - { - List values = new ArrayList(); - List plugins = model.getBuild().getPlugins(); - for ( Iterator it = plugins.iterator(); it.hasNext(); ) - { - Plugin plugin = (Plugin) it.next(); - values.add( getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ) ); - } - - return values; - } - - /** - * Method for getting the hits or matches in the reporting plugins specified in the pom - * - * @param model the Model object that contains the pom values - * @return a List of matched reporting plugins - */ - private List getReportPlugins( Model model ) - { - List values = new ArrayList(); - List plugins = model.getReporting().getPlugins(); - for ( Iterator it = plugins.iterator(); it.hasNext(); ) - { - ReportPlugin plugin = (ReportPlugin) it.next(); - values.add( getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ) ); - } - - return values; - } - -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java deleted file mode 100644 index c636dbb8e..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java +++ /dev/null @@ -1,327 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.lucene.document.Document; -import org.apache.lucene.index.Term; -import org.apache.lucene.queryParser.ParseException; -import org.apache.lucene.queryParser.QueryParser; -import org.apache.lucene.search.BooleanClause; -import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.Hits; -import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.search.TermQuery; -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.metadata.ArtifactRepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.Metadata; -import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; -import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.repository.indexing.query.CompoundQuery; -import org.apache.maven.repository.indexing.query.CompoundQueryTerm; -import org.apache.maven.repository.indexing.query.Query; -import org.apache.maven.repository.indexing.query.RangeQuery; -import org.apache.maven.repository.indexing.query.SingleTermQuery; -import org.codehaus.plexus.logging.AbstractLogEnabled; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.net.MalformedURLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.StringTokenizer; - -/** - * Implementation Class for searching through the index. - * - * @plexus.component role="org.apache.maven.repository.indexing.RepositoryIndexSearcher" - */ -public class DefaultRepositoryIndexSearcher - extends AbstractLogEnabled - implements RepositoryIndexSearcher -{ - /** - * @plexus.requirement - */ - private ArtifactFactory factory; - - public List search( Query query, RepositoryIndex index ) - throws RepositoryIndexSearchException - { - org.apache.lucene.search.Query luceneQuery; - try - { - luceneQuery = createLuceneQuery( query, index ); - } - catch ( ParseException e ) - { - throw new RepositoryIndexSearchException( "Unable to construct query: " + e.getMessage(), e ); - } - - IndexSearcher searcher; - try - { - searcher = new IndexSearcher( index.getIndexPath().getAbsolutePath() ); - } - catch ( IOException e ) - { - throw new RepositoryIndexSearchException( "Unable to open index: " + e.getMessage(), e ); - } - - List docs = new ArrayList(); - try - { - Hits hits = searcher.search( luceneQuery ); - for ( int i = 0; i < hits.length(); i++ ) - { - Document doc = hits.doc( i ); - docs.add( createSearchedObjectFromIndexDocument( doc, index.getRepository() ) ); - } - } - catch ( MalformedURLException e ) - { - throw new RepositoryIndexSearchException( "Unable to search index: " + e.getMessage(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexSearchException( "Unable to search index: " + e.getMessage(), e ); - } - finally - { - try - { - searcher.close(); - } - catch ( IOException e ) - { - getLogger().error( "Unable to close index searcher", e ); - } - } - - return docs; - } - - private org.apache.lucene.search.Query createLuceneQuery( Query query, RepositoryIndex index ) - throws ParseException - { - org.apache.lucene.search.Query luceneQuery = null; - // TODO: hacked in temporarily - if ( query instanceof CompoundQuery ) - { - BooleanQuery booleanQuery = new BooleanQuery(); - List queries = ( (CompoundQuery) query ).getCompoundQueryTerms(); - for ( Iterator i = queries.iterator(); i.hasNext(); ) - { - CompoundQueryTerm queryTerm = (CompoundQueryTerm) i.next(); - - booleanQuery.add( createLuceneQuery( queryTerm.getQuery(), index ), queryTerm.isRequired() - ? BooleanClause.Occur.MUST - : queryTerm.isProhibited() ? BooleanClause.Occur.MUST_NOT : BooleanClause.Occur.SHOULD ); - } - luceneQuery = booleanQuery; - } - else if ( query instanceof SingleTermQuery ) - { - org.apache.lucene.search.Query qry; - if ( index.isKeywordField( ( (SingleTermQuery) query ).getField() ) ) - { - qry = new TermQuery( - new Term( ( (SingleTermQuery) query ).getField(), ( (SingleTermQuery) query ).getValue() ) ); - } - else - { - // TODO: doesn't seem like the right place for this here! - QueryParser parser = new QueryParser( ( (SingleTermQuery) query ).getField(), index.getAnalyzer() ); - qry = parser.parse( ( (SingleTermQuery) query ).getValue() ); - } - luceneQuery = qry; - } - else if ( query instanceof RangeQuery ) - { - Term beginTerm = null; - if ( ( (RangeQuery) query ).getBegin() != null ) - { - beginTerm = new Term( ( (RangeQuery) query ).getBegin().getField(), - ( (RangeQuery) query ).getBegin().getValue() ); - } - Term endTerm = null; - if ( ( (RangeQuery) query ).getEnd() != null ) - { - endTerm = - new Term( ( (RangeQuery) query ).getEnd().getField(), ( (RangeQuery) query ).getEnd().getValue() ); - } - luceneQuery = - new org.apache.lucene.search.RangeQuery( beginTerm, endTerm, ( (RangeQuery) query ).isInclusive() ); - } - return luceneQuery; - } - - private RepositoryIndexSearchHit createSearchedObjectFromIndexDocument( Document doc, - ArtifactRepository repository ) - throws RepositoryIndexSearchException - { - RepositoryIndexSearchHit searchHit = null; - - // the document is of type artifact - String groupId = doc.get( RepositoryIndex.FLD_GROUPID ); - String artifactId = doc.get( RepositoryIndex.FLD_ARTIFACTID ); - String version = doc.get( RepositoryIndex.FLD_VERSION ); - if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.ARTIFACT ) ) - { - String packaging = doc.get( RepositoryIndex.FLD_PACKAGING ); - Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version, packaging ); - - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - - // TODO: introduce strongly types search result! - Map map = new HashMap(); - map.put( RepositoryIndex.ARTIFACT, artifact ); - map.put( RepositoryIndex.FLD_CLASSES, doc.get( RepositoryIndex.FLD_CLASSES ) ); - map.put( RepositoryIndex.FLD_PACKAGES, doc.get( RepositoryIndex.FLD_PACKAGES ) ); - map.put( RepositoryIndex.FLD_FILES, doc.get( RepositoryIndex.FLD_FILES ) ); - map.put( RepositoryIndex.FLD_MD5, doc.get( RepositoryIndex.FLD_MD5 ) ); - map.put( RepositoryIndex.FLD_SHA1, doc.get( RepositoryIndex.FLD_SHA1 ) ); - map.put( RepositoryIndex.FLD_PACKAGING, doc.get( RepositoryIndex.FLD_PACKAGING ) ); - - searchHit = new RepositoryIndexSearchHit( true, false, false ); - searchHit.setObject( map ); - } - // the document is of type model - else if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.POM ) ) - { - Artifact pomArtifact = factory.createProjectArtifact( groupId, artifactId, version ); - - // TODO: introduce strongly types search result! Don't read the POM here, though - populate with the data from the index - searchHit = new RepositoryIndexSearchHit( false, false, true ); - searchHit.setObject( readPom( pomArtifact, repository ) ); - } - // the document is of type metadata - else if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.METADATA ) ) - { - List pathParts = new ArrayList(); - StringTokenizer st = new StringTokenizer( doc.get( RepositoryIndex.FLD_NAME ), "/\\" ); - while ( st.hasMoreTokens() ) - { - pathParts.add( st.nextToken() ); - } - - Collections.reverse( pathParts ); - String tmpDir = (String) pathParts.get( 1 ); - - RepositoryMetadata repoMetadata; - - if ( tmpDir.equals( version ) ) - { - repoMetadata = new SnapshotArtifactRepositoryMetadata( - factory.createProjectArtifact( groupId, artifactId, version ) ); - } - else if ( tmpDir.equals( artifactId ) ) - { - repoMetadata = - new ArtifactRepositoryMetadata( factory.createProjectArtifact( groupId, artifactId, version ) ); - } - else - { - repoMetadata = new GroupRepositoryMetadata( groupId ); - } - - // TODO: introduce strongly types search result! Don't read the metadata here, though - populate with the data from the index - repoMetadata.setMetadata( readMetadata( repoMetadata, repository ) ); - - searchHit = new RepositoryIndexSearchHit( false, true, false ); - searchHit.setObject( repoMetadata ); - } - - return searchHit; - } - - private Metadata readMetadata( RepositoryMetadata repoMetadata, ArtifactRepository repository ) - throws RepositoryIndexSearchException - { - File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repoMetadata ) ); - - MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); - - FileReader reader = null; - try - { - reader = new FileReader( file ); - return metadataReader.read( reader ); - } - catch ( FileNotFoundException e ) - { - throw new RepositoryIndexSearchException( "Unable to find metadata file: " + e.getMessage(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexSearchException( "Unable to read metadata file: " + e.getMessage(), e ); - } - catch ( XmlPullParserException xe ) - { - throw new RepositoryIndexSearchException( "Unable to parse metadata file: " + xe.getMessage(), xe ); - } - finally - { - IOUtil.close( reader ); - } - } - - private Model readPom( Artifact pomArtifact, ArtifactRepository repository ) - throws RepositoryIndexSearchException - { - File file = new File( repository.getBasedir(), repository.pathOf( pomArtifact ) ); - - MavenXpp3Reader r = new MavenXpp3Reader(); - - FileReader reader = null; - try - { - reader = new FileReader( file ); - return r.read( reader ); - } - catch ( FileNotFoundException e ) - { - throw new RepositoryIndexSearchException( "Unable to find requested POM: " + e.getMessage(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexSearchException( "Unable to read POM: " + e.getMessage(), e ); - } - catch ( XmlPullParserException xe ) - { - throw new RepositoryIndexSearchException( "Unable to parse POM: " + xe.getMessage(), xe ); - } - finally - { - IOUtil.close( reader ); - } - } - -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java deleted file mode 100644 index 45c08f134..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.repository.digest.Digester; - -import java.io.File; - -/** - * @author Edwin Punzalan - * @plexus.component role="org.apache.maven.repository.indexing.RepositoryIndexingFactory" - */ -public class DefaultRepositoryIndexingFactory - implements RepositoryIndexingFactory -{ - /** - * @plexus.requirement - */ - private Digester digester; - - /** - * @plexus.requirement - */ - private ArtifactFactory artifactFactory; - - public ArtifactRepositoryIndex createArtifactRepositoryIndex( File indexPath, ArtifactRepository repository ) - throws RepositoryIndexException - { - return new ArtifactRepositoryIndex( indexPath, repository, digester ); - } - - public MetadataRepositoryIndex createMetadataRepositoryIndex( File indexPath, ArtifactRepository repository ) - throws RepositoryIndexException - { - return new MetadataRepositoryIndex( indexPath, repository ); - } - -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/EclipseRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/EclipseRepositoryIndex.java deleted file mode 100644 index a44c76f99..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/EclipseRepositoryIndex.java +++ /dev/null @@ -1,343 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.lucene.analysis.Analyzer; -import org.apache.lucene.analysis.CharTokenizer; -import org.apache.lucene.analysis.SimpleAnalyzer; -import org.apache.lucene.analysis.TokenStream; -import org.apache.lucene.document.DateTools; -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; -import org.apache.maven.repository.digest.Digester; -import org.apache.maven.repository.digest.DigesterException; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.IOUtil; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.Reader; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.Iterator; -import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipException; -import java.util.zip.ZipFile; -import java.util.zip.ZipOutputStream; - -/** - * @author Edwin Punzalan - */ -public class EclipseRepositoryIndex - extends AbstractRepositoryIndex -{ - // TODO: change constants to an enumerated type - /** - * Field name for the JAR filename. - */ - private static final String JAR_NAME = "j"; - - /** - * Field name for the JAR file size. - */ - private static final String JAR_SIZE = "s"; - - /** - * Field name for the JAR last modified timestamp. - */ - private static final String JAR_DATE = "d"; - - /** - * Field name for the list of classes in the JAR. - */ - private static final String NAMES = "c"; - - /** - * Field name for the MD5 checksum of the JAR. - */ - private static final String MD5 = "m"; - - private Digester digester; - - - /** - * Class constructor - * - * @param indexPath the path where the lucene index will be created/updated. - * @param repository the repository where the indexed artifacts are located - * @param digester the digester object to generate the checksum strings - */ - public EclipseRepositoryIndex( File indexPath, ArtifactRepository repository, Digester digester ) - throws RepositoryIndexException - { - super( indexPath, repository ); - - this.digester = digester; - } - - /** - * @see AbstractRepositoryIndex#getAnalyzer() - */ - public Analyzer getAnalyzer() - { - return new EclipseIndexAnalyzer( new SimpleAnalyzer() ); - } - - /** - * Indexes the artifacts inside the provided list - * - * @param artifactList - * @throws RepositoryIndexException - */ - public void indexArtifacts( List artifactList ) - throws RepositoryIndexException - { - List docs = new ArrayList(); - - for ( Iterator artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - Artifact artifact = (Artifact) artifacts.next(); - - Document doc = createDocument( artifact ); - - if ( doc != null ) - { - docs.add( doc ); - } - } - - addDocuments( docs ); - } - - /** - * Method to index a given artifact for use with the eclipse plugin - * - * @param artifact the Artifact object to be indexed - * @throws RepositoryIndexException - */ - public void indexArtifact( Artifact artifact ) - throws RepositoryIndexException - { - Document doc = createDocument( artifact ); - if ( doc != null ) - { - addDocuments( Collections.singletonList( doc ) ); - } - } - - /** - * Creates a Lucene Document from an artifact; used for index additions - * - * @param artifact - * @return - * @throws RepositoryIndexException - */ - private Document createDocument( Artifact artifact ) - throws RepositoryIndexException - { - Document doc = null; - - File artifactFile = artifact.getFile(); - if ( artifactFile != null && artifactFile.getName().endsWith( ".jar" ) && artifactFile.exists() ) - { - String md5; - try - { - md5 = digester.createChecksum( artifactFile, "MD5" ); - } - catch ( DigesterException e ) - { - throw new RepositoryIndexException( "Unable to compute checksum.", e ); - } - - StringBuffer classes; - try - { - // TODO: improve - classes = new StringBuffer(); - if ( "jar".equals( artifact.getType() ) ) - { - ZipFile jar = new ZipFile( artifact.getFile() ); - - for ( Enumeration entries = jar.entries(); entries.hasMoreElements(); ) - { - ZipEntry entry = (ZipEntry) entries.nextElement(); - addIfClassEntry( entry, classes ); - } - } - } - catch ( ZipException e ) - { - throw new RepositoryIndexException( "Error reading from artifact file: " + artifact.getFile(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Error reading from artifact file", e ); - } - - doc = new Document(); - doc.add( new Field( MD5, md5, Field.Store.YES, Field.Index.UN_TOKENIZED ) ); - doc.add( new Field( JAR_NAME, artifactFile.getName(), Field.Store.YES, Field.Index.TOKENIZED ) ); - doc.add( new Field( JAR_DATE, - DateTools.timeToString( artifactFile.lastModified(), DateTools.Resolution.SECOND ), - Field.Store.YES, Field.Index.UN_TOKENIZED ) ); - doc.add( new Field( JAR_SIZE, Long.toString( artifactFile.length() ), Field.Store.YES, - Field.Index.UN_TOKENIZED ) ); - doc.add( new Field( NAMES, classes.toString(), Field.Store.YES, Field.Index.TOKENIZED ) ); - } - - return doc; - } - - /** - * method to create an archived copy of the index contents - * - * @return File object to the archive - * @throws IOException - */ - public File getCompressedCopy() - throws IOException - { - File indexPath = getIndexPath(); - String name = indexPath.getName(); - - File outputFile = new File( indexPath.getParent(), name + ".zip" ); - FileUtils.fileDelete( outputFile.getAbsolutePath() ); - - ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( outputFile ) ); - zos.setLevel( 9 ); - - File[] files = indexPath.listFiles(); - try - { - for ( int i = 0; i < files.length; i++ ) - { - writeFile( zos, files[i] ); - } - } - finally - { - zos.close(); - } - - return outputFile; - } - - private static void writeFile( ZipOutputStream zos, File file ) - throws IOException - { - ZipEntry e = new ZipEntry( file.getName() ); - zos.putNextEntry( e ); - - FileInputStream is = new FileInputStream( file ); - try - { - IOUtil.copy( is, zos ); - } - finally - { - is.close(); - } - zos.flush(); - - zos.closeEntry(); - } - - /** - * Class used to analyze the lucene index - */ - private static class EclipseIndexAnalyzer - extends Analyzer - { - private Analyzer defaultAnalyzer; - - /** - * constructor to for this analyzer - * - * @param defaultAnalyzer the analyzer to use as default for the general fields of the artifact indeces - */ - EclipseIndexAnalyzer( Analyzer defaultAnalyzer ) - { - this.defaultAnalyzer = defaultAnalyzer; - } - - /** - * Method called by lucence during indexing operations - * - * @param fieldName the field name that the lucene object is currently processing - * @param reader a Reader object to the index stream - * @return an analyzer to specific to the field name or the default analyzer if none is present - */ - public TokenStream tokenStream( String fieldName, Reader reader ) - { - TokenStream tokenStream; - - if ( JAR_SIZE.equals( fieldName ) ) - { - tokenStream = new EclipseIndexTokenizer( reader ); - } - else if ( JAR_DATE.equals( fieldName ) ) - { - tokenStream = new EclipseIndexTokenizer( reader ); - } - else if ( MD5.equals( fieldName ) ) - { - tokenStream = new EclipseIndexTokenizer( reader ); - } - else - { - tokenStream = defaultAnalyzer.tokenStream( fieldName, reader ); - } - - return tokenStream; - } - } - - /** - * Class used to tokenize the eclipse index - */ - private static class EclipseIndexTokenizer - extends CharTokenizer - { - /** - * Constructor with the required reader to the index stream - * - * @param reader the Reader object of the index stream - */ - EclipseIndexTokenizer( Reader reader ) - { - super( reader ); - } - - /** - * method that lucene calls to check tokenization of a stream character - * - * @param character char currently being processed - * @return true if the char is a token, false if the char is a stop char - */ - protected boolean isTokenChar( char character ) - { - return true; - } - } -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java deleted file mode 100644 index df05fe3d4..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java +++ /dev/null @@ -1,218 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.lucene.document.Document; -import org.apache.lucene.document.Field; -import org.apache.lucene.index.Term; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.metadata.Metadata; -import org.apache.maven.artifact.repository.metadata.Plugin; -import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.Versioning; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -/** - * This class indexes the metadata in the repository. - */ -public class MetadataRepositoryIndex - extends AbstractRepositoryIndex -{ - /** - * Class Constructor - * - * @param indexPath the path to the index - * @param repository the repository where the metadata to be indexed is located - */ - public MetadataRepositoryIndex( File indexPath, ArtifactRepository repository ) - throws RepositoryIndexException - { - super( indexPath, repository ); - } - - /** - * Index the contents of the specified RepositoryMetadata parameter object - * - * @param repoMetadata the metadata object to be indexed - * @throws RepositoryIndexException - */ - public void indexMetadata( RepositoryMetadata repoMetadata ) - throws RepositoryIndexException - { - indexMetadata( Collections.singletonList( repoMetadata ) ); - } - - /** - * Index the metadata found within the provided list. Deletes existing entries in the index first before - * proceeding with the index additions. - * - * @param metadataList - * @throws RepositoryIndexException - */ - public void indexMetadata( List metadataList ) - throws RepositoryIndexException - { - try - { - deleteDocuments( getTermList( metadataList ) ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Failed to delete an index document", e ); - } - - addDocuments( getDocumentList( metadataList ) ); - } - - /** - * Creates a list of Lucene Term object used in index deletion - * - * @param metadataList - * @return List of Term object - */ - private List getTermList( List metadataList ) - { - List terms = new ArrayList(); - - for ( Iterator metadata = metadataList.iterator(); metadata.hasNext(); ) - { - RepositoryMetadata repoMetadata = (RepositoryMetadata) metadata.next(); - - terms.add( new Term( FLD_ID, (String) repoMetadata.getKey() ) ); - } - - return terms; - } - - /** - * Creates a list of Lucene documents - * - * @param metadataList - * @return List of Lucene Documents - */ - private List getDocumentList( List metadataList ) - { - List docs = new ArrayList(); - - for ( Iterator metadata = metadataList.iterator(); metadata.hasNext(); ) - { - RepositoryMetadata repoMetadata = (RepositoryMetadata) metadata.next(); - - docs.add( createDocument( repoMetadata ) ); - } - - return docs; - } - - /** - * Creates a Lucene Document from a RepositoryMetadata; used for index additions - * - * @param repoMetadata - * @return Lucene Document - */ - private Document createDocument( RepositoryMetadata repoMetadata ) - { - //get lastUpdated from Versioning (specified in Metadata object) - //get pluginPrefixes from Plugin (spcified in Metadata object) -----> concatenate/append??? - //get the metadatapath: check where metadata is located, then concatenate the groupId, - // artifactId, version based on its location - Document doc = new Document(); - doc.add( createKeywordField( FLD_ID, (String) repoMetadata.getKey() ) ); - Metadata metadata = repoMetadata.getMetadata(); - - doc.add( createTextField( FLD_NAME, repository.pathOfRemoteRepositoryMetadata( repoMetadata ) ) ); - - Versioning versioning = metadata.getVersioning(); - if ( versioning != null ) - { - doc.add( createTextField( FLD_LASTUPDATE, versioning.getLastUpdated() ) ); - } - else - { - doc.add( createTextField( FLD_LASTUPDATE, "" ) ); - } - - List plugins = metadata.getPlugins(); - String pluginAppended = ""; - for ( Iterator iter = plugins.iterator(); iter.hasNext(); ) - { - Plugin plugin = (Plugin) iter.next(); - if ( plugin.getPrefix() != null && !"".equals( plugin.getPrefix() ) ) - { - pluginAppended = plugin.getPrefix() + "\n"; - } - } - doc.add( createTextField( FLD_PLUGINPREFIX, pluginAppended ) ); - - if ( metadata.getGroupId() != null ) - { - doc.add( createTextField( FLD_GROUPID, metadata.getGroupId() ) ); - } - else - { - doc.add( createTextField( FLD_GROUPID, "" ) ); - } - - if ( metadata.getArtifactId() != null ) - { - doc.add( createTextField( FLD_ARTIFACTID, metadata.getArtifactId() ) ); - } - else - { - doc.add( createTextField( FLD_ARTIFACTID, "" ) ); - } - - if ( metadata.getVersion() != null ) - { - doc.add( createTextField( FLD_VERSION, metadata.getVersion() ) ); - } - else - { - doc.add( createTextField( FLD_VERSION, "" ) ); - } - // TODO: do we need to add all these empty fields? - doc.add( createTextField( FLD_DOCTYPE, METADATA ) ); - doc.add( createKeywordField( FLD_PACKAGING, "" ) ); - doc.add( createTextField( FLD_SHA1, "" ) ); - doc.add( createTextField( FLD_MD5, "" ) ); - doc.add( createTextField( FLD_CLASSES, "" ) ); - doc.add( createTextField( FLD_PACKAGES, "" ) ); - doc.add( createTextField( FLD_FILES, "" ) ); - doc.add( createKeywordField( FLD_LICENSE_URLS, "" ) ); - doc.add( createKeywordField( FLD_DEPENDENCIES, "" ) ); - doc.add( createKeywordField( FLD_PLUGINS_BUILD, "" ) ); - doc.add( createKeywordField( FLD_PLUGINS_REPORT, "" ) ); - doc.add( createKeywordField( FLD_PLUGINS_ALL, "" ) ); - return doc; - } - - private static Field createTextField( String name, String value ) - { - return new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ); - } - - private static Field createKeywordField( String name, String value ) - { - return new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ); - } -} 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 deleted file mode 100644 index 733128392..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java +++ /dev/null @@ -1,154 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.lucene.analysis.Analyzer; -import org.apache.maven.artifact.repository.ArtifactRepository; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; -import java.util.List; - -/** - * @author Edwin Punzalan - * @todo can we move all of these constants out of the interface? Perhaps they should be an enumerated type? - */ -public interface RepositoryIndex -{ - String POM = "POM"; - - String METADATA = "METADATA"; - - String ARTIFACT = "ARTIFACT"; - - String FLD_ID = "id"; - - String FLD_NAME = "name"; - - String FLD_DOCTYPE = "doctype"; - - String FLD_GROUPID = "groupId"; - - String FLD_ARTIFACTID = "artifactId"; - - String FLD_VERSION = "version"; - - String FLD_PACKAGING = "packaging"; - - String FLD_SHA1 = "sha1"; - - String FLD_MD5 = "md5"; - - String FLD_LASTUPDATE = "last update"; - - String FLD_PLUGINPREFIX = "plugin prefix"; - - String FLD_CLASSES = "class"; - - String FLD_PACKAGES = "package"; - - String FLD_FILES = "file"; - - String FLD_LICENSE_URLS = "license url"; - - String FLD_DEPENDENCIES = "dependency"; - - String FLD_PLUGINS_BUILD = "build plugin"; - - String FLD_PLUGINS_REPORT = "report plugin"; - - String FLD_PLUGINS_ALL = "plugins_all"; - - String[] FIELDS = {FLD_ID, FLD_NAME, FLD_DOCTYPE, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_PACKAGING, FLD_SHA1, - FLD_MD5, FLD_LASTUPDATE, FLD_PLUGINPREFIX, FLD_CLASSES, FLD_PACKAGES, FLD_FILES, FLD_LICENSE_URLS, - FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL}; - - List KEYWORD_FIELDS = Arrays.asList( new String[]{FLD_ID, FLD_PACKAGING, FLD_LICENSE_URLS, FLD_DEPENDENCIES, - FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL} ); - - String[] MODEL_FIELDS = {FLD_PACKAGING, FLD_LICENSE_URLS, FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT}; - - ArtifactRepository getRepository(); - - /** - * Method to encapsulate the optimize() method for lucene - * - * @throws RepositoryIndexException if there is a problem optimizing the index. - */ - void optimize() - throws RepositoryIndexException; - - /** - * Method to retrieve the lucene analyzer object used in creating the document fields for this index - * - * @return lucene Analyzer object used in creating the index fields - */ - Analyzer getAnalyzer(); - - /** - * Method to retrieve the path where the index is made available - * - * @return the path where the index resides - */ - File getIndexPath(); - - /** - * Tests an index field if it is a keyword field - * - * @param field the name of the index field to test - * @return true if the index field passed is a keyword, otherwise its false - */ - boolean isKeywordField( String field ); - - /** - * method for validating an index directory - * - * @throws RepositoryIndexException if the given indexPath is not valid for this type of RepositoryIndex - */ - void validate() - throws RepositoryIndexException, IOException; - - /** - * Opens the lucene index and add all the lucene documents inside the list into the index. - * Closes the index at the end. - * - * @param docList List of Lucene Documents - * @throws RepositoryIndexException when an error occurred during the indexing of the documents - */ - void addDocuments( List docList ) - throws RepositoryIndexException; - - /** - * Delete from the index matching the list of lucene Terms - * - * @param termList List of Lucene Term - * @throws RepositoryIndexException - * @throws IOException - */ - void deleteDocuments( List termList ) - throws RepositoryIndexException, IOException; - - /** - * Check if the index already exists. - * - * @return true if the index already exists - * @throws RepositoryIndexException - */ - boolean indexExists() - throws RepositoryIndexException; -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchHit.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchHit.java deleted file mode 100644 index c1b00f346..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchHit.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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. - */ - -/** - * This class is the object type contained in the list returned by the DefaultRepositoryIndexSearcher - */ -public class RepositoryIndexSearchHit -{ - private Object obj; - - private boolean isHashMap; - - private boolean isMetadata; - - private boolean isModel; - - /** - * Class constructor - * - * @param isHashMap indicates whether the object is a HashMap object - * @param isMetadata indicates whether the object is a RepositoryMetadata object - * @param isModel indicates whether the object is a Model object - */ - public RepositoryIndexSearchHit( boolean isHashMap, boolean isMetadata, boolean isModel ) - { - this.isHashMap = isHashMap; - this.isMetadata = isMetadata; - this.isModel = isModel; - } - - /** - * Getter method for obj variable - * - * @return the Object - */ - public Object getObject() - { - return obj; - } - - /** - * Setter method for obj variable - * - * @param obj - */ - public void setObject( Object obj ) - { - this.obj = obj; - } - - /** - * Method that indicates if the object is a HashMap - * - * @return boolean - */ - public boolean isHashMap() - { - return isHashMap; - } - - /** - * Method that indicates if the object is a RepositoryMetadata - * - * @return boolean - */ - public boolean isMetadata() - { - return isMetadata; - } - - /** - * Method that indicates if the object is a Model - * - * @return boolean - */ - public boolean isModel() - { - return isModel; - } - -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayer.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayer.java deleted file mode 100644 index 94ef76d53..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayer.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.repository.indexing.query.Query; - -import java.util.List; - -/** - * Repository search layer. - * - * @author Brett Porter - */ -public interface RepositoryIndexSearchLayer -{ - /** - * The Plexus component role name. - */ - String ROLE = RepositoryIndexSearchLayer.class.getName(); - - /** - * Method for searching the keyword in all the fields in the index. "Query everything" search. - * The index fields will be retrieved and query objects will be constructed using the - * optional (OR) CompoundQuery. - * - * @param keyword - * @param index - * @return - * @throws RepositoryIndexSearchException - * - */ - List searchGeneral( String keyword, RepositoryIndex index ) - throws RepositoryIndexSearchException; - - /** - * Method for "advanced search" of the index - * - * @param qry the query object that will be used for searching the index - * @param index - * @return - * @throws RepositoryIndexSearchException - * - */ - List searchAdvanced( Query qry, RepositoryIndex index ) - throws RepositoryIndexSearchException; -} 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 deleted file mode 100644 index 464e54728..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearcher.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.repository.indexing.query.Query; - -import java.util.List; - -/** - * - */ -public interface RepositoryIndexSearcher -{ - /** - * Plexus component role name. - */ - String ROLE = RepositoryIndexSearcher.class.getName(); - - /** - * Search the artifact based on the search criteria specified in the query object. Returns a list of - * artifact objects. - * - * @param query The query object that contains the search criteria. - * @param index - * @return List - * @throws RepositoryIndexSearchException - */ - List search( Query query, RepositoryIndex index ) - throws RepositoryIndexSearchException; -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java deleted file mode 100644 index 26b76ed89..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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 java.io.File; - - -/** - * @author Edwin Punzalan - */ -public interface RepositoryIndexingFactory -{ - String ROLE = RepositoryIndexingFactory.class.getName(); - - /** - * Method to create an instance of the ArtifactRepositoryIndex - * - * @param indexPath the path where the index will be created/updated - * @param repository the repository where the indexed artifacts are located - * @return the ArtifactRepositoryIndex instance - * @throws RepositoryIndexException - */ - ArtifactRepositoryIndex createArtifactRepositoryIndex( File indexPath, ArtifactRepository repository ) - throws RepositoryIndexException; - - /** - * Method to create instance of the MetadataRepositoryIndex - * - * @param indexPath the path where the index will be created/updated - * @param repository the repository where the indexed metadata are located - * @return the MetadataRepositoryIndex instance - * @throws RepositoryIndexException - */ - MetadataRepositoryIndex createMetadataRepositoryIndex( File indexPath, ArtifactRepository repository ) - throws RepositoryIndexException; - -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/SearchResult.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/SearchResult.java deleted file mode 100644 index a42027bc8..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/SearchResult.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.Artifact; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -/** - * This is the object type contained in the list that will be returned by the - * RepositoryIndexSearchLayer to the action class - */ -public class SearchResult -{ - private Artifact artifact; - - private Map fieldMatches; - - /** - * Class constructor - */ - public SearchResult() - { - fieldMatches = new HashMap(); - } - - /** - * Getter method for artifact - * - * @return Artifact - */ - public Artifact getArtifact() - { - return artifact; - } - - /** - * Setter method for artifact - * - * @param artifact - */ - public void setArtifact( Artifact artifact ) - { - this.artifact = artifact; - } - - /** - * Getter method for fieldMatches - * - * @return Map - */ - public Map getFieldMatches() - { - return fieldMatches; - } - - /** - * Setter method for fieldMatches - * - * @param fieldMatches - */ - public void setFieldMatches( Map fieldMatches ) - { - this.fieldMatches = fieldMatches; - } - - /** - * Getter method for derived value MapEntrySet - * - * @return Map - */ - public Set getFieldMatchesEntrySet() - { - return this.fieldMatches.entrySet(); - } -} 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 deleted file mode 100644 index b98d4d319..000000000 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java +++ /dev/null @@ -1,679 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.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; -import org.apache.maven.repository.digest.DefaultDigester; -import org.apache.maven.repository.digest.Digester; -import org.apache.maven.repository.digest.DigesterException; -import org.apache.maven.repository.indexing.query.CompoundQuery; -import org.apache.maven.repository.indexing.query.QueryTerm; -import org.apache.maven.repository.indexing.query.SingleTermQuery; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; - -import java.io.File; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -/** - * @author Edwin Punzalan - */ -public class ArtifactRepositoryIndexingTest - extends PlexusTestCase -{ - private ArtifactFactory artifactFactory; - - private ArtifactRepository repository; - - private File indexPath; - - private Digester digester; - - protected void setUp() - throws Exception - { - super.setUp(); - - File repositoryDirectory = getTestFile( "src/test/repository" ); - String repoDir = repositoryDirectory.toURI().toURL().toString(); - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); - digester = new DefaultDigester(); - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - - indexPath = getTestFile( "target/index" ); - FileUtils.deleteDirectory( indexPath ); - } - - public void testInheritedFields() - throws Exception - { - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - Artifact artifact = createArtifact( "test.inherited", "test-inherited", "1.0.15", "pom" ); - - ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - indexer.indexArtifact( artifact ); - - RepositoryIndexSearchLayer repoSearchLayer = - (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); - - // search version - QueryTerm queryTerm = new QueryTerm( RepositoryIndex.FLD_VERSION, "1.0.15" ); - List artifactList = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 1, artifactList.size() ); - for ( Iterator iter = artifactList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact a = result.getArtifact(); - assertEquals( "1.0.15", a.getVersion() ); - } - - // search group id - queryTerm = new QueryTerm( RepositoryIndex.FLD_GROUPID, "test.inherited" ); - artifactList = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 1, artifactList.size() ); - Iterator artifacts = artifactList.iterator(); - if ( artifacts.hasNext() ) - { - SearchResult result = (SearchResult) artifacts.next(); - Artifact a = result.getArtifact(); - assertEquals( "test.inherited", a.getGroupId() ); - } - - } - - /** - * Method for testing the exceptions thrown by ArtifactRepositoryIndex - * - * @throws Exception - */ - public void testIndexerExceptions() - throws Exception - { - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - - try - { - File notIndexDir = new File( "." ); - factory.createArtifactRepositoryIndex( notIndexDir, repository ); - fail( "Must throw an exception on a non-index directory" ); - } - catch ( RepositoryIndexException e ) - { - assertTrue( true ); - } - - try - { - File notIndexDir = new File( "pom.xml" ); - factory.createArtifactRepositoryIndex( notIndexDir, repository ); - fail( "Must throw exception on non-directory index directory" ); - } - catch ( RepositoryIndexException e ) - { - assertTrue( true ); - } - } - - /** - * Create an index that will be used for testing. - * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ], - * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ]. - * - * @throws Exception - */ - private void createTestIndex() - throws Exception - { - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - - List artifacts = new ArrayList(); - - Artifact artifact = createArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ); - artifacts.add( artifact ); - - artifact = createArtifact( "org.apache.maven", "maven-model", "2.0" ); - artifacts.add( artifact ); - - artifact = createArtifact( "test", "test-artifactId", "1.0" ); - artifacts.add( artifact ); - - artifact = createArtifact( "org.apache.maven", "maven-artifact", "2.0.1", "pom" ); - artifacts.add( artifact ); - - artifact = createArtifact( "org.apache.maven", "maven-model", "2.0", "pom" ); - artifacts.add( artifact ); - - artifact = createArtifact( "test", "test-artifactId", "1.0", "pom" ); - artifacts.add( artifact ); - - indexer.indexArtifacts( artifacts ); - } - - /** - * Test the ArtifactRepositoryIndex using a single-phrase search. - * - * @throws Exception - */ - public void testSearchSingle() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearchLayer repoSearchLayer = - (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); - - ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - - // search version - QueryTerm queryTerm = new QueryTerm( RepositoryIndex.FLD_VERSION, "1.0" ); - List artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 2, artifacts.size() ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "1.0", artifact.getVersion() ); - } - - // search group id - queryTerm = new QueryTerm( RepositoryIndex.FLD_GROUPID, "org.apache.maven" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 4, artifacts.size() ); - Iterator iter = artifacts.iterator(); - if ( iter.hasNext() ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - } - - // search artifact id - queryTerm = new QueryTerm( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 2, artifacts.size() ); - for ( iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - } - - // search version - queryTerm = new QueryTerm( RepositoryIndex.FLD_VERSION, "2" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 4, artifacts.size() ); - for ( iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertTrue( artifact.getVersion().indexOf( "2" ) != -1 ); - } - - // search classes - queryTerm = new QueryTerm( RepositoryIndex.FLD_CLASSES, "App" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 1, artifacts.size() ); - for ( iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "test-artifactId", artifact.getArtifactId() ); - } - - // search packages - queryTerm = new QueryTerm( RepositoryIndex.FLD_PACKAGES, "groupId" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 1, artifacts.size() ); - for ( iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "test-artifactId", artifact.getArtifactId() ); - } - - // search files - queryTerm = new QueryTerm( RepositoryIndex.FLD_FILES, "pom.xml" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 3, artifacts.size() ); - iter = artifacts.iterator(); - if ( iter.hasNext() ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "test-artifactId", artifact.getArtifactId() ); - } - - // search packaging - queryTerm = new QueryTerm( RepositoryIndex.FLD_PACKAGING, "jar" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 3, artifacts.size() ); - for ( iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - assertEquals( "jar", (String) map.get( RepositoryIndex.FLD_PACKAGING ) ); - } - - //search license url - queryTerm = new QueryTerm( RepositoryIndex.FLD_LICENSE_URLS, "http://www.apache.org/licenses/LICENSE-2.0.txt" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 2, artifacts.size() ); - for ( iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - List matches = (List) map.get( RepositoryIndex.FLD_LICENSE_URLS ); - for ( Iterator it = matches.iterator(); it.hasNext(); ) - { - assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) it.next() ); - } - } - - //search dependencies - queryTerm = new QueryTerm( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 2, artifacts.size() ); - for ( iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - boolean depFound = false; - List list = (List) map.get( RepositoryIndex.FLD_DEPENDENCIES ); - Iterator dependencies = list.iterator(); - while ( dependencies.hasNext() ) - { - String dep = (String) dependencies.next(); - if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( dep ) ) - { - depFound = true; - break; - } - } - assertTrue( "Searched dependency not found.", depFound ); - } - - //search build plugin - queryTerm = new QueryTerm( RepositoryIndex.FLD_PLUGINS_BUILD, "org.codehaus.modello:modello-maven-plugin:2.0" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 1, artifacts.size() ); - for ( iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_BUILD ); - Iterator plugins = list.iterator(); - boolean found = false; - while ( plugins.hasNext() ) - { - String plugin = (String) plugins.next(); - if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( plugin ) ) - { - found = true; - break; - } - } - assertTrue( "Searched plugin not found.", found ); - } - - //search reporting plugin - queryTerm = - new QueryTerm( RepositoryIndex.FLD_PLUGINS_REPORT, "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" ); - artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 1, artifacts.size() ); - for ( iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_REPORT ); - Iterator plugins = list.iterator(); - boolean found = false; - while ( plugins.hasNext() ) - { - String plugin = (String) plugins.next(); - if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals( plugin ) ) - { - found = true; - break; - } - } - assertTrue( "Searched report plugin not found.", found ); - } - - Artifact artifact = createArtifact( "org.apache.maven", "maven-model", "2.0" ); - - confirmChecksum( artifact, Digester.SHA1, RepositoryIndex.FLD_SHA1, repoSearchLayer, indexer ); - confirmChecksum( artifact, Digester.MD5, RepositoryIndex.FLD_MD5, repoSearchLayer, indexer ); - - artifact = createArtifact( "org.apache.maven", "maven-model", "2.0", "pom" ); - - confirmChecksum( artifact, Digester.SHA1, RepositoryIndex.FLD_SHA1, repoSearchLayer, indexer ); - confirmChecksum( artifact, Digester.MD5, RepositoryIndex.FLD_MD5, repoSearchLayer, indexer ); - } - - private void confirmChecksum( Artifact artifact, String algorithm, String field, - RepositoryIndexSearchLayer repoSearchLayer, ArtifactRepositoryIndex indexer ) - throws DigesterException, RepositoryIndexSearchException - { - String sha1 = digester.createChecksum( artifact.getFile(), algorithm ); - - QueryTerm queryTerm = new QueryTerm( field, sha1.trim() ); - List artifacts = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 1, artifacts.size() ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact2 = result.getArtifact(); - String sha1Tmp = digester.createChecksum( artifact2.getFile(), algorithm ); - assertEquals( sha1, sha1Tmp ); - } - } - - /** - * Test the ArtifactRepositoryIndex using compound search (AND, OR). - * - * @throws Exception - */ - public void testSearchCompound() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearchLayer repoSearchLayer = - (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); - - ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - - // Criteria 1: required query - // ex. artifactId=maven-artifact AND groupId=org.apache.maven - QueryTerm qry1 = new QueryTerm( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); - QueryTerm qry2 = new QueryTerm( RepositoryIndex.FLD_GROUPID, "org.apache.maven" ); - CompoundQuery rQry = new CompoundQuery(); - rQry.and( qry1 ); - rQry.and( qry2 ); - - List artifacts = repoSearchLayer.searchAdvanced( rQry, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - } - - // Criteria 2: nested required query - // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR - // version=2.0.3 - QueryTerm qry3 = new QueryTerm( RepositoryIndex.FLD_VERSION, "2.0.3" ); - CompoundQuery oQry = new CompoundQuery(); - oQry.or( rQry ); - oQry.or( qry3 ); - - artifacts = repoSearchLayer.searchAdvanced( oQry, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - } - - // Criteria 3: nested required query - // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND - // (version=2.0.3 OR version=2.0.1) - // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact) - QueryTerm qry4 = new QueryTerm( RepositoryIndex.FLD_VERSION, "2.0.1" ); - oQry = new CompoundQuery(); - oQry.or( qry3 ); - oQry.or( qry4 ); - - CompoundQuery oQry5 = new CompoundQuery(); - QueryTerm qry9 = new QueryTerm( RepositoryIndex.FLD_NAME, "maven-artifact-2.0.1.jar" ); - QueryTerm qry10 = new QueryTerm( RepositoryIndex.FLD_NAME, "maven-artifact" ); - oQry5.or( qry9 ); - oQry5.or( qry10 ); - - CompoundQuery rQry2 = new CompoundQuery(); - rQry2.or( oQry ); - rQry2.and( rQry ); - rQry2.and( oQry5 ); - - artifacts = repoSearchLayer.searchAdvanced( rQry2, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - assertEquals( "2.0.1", artifact.getVersion() ); - } - - CompoundQuery oQry6 = new CompoundQuery(); - QueryTerm qry11 = new QueryTerm( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); - QueryTerm qry12 = new QueryTerm( RepositoryIndex.FLD_DEPENDENCIES, - "org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" ); - oQry6.or( qry11 ); - oQry6.or( qry12 ); - - CompoundQuery rQry3 = new CompoundQuery(); - rQry3.or( oQry ); - rQry3.and( rQry ); - rQry3.and( oQry6 ); - - artifacts = repoSearchLayer.searchAdvanced( rQry3, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - assertEquals( "2.0.1", artifact.getVersion() ); - } - - // Criteria 4: nested required query - // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND - // (version=2.0.3 OR version=2.0.1) - // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] - // OR [(artifactId=sample AND groupId=test)] - CompoundQuery rQry4 = new CompoundQuery(); - QueryTerm qry5 = new QueryTerm( RepositoryIndex.FLD_ARTIFACTID, "sample" ); - QueryTerm qry6 = new QueryTerm( RepositoryIndex.FLD_GROUPID, "test" ); - rQry4.and( qry5 ); - rQry4.and( qry6 ); - CompoundQuery oQry2 = new CompoundQuery(); - oQry2.and( rQry4 ); - oQry2.and( rQry4 ); - - artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - assertEquals( "2.0.1", artifact.getVersion() ); - } - - // Criteria 4: nested required query - // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND - // (version=2.0.3 OR version=2.0.1) - // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR - // [(artifactId=sample AND groupId=test)] OR - // [(artifactId=sample2 AND groupId=test)] - CompoundQuery rQry5 = new CompoundQuery(); - QueryTerm qry7 = new QueryTerm( RepositoryIndex.FLD_ARTIFACTID, "sample2" ); - QueryTerm qry8 = new QueryTerm( RepositoryIndex.FLD_GROUPID, "test" ); - rQry5.and( qry7 ); - rQry5.and( qry8 ); - oQry2.and( rQry5 ); - - artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - } - } - - /** - * Test the exceptions thrown by DefaultRepositoryIndexSearcher - * - * @throws Exception - */ - public void testSearchExceptions() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearchLayer repoSearchLayer = - (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); - - ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - - try - { - QueryTerm queryTerm = new QueryTerm( RepositoryIndex.FLD_VERSION, "~~~~~" ); - repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - fail( "Must throw an exception on unparseable query." ); - } - catch ( RepositoryIndexSearchException re ) - { - assertTrue( true ); - } - - indexer = factory.createArtifactRepositoryIndex( getTestFile( "target/index/sample" ), repository ); - - try - { - QueryTerm queryTerm = new QueryTerm( RepositoryIndex.FLD_VERSION, "1.0" ); - repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - fail( "Must throw an exception on invalid index location." ); - } - catch ( RepositoryIndexSearchException re ) - { - assertTrue( true ); - } - - } - - /** - * Test delete of document from the artifact index. - * - * @throws Exception - */ - public void testDeleteArtifactDocument() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearcher repoSearcher = (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE ); - - ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - - Artifact artifact = createArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ); - Artifact pomArtifact = - createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "pom" ); - - QueryTerm queryTerm = - new QueryTerm( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + ":" + artifact.getId() ); - List results = repoSearcher.search( new SingleTermQuery( queryTerm ), indexer ); - assertFalse( results.isEmpty() ); - - queryTerm = new QueryTerm( RepositoryIndex.FLD_ID, RepositoryIndex.POM + ":" + pomArtifact.getId() ); - results = repoSearcher.search( new SingleTermQuery( queryTerm ), indexer ); - assertFalse( results.isEmpty() ); - - indexer.deleteArtifact( artifact ); - indexer.deleteArtifact( pomArtifact ); - - queryTerm = new QueryTerm( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + ":" + artifact.getId() ); - results = repoSearcher.search( new SingleTermQuery( queryTerm ), indexer ); - assertTrue( results.isEmpty() ); - - queryTerm = new QueryTerm( RepositoryIndex.FLD_ID, RepositoryIndex.POM + ":" + pomArtifact.getId() ); - results = repoSearcher.search( new SingleTermQuery( queryTerm ), indexer ); - assertTrue( results.isEmpty() ); - } - - /** - * Test delete of document from the artifact index. - * - * @throws Exception - */ - public void testCorruptJar() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearcher repoSearcher = (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE ); - - ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - - Artifact artifact = createArtifact( "org.apache.maven", "maven-corrupt-jar", "2.0" ); - indexer.indexArtifact( artifact ); - - QueryTerm queryTerm = new QueryTerm( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() ); - List artifacts = repoSearcher.search( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 0, artifacts.size() ); - } - - /** - * Method for creating artifact object - * - * @param groupId the groupId of the artifact to be created - * @param artifactId the artifactId of the artifact to be created - * @param version the version of the artifact to be created - * @return Artifact object - * @throws Exception - */ - private Artifact createArtifact( String groupId, String artifactId, String version ) - throws Exception - { - return createArtifact( groupId, artifactId, version, "jar" ); - } - - private Artifact createArtifact( String groupId, String artifactId, String version, String type ) - { - Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, type ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - return artifact; - } - - protected void tearDown() - throws Exception - { - repository = null; - - super.tearDown(); - } -} diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/EclipseRepositoryIndexTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/EclipseRepositoryIndexTest.java deleted file mode 100644 index 042cbfaa2..000000000 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/EclipseRepositoryIndexTest.java +++ /dev/null @@ -1,238 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.lucene.document.DateTools; -import org.apache.lucene.document.Document; -import org.apache.lucene.queryParser.QueryParser; -import org.apache.lucene.search.Hits; -import org.apache.lucene.search.IndexSearcher; -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; -import org.apache.maven.repository.digest.DefaultDigester; -import org.apache.maven.repository.digest.Digester; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -/** - * @author Edwin Punzalan - */ -public class EclipseRepositoryIndexTest - extends PlexusTestCase -{ - private ArtifactFactory artifactFactory; - - private ArtifactRepository repository; - - private File indexPath; - - private Digester digester; - - private long artifactFileTime; - - private static final long TIME_DIFFERENCE = 10000L; - - protected void setUp() - throws Exception - { - super.setUp(); - - File repositoryDirectory = getTestFile( "src/test/repository" ); - String repoDir = repositoryDirectory.toURL().toString(); - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); - digester = new DefaultDigester(); - - indexPath = getTestFile( "target/index" ); - FileUtils.deleteDirectory( indexPath ); - } - - /** - * Create an index that will be used for testing. - * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ], - * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ]. - * - * @throws Exception - */ - private EclipseRepositoryIndex createTestIndex() - throws Exception - { - EclipseRepositoryIndex indexer = new EclipseRepositoryIndex( indexPath, repository, new DefaultDigester() ); - - List artifacts = new ArrayList(); - - Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifactFileTime = artifact.getFile().lastModified(); - artifacts.add( artifact ); - - long historicTime = artifactFileTime - TIME_DIFFERENCE; - - artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifact.getFile().setLastModified( historicTime ); - artifacts.add( artifact ); - - artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifact.getFile().setLastModified( historicTime ); - artifacts.add( artifact ); - - indexer.indexArtifacts( artifacts ); - - artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifact.getFile().setLastModified( historicTime ); - indexer.indexArtifact( artifact ); - - indexer.optimize(); - - return indexer; - } - - /** - * Method for testing the exceptions thrown by ArtifactRepositoryIndex - * - * @throws Exception - */ - public void testIndexerExceptions() - throws Exception - { - Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - - try - { - File notIndexDir = new File( "pom.xml" ); - EclipseRepositoryIndex indexer = new EclipseRepositoryIndex( notIndexDir, repository, digester ); - indexer.indexArtifact( artifact ); - fail( "Must throw exception on non-directory index directory" ); - } - catch ( RepositoryIndexException e ) - { - assertTrue( true ); - } - - try - { - File notIndexDir = new File( "." ); - EclipseRepositoryIndex indexer = new EclipseRepositoryIndex( notIndexDir, repository, digester ); - indexer.indexArtifact( artifact ); - fail( "Must throw an exception on a non-index directory" ); - } - catch ( RepositoryIndexException e ) - { - assertTrue( true ); - } - } - - /** - * Test the ArtifactRepositoryIndex using a single-phrase search. - * - * @throws Exception - */ - public void testSearch() - throws Exception - { - EclipseRepositoryIndex index = createTestIndex(); - - IndexSearcher searcher = new IndexSearcher( index.getIndexPath().getAbsolutePath() ); - try - { - QueryParser parser = new QueryParser( "j", index.getAnalyzer() ); - Hits hits = searcher.search( parser.parse( "maven-artifact-2.0.1.jar" ) ); - - assertEquals( "Total hits", 1, hits.length() ); - Document doc = hits.doc( 0 ); - assertEquals( "Check jar name", "maven-artifact-2.0.1.jar", doc.get( "j" ) ); - - parser = new QueryParser( "s", index.getAnalyzer() ); - hits = searcher.search( parser.parse( "78377" ) ); - assertEquals( "Total hits", 1, hits.length() ); - - doc = hits.doc( 0 ); - assertEquals( "Check jar name", "maven-artifact-2.0.1.jar", doc.get( "j" ) ); - - parser = new QueryParser( "d", index.getAnalyzer() ); - hits = searcher.search( - parser.parse( DateTools.timeToString( artifactFileTime, DateTools.Resolution.SECOND ) ) ); - - assertEquals( "Total hits", 1, hits.length() ); - doc = hits.doc( 0 ); - assertEquals( "Check jar name", "maven-artifact-2.0.1.jar", doc.get( "j" ) ); - - parser = new QueryParser( "m", index.getAnalyzer() ); - hits = searcher.search( parser.parse( "AE55D9B5720E11B6CF19FE1E31A42E51" ) ); - assertEquals( "Total hits", 1, hits.length() ); - - doc = hits.doc( 0 ); - assertEquals( "Check jar name", "maven-artifact-2.0.1.jar", doc.get( "j" ) ); - - parser = new QueryParser( "c", index.getAnalyzer() ); - hits = searcher.search( parser.parse( "MavenXpp3Reader" ) ); - - assertEquals( "Total hits", 1, hits.length() ); - doc = hits.doc( 0 ); - assertEquals( "Check jar name", "maven-model-2.0.jar", doc.get( "j" ) ); - - parser = new QueryParser( "j", index.getAnalyzer() ); - hits = searcher.search( parser.parse( "maven" ) ); - - assertEquals( "Total hits", 2, hits.length() ); - } - finally - { - searcher.close(); - } - } - - /** - * Method for creating artifact object - * - * @param groupId the groupId of the artifact to be created - * @param artifactId the artifactId of the artifact to be created - * @param version the version of the artifact to be created - * @return Artifact object - * @throws Exception - */ - private Artifact getArtifact( String groupId, String artifactId, String version ) - throws Exception - { - if ( artifactFactory == null ) - { - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - } - - return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); - } - - protected void tearDown() - throws Exception - { - repository = null; - - super.tearDown(); - } -} diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java deleted file mode 100644 index de3b257c5..000000000 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java +++ /dev/null @@ -1,286 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.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; -import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.Metadata; -import org.apache.maven.artifact.repository.metadata.Plugin; -import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.Versioning; -import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; -import org.apache.maven.repository.indexing.query.QueryTerm; -import org.apache.maven.repository.indexing.query.RangeQuery; -import org.apache.maven.repository.indexing.query.SingleTermQuery; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** - * This class tests the MetadataRepositoryIndex. - */ -public class MetadataRepositoryIndexingTest - extends PlexusTestCase -{ - private ArtifactRepository repository; - - private File indexPath; - - private ArtifactFactory artifactFactory; - - /** - * Set up. - * - * @throws Exception - */ - public void setUp() - throws Exception - { - super.setUp(); - File repositoryDirectory = getTestFile( "src/test/repository" ); - String repoDir = repositoryDirectory.toURL().toString(); - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); - - indexPath = getTestFile( "target/index" ); - FileUtils.deleteDirectory( indexPath ); - } - - /** - * Tear down. - * - * @throws Exception - */ - public void tearDown() - throws Exception - { - repository = null; - super.tearDown(); - } - - /** - * Create the test index. - * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ], - * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ]. - * - * @throws Exception - */ - private void createTestIndex() - throws Exception - { - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository ); - - List metadataList = new ArrayList(); - - RepositoryMetadata repoMetadata = new GroupRepositoryMetadata( "org.apache.maven" ); - repoMetadata.setMetadata( readMetadata( repoMetadata ) ); - metadataList.add( repoMetadata ); - - repoMetadata = new ArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ) ); - repoMetadata.setMetadata( readMetadata( repoMetadata ) ); - metadataList.add( repoMetadata ); - - repoMetadata = - new SnapshotArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ) ); - repoMetadata.setMetadata( readMetadata( repoMetadata ) ); - metadataList.add( repoMetadata ); - - repoMetadata = new GroupRepositoryMetadata( "test" ); - repoMetadata.setMetadata( readMetadata( repoMetadata ) ); - metadataList.add( repoMetadata ); - - indexer.indexMetadata( metadataList ); - indexer.optimize(); - } - - /** - * Test the ArtifactRepositoryIndex using a single-phrase search. - * - * @throws Exception - */ - public void testSearch() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearchLayer repoSearchLayer = - (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); - - MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository ); - - // search last update - QueryTerm queryTerm = new QueryTerm( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" ); - List metadataList = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - //assertEquals( 1, metadataList.size() ); - for ( Iterator iter = metadataList.iterator(); iter.hasNext(); ) - { - RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next(); - if ( hit.isMetadata() ) - { - RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject(); - Metadata metadata = repoMetadata.getMetadata(); - Versioning versioning = metadata.getVersioning(); - assertEquals( "20051212044643", versioning.getLastUpdated() ); - } - } - - // search plugin prefix - queryTerm = new QueryTerm( RepositoryIndex.FLD_PLUGINPREFIX, "org.apache.maven" ); - metadataList = repoSearchLayer.searchAdvanced( new SingleTermQuery( queryTerm ), indexer ); - //assertEquals( 1, metadataList.size() ); - for ( Iterator iter = metadataList.iterator(); iter.hasNext(); ) - { - RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next(); - if ( hit.isMetadata() ) - { - RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject(); - Metadata metadata = repoMetadata.getMetadata(); - List plugins = metadata.getPlugins(); - for ( Iterator it = plugins.iterator(); it.hasNext(); ) - { - Plugin plugin = (Plugin) it.next(); - assertEquals( "org.apache.maven", plugin.getPrefix() ); - } - } - } - - // search last update using INCLUSIVE Range Query - QueryTerm qry1 = new QueryTerm( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" ); - QueryTerm qry2 = new QueryTerm( RepositoryIndex.FLD_LASTUPDATE, "20051212235959" ); - RangeQuery rQry = RangeQuery.createInclusiveRange( qry1, qry2 ); - - metadataList = repoSearchLayer.searchAdvanced( rQry, indexer ); - for ( Iterator iter = metadataList.iterator(); iter.hasNext(); ) - { - RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next(); - if ( hit.isMetadata() ) - { - RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject(); - Metadata metadata = repoMetadata.getMetadata(); - Versioning versioning = metadata.getVersioning(); - assertEquals( "20051212044643", versioning.getLastUpdated() ); - } - } - - // search last update using EXCLUSIVE Range Query - qry1 = new QueryTerm( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" ); - qry2 = new QueryTerm( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" ); - - rQry = RangeQuery.createExclusiveRange( qry1, qry2 ); - - metadataList = repoSearchLayer.searchAdvanced( rQry, indexer ); - assertEquals( 0, metadataList.size() ); - } - - /** - * Test delete of document from metadata index. - * - * @throws Exception - */ - public void testDeleteMetadataDocument() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearcher repoSearcher = (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE ); - - MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository ); - - RepositoryMetadata repoMetadata = new GroupRepositoryMetadata( "org.apache.maven" ); - repoMetadata.setMetadata( readMetadata( repoMetadata ) ); - indexer.deleteDocument( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() ); - - QueryTerm queryTerm = new QueryTerm( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() ); - List metadataList = repoSearcher.search( new SingleTermQuery( queryTerm ), indexer ); - assertEquals( 0, metadataList.size() ); - } - - - /** - * Create artifact object. - * - * @param groupId the groupId of the artifact - * @param artifactId the artifactId of the artifact - * @param version the version of the artifact - * @return Artifact - * @throws Exception - */ - private Artifact getArtifact( String groupId, String artifactId, String version ) - throws Exception - { - if ( artifactFactory == null ) - { - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - } - return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); - } - - /** - * Create RepositoryMetadata object. - * - * @return RepositoryMetadata - */ - private Metadata readMetadata( RepositoryMetadata repoMetadata ) - throws RepositoryIndexSearchException - { - File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repoMetadata ) ); - - MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); - - FileReader reader = null; - try - { - reader = new FileReader( file ); - return metadataReader.read( reader ); - } - catch ( FileNotFoundException e ) - { - throw new RepositoryIndexSearchException( "Unable to find metadata file: " + e.getMessage(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexSearchException( "Unable to read metadata file: " + e.getMessage(), e ); - } - catch ( XmlPullParserException xe ) - { - throw new RepositoryIndexSearchException( "Unable to parse metadata file: " + xe.getMessage(), xe ); - } - finally - { - IOUtil.close( reader ); - } - } -} diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java deleted file mode 100644 index 770bc45cb..000000000 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java +++ /dev/null @@ -1,414 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * Copyright 2005-2006 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.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; -import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.Metadata; -import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; -import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; -import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - *

- * This class tests the RepositoryIndexSearchLayer. - */ -public class RepositoryIndexSearchLayerTest - extends PlexusTestCase -{ - private ArtifactRepository repository; - - private ArtifactFactory artifactFactory; - - private File indexPath; - - /** - * Setup method - * - * @throws Exception - */ - protected void setUp() - throws Exception - { - super.setUp(); - File repositoryDirectory = getTestFile( "src/test/repository" ); - String repoDir = repositoryDirectory.toURL().toString(); - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); - - indexPath = getTestFile( "target/index" ); - FileUtils.deleteDirectory( indexPath ); - } - - /** - * Method for creating the index used for testing - * - * @throws Exception - */ - private void createTestIndex() - throws Exception - { - 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 ) ) ); - indexer.indexArtifact( artifact ); - - artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.indexArtifact( artifact ); - - artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.indexArtifact( artifact ); - - artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.indexArtifact( artifact ); - - MetadataRepositoryIndex metaIndexer = factory.createMetadataRepositoryIndex( indexPath, repository ); - RepositoryMetadata repoMetadata = new GroupRepositoryMetadata( "org.apache.maven" ); - repoMetadata.setMetadata( readMetadata( repoMetadata ) ); - metaIndexer.indexMetadata( repoMetadata ); - - repoMetadata = new ArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ) ); - repoMetadata.setMetadata( readMetadata( repoMetadata ) ); - metaIndexer.indexMetadata( repoMetadata ); - - repoMetadata = - new SnapshotArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ) ); - repoMetadata.setMetadata( readMetadata( repoMetadata ) ); - metaIndexer.indexMetadata( repoMetadata ); - - repoMetadata = new GroupRepositoryMetadata( "test" ); - repoMetadata.setMetadata( readMetadata( repoMetadata ) ); - metaIndexer.indexMetadata( repoMetadata ); - - metaIndexer.optimize(); - } - - /** - * Method for testing the "query everything" searcher - * - * @throws Exception - */ - public void testGeneralSearcher() - throws Exception - { - createTestIndex(); - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearchLayer searchLayer = (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); - - ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - - List returnList = searchLayer.searchGeneral( "org.apache.maven", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - Set entries = map.entrySet(); - for ( Iterator it = entries.iterator(); it.hasNext(); ) - { - Map.Entry entry = (Map.Entry) it.next(); - if ( entry.getKey().equals( RepositoryIndex.FLD_PACKAGES ) ) - { - List packages = (List) entry.getValue(); - for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) - { - assertTrue( ( (String) iterator.next() ).indexOf( "org.apache.maven" ) != -1 ); - } - } - } - } - - //POM license urls - returnList = searchLayer.searchGeneral( "http://www.apache.org/licenses/LICENSE-2.0.txt", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - Set entries = map.entrySet(); - for ( Iterator it = entries.iterator(); it.hasNext(); ) - { - Map.Entry entry = (Map.Entry) it.next(); - if ( entry.getKey().equals( RepositoryIndex.FLD_LICENSE_URLS ) ) - { - List packages = (List) entry.getValue(); - for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) - { - assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) iterator.next() ); - } - } - } - } - - //POM dependency - returnList = searchLayer.searchGeneral( "org.codehaus.plexus:plexus-utils:1.0.5", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - Set entries = map.entrySet(); - for ( Iterator it = entries.iterator(); it.hasNext(); ) - { - Map.Entry entry = (Map.Entry) it.next(); - if ( entry.getKey().equals( RepositoryIndex.FLD_DEPENDENCIES ) ) - { - List packages = (List) entry.getValue(); - for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) - { - assertEquals( "org.codehaus.plexus:plexus-utils:1.0.5", (String) iterator.next() ); - } - } - } - } - - // POM reporting plugin - returnList = searchLayer.searchGeneral( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - Set entries = map.entrySet(); - for ( Iterator it = entries.iterator(); it.hasNext(); ) - { - Map.Entry entry = (Map.Entry) it.next(); - if ( entry.getKey().equals( RepositoryIndex.FLD_PLUGINS_REPORT ) ) - { - List packages = (List) entry.getValue(); - for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) - { - assertEquals( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0", - (String) iterator.next() ); - } - } - } - } - - // POM build plugin - returnList = searchLayer.searchGeneral( "org.codehaus.modello:modello-maven-plugin:2.0", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - Set entries = map.entrySet(); - for ( Iterator it = entries.iterator(); it.hasNext(); ) - { - Map.Entry entry = (Map.Entry) it.next(); - if ( entry.getKey().equals( RepositoryIndex.FLD_PLUGINS_BUILD ) ) - { - List packages = (List) entry.getValue(); - for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) - { - assertEquals( "org.codehaus.modello:modello-maven-plugin:2.0", (String) iterator.next() ); - } - } - } - } - - //maven-artifact-2.0.1.jar MD5 checksum - returnList = searchLayer.searchGeneral( "F5A934ABBBC70A33136D89A996B9D5C09F652766", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - Set entries = map.entrySet(); - for ( Iterator it = entries.iterator(); it.hasNext(); ) - { - Map.Entry entry = (Map.Entry) it.next(); - if ( entry.getKey().equals( RepositoryIndex.FLD_MD5 ) ) - { - assertTrue( - ( (String) entry.getValue() ).indexOf( "F5A934ABBBC70A33136D89A996B9D5C09F652766" ) != -1 ); - } - } - } - - //maven-artifact-2.0.1.jar SHA1 checksum - returnList = searchLayer.searchGeneral( "AE55D9B5720E11B6CF19FE1E31A42E51", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - Set entries = map.entrySet(); - for ( Iterator it = entries.iterator(); it.hasNext(); ) - { - Map.Entry entry = (Map.Entry) it.next(); - if ( entry.getKey().equals( RepositoryIndex.FLD_SHA1 ) ) - { - assertTrue( ( (String) entry.getValue() ).indexOf( "AE55D9B5720E11B6CF19FE1E31A42E516" ) != -1 ); - } - } - } - - //packaging jar - returnList = searchLayer.searchGeneral( "jar", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Map map = result.getFieldMatches(); - Set entries = map.entrySet(); - for ( Iterator it = entries.iterator(); it.hasNext(); ) - { - Map.Entry entry = (Map.Entry) it.next(); - if ( entry.getKey().equals( RepositoryIndex.FLD_PACKAGING ) ) - { - assertEquals( "jar", (String) entry.getValue() ); - } - } - } - - returnList = searchLayer.searchGeneral( "test", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - assertEquals( "test", result.getArtifact().getGroupId() ); - } - - returnList = searchLayer.searchGeneral( "test-artifactId", indexer ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - assertEquals( "test-artifactId", result.getArtifact().getArtifactId() ); - } - - } - - - /** - * Method for creating Artifact object - * - * @param groupId the groupId of the artifact to be created - * @param artifactId the artifactId of the artifact to be created - * @param version the version of the artifact to be created - * @return Artifact object - * @throws Exception - */ - private Artifact getArtifact( String groupId, String artifactId, String version ) - throws Exception - { - if ( artifactFactory == null ) - { - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - } - - return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); - } - - /** - * Method for creating a Model object given the groupId, artifactId and version - * - * @param groupId the groupId of the model to be created - * @param artifactId the artifactId of the model to be created - * @param version the version of the model to be created - * @return Model object - * @throws Exception - */ - private Model getPom( String groupId, String artifactId, String version ) - throws Exception - { - Artifact artifact = getArtifact( groupId, artifactId, version ); - - return getPom( artifact ); - } - - /** - * Method for creating a Model object given an artifact - * - * @param artifact the artifact to be created a Model object for - * @return Model object - * @throws Exception - */ - private Model getPom( Artifact artifact ) - throws Exception - { - File pomFile = getPomFile( artifact ); - - MavenXpp3Reader pomReader = new MavenXpp3Reader(); - return pomReader.read( new FileReader( pomFile ) ); - } - - /** - * Method for creating a pom file - * - * @param artifact - * @return File - */ - private File getPomFile( Artifact artifact ) - { - String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath(); - return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" ); - } - - /** - * Create RepositoryMetadata object. - * - * @return RepositoryMetadata - */ - private Metadata readMetadata( RepositoryMetadata repoMetadata ) - throws RepositoryIndexSearchException - { - File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repoMetadata ) ); - - MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); - - FileReader reader = null; - try - { - reader = new FileReader( file ); - return metadataReader.read( reader ); - } - catch ( FileNotFoundException e ) - { - throw new RepositoryIndexSearchException( "Unable to find metadata file: " + e.getMessage(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexSearchException( "Unable to read metadata file: " + e.getMessage(), e ); - } - catch ( XmlPullParserException xe ) - { - throw new RepositoryIndexSearchException( "Unable to parse metadata file: " + xe.getMessage(), xe ); - } - finally - { - IOUtil.close( reader ); - } - } -} -- 2.39.5