From c2939d7bd41082a0ae72f77aca3191dbcf0ea33c Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Tue, 25 Jul 2006 12:02:08 +0000 Subject: [PATCH] [MRM-127] add index record classes git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@425379 13f79535-47bb-0310-9956-ffa450edef68 --- .../indexing/EclipseRepositoryIndex.java | 16 ++ .../AbstractArtifactIndexRecordFactory.java | 80 ++++++++ .../record/MinimalArtifactIndexRecord.java | 162 +++++++++++++++ .../MinimalArtifactIndexRecordFactory.java | 87 ++++++++ .../record/RepositoryIndexRecord.java | 26 +++ .../record/RepositoryIndexRecordFactory.java | 40 ++++ .../record/StandardArtifactIndexRecord.java | 191 ++++++++++++++++++ .../StandardArtifactIndexRecordFactory.java | 143 +++++++++++++ .../src/site/apt/design.apt | 8 +- ...MinimalArtifactIndexRecordFactoryTest.java | 117 +++++++++++ ...tandardArtifactIndexRecordFactoryTest.java | 141 +++++++++++++ .../1.0/test-corrupt-jar-1.0.jar | 0 .../test-dll/1.0.1.34/test-dll-1.0.1.34.dll | 0 .../record/test-jar/1.0/test-jar-1.0.jar | Bin 0 -> 1408 bytes 14 files changed, 1007 insertions(+), 4 deletions(-) create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecord.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecordFactory.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecord.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecordFactory.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecord.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java create mode 100644 maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecordFactoryTest.java create mode 100644 maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java create mode 100644 maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-corrupt-jar/1.0/test-corrupt-jar-1.0.jar create mode 100644 maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-dll/1.0.1.34/test-dll-1.0.1.34.dll create mode 100644 maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-jar/1.0/test-jar-1.0.jar 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 index 20eb10375..ebce7fa7b 100644 --- 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 @@ -51,14 +51,30 @@ import java.util.zip.ZipOutputStream; 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; diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java new file mode 100644 index 000000000..89ded1b62 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java @@ -0,0 +1,80 @@ +package org.apache.maven.repository.indexing.record; + +/* + * 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.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; +import org.codehaus.plexus.logging.AbstractLogEnabled; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +/** + * Base class for the index record factories. + * + * @author Brett Porter + */ +public abstract class AbstractArtifactIndexRecordFactory + extends AbstractLogEnabled + implements RepositoryIndexRecordFactory +{ + /** + * @plexus.requirement + */ + private Digester digester; + + protected String readChecksum( File file, String algorithm ) + { + String checksum; + try + { + checksum = digester.createChecksum( file, algorithm ).toLowerCase(); + } + catch ( DigesterException e ) + { + getLogger().error( "Error getting checksum for artifact file, leaving empty in index: " + e.getMessage() ); + checksum = null; + } + return checksum; + } + + protected List readFilesInArchive( File file ) + throws IOException + { + ZipFile zipFile = new ZipFile( file ); + List files = new ArrayList( zipFile.size() ); + + for ( Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) + { + ZipEntry entry = (ZipEntry) entries.nextElement(); + + files.add( entry.getName() ); + } + return files; + } + + protected static boolean isClass( String name ) + { + // TODO: verify if class is public or protected (this might require the original ZipEntry) + return name.endsWith( ".class" ) && name.lastIndexOf( "$" ) < 0; + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecord.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecord.java new file mode 100644 index 000000000..9784a2adf --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecord.java @@ -0,0 +1,162 @@ +package org.apache.maven.repository.indexing.record; + +import java.util.Date; + +/* + * 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. + */ + +/** + * The a record with the fields in the minimal index. + * + * @author Brett Porter + */ +public class MinimalArtifactIndexRecord + implements RepositoryIndexRecord +{ + /** + * The classes in the archive for the artifact, if it is a JAR. The package name is not included. + */ + private String classes; + + /** + * The MD5 checksum of the artifact file. + */ + private String md5Checksum; + + /** + * The filename of the artifact file (no path). + */ + private String filename; + + /** + * The timestamp that the artifact file was last modified. + */ + private long lastModified; + + /** + * The size of the artifact file in bytes. + */ + private long size; + + public void setClasses( String classes ) + { + this.classes = classes; + } + + public void setMd5Checksum( String md5Checksum ) + { + this.md5Checksum = md5Checksum; + } + + public void setFilename( String filename ) + { + this.filename = filename; + } + + public void setLastModified( long lastModified ) + { + this.lastModified = lastModified; + } + + public void setSize( long size ) + { + this.size = size; + } + + public String getClasses() + { + return classes; + } + + public String getMd5Checksum() + { + return md5Checksum; + } + + public String getFilename() + { + return filename; + } + + public long getLastModified() + { + return lastModified; + } + + public long getSize() + { + return size; + } + + /** + * @noinspection RedundantIfStatement + */ + public boolean equals( Object obj ) + { + if ( this == obj ) + { + return true; + } + if ( obj == null || getClass() != obj.getClass() ) + { + return false; + } + + MinimalArtifactIndexRecord that = (MinimalArtifactIndexRecord) obj; + + if ( lastModified != that.lastModified ) + { + return false; + } + if ( size != that.size ) + { + return false; + } + if ( classes != null ? !classes.equals( that.classes ) : that.classes != null ) + { + return false; + } + if ( !filename.equals( that.filename ) ) + { + return false; + } + if ( md5Checksum != null ? !md5Checksum.equals( that.md5Checksum ) : that.md5Checksum != null ) + { + return false; + } + + return true; + } + + /** + * @noinspection UnnecessaryParentheses + */ + public int hashCode() + { + int result = classes != null ? classes.hashCode() : 0; + result = 31 * result + ( md5Checksum != null ? md5Checksum.hashCode() : 0 ); + result = 31 * result + filename.hashCode(); + result = 31 * result + (int) ( lastModified ^ ( lastModified >>> 32 ) ); + result = 31 * result + (int) ( size ^ ( size >>> 32 ) ); + return result; + } + + public String toString() + { + return "Filename: " + filename + "; checksum: " + md5Checksum + "; size: " + size + "; lastModified: " + + new Date( lastModified ) + "; classes: " + classes; + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecordFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecordFactory.java new file mode 100644 index 000000000..5f9509e26 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecordFactory.java @@ -0,0 +1,87 @@ +package org.apache.maven.repository.indexing.record; + +/* + * 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.repository.digest.Digester; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * An index record type for the minimal index. + * + * @author Edwin Punzalan + * @author Brett Porter + * @plexus.component role="org.apache.maven.repository.indexing.record.RepositoryIndexRecordFactory" role-hint="minimal" + */ +public class MinimalArtifactIndexRecordFactory + extends AbstractArtifactIndexRecordFactory +{ + public RepositoryIndexRecord createRecord( Artifact artifact ) + { + MinimalArtifactIndexRecord record = null; + + File file = artifact.getFile(); + if ( file != null && "jar".equals( artifact.getType() ) && file.exists() ) + { + String md5 = readChecksum( file, Digester.MD5 ); + + List files = null; + try + { + files = readFilesInArchive( file ); + } + catch ( IOException e ) + { + getLogger().error( "Error reading artifact file, omitting from index: " + e.getMessage() ); + } + + if ( files != null ) + { + record = new MinimalArtifactIndexRecord(); + record.setMd5Checksum( md5 ); + record.setFilename( file.getName() ); + record.setLastModified( file.lastModified() ); + record.setSize( file.length() ); + record.setClasses( getClassesFromFiles( files ) ); + } + } + return record; + } + + private String getClassesFromFiles( List files ) + { + StringBuffer classes = new StringBuffer(); + + for ( Iterator i = files.iterator(); i.hasNext(); ) + { + String name = (String) i.next(); + + if ( isClass( name ) ) + { + int idx = name.lastIndexOf( '/' ); + String classname = name.substring( idx + 1, name.length() - 6 ); + classes.append( classname ).append( "\n" ); + } + } + + return classes.toString(); + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecord.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecord.java new file mode 100644 index 000000000..472e546c7 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecord.java @@ -0,0 +1,26 @@ +package org.apache.maven.repository.indexing.record; + +/* + * 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. + */ + +/** + * A repository index record. + * + * @author Brett Porter + */ +public interface RepositoryIndexRecord +{ +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecordFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecordFactory.java new file mode 100644 index 000000000..b1c9fb090 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecordFactory.java @@ -0,0 +1,40 @@ +package org.apache.maven.repository.indexing.record; + +import org.apache.maven.artifact.Artifact; + +/* + * 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. + */ + +/** + * The layout of a record in a repository index. + * + * @author Brett Porter + */ +public interface RepositoryIndexRecordFactory +{ + /** + * The Plexus role. + */ + String ROLE = RepositoryIndexRecordFactory.class.getName(); + + /** + * Create an index record from an artifact. + * + * @param artifact the artifact + * @return the index record + */ + RepositoryIndexRecord createRecord( Artifact artifact ); +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecord.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecord.java new file mode 100644 index 000000000..30aad6504 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecord.java @@ -0,0 +1,191 @@ +package org.apache.maven.repository.indexing.record; + +/* + * 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. + */ + +/** + * The a record with the fields in the standard index. + * + * @author Brett Porter + */ +public class StandardArtifactIndexRecord + extends MinimalArtifactIndexRecord +{ + /** + * The SHA-1 checksum of the artifact file. + */ + private String sha1Checksum; + + /** + * The artifact's group. + */ + private String groupId; + + /** + * The artifact's identifier within the group. + */ + private String artifactId; + + /** + * The artifact's version. + */ + private String version; + + /** + * The classifier, if there is one. + */ + private String classifier; + + /** + * The artifact type (from the file). + */ + private String type; + + /** + * A list of packages (separated by '\n') in the artifact if it contains Java classes. + */ + private String packages; + + /** + * A list of files (separated by '\n') in the artifact if it is an archive. + */ + private String files; + + /** + * The identifier of the repository that the artifact came from. + */ + private String repository; + + public void setSha1Checksum( String sha1Checksum ) + { + this.sha1Checksum = sha1Checksum; + } + + public void setGroupId( String groupId ) + { + this.groupId = groupId; + } + + public void setArtifactId( String artifactId ) + { + this.artifactId = artifactId; + } + + public void setVersion( String version ) + { + this.version = version; + } + + public void setClassifier( String classifier ) + { + this.classifier = classifier; + } + + public void setType( String type ) + { + this.type = type; + } + + public void setPackages( String packages ) + { + this.packages = packages; + } + + public void setFiles( String files ) + { + this.files = files; + } + + public void setRepository( String repository ) + { + this.repository = repository; + } + + /** + * @noinspection RedundantIfStatement + */ + public boolean equals( Object obj ) + { + if ( this == obj ) + { + return true; + } + if ( obj == null || getClass() != obj.getClass() ) + { + return false; + } + if ( !super.equals( obj ) ) + { + return false; + } + + StandardArtifactIndexRecord that = (StandardArtifactIndexRecord) obj; + + if ( !artifactId.equals( that.artifactId ) ) + { + return false; + } + if ( classifier != null ? !classifier.equals( that.classifier ) : that.classifier != null ) + { + return false; + } + if ( files != null ? !files.equals( that.files ) : that.files != null ) + { + return false; + } + if ( !groupId.equals( that.groupId ) ) + { + return false; + } + if ( packages != null ? !packages.equals( that.packages ) : that.packages != null ) + { + return false; + } + if ( repository != null ? !repository.equals( that.repository ) : that.repository != null ) + { + return false; + } + if ( sha1Checksum != null ? !sha1Checksum.equals( that.sha1Checksum ) : that.sha1Checksum != null ) + { + return false; + } + if ( type != null ? !type.equals( that.type ) : that.type != null ) + { + return false; + } + if ( !version.equals( that.version ) ) + { + return false; + } + + return true; + } + + public int hashCode() + { + int result = super.hashCode(); + result = 31 * result + ( sha1Checksum != null ? sha1Checksum.hashCode() : 0 ); + result = 31 * result + groupId.hashCode(); + result = 31 * result + artifactId.hashCode(); + result = 31 * result + version.hashCode(); + result = 31 * result + ( classifier != null ? classifier.hashCode() : 0 ); + result = 31 * result + ( type != null ? type.hashCode() : 0 ); + result = 31 * result + ( packages != null ? packages.hashCode() : 0 ); + result = 31 * result + ( files != null ? files.hashCode() : 0 ); + result = 31 * result + ( repository != null ? repository.hashCode() : 0 ); + return result; + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java new file mode 100644 index 000000000..ab78e2c4c --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java @@ -0,0 +1,143 @@ +package org.apache.maven.repository.indexing.record; + +/* + * 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.repository.digest.Digester; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/** + * An index record type for the standard index. + * + * @author Edwin Punzalan + * @author Brett Porter + * @plexus.component role="org.apache.maven.repository.indexing.record.RepositoryIndexRecordFactory" role-hint="standard" + */ +public class StandardArtifactIndexRecordFactory + extends AbstractArtifactIndexRecordFactory +{ + /** + * A list of artifact types to treat as a zip archive. + * + * @todo this should be smarter (perhaps use plexus archiver to look for an unarchiver, and make the ones for zip configurable since sar, par, etc can be added at random. + */ + private static final Set ARCHIVE_TYPES = + new HashSet( Arrays.asList( new String[]{"jar", "zip", "ejb", "par", "sar", "war", "ear"} ) ); + + public RepositoryIndexRecord createRecord( Artifact artifact ) + { + StandardArtifactIndexRecord record = null; + + File file = artifact.getFile(); + if ( file != null && file.exists() ) + { + String md5 = readChecksum( file, Digester.MD5 ); + String sha1 = readChecksum( file, Digester.SHA1 ); + + List files = null; + try + { + if ( ARCHIVE_TYPES.contains( artifact.getType() ) ) + { + files = readFilesInArchive( file ); + } + else + { + files = Collections.EMPTY_LIST; + } + } + catch ( IOException e ) + { + getLogger().error( "Error reading artifact file, omitting from index: " + e.getMessage() ); + } + + if ( files != null ) + { + record = new StandardArtifactIndexRecord(); + + record.setGroupId( artifact.getGroupId() ); + record.setArtifactId( artifact.getArtifactId() ); + record.setVersion( artifact.getVersion() ); + record.setClassifier( artifact.getClassifier() ); + record.setType( artifact.getType() ); + record.setMd5Checksum( md5 ); + record.setSha1Checksum( sha1 ); + record.setFilename( file.getName() ); + record.setLastModified( file.lastModified() ); + record.setSize( file.length() ); + record.setRepository( artifact.getRepository().getId() ); +/* TODO! these come from the POM and metadata, so probably part of an update record method instead + record.setPluginPrefix( pluginPrefix ); + record.setPackaging( packaging ); + indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); + indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); + record.setDependencies( dependencies ); + record.setLicenses( licenses ); +*/ + populateArchiveEntries( files, record ); + } + } + + return record; + } + + private void populateArchiveEntries( List files, StandardArtifactIndexRecord record ) + { + StringBuffer classes = new StringBuffer(); + StringBuffer packages = new StringBuffer(); + StringBuffer fileBuffer = new StringBuffer(); + + for ( Iterator i = files.iterator(); i.hasNext(); ) + { + String name = (String) i.next(); + + // ignore directories + if ( !name.endsWith( "/" ) ) + { + fileBuffer.append( name ).append( "\n" ); + + if ( isClass( name ) ) + { + int idx = name.lastIndexOf( '/' ); + String classname = name.substring( idx + 1, name.length() - 6 ); + classes.append( classname ).append( "\n" ); + + if ( idx > 0 ) + { + String packageName = name.substring( 0, idx ).replace( '/', '.' ); + if ( packages.indexOf( packageName ) < 0 ) + { + packages.append( packageName ).append( "\n" ); + } + } + } + } + } + + record.setClasses( classes.toString() ); + record.setPackages( packages.toString() ); + record.setFiles( fileBuffer.toString() ); + } +} diff --git a/maven-repository-indexer/src/site/apt/design.apt b/maven-repository-indexer/src/site/apt/design.apt index f680455ab..fbad62ff6 100644 --- a/maven-repository-indexer/src/site/apt/design.apt +++ b/maven-repository-indexer/src/site/apt/design.apt @@ -66,10 +66,10 @@ Indexer Design * Reduced Size Index An additional index is maintained by the repository manager in the - {{{../apidocs/org/apache/maven/repository/indexing/MinimalIndex.html} MinimalIndex}} class. This indexes all of the - same artifacts as the first index, but stores them with shorter field names and less information to maintain a smaller - size. This index is appropriate for use by certain clients such as IDE integration for fast searching. For a fuller - interface to the repository information, the integration should use the XMLRPC interface. + {{{../apidocs/org/apache/maven/repository/indexing/MinimalArtifactIndexRecord.html} MinimalIndex}} class. This + indexes all of the same artifacts as the first index, but stores them with shorter field names and less information to + maintain a smaller size. This index is appropriate for use by certain clients such as IDE integration for fast + searching. For a fuller interface to the repository information, the integration should use the XMLRPC interface. The following fields are in the reduced index: diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecordFactoryTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecordFactoryTest.java new file mode 100644 index 000000000..0ec38f86a --- /dev/null +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecordFactoryTest.java @@ -0,0 +1,117 @@ +package org.apache.maven.repository.indexing.record; + +/* + * 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.codehaus.plexus.PlexusTestCase; + +import java.io.File; + +/** + * Test the minimal artifact index record. + * + * @author Brett Porter + */ +public class MinimalArtifactIndexRecordFactoryTest + extends PlexusTestCase +{ + private RepositoryIndexRecordFactory factory; + + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private static final String TEST_GROUP_ID = "org.apache.maven.repository.record"; + + protected void setUp() + throws Exception + { + super.setUp(); + + factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" ); + + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + + ArtifactRepositoryFactory repositoryFactory = + (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + File file = getTestFile( "src/test/managed-repository" ); + repository = + repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); + } + + public void testIndexedJar() + { + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( "test-jar-1.0.jar" ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( "A\nB\nC\n" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testCorruptJar() + { + Artifact artifact = createArtifact( "test-corrupt-jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + public void testNonJar() + { + Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + public void testMissingFile() + { + Artifact artifact = createArtifact( "test-foo" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + private Artifact createArtifact( String artifactId ) + { + return createArtifact( artifactId, "1.0", "jar" ); + } + + private Artifact createArtifact( String artifactId, String version, String type ) + { + Artifact artifact = artifactFactory.createBuildArtifact( TEST_GROUP_ID, artifactId, version, type ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + return artifact; + } +} diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java new file mode 100644 index 000000000..898bbd123 --- /dev/null +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java @@ -0,0 +1,141 @@ +package org.apache.maven.repository.indexing.record; + +/* + * 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.codehaus.plexus.PlexusTestCase; + +import java.io.File; + +/** + * Test the minimal artifact index record. + * + * @author Brett Porter + */ +public class StandardArtifactIndexRecordFactoryTest + extends PlexusTestCase +{ + private RepositoryIndexRecordFactory factory; + + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private static final String TEST_GROUP_ID = "org.apache.maven.repository.record"; + + protected void setUp() + throws Exception + { + super.setUp(); + + factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" ); + + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + + ArtifactRepositoryFactory repositoryFactory = + (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + File file = getTestFile( "src/test/managed-repository" ); + repository = + repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); + } + + public void testIndexedJar() + { + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( "test-jar-1.0.jar" ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( "A\nB\nC\n" ); + expectedRecord.setArtifactId( "test-jar" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setVersion( "1.0" ); + expectedRecord.setFiles( "META-INF/MANIFEST.MF\nA.class\nb/B.class\nb/c/C.class\n" ); + expectedRecord.setPackages( "b\nb.c\n" ); + expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); + expectedRecord.setType( "jar" ); + expectedRecord.setRepository( "test" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testCorruptJar() + { + Artifact artifact = createArtifact( "test-corrupt-jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + public void testDll() + { + Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "d41d8cd98f00b204e9800998ecf8427e" ); + expectedRecord.setFilename( "test-dll-1.0.1.34.dll" ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setArtifactId( "test-dll" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setVersion( "1.0.1.34" ); + expectedRecord.setSha1Checksum( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ); + expectedRecord.setType( "dll" ); + expectedRecord.setRepository( "test" ); + expectedRecord.setClasses( "" ); + expectedRecord.setPackages( "" ); + expectedRecord.setFiles( "" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testMissingFile() + { + Artifact artifact = createArtifact( "test-foo" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + private Artifact createArtifact( String artifactId ) + { + return createArtifact( artifactId, "1.0", "jar" ); + } + + private Artifact createArtifact( String artifactId, String version, String type ) + { + Artifact artifact = artifactFactory.createBuildArtifact( TEST_GROUP_ID, artifactId, version, type ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + artifact.setRepository( repository ); + return artifact; + } +} diff --git a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-corrupt-jar/1.0/test-corrupt-jar-1.0.jar b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-corrupt-jar/1.0/test-corrupt-jar-1.0.jar new file mode 100644 index 000000000..e69de29bb diff --git a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-dll/1.0.1.34/test-dll-1.0.1.34.dll b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-dll/1.0.1.34/test-dll-1.0.1.34.dll new file mode 100644 index 000000000..e69de29bb diff --git a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-jar/1.0/test-jar-1.0.jar b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-jar/1.0/test-jar-1.0.jar new file mode 100644 index 0000000000000000000000000000000000000000..b78be2eb81b2dae5b108881937300447b7d9ce7e GIT binary patch literal 1408 zcmWIWW@Zs#-~hrSYk!(BAOTJW76xC}5Jz24KR5kbe@5RzpESxi5 zO1@BAI!#4NGlOONGtD1qYT{4D!^NgAc_yY^@l5q~=BJ{MMT}s#?5<2%?FF<6gb{Ap zh~^P?psb@_a!z7#v8&%%zJmz@b8=TN?OG-N;K_4`M_wvQg+hEs{%XtrToSeNQyiaC z;RgmeuQ$&h8CmbPo;maT^Pfx?+|Ro5J+;wdjrbrNBFn-e)RIsp5Pq@SWzT!v%^^pB zGai&%6_KjkbTex`=QWj^UQVgYk}UVjIL}g-b-*&8CYx5*fVanB= z`@AphvY4w>nOCvlVZoyr5B@WYKXlA9GT9N*S1rV&``hr6S&v@6?>^!4NzWNyu^f|q z_EufXXxE=BcbZPVFf(%*>1g= z=kwpa_6*lJY9lyodTe?)rwHzG>GC-u-L(7a(!4U3?0q`~-t5?=l3 zRH|T_{)NkX*nO_g5WD>R?%@yoAn)5UoZ7bn==c*r4Dud!@3Vltmy9O>xS=YXVQIvg z>rjBe9N#n7e{`#F_!KO$AvA|6Y*A~{|G1h{!oCVF?^6|AYJW}gD%4faXc=zX@ z`|dHQz4*1^LdnHX0nWD_k2`u8@JG6FOjj=yz8JaWct-FOD_cL#Z$8U+&Dfk~a7Qq! za9eoGwV>r$rF%W5=C|5>K5{?hx=kQ=tdOYYRH<6Mp zC~qRbNgxxNQ$d=z7_dhv%;gY`Cm12=8{tflCCG^bl)@3<1z0^az2mb6HCT=VZB5b# kr6W{}kb@bNk`Uk}R0lL&1$eWvfh5_1um$LbXUrfT0LLw<`v3p{ literal 0 HcmV?d00001 -- 2.39.5