You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2017/08/23 14:49:49 UTC

[1/3] karaf git commit: Use java.util.file.Files.newInputStream()

Repository: karaf
Updated Branches:
  refs/heads/master 49e484437 -> edcda0b0e


Use java.util.file.Files.newInputStream()

Instead of wiring to FileInputStream, which is hard to GC due to
the presence of finalize(), use Files.newInputStream.

Brings down feature generation time in OpenDaylight, before:
real    2m5.828s
user    2m14.886s
sys     0m20.849s

after:
real    1m46.523s
user    1m59.258s
sys     0m17.048s

Signed-off-by: Robert Varga <ni...@hq.sk>


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/ed797960
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/ed797960
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/ed797960

Branch: refs/heads/master
Commit: ed797960d2a43eee8c04a9572478320af2420cfb
Parents: 49e4844
Author: Robert Varga <ni...@hq.sk>
Authored: Sun Aug 13 18:56:29 2017 +0200
Committer: Robert Varga <ni...@hq.sk>
Committed: Wed Aug 23 14:22:20 2017 +0200

----------------------------------------------------------------------
 .../features/GenerateDescriptorMojo.java        | 36 +++++++++-----------
 .../apache/karaf/tooling/utils/MojoSupport.java | 12 +++----
 2 files changed, 22 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/ed797960/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
index 1b84004..a6141ba 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
@@ -23,7 +23,6 @@ import static org.apache.karaf.deployer.kar.KarArtifactInstaller.FEATURE_CLASSIF
 import java.io.BufferedInputStream;
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -31,6 +30,7 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintStream;
 import java.io.StringWriter;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Comparator;
@@ -601,7 +601,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
         }
     }
 
