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 index 2fafafe43..7419ec0d3 100644 --- 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 @@ -18,6 +18,7 @@ package org.apache.maven.repository.indexing; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; +import org.apache.maven.artifact.repository.ArtifactRepository; import java.io.File; import java.io.IOException; @@ -39,6 +40,17 @@ public abstract class AbstractRepositoryIndex private IndexWriter indexWriter; + private ArtifactRepository repository; + + public AbstractRepositoryIndex( ArtifactRepository repository, String indexPath ) + throws RepositoryIndexException + { + this.repository = repository; + this.indexPath = indexPath; + + open( indexPath ); + } + /** * method to encapsulate the optimize() method for lucene */ @@ -186,4 +198,9 @@ public abstract class AbstractRepositoryIndex indexOpen = true; } + + public ArtifactRepository getRepository() + { + return repository; + } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java index dc8acabdb..9467c04e1 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java @@ -39,7 +39,7 @@ import java.util.List; public abstract class AbstractRepositoryIndexSearcher implements RepositoryIndexSearcher { - protected ArtifactRepositoryIndex index; + protected RepositoryIndex index; private BooleanQuery bQry; @@ -52,7 +52,7 @@ public abstract class AbstractRepositoryIndexSearcher * * @param index the index object */ - public AbstractRepositoryIndexSearcher( ArtifactRepositoryIndex index ) + public AbstractRepositoryIndexSearcher( RepositoryIndex index ) { this.index = index; } 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 index e61f536d9..581c5bc00 100644 --- 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 @@ -41,39 +41,36 @@ import java.util.zip.ZipFile; public class ArtifactRepositoryIndex extends AbstractRepositoryIndex { - private static final String NAME = "name"; + protected static final String FLD_NAME = "name"; - private static final String GROUPID = "groupId"; + protected static final String FLD_GROUPID = "groupId"; - private static final String ARTIFACTID = "artifactId"; + protected static final String FLD_ARTIFACTID = "artifactId"; - private static final String VERSION = "version"; + protected static final String FLD_VERSION = "version"; - private static final String SHA1 = "sha1"; + protected static final String FLD_SHA1 = "sha1"; - private static final String MD5 = "md5"; + protected static final String FLD_MD5 = "md5"; - private static final String CLASSES = "classes"; + protected static final String FLD_CLASSES = "classes"; - private static final String PACKAGES = "packages"; + protected static final String FLD_PACKAGES = "packages"; - private static final String FILES = "files"; + protected static final String FLD_FILES = "files"; - private static final String[] FIELDS = {NAME, GROUPID, ARTIFACTID, VERSION, SHA1, MD5, CLASSES, PACKAGES, FILES}; + private static final String[] FIELDS = + {FLD_NAME, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_SHA1, FLD_MD5, FLD_CLASSES, FLD_PACKAGES, FLD_FILES}; private Analyzer analyzer; private Digester digester; - private ArtifactRepository repository; - public ArtifactRepositoryIndex( String indexPath, ArtifactRepository repository, Digester digester ) throws RepositoryIndexException { - this.repository = repository; + super( repository, indexPath ); this.digester = digester; - - open( indexPath ); } /** @@ -175,15 +172,15 @@ public class ArtifactRepositoryIndex //@todo should some of these fields be Keyword instead of Text ? Document doc = new Document(); - doc.add( Field.Text( NAME, artifact.getFile().getName() ) ); - doc.add( Field.Text( GROUPID, artifact.getGroupId() ) ); - doc.add( Field.Text( ARTIFACTID, artifact.getArtifactId() ) ); - doc.add( Field.Text( VERSION, artifact.getVersion() ) ); - doc.add( Field.Text( SHA1, sha1sum ) ); - doc.add( Field.Text( MD5, md5sum ) ); - doc.add( Field.Text( CLASSES, classes.toString() ) ); - doc.add( Field.Text( PACKAGES, packages.toString() ) ); - doc.add( Field.Text( FILES, files.toString() ) ); + doc.add( Field.Text( FLD_NAME, artifact.getFile().getName() ) ); + doc.add( Field.Text( FLD_GROUPID, artifact.getGroupId() ) ); + doc.add( Field.Text( FLD_ARTIFACTID, artifact.getArtifactId() ) ); + doc.add( Field.Text( FLD_VERSION, artifact.getVersion() ) ); + doc.add( Field.Text( FLD_SHA1, sha1sum ) ); + doc.add( Field.Text( FLD_MD5, md5sum ) ); + doc.add( Field.Text( FLD_CLASSES, classes.toString() ) ); + doc.add( Field.Text( FLD_PACKAGES, packages.toString() ) ); + doc.add( Field.Text( FLD_FILES, files.toString() ) ); try { @@ -256,9 +253,4 @@ public class ArtifactRepositoryIndex return isAdded; } - - public ArtifactRepository getRepository() - { - return repository; - } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexSearcher.java index 6cdb0799b..e5f5ca386 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexSearcher.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexSearcher.java @@ -32,14 +32,6 @@ import java.io.File; public class ArtifactRepositoryIndexSearcher extends AbstractRepositoryIndexSearcher { - private static final String NAME = "name"; - - private static final String GROUPID = "groupId"; - - private static final String ARTIFACTID = "artifactId"; - - private static final String VERSION = "version"; - private ArtifactFactory factory; private BooleanQuery bQry; @@ -62,10 +54,10 @@ public class ArtifactRepositoryIndexSearcher protected Object createSearchedObjectFromIndexDocument( Document doc ) { - String groupId = doc.get( GROUPID ); - String artifactId = doc.get( ARTIFACTID ); - String version = doc.get( VERSION ); - String name = doc.get( NAME ); + String groupId = doc.get( ArtifactRepositoryIndex.FLD_GROUPID ); + String artifactId = doc.get( ArtifactRepositoryIndex.FLD_ARTIFACTID ); + String version = doc.get( ArtifactRepositoryIndex.FLD_VERSION ); + String name = doc.get( ArtifactRepositoryIndex.FLD_NAME ); String packaging = name.substring( name.lastIndexOf( '.' ) + 1 ); Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version, packaging ); String groupIdTemp = groupId.replace( '.', '/' ); diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java new file mode 100644 index 000000000..7ee15b189 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java @@ -0,0 +1,166 @@ +package org.apache.maven.repository.indexing; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.SimpleAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +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.artifact.repository.ArtifactRepository; +import org.codehaus.plexus.util.StringUtils; + +import java.io.IOException; +import java.util.Iterator; + +/** + * @author Edwin Punzalan + */ +public class PomRepositoryIndex + extends AbstractRepositoryIndex +{ + protected static final String FLD_GROUPID = "groupId"; + + protected static final String FLD_ARTIFACTID = "artifactId"; + + protected static final String FLD_VERSION = "version"; + + protected static final String FLD_PACKAGING = "packaging"; + + protected static final String FLD_LICENSE_URLS = "license_urls"; + + protected static final String FLD_DEPENDENCIES = "dependencies"; + + protected static final String FLD_PLUGINS_BUILD = "plugins_build"; + + protected static final String FLD_PLUGINS_REPORT = "plugins_report"; + + protected static final String FLD_PLUGINS_ALL = "plugins_all"; + + private static final String[] FIELDS = {FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_PACKAGING, FLD_LICENSE_URLS, FLD_DEPENDENCIES, + FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL}; + + private Analyzer analyzer; + + public PomRepositoryIndex( ArtifactRepository repository, String indexPath ) + throws RepositoryIndexException + { + super( repository, indexPath ); + } + + public Analyzer getAnalyzer() + { + if ( analyzer == null ) + { + analyzer = new SimpleAnalyzer(); + } + + return analyzer; + } + + public void index( Object obj ) + throws RepositoryIndexException + { + if ( obj instanceof Model ) + { + indexModel( (Model) obj ); + } + else + { + throw new RepositoryIndexException( + "This instance of indexer cannot index instances of " + obj.getClass().getName() ); + } + } + + public String[] getIndexFields() + { + return FIELDS; + } + + public void indexModel( Model pom ) + throws RepositoryIndexException + { + if ( !isOpen() ) + { + throw new RepositoryIndexException( "Unable to add pom index on a closed index" ); + } + + Document doc = new Document(); + doc.add( Field.Text( FLD_GROUPID, pom.getGroupId() ) ); + doc.add( Field.Text( FLD_ARTIFACTID, pom.getArtifactId() ) ); + doc.add( Field.Text( FLD_VERSION, pom.getVersion() ) ); + doc.add( Field.Keyword( FLD_PACKAGING, pom.getPackaging() ) ); + + indexLicenseUrls( doc, pom.getLicenses().iterator() ); + indexDependencies( doc, pom.getDependencies().iterator() ); + indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); + indexPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); + indexPlugins( doc, FLD_PLUGINS_ALL, pom.getBuild().getPlugins().iterator() ); + indexPlugins( doc, FLD_PLUGINS_ALL, pom.getReporting().getPlugins().iterator() ); + + try + { + getIndexWriter().addDocument( doc ); + } + catch ( IOException e ) + { + throw new RepositoryIndexException( "Error opening index", e ); + } + } + + private void indexLicenseUrls( Document doc, Iterator licenses ) + { + while ( licenses.hasNext() ) + { + License license = (License) licenses.next(); + String url = license.getUrl(); + if ( StringUtils.isNotEmpty( url ) ) + { + doc.add( Field.Keyword( FLD_LICENSE_URLS, url ) ); + } + } + } + + private void indexDependencies( Document doc, Iterator dependencies ) + { + while ( dependencies.hasNext() ) + { + Dependency dep = (Dependency) dependencies.next(); + String id = getId( dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ); + doc.add( Field.Keyword( FLD_DEPENDENCIES, id ) ); + } + } + + 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( Field.Keyword( field, id ) ); + } + } + + private String getId( String groupId, String artifactId, String version ) + { + return groupId + ":" + artifactId + ":" + version; + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndexSearcher.java new file mode 100644 index 000000000..307025065 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndexSearcher.java @@ -0,0 +1,52 @@ +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.maven.artifact.factory.ArtifactFactory; + +/** + * @author Edwin Punzalan + */ +public class PomRepositoryIndexSearcher + extends AbstractRepositoryIndexSearcher +{ + private static final String GROUPID = "groupId"; + + private static final String ARTIFACTID = "artifactId"; + + private static final String VERSION = "version"; + + private static final String PACKAGING = "packaging"; + + private ArtifactFactory factory; + + public PomRepositoryIndexSearcher( RepositoryIndex index, ArtifactFactory artifactFactory ) + { + super( index ); + this.factory = artifactFactory; + } + + protected Object createSearchedObjectFromIndexDocument( Document doc ) + { + String groupId = doc.get( PomRepositoryIndex.FLD_GROUPID ) ; + String artifactId = doc.get( PomRepositoryIndex.FLD_ARTIFACTID ); + String version = doc.get( PomRepositoryIndex.FLD_VERSION ); + String packaging = doc.get( PomRepositoryIndex.FLD_PACKAGING ); + return factory.createBuildArtifact( groupId, artifactId, version, packaging ); + } +} 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 index da1f00d82..7becb79a7 100644 --- 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 @@ -17,6 +17,7 @@ package org.apache.maven.repository.indexing; */ import org.apache.lucene.analysis.Analyzer; +import org.apache.maven.artifact.repository.ArtifactRepository; /** * @author Edwin Punzalan @@ -35,9 +36,7 @@ public interface RepositoryIndex void close() throws RepositoryIndexException; -/* void open( String indexPath ) - throws RepositoryIndexException; -*/ + ArtifactRepository getRepository(); void optimize() throws RepositoryIndexException;