From 99757aa34324a58a7d56dcd916ba7fe96a384ae9 Mon Sep 17 00:00:00 2001 From: "Edwin L. Punzalan" Date: Wed, 21 Dec 2005 01:54:19 +0000 Subject: [PATCH] Adding maven-repository-indexer git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@358176 13f79535-47bb-0310-9956-ffa450edef68 --- maven-repository-indexer/pom.xml | 29 ++++ .../indexing/AbstractRepositoryIndexer.java | 32 ++++ .../indexing/ArtifactRepositoryIndexer.java | 164 ++++++++++++++++++ .../DefaultRepositoryIndexerFactory.java | 54 ++++++ .../indexing/RepositoryIndexer.java | 29 ++++ .../indexing/RepositoryIndexerFactory.java | 31 ++++ pom.xml | 1 + 7 files changed, 340 insertions(+) create mode 100644 maven-repository-indexer/pom.xml create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexerFactory.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.java diff --git a/maven-repository-indexer/pom.xml b/maven-repository-indexer/pom.xml new file mode 100644 index 000000000..81a8d7e28 --- /dev/null +++ b/maven-repository-indexer/pom.xml @@ -0,0 +1,29 @@ + + + org.apache.maven.repository + maven-repository-manager + 1.0-SNAPSHOT + + 4.0.0 + maven-repository-indexer + jar + Maven Repository Indexer + + + org.apache.maven + maven-artifact + + + lucene + lucene + 1.4.3 + + + junit + junit + 3.8.1 + test + + + diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java new file mode 100644 index 000000000..da8fa397f --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java @@ -0,0 +1,32 @@ +package org.apache.maven.repository.indexing; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * + * @author Edwin Punzalan + */ +public abstract class AbstractRepositoryIndexer + implements RepositoryIndexer +{ + + protected void addDocument( Object obj ) + { + + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java new file mode 100644 index 000000000..8d6d45d15 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java @@ -0,0 +1,164 @@ +package org.apache.maven.repository.indexing; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Collection; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; + +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; + +/** + * + * @author Edwin Punzalan + */ +public class ArtifactRepositoryIndexer + extends AbstractRepositoryIndexer +{ + private static final String NAME = "name"; + private static final String GROUPID = "groupId"; + private static final String ARTIFACTID = "artifactId"; + private static final String VERSION = "version"; + private static final String SHA1 = "sha1"; + private static final String MD5 = "md5"; + private static final String CLASSES = "classes"; + private static final String PACKAGES = "packages"; + + private static final String[] FIELDS = { NAME, GROUPID, ARTIFACTID, VERSION, SHA1, MD5, CLASSES, PACKAGES }; + + String indexPath; + ArtifactRepository repository; + IndexReader indexReader; + IndexWriter indexWriter; + + public ArtifactRepositoryIndexer( ArtifactRepository repository, String path ) + { + try + { + this.repository = repository; + validateIndex(); + } + catch ( IOException e ) + { + + } + } + + public void addArtifactIndex( Artifact artifact ) + throws IOException, NoSuchAlgorithmException + { + getIndexWriter(); + + Document doc = new Document(); + doc.add( Field.Text( NAME, repository.pathOf( artifact ) ) ); + doc.add( Field.Text( GROUPID, artifact.getGroupId() ) ); + doc.add( Field.Text( ARTIFACTID, artifact.getArtifactId() ) ); + doc.add( Field.Text( VERSION, artifact.getVersion() ) ); + doc.add( Field.Text( SHA1, getSha1( artifact ) ) ); + doc.add( Field.Text( MD5, getMd5( artifact ) ) ); + doc.add( Field.Text( CLASSES, getClasses( artifact ) ) ); + doc.add( Field.Text( PACKAGES, getPackages( artifact ) ) ); + indexWriter.addDocument( doc ); + } + + private String getSha1( Artifact artifact ) + throws FileNotFoundException, IOException, NoSuchAlgorithmException + { + FileInputStream fIn = new FileInputStream( artifact.getFile() ); + return new String( getChecksum( fIn, "SHA-1" ) ); + } + + private String getMd5( Artifact artifact ) + throws FileNotFoundException, IOException, NoSuchAlgorithmException + { + FileInputStream fIn = new FileInputStream( artifact.getFile() ); + return new String( getChecksum( fIn, "MD5" ) ); + } + + private byte[] getChecksum( InputStream inStream, String algorithm ) + throws IOException, NoSuchAlgorithmException + { + byte[] buffer = new byte[ 256 ]; + MessageDigest complete = MessageDigest.getInstance( algorithm ); + int numRead; + do + { + numRead = inStream.read( buffer ); + if ( numRead > 0 ) + { + complete.update( buffer, 0, numRead ); + } + } + while ( numRead != -1 ); + inStream.close(); + + return complete.digest(); + } + + private String getClasses( Artifact artifact ) + { + return null; + } + + private String getPackages( Artifact artifact ) + { + return null; + } + + private void getIndexWriter() + throws IOException + { + if ( indexWriter == null ) + { + indexWriter = new IndexWriter( indexPath, new StandardAnalyzer(), true ); + } + } + + private void getIndexReader() + throws IOException + { + if ( indexReader == null ) + { + indexReader = IndexReader.open( indexPath ); + } + } + + private void validateIndex() + throws IOException + { + getIndexReader(); + Collection fields = indexReader.getFieldNames(); + for( int idx=0; idx maven-repository-discovery maven-repository-reports-standard + maven-repository-indexer -- 2.39.5