-    private Dependency findMatchingDependency(List<Dependency> dependencies, Dependency reference) {
+    private static Dependency findMatchingDependency(List<Dependency> dependencies, Dependency reference) {
         String referenceName = reference.getName();
         for (Dependency dependency : dependencies) {
             if (referenceName.equals(dependency.getName())) {
@@ -611,7 +611,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
         return null;
     }
 
-    private void mergeDependencies(Dependency target, Dependency source) {
+    private static void mergeDependencies(Dependency target, Dependency source) {
         if (target.getVersion() == null || Feature.DEFAULT_VERSION.equals(target.getVersion())) {
             target.setVersion(source.getVersion());
         }
@@ -642,30 +642,28 @@ public class GenerateDescriptorMojo extends MojoSupport {
      */
 
     private Manifest getManifest(File file) throws IOException {
-        InputStream is;
+        final InputStream is;
         try {
-            is = new BufferedInputStream(new FileInputStream(file));
+            is = Files.newInputStream(file.toPath());
         } catch (Exception e) {
             getLog().warn("Error while opening artifact", e);
             return null;
         }
 
-        try {
-            is.mark(256 * 1024);
-            JarInputStream jar = new JarInputStream(is);
-            Manifest m = jar.getManifest();
-            if (m == null) {
-                getLog().warn("Manifest not present in the first entry of the zip - " + file.getName());
+        try (BufferedInputStream bis = new BufferedInputStream(is)) {
+            bis.mark(256 * 1024);
+
+            try (JarInputStream jar = new JarInputStream(bis)) {
+                Manifest m = jar.getManifest();
+                if (m == null) {
+                    getLog().warn("Manifest not present in the first entry of the zip - " + file.getName());
+                }
+                return m;
             }
-            jar.close();
-            return m;
-        } finally {
-            // just in case when we did not open bundle
-            is.close();
         }
     }
 
-    private Features readFeaturesFile(File featuresFile) throws XMLStreamException, JAXBException, IOException {
+    private static Features readFeaturesFile(File featuresFile) throws XMLStreamException, JAXBException, IOException {
         return JaxbUtil.unmarshal(featuresFile.toURI().toASCIIString(), false);
     }
 
@@ -866,7 +864,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
         }
     }
 
-    private Features toFeatures(Collection<Bundle> addedBundles, Collection<Dependency> addedDependencys, ObjectFactory objectFactory) {
+    private static Features toFeatures(Collection<Bundle> addedBundles, Collection<Dependency> addedDependencys, ObjectFactory objectFactory) {
         Features features = objectFactory.createFeaturesRoot();
         Feature feature = objectFactory.createFeature();
         feature.getBundle().addAll(addedBundles);
@@ -876,7 +874,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
     }
 
 
-    private void writeDependencies(Features features, File file) throws JAXBException, IOException {
+    private static void writeDependencies(Features features, File file) throws JAXBException, IOException {
         file.getParentFile().mkdirs();
         if (!file.getParentFile().exists() || !file.getParentFile().isDirectory()) {
             throw new IOException("Cannot create directory at " + file.getParent());

http://git-wip-us.apache.org/repos/asf/karaf/blob/ed797960/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
index ea79b40..a11c4e2 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
@@ -18,11 +18,12 @@
 package org.apache.karaf.tooling.utils;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.nio.file.Files;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -350,11 +351,8 @@ public abstract class MojoSupport extends AbstractMojo {
         File targetDir = destFile.getParentFile();
         ensureDirExists(targetDir);
 
-        try {
-            try (
-                FileInputStream is = new FileInputStream(sourceFile);
-                FileOutputStream bos = new FileOutputStream(destFile)
-            ) {
+        try (InputStream is = Files.newInputStream(sourceFile.toPath())) {
+            try (OutputStream bos = Files.newOutputStream(destFile.toPath())) {
                 StreamUtils.copy(is, bos);
             }
         } catch (IOException e) {


[3/3] karaf git commit: Reuse extracted manifest

Posted by cs...@apache.org.
Reuse extracted manifest

Obtaining the manifest can cost us some IO. Treat it as an invariant
when determining whether a file is a bundle.

Signed-off-by: Robert Varga <ni...@hq.sk>


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/edcda0b0
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/edcda0b0
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/edcda0b0

Branch: refs/heads/master
Commit: edcda0b0ea4a50ae8e8019af1eaff161507e3ee4
Parents: 9714d76
Author: Robert Varga <ni...@hq.sk>
Authored: Sun Aug 13 20:26:17 2017 +0200
Committer: Robert Varga <ni...@hq.sk>
Committed: Wed Aug 23 14:23:01 2017 +0200

----------------------------------------------------------------------
 .../apache/karaf/tooling/features/GenerateDescriptorMojo.java  | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/edcda0b0/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
index e4bcf07..f262ac3 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
@@ -483,8 +483,6 @@ public class GenerateDescriptorMojo extends MojoSupport {
 
                 if (!this.dependencyHelper.isArtifactAFeature(artifact)) {
                     String bundleName = this.dependencyHelper.artifactToMvn(artifact, getVersionOrRange(entry.getParent(), artifact));
-                    File bundleFile = this.dependencyHelper.resolve(artifact, getLog());
-                    Manifest manifest = getManifest(bundleFile);
 
                     for (ConfigFile cf : feature.getConfigfile()) {
                         if (bundleName.equals(cf.getLocation().replace('\n', ' ').trim())) {
@@ -493,7 +491,9 @@ public class GenerateDescriptorMojo extends MojoSupport {
                         }
                     }
 
-                    if (manifest == null || !ManifestUtils.isBundle(getManifest(bundleFile))) {
+                    File bundleFile = this.dependencyHelper.resolve(artifact, getLog());
+                    Manifest manifest = getManifest(bundleFile);
+                    if (manifest == null || !ManifestUtils.isBundle(manifest)) {
                         bundleName = "wrap:" + bundleName;
                         needWrap = true;
                     }


[2/3] karaf git commit: Cache unmarshalled Features

Posted by cs...@apache.org.
Cache unmarshalled Features

If we are processing a deep feature tree with multiple dependencies
referencing same features (like in the case of OpenDaylight), we end
up unmarshalling the same features over and over again.

Rather than doing that, instantiate a cache, which will hold a weak
reference to features already encountered.

Before:
real    1m46.523s
user    1m59.258s
sys     0m17.048s

After:
real    0m42.642s
user    1m0.892s
sys     0m10.148s

Signed-off-by: Robert Varga <ni...@hq.sk>


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/9714d76b
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/9714d76b
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/9714d76b

Branch: refs/heads/master
Commit: 9714d76be6442faca13e6f45f02a27eac52b8ebb
Parents: ed79796
Author: Robert Varga <ni...@hq.sk>
Authored: Sun Aug 13 19:38:26 2017 +0200
Committer: Robert Varga <ni...@hq.sk>
Committed: Wed Aug 23 14:22:59 2017 +0200

----------------------------------------------------------------------
 .../features/GenerateDescriptorMojo.java        | 28 ++++++++++++++++----
 1 file changed, 23 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/9714d76b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
index a6141ba..e4bcf07 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
@@ -38,6 +38,7 @@ import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.WeakHashMap;
 import java.util.jar.JarInputStream;
 import java.util.jar.Manifest;
 
@@ -456,6 +457,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
         // TODO Initialise the repositories from the existing feature file if any
         Map<Dependency, Feature> otherFeatures = new HashMap<>();
         Map<Feature, String> featureRepositories = new HashMap<>();
+        FeaturesCache cache = new FeaturesCache();
         for (final LocalDependency entry : localDependencies) {
             Object artifact = entry.getArtifact();
 
@@ -463,9 +465,11 @@ public class GenerateDescriptorMojo extends MojoSupport {
                 continue;
             }
 
-            processFeatureArtifact(features, feature, otherFeatures, featureRepositories, artifact, entry.getParent(),
-                    true);
+            processFeatureArtifact(features, feature, otherFeatures, featureRepositories, cache, artifact,
+                    entry.getParent(), true);
         }
+        // Do not retain cache beyond this point
+        cache = null;
 
         // Second pass to look for bundles
         if (addBundlesToPrimaryFeature) {
@@ -557,7 +561,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
     }
 
     private void processFeatureArtifact(Features features, Feature feature, Map<Dependency, Feature> otherFeatures,
-                                        Map<Feature, String> featureRepositories,
+                                        Map<Feature, String> featureRepositories, FeaturesCache cache,
                                         Object artifact, Object parent, boolean add)
             throws MojoExecutionException, XMLStreamException, JAXBException, IOException {
         if (this.dependencyHelper.isArtifactAFeature(artifact) && FEATURE_CLASSIFIER.equals(
@@ -567,9 +571,9 @@ public class GenerateDescriptorMojo extends MojoSupport {
                 throw new MojoExecutionException(
                         "Cannot locate file for feature: " + artifact + " at " + featuresFile);
             }
-            Features includedFeatures = readFeaturesFile(featuresFile);
+            Features includedFeatures = cache.get(featuresFile);
             for (String repository : includedFeatures.getRepository()) {
-                processFeatureArtifact(features, feature, otherFeatures, featureRepositories,
+                processFeatureArtifact(features, feature, otherFeatures, featureRepositories, cache,
                         new DefaultArtifact(MavenUtil.mvnToAether(repository)), parent, false);
             }
             for (Feature includedFeature : includedFeatures.getFeature()) {
@@ -921,4 +925,18 @@ public class GenerateDescriptorMojo extends MojoSupport {
         return "\tTree listing is saved here: " + treeListFile.getAbsolutePath() + "\n";
     }
 
+    private static final class FeaturesCache {
+        private final Map<File, Features> map = new WeakHashMap<>();
+
+        Features get(final File featuresFile) throws XMLStreamException, JAXBException, IOException {
+            final Features existing = map.get(featuresFile);
+            if (existing != null) {
+                return existing;
+            }
+
+            final Features computed = readFeaturesFile(featuresFile);
+            map.put(featuresFile, computed);
+            return computed;
+        }
+    }
 }