brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [07/10] brooklyn-server git commit: tidy of BundleMaker and associated items to support creating ZIPs
Date Thu, 02 Mar 2017 12:16:08 GMT
tidy of BundleMaker and associated items to support creating ZIPs


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

Branch: refs/heads/master
Commit: b5cb107386c703e7c2e679a2c7d4f7c7d8fe16ec
Parents: 3376116
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Sat Feb 18 13:45:06 2017 +0000
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Wed Mar 1 09:00:33 2017 +0000

----------------------------------------------------------------------
 .../brooklyn/util/core/file/ArchiveUtils.java   | 29 ++++++++++++++--
 .../brooklyn/util/core/osgi/BundleMaker.java    | 35 +++++++++-----------
 .../java/org/apache/brooklyn/util/os/Os.java    |  2 +-
 3 files changed, 44 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b5cb1073/core/src/main/java/org/apache/brooklyn/util/core/file/ArchiveUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/file/ArchiveUtils.java b/core/src/main/java/org/apache/brooklyn/util/core/file/ArchiveUtils.java
index b10d7ae..ae01aeb 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/file/ArchiveUtils.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/file/ArchiveUtils.java
@@ -21,15 +21,19 @@ package org.apache.brooklyn.util.core.file;
 import static java.lang.String.format;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.EnumSet;
+import java.util.Enumeration;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.apache.brooklyn.location.ssh.SshMachineLocation;
 import org.apache.brooklyn.util.collections.MutableList;
 import org.apache.brooklyn.util.collections.MutableMap;
@@ -42,7 +46,10 @@ import org.apache.brooklyn.util.javalang.StackTraceSimplifier;
 import org.apache.brooklyn.util.net.Urls;
 import org.apache.brooklyn.util.os.Os;
 import org.apache.brooklyn.util.ssh.BashCommands;
+import org.apache.brooklyn.util.stream.Streams;
 import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Charsets;
 import com.google.common.base.Preconditions;
@@ -347,4 +354,22 @@ public class ArchiveUtils {
         }
     }
 
+    public static void extractZip(final ZipFile zip, final String targetFolder) {
+        new File(targetFolder).mkdir();
+        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
+        while (zipFileEntries.hasMoreElements()) {
+            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
+            File destFile = new File(targetFolder, entry.getName());
+            destFile.getParentFile().mkdirs();
+
+            if (!entry.isDirectory()) {
+                try (InputStream in=zip.getInputStream(entry); OutputStream out=new FileOutputStream(destFile))
{
+                    Streams.copy(in, out);
+                } catch (IOException e) {
+                    throw Exceptions.propagate(e);
+                }
+            }
+        }
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b5cb1073/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java b/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java
index 2be2612..1cdcff8 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java
@@ -33,6 +33,8 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 
+import javax.annotation.Nonnull;
+
 import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
 import org.apache.brooklyn.util.collections.MutableMap;
@@ -60,16 +62,22 @@ public class BundleMaker {
     private ResourceUtils resources;
     private Class<?> optionalDefaultClassForLoading;
 
-    public BundleMaker(Framework f, ResourceUtils resources) {
+    /** Constructor for use when not expecting to use with a framework */
+    public BundleMaker(@Nonnull ResourceUtils resources) {
+        this.resources = resources;
+    }
+    
+    public BundleMaker(@Nonnull Framework f, @Nonnull ResourceUtils resources) {
         this.framework = f;
         this.resources = resources;
     }
     
-    public BundleMaker(ManagementContext mgmt) {
+    public BundleMaker(@Nonnull ManagementContext mgmt) {
         this( ((LocalManagementContext)mgmt).getOsgiManager().get().getFramework(), ResourceUtils.create()
);
     }
 
-    /** if set, this will be used to resolve relative classpath fragments */
+    /** if set, this will be used to resolve relative classpath fragments;
+     * the {@link ResourceUtils} supplied in the constructor must also be with respect to
the given class */
     public void setDefaultClassForLoading(Class<?> optionalDefaultClassForLoading)
{
         this.optionalDefaultClassForLoading = optionalDefaultClassForLoading;
     }
@@ -91,24 +99,13 @@ public class BundleMaker {
                 path = "classpath:"+path;
             }
             
-            InputStream min;
-            try {
-                min = resources.getResourceFromUrl(Urls.mergePaths(path, MANIFEST_PATH));
-            } catch (RuntimeException e) {
-                Exceptions.propagateIfFatal(e);
-                IOException ioe = Exceptions.getFirstThrowableOfType(e, IOException.class);
-                if (ioe != null && ioe.toString().contains("not found on classpath"))
{
-                    min = null;
-                } else {
-                    throw e;
-                }
-            }
-            if (min==null) {
-                zout = new JarOutputStream(new FileOutputStream(f));
-                addUrlItemRecursively(zout, path, path, Predicates.alwaysTrue());
-            } else {
+            if (resources.doesUrlExist(Urls.mergePaths(path, MANIFEST_PATH))) {
+                InputStream min = resources.getResourceFromUrl(Urls.mergePaths(path, MANIFEST_PATH));
                 zout = new JarOutputStream(new FileOutputStream(f), new Manifest(min));
                 addUrlItemRecursively(zout, path, path, Predicates.not(Predicates.equalTo(MANIFEST_PATH)));
+            } else {
+                zout = new JarOutputStream(new FileOutputStream(f));
+                addUrlItemRecursively(zout, path, path, Predicates.alwaysTrue());
             }
             
             Streams.closeQuietly(zout);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b5cb1073/utils/common/src/main/java/org/apache/brooklyn/util/os/Os.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/os/Os.java b/utils/common/src/main/java/org/apache/brooklyn/util/os/Os.java
index 27a2c70..bd28558 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/os/Os.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/os/Os.java
@@ -553,7 +553,7 @@ public class Os {
 
     /** creates a temp dir which will be deleted on exit */
     public static File newTempDir(String prefix) {
-        String sanitizedPrefix = (prefix==null ? "" : prefix + "-");
+        String sanitizedPrefix = (prefix==null ? "" : Strings.makeValidFilename(prefix) +
"-");
         String tmpParent = tmp();
         
         //With lots of stale temp dirs it is possible to have 


Mime
View raw message