You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2011/03/30 12:08:24 UTC
svn commit: r1086892 - in
/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index: ./
creator/ util/zip/
Author: cstamas
Date: Wed Mar 30 10:08:24 2011
New Revision: 1086892
URL: http://svn.apache.org/viewvc?rev=1086892&view=rev
Log:
Integrated ZipFacade into indexer, no more direct ZipFile usages in indexer.
Removed:
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/util/zip/DefaultZipFacade.java
Modified:
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/JarFileContentsIndexCreator.java
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenArchetypeArtifactInfoIndexCreator.java
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenPluginArtifactInfoIndexCreator.java
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipFacade.java
Modified: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java
URL: http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java?rev=1086892&r1=1086891&r2=1086892&view=diff
==============================================================================
--- maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java (original)
+++ maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java Wed Mar 30 10:08:24 2011
@@ -26,10 +26,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
-import java.util.Enumeration;
import java.util.List;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
@@ -38,6 +35,8 @@ import org.apache.lucene.document.Field.
import org.apache.maven.index.artifact.Gav;
import org.apache.maven.index.context.IndexCreator;
import org.apache.maven.index.context.IndexingContext;
+import org.apache.maven.index.util.zip.ZipFacade;
+import org.apache.maven.index.util.zip.ZipHandle;
import org.apache.maven.model.Model;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
@@ -102,23 +101,18 @@ public class ArtifactContext
// Otherwise, check for pom contained in maven generated artifact
else if ( getArtifact() != null )
{
- ZipFile jar = null;
+ ZipHandle handle = null;
try
{
- jar = new ZipFile( getArtifact() );
+ handle = ZipFacade.getZipHandle( getArtifact() );
- Enumeration<? extends ZipEntry> en = jar.entries();
- while ( en.hasMoreElements() )
- {
- ZipEntry e = en.nextElement();
- String name = e.getName();
+ final String embeddedPomPath =
+ "META-INF/maven/" + getGav().getGroupId() + "/" + getGav().getArtifactId() + "/pom.xml";
- // pom will be stored under /META-INF/maven/groupId/artifactId/pom.xml
- if ( name.equals( "META-INF/maven/" + gav.getGroupId() + "/" + gav.getArtifactId() + "/pom.xml" ) )
- {
- return new ModelReader().readModel( jar.getInputStream( e ) );
- }
+ if ( handle.hasEntry( embeddedPomPath ) )
+ {
+ return new ModelReader().readModel( handle.getEntryContent( embeddedPomPath ) );
}
}
catch ( IOException e )
@@ -126,15 +120,12 @@ public class ArtifactContext
}
finally
{
- if ( jar != null )
+ try
+ {
+ ZipFacade.close( handle );
+ }
+ catch ( Exception e )
{
- try
- {
- jar.close();
- }
- catch ( Exception e )
- {
- }
}
}
}
Modified: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/JarFileContentsIndexCreator.java
URL: http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/JarFileContentsIndexCreator.java?rev=1086892&r1=1086891&r2=1086892&view=diff
==============================================================================
--- maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/JarFileContentsIndexCreator.java (original)
+++ maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/JarFileContentsIndexCreator.java Wed Mar 30 10:08:24 2011
@@ -22,9 +22,7 @@ import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Enumeration;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
+import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Index;
@@ -35,6 +33,8 @@ import org.apache.maven.index.IndexerFie
import org.apache.maven.index.IndexerFieldVersion;
import org.apache.maven.index.MAVEN;
import org.apache.maven.index.context.IndexCreator;
+import org.apache.maven.index.util.zip.ZipFacade;
+import org.apache.maven.index.util.zip.ZipHandle;
import org.codehaus.plexus.component.annotations.Component;
/**
@@ -136,21 +136,18 @@ public class JarFileContentsIndexCreator
private void updateArtifactInfo( ArtifactInfo ai, File f )
throws IOException
{
- ZipFile jar = null;
+ ZipHandle handle = null;
try
{
- jar = new ZipFile( f );
+ handle = ZipFacade.getZipHandle( f );
- StringBuilder sb = new StringBuilder();
+ final List<String> entries = handle.getEntries();
- Enumeration<?> en = jar.entries();
- while ( en.hasMoreElements() )
- {
- ZipEntry e = (ZipEntry) en.nextElement();
-
- String name = e.getName();
+ final StringBuilder sb = new StringBuilder();
+ for ( String name : entries )
+ {
if ( name.endsWith( ".class" ) )
{
// TODO verify if class is public or protected
@@ -171,9 +168,11 @@ public class JarFileContentsIndexCreator
}
}
- if ( sb.toString().trim().length() != 0 )
+ final String fieldValue = sb.toString().trim();
+
+ if ( fieldValue.length() != 0 )
{
- ai.classNames = sb.toString();
+ ai.classNames = fieldValue;
}
else
{
@@ -182,16 +181,13 @@ public class JarFileContentsIndexCreator
}
finally
{
- if ( jar != null )
+ try
{
- try
- {
- jar.close();
- }
- catch ( Exception e )
- {
- getLogger().error( "Could not close jar file properly.", e );
- }
+ ZipFacade.close( handle );
+ }
+ catch ( Exception e )
+ {
+ getLogger().error( "Could not close jar file properly.", e );
}
}
}
Modified: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenArchetypeArtifactInfoIndexCreator.java
URL: http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenArchetypeArtifactInfoIndexCreator.java?rev=1086892&r1=1086891&r2=1086892&view=diff
==============================================================================
--- maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenArchetypeArtifactInfoIndexCreator.java (original)
+++ maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenArchetypeArtifactInfoIndexCreator.java Wed Mar 30 10:08:24 2011
@@ -22,14 +22,14 @@ import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
import org.apache.lucene.document.Document;
import org.apache.maven.index.ArtifactContext;
import org.apache.maven.index.ArtifactInfo;
import org.apache.maven.index.IndexerField;
import org.apache.maven.index.context.IndexCreator;
+import org.apache.maven.index.util.zip.ZipFacade;
+import org.apache.maven.index.util.zip.ZipHandle;
import org.codehaus.plexus.component.annotations.Component;
/**
@@ -77,16 +77,18 @@ public class MavenArchetypeArtifactInfoI
*/
private void checkMavenArchetype( ArtifactInfo ai, File artifact )
{
- ZipFile jf = null;
+ ZipHandle handle = null;
try
{
- jf = new ZipFile( artifact );
+ handle = ZipFacade.getZipHandle( artifact );
- for ( String location : ARCHETYPE_XML_LOCATIONS )
+ for ( String path : ARCHETYPE_XML_LOCATIONS )
{
- if ( checkEntry( ai, jf, location ) )
+ if ( handle.hasEntry( path ) )
{
+ ai.packaging = MAVEN_ARCHETYPE_PACKAGING;
+
return;
}
}
@@ -106,21 +108,14 @@ public class MavenArchetypeArtifactInfoI
}
finally
{
- close( jf );
- }
- }
-
- private boolean checkEntry( ArtifactInfo ai, ZipFile jf, String entryName )
- {
- ZipEntry entry = jf.getEntry( entryName );
-
- if ( entry != null )
- {
- ai.packaging = MAVEN_ARCHETYPE_PACKAGING;
-
- return true;
+ try
+ {
+ ZipFacade.close( handle );
+ }
+ catch ( IOException ex )
+ {
+ }
}
- return false;
}
public void updateDocument( ArtifactInfo ai, Document doc )
@@ -137,20 +132,6 @@ public class MavenArchetypeArtifactInfoI
// ==
- private void close( ZipFile zf )
- {
- if ( zf != null )
- {
- try
- {
- zf.close();
- }
- catch ( IOException ex )
- {
- }
- }
- }
-
@Override
public String toString()
{
Modified: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenPluginArtifactInfoIndexCreator.java
URL: http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenPluginArtifactInfoIndexCreator.java?rev=1086892&r1=1086891&r2=1086892&view=diff
==============================================================================
--- maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenPluginArtifactInfoIndexCreator.java (original)
+++ maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/MavenPluginArtifactInfoIndexCreator.java Wed Mar 30 10:08:24 2011
@@ -26,8 +26,6 @@ import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Index;
@@ -38,10 +36,11 @@ import org.apache.maven.index.IndexerFie
import org.apache.maven.index.IndexerFieldVersion;
import org.apache.maven.index.MAVEN;
import org.apache.maven.index.context.IndexCreator;
+import org.apache.maven.index.util.zip.ZipFacade;
+import org.apache.maven.index.util.zip.ZipHandle;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
-import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
/**
@@ -83,35 +82,39 @@ public class MavenPluginArtifactInfoInde
private void checkMavenPlugin( ArtifactInfo ai, File artifact )
{
- ZipFile jf = null;
-
- InputStream is = null;
+ ZipHandle handle = null;
try
{
- jf = new ZipFile( artifact );
+ handle = ZipFacade.getZipHandle( artifact );
- ZipEntry entry = jf.getEntry( "META-INF/maven/plugin.xml" );
+ final String pluginDescriptorPath = "META-INF/maven/plugin.xml";
- if ( entry == null )
+ if ( handle.hasEntry( pluginDescriptorPath ) )
{
- return;
- }
-
- is = new BufferedInputStream( jf.getInputStream( entry ) );
-
- PlexusConfiguration plexusConfig =
- new XmlPlexusConfiguration( Xpp3DomBuilder.build( new InputStreamReader( is ) ) );
-
- ai.prefix = plexusConfig.getChild( "goalPrefix" ).getValue();
-
- ai.goals = new ArrayList<String>();
+ InputStream is = new BufferedInputStream( handle.getEntryContent( pluginDescriptorPath ) );
- PlexusConfiguration[] mojoConfigs = plexusConfig.getChild( "mojos" ).getChildren( "mojo" );
-
- for ( PlexusConfiguration mojoConfig : mojoConfigs )
- {
- ai.goals.add( mojoConfig.getChild( "goal" ).getValue() );
+ try
+ {
+ // here the reader is closed
+ PlexusConfiguration plexusConfig =
+ new XmlPlexusConfiguration( Xpp3DomBuilder.build( new InputStreamReader( is ) ) );
+
+ ai.prefix = plexusConfig.getChild( "goalPrefix" ).getValue();
+
+ ai.goals = new ArrayList<String>();
+
+ PlexusConfiguration[] mojoConfigs = plexusConfig.getChild( "mojos" ).getChildren( "mojo" );
+
+ for ( PlexusConfiguration mojoConfig : mojoConfigs )
+ {
+ ai.goals.add( mojoConfig.getChild( "goal" ).getValue() );
+ }
+ }
+ finally
+ {
+ is.close();
+ }
}
}
catch ( Exception e )
@@ -120,9 +123,13 @@ public class MavenPluginArtifactInfoInde
}
finally
{
- close( jf );
-
- IOUtil.close( is );
+ try
+ {
+ ZipFacade.close( handle );
+ }
+ catch ( IOException e )
+ {
+ }
}
}
@@ -160,20 +167,6 @@ public class MavenPluginArtifactInfoInde
return res;
}
- private void close( ZipFile zf )
- {
- if ( zf != null )
- {
- try
- {
- zf.close();
- }
- catch ( IOException ex )
- {
- }
- }
- }
-
@Override
public String toString()
{
Modified: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipFacade.java
URL: http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipFacade.java?rev=1086892&r1=1086891&r2=1086892&view=diff
==============================================================================
--- maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipFacade.java (original)
+++ maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipFacade.java Wed Mar 30 10:08:24 2011
@@ -3,8 +3,55 @@ package org.apache.maven.index.util.zip;
import java.io.File;
import java.io.IOException;
-public interface ZipFacade
+public class ZipFacade
{
- ZipHandle getZipHandle( File targetFile )
- throws IOException;
+ public static final long MEGABYTE = 1048576L;
+
+ public static final long JAVA_ZIPFILE_SIZE_THRESHOLD = Long.getLong(
+ "org.apache.maven.index.util.zip.ZipFacade.javaZipFileSizeThreshold", 100L * MEGABYTE );
+
+ private static final boolean TRUEZIP_AVAILABLE;
+
+ static
+ {
+ Class<?> clazz;
+
+ try
+ {
+ clazz = Class.forName( "de.schlichtherle.truezip.zip.ZipFile" );
+ }
+ catch ( ClassNotFoundException e )
+ {
+ clazz = null;
+ }
+
+ TRUEZIP_AVAILABLE = clazz != null;
+ }
+
+ public static ZipHandle getZipHandle( File targetFile )
+ throws IOException
+ {
+ if ( targetFile.isFile() )
+ {
+ if ( TRUEZIP_AVAILABLE && targetFile.length() > JAVA_ZIPFILE_SIZE_THRESHOLD )
+ {
+ return new TrueZipZipFileHandle( targetFile );
+ }
+ else
+ {
+ return new JavaZipFileHandle( targetFile );
+ }
+ }
+
+ throw new IOException( "The targetFile should point to an existing ZIP file!" );
+ }
+
+ public static void close( ZipHandle handle )
+ throws IOException
+ {
+ if ( handle != null )
+ {
+ handle.close();
+ }
+ }
}