maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [6/6] maven-indexer git commit: improve logging and close InputStream
Date Mon, 24 Jul 2017 03:48:39 GMT
improve logging and close InputStream

Signed-off-by: olivier lamy <olamy@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/maven-indexer/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-indexer/commit/1a5d1560
Tree: http://git-wip-us.apache.org/repos/asf/maven-indexer/tree/1a5d1560
Diff: http://git-wip-us.apache.org/repos/asf/maven-indexer/diff/1a5d1560

Branch: refs/heads/master
Commit: 1a5d1560d165ccf136388816902712a604336c69
Parents: ce2f831
Author: olivier lamy <olamy@apache.org>
Authored: Mon Jul 24 13:36:58 2017 +1000
Committer: olivier lamy <olamy@apache.org>
Committed: Mon Jul 24 13:36:58 2017 +1000

----------------------------------------------------------------------
 .../org/apache/maven/index/ArtifactContext.java | 52 ++++++++------------
 .../apache/maven/index/util/zip/ZipHandle.java  |  2 +
 .../examples/indexing/IndexerConfiguration.java | 10 ++--
 .../examples/indexing/RepositoryIndexer.java    |  1 -
 .../indexing/RepositoryIndexerFactory.java      |  2 -
 5 files changed, 26 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-indexer/blob/1a5d1560/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java
----------------------------------------------------------------------
diff --git a/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java b/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java
index 80ed236..45c70ff 100644
--- a/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java
+++ b/indexer-core/src/main/java/org/apache/maven/index/ArtifactContext.java
@@ -22,6 +22,8 @@ package org.apache.maven.index;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -37,18 +39,22 @@ import org.apache.maven.index.util.zip.ZipHandle;
 import org.apache.maven.model.Model;
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * An artifact context used to provide information about artifact during scanning. It is
passed to the
  * {@link IndexCreator}, which can populate {@link ArtifactInfo} for the given artifact.
  * 
  * @see IndexCreator#populateArtifactInfo(ArtifactContext)
- * @see Indexer#scan(IndexingContext)
  * @author Jason van Zyl
  * @author Tamas Cservenak
  */
 public class ArtifactContext
 {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger( ArtifactContext.class );
+
     private final File pom;
 
     private final File artifact;
@@ -84,55 +90,39 @@ public class ArtifactContext
     public Model getPomModel()
     {
         // First check for local pom file
-        if ( getPom() != null && getPom().isFile() )
+        File pom = getPom();
+        if ( pom != null && pom.isFile() )
         {
-            try
-            {
-                return new MavenXpp3Reader().read( new FileInputStream( getPom() ), false
);
-            }
-            catch ( IOException e )
+            try (InputStream inputStream = Files.newInputStream( pom.toPath() ))
             {
-                e.printStackTrace();
+                return new MavenXpp3Reader().read( inputStream, false );
             }
-            catch ( XmlPullParserException e )
+            catch ( IOException | XmlPullParserException e )
             {
-                e.printStackTrace();
+                LOGGER.warn( "skip error reading pom: " + pom, e );
             }
         }
         // Otherwise, check for pom contained in maven generated artifact
         else if ( getArtifact() != null && getArtifact().isFile() )
         {
-            ZipHandle handle = null;
-
-            try
+            File artifact = getArtifact();
+            try(ZipHandle handle = ZipFacade.getZipHandle( artifact ))
             {
-                handle = ZipFacade.getZipHandle( getArtifact() );
 
                 final String embeddedPomPath =
                     "META-INF/maven/" + getGav().getGroupId() + "/" + getGav().getArtifactId()
+ "/pom.xml";
 
                 if ( handle.hasEntry( embeddedPomPath ) )
                 {
-                    return new MavenXpp3Reader().read( handle.getEntryContent( embeddedPomPath
), false );
+                    try(InputStream inputStream = handle.getEntryContent( embeddedPomPath
))
+                    {
+                        return new MavenXpp3Reader().read( inputStream, false );
+                    }
                 }
             }
-            catch ( IOException e )
+            catch ( IOException | XmlPullParserException e )
             {
-                e.printStackTrace();
-            }
-            catch ( XmlPullParserException e )
-            {
-                e.printStackTrace();
-            }
-            finally
-            {
-                try
-                {
-                    ZipFacade.close( handle );
-                }
-                catch ( Exception e )
-                {
-                }
+                LOGGER.warn( "skip error reading pom withing artifact:" + artifact, e );
             }
         }
 

http://git-wip-us.apache.org/repos/asf/maven-indexer/blob/1a5d1560/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipHandle.java
----------------------------------------------------------------------
diff --git a/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipHandle.java b/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipHandle.java
index 51dd876..c278a68 100644
--- a/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipHandle.java
+++ b/indexer-core/src/main/java/org/apache/maven/index/util/zip/ZipHandle.java
@@ -19,6 +19,7 @@ package org.apache.maven.index.util.zip;
  * under the License.
  */
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
@@ -30,6 +31,7 @@ import java.util.List;
  * @author cstamas
  */
 public interface ZipHandle
+    extends Closeable
 {
     /**
      * Returns true if Zip file this handle is pointing to contains an entry at given path.

http://git-wip-us.apache.org/repos/asf/maven-indexer/blob/1a5d1560/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/IndexerConfiguration.java
----------------------------------------------------------------------
diff --git a/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/IndexerConfiguration.java
b/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/IndexerConfiguration.java
index 814cd40..40717a3 100644
--- a/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/IndexerConfiguration.java
+++ b/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/IndexerConfiguration.java
@@ -58,13 +58,9 @@ public class IndexerConfiguration
 
     public List<IndexCreator> getIndexersAsList()
     {
-        List<IndexCreator> indexersAsList = new ArrayList<>();
-        for ( Map.Entry entry : indexers.entrySet() )
-        {
-            indexersAsList.add( (IndexCreator) entry.getValue() );
-        }
-
-        return indexersAsList;
+        return indexers == null ? //
+            new ArrayList<IndexCreator>( 0 ) //
+            : new ArrayList<>( indexers.values() );
     }
 
     public Indexer getIndexer()

http://git-wip-us.apache.org/repos/asf/maven-indexer/blob/1a5d1560/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexer.java
----------------------------------------------------------------------
diff --git a/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexer.java
b/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexer.java
index 4fd7047..fada90d 100644
--- a/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexer.java
+++ b/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexer.java
@@ -24,7 +24,6 @@ import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
 import org.apache.lucene.queryparser.classic.ParseException;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.Query;
-import org.apache.lucene.util.Version;
 import org.apache.maven.index.ArtifactContext;
 import org.apache.maven.index.ArtifactInfo;
 import org.apache.maven.index.ArtifactScanningListener;

http://git-wip-us.apache.org/repos/asf/maven-indexer/blob/1a5d1560/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexerFactory.java
----------------------------------------------------------------------
diff --git a/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexerFactory.java
b/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexerFactory.java
index c74777b..40918f4 100644
--- a/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexerFactory.java
+++ b/indexer-examples/indexer-examples-spring/src/main/java/org/apache/maven/indexer/examples/indexing/RepositoryIndexerFactory.java
@@ -23,8 +23,6 @@ import org.apache.maven.index.Indexer;
 import org.apache.maven.index.Scanner;
 import org.apache.maven.index.context.IndexCreator;
 import org.apache.maven.index.context.IndexingContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import javax.inject.Inject;
 import javax.inject.Singleton;


Mime
View raw message