git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@358176 13f79535-47bb-0310-9956-ffa450edef68tags/archiva-0.9-alpha-1
@@ -0,0 +1,29 @@ | |||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |||
<parent> | |||
<groupId>org.apache.maven.repository</groupId> | |||
<artifactId>maven-repository-manager</artifactId> | |||
<version>1.0-SNAPSHOT</version> | |||
</parent> | |||
<modelVersion>4.0.0</modelVersion> | |||
<artifactId>maven-repository-indexer</artifactId> | |||
<packaging>jar</packaging> | |||
<name>Maven Repository Indexer</name> | |||
<dependencies> | |||
<dependency> | |||
<groupId>org.apache.maven</groupId> | |||
<artifactId>maven-artifact</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>lucene</groupId> | |||
<artifactId>lucene</artifactId> | |||
<version>1.4.3</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>junit</groupId> | |||
<artifactId>junit</artifactId> | |||
<version>3.8.1</version> | |||
<scope>test</scope> | |||
</dependency> | |||
</dependencies> | |||
</project> |
@@ -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 ) | |||
{ | |||
} | |||
} |
@@ -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<FIELDS.length; idx++ ) | |||
{ | |||
if ( !fields.contains( FIELDS[ idx ] ) ) | |||
{ | |||
//should throw something | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,54 @@ | |||
package org.apache.maven.repository.indexing; | |||
/* | |||
* Copyright 2001-2005 The Apache Software Foundation. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import org.apache.maven.artifact.Artifact; | |||
import org.apache.maven.artifact.repository.ArtifactRepository; | |||
/** | |||
* | |||
* @author Edwin Punzalan | |||
*/ | |||
public class DefaultRepositoryIndexerFactory | |||
implements RepositoryIndexerFactory | |||
{ | |||
Map indexerMap = new HashMap(); | |||
public RepositoryIndexer getRepositoryIndexer( ArtifactRepository repository, Class indexType ) | |||
{ | |||
if ( !indexerMap.containsKey( indexType ) ) | |||
{ | |||
RepositoryIndexer indexer; | |||
if ( Artifact.class == indexType ) | |||
{ | |||
indexer = new ArtifactRepositoryIndexer( ); | |||
} | |||
else | |||
{ | |||
indexer = null; | |||
} | |||
indexerMap.put( indexType, indexer ); | |||
} | |||
return (RepositoryIndexer) indexerMap.get( indexType ); | |||
} | |||
} |
@@ -0,0 +1,29 @@ | |||
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 interface RepositoryIndexer | |||
{ | |||
String ROLE = RepositoryIndexer.class.getName(); | |||
} |
@@ -0,0 +1,31 @@ | |||
package org.apache.maven.repository.indexing; | |||
/* | |||
* Copyright 2001-2005 The Apache Software Foundation. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
import org.apache.maven.artifact.repository.ArtifactRepository; | |||
/** | |||
* | |||
* @author Edwin Punzalan | |||
*/ | |||
public interface RepositoryIndexerFactory | |||
{ | |||
String ROLE = RepositoryIndexerFactory.class.getName(); | |||
RepositoryIndexer getRepositoryIndexer( ArtifactRepository repository, Class indexType ); | |||
} |
@@ -101,6 +101,7 @@ | |||
<modules> | |||
<module>maven-repository-discovery</module> | |||
<module>maven-repository-reports-standard</module> | |||
<module>maven-repository-indexer</module> | |||
</modules> | |||
<repositories> | |||
<repository> |