ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hi...@apache.org
Subject [01/35] git commit: Add support for packed jar within an OSGi bundle
Date Tue, 28 Oct 2014 21:37:04 GMT
Repository: ant-ivy
Updated Branches:
  refs/heads/2.4.x 0d55ab15c -> a5bbbec54


Add support for packed jar within an OSGi bundle


git-svn-id: https://svn.apache.org/repos/asf/ant/ivy/core/trunk@1587110 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/ant-ivy/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant-ivy/commit/069d003e
Tree: http://git-wip-us.apache.org/repos/asf/ant-ivy/tree/069d003e
Diff: http://git-wip-us.apache.org/repos/asf/ant-ivy/diff/069d003e

Branch: refs/heads/2.4.x
Commit: 069d003efc343a9a2d627d5805431be5d88e5344
Parents: 01cf090
Author: Nicolas Lalevee <hibou@apache.org>
Authored: Sun Apr 13 22:14:03 2014 +0000
Committer: Nicolas Lalevee <hibou@apache.org>
Committed: Sun Apr 13 22:14:03 2014 +0000

----------------------------------------------------------------------
 doc/concept.html                                |  1 +
 .../apache/ivy/core/pack/OsgiBundlePacking.java | 48 +++++++++++
 .../apache/ivy/core/pack/Pack200Packing.java    | 29 +------
 .../apache/ivy/core/pack/PackingRegistry.java   |  1 +
 .../org/apache/ivy/core/pack/ZipPacking.java    | 25 +++---
 .../apache/ivy/osgi/core/BundleInfoAdapter.java |  4 +-
 .../apache/ivy/osgi/core/ManifestParser.java    |  1 +
 .../ivy/osgi/repo/AbstractOSGiResolver.java     |  3 +
 src/java/org/apache/ivy/util/FileUtil.java      | 91 ++++++++++++++++++++
 .../apache/ivy/osgi/p2/P2DescriptorTest.java    |  7 +-
 10 files changed, 171 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/doc/concept.html
----------------------------------------------------------------------
diff --git a/doc/concept.html b/doc/concept.html
index 77b6ebe..76029f2 100644
--- a/doc/concept.html
+++ b/doc/concept.html
@@ -289,6 +289,7 @@ A <i>packaged</i> artifact needs to be declared as such in
the module descriptor
 <ul>
     <li><tt>zip</tt>, <tt>jar</tt> or <tt>war</tt>:
the artifact will be uncompressed as a folder</li>
     <li><tt>pack200</tt>: the artifact will be unpacked to a file via the
<a href="http://docs.oracle.com/javase/7/docs/technotes/tools/share/pack200.html">pack200</a>
algorithm</li>
+    <li><tt>bundle</tt>: the OSGi artifact will be uncompressed as a folder,
and every embedded jar file entry which is packed via the the <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/share/pack200.html">pack200</a>
algorithm will be unpacked</li>
 </ul>
 
 So, if in an <tt>ivy.xml</tt>, there would be declared a such artifact:

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/src/java/org/apache/ivy/core/pack/OsgiBundlePacking.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/core/pack/OsgiBundlePacking.java b/src/java/org/apache/ivy/core/pack/OsgiBundlePacking.java
new file mode 100644
index 0000000..a9f9344
--- /dev/null
+++ b/src/java/org/apache/ivy/core/pack/OsgiBundlePacking.java
@@ -0,0 +1,48 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.
+ *
+ */
+package org.apache.ivy.core.pack;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.ivy.util.FileUtil;
+
+/**
+ * Packaging which handle OSGi bundles with inner packed jar
+ */
+public class OsgiBundlePacking extends ZipPacking {
+
+    private static final String[] NAMES = {"bundle"};
+
+    @Override
+    public String[] getNames() {
+        return NAMES;
+    }
+
+    @Override
+    protected void writeFile(InputStream zip, File f) throws FileNotFoundException, IOException
{
+        // XXX maybe we should only unpack file listed by the 'Bundle-ClassPath' MANIFEST
header ?
+        if (f.getName().endsWith(".jar.pack.gz")) {
+            zip = FileUtil.unwrapPack200(zip);
+            f = new File(f.getParentFile(), f.getName().substring(0, f.getName().length()
- 8));
+        }
+        super.writeFile(zip, f);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/src/java/org/apache/ivy/core/pack/Pack200Packing.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/core/pack/Pack200Packing.java b/src/java/org/apache/ivy/core/pack/Pack200Packing.java
index 13b945f..f2039d1 100644
--- a/src/java/org/apache/ivy/core/pack/Pack200Packing.java
+++ b/src/java/org/apache/ivy/core/pack/Pack200Packing.java
@@ -17,15 +17,10 @@
  */
 package org.apache.ivy.core.pack;
 
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.jar.JarOutputStream;
-import java.util.jar.Pack200;
-import java.util.jar.Pack200.Unpacker;
-import java.util.zip.GZIPInputStream;
+
+import org.apache.ivy.util.FileUtil;
 
 public class Pack200Packing extends StreamPacking {
 
@@ -54,25 +49,7 @@ public class Pack200Packing extends StreamPacking {
 
     @Override
     public InputStream unpack(InputStream packed) throws IOException {
-        BufferedInputStream buffered = new BufferedInputStream(packed);
-        buffered.mark(4);
-        byte[] magic = new byte[4];
-        buffered.read(magic, 0, 4);
-        buffered.reset();
-
-        InputStream in = buffered;
-
-        if (magic[0] == (byte) 0x1F && magic[1] == (byte) 0x8B && magic[2]
== (byte) 0x08) {
-            // this is a gziped pack200
-            in = new GZIPInputStream(in);
-        }
-
-        Unpacker unpacker = Pack200.newUnpacker();
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        JarOutputStream jar = new JarOutputStream(baos);
-        unpacker.unpack(in, jar);
-        jar.close();
-        return new ByteArrayInputStream(baos.toByteArray());
+        return FileUtil.unwrapPack200(packed);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/src/java/org/apache/ivy/core/pack/PackingRegistry.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/core/pack/PackingRegistry.java b/src/java/org/apache/ivy/core/pack/PackingRegistry.java
index a42f4ce..6688677f 100644
--- a/src/java/org/apache/ivy/core/pack/PackingRegistry.java
+++ b/src/java/org/apache/ivy/core/pack/PackingRegistry.java
@@ -28,6 +28,7 @@ public class PackingRegistry {
         // register defaults
         register(new ZipPacking());
         register(new Pack200Packing());
+        register(new OsgiBundlePacking());
     }
 
     public void register(ArchivePacking packing) {

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/src/java/org/apache/ivy/core/pack/ZipPacking.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/core/pack/ZipPacking.java b/src/java/org/apache/ivy/core/pack/ZipPacking.java
index aee058a..87cfbdf 100644
--- a/src/java/org/apache/ivy/core/pack/ZipPacking.java
+++ b/src/java/org/apache/ivy/core/pack/ZipPacking.java
@@ -18,6 +18,7 @@
 package org.apache.ivy.core.pack;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -66,16 +67,7 @@ public class ZipPacking extends ArchivePacking {
                 if (entry.isDirectory()) {
                     f.mkdirs();
                 } else {
-                    FileOutputStream out = new FileOutputStream(f);
-                    try {
-                        FileUtil.copy(zip, out, null, false);
-                    } finally {
-                        try {
-                            out.close();
-                        } catch (IOException e) {
-                            // ignore
-                        }
-                    }
+                    writeFile(zip, f);
                 }
 
                 f.setLastModified(entry.getTime());
@@ -91,4 +83,17 @@ public class ZipPacking extends ArchivePacking {
         }
     }
 
+    protected void writeFile(InputStream zip, File f) throws FileNotFoundException, IOException
{
+        FileOutputStream out = new FileOutputStream(f);
+        try {
+            FileUtil.copy(zip, out, null, false);
+        } finally {
+            try {
+                out.close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/src/java/org/apache/ivy/osgi/core/BundleInfoAdapter.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/osgi/core/BundleInfoAdapter.java b/src/java/org/apache/ivy/osgi/core/BundleInfoAdapter.java
index 2945dd9..29e3341 100644
--- a/src/java/org/apache/ivy/osgi/core/BundleInfoAdapter.java
+++ b/src/java/org/apache/ivy/osgi/core/BundleInfoAdapter.java
@@ -115,8 +115,8 @@ public class BundleInfoAdapter {
                 String type = "jar";
                 String ext = "jar";
                 String packaging = null;
-                if (bundle.hasInnerClasspath()) {
-                    packaging = "zip";
+                if (bundle.hasInnerClasspath() && !bundleArtifact.isSource()) {
+                    packaging = "bundle";
                 }
                 if ("packed".equals(bundleArtifact.getFormat())) {
                     ext = "jar.pack.gz";

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/src/java/org/apache/ivy/osgi/core/ManifestParser.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/osgi/core/ManifestParser.java b/src/java/org/apache/ivy/osgi/core/ManifestParser.java
index d98c4fd..2aec72f 100644
--- a/src/java/org/apache/ivy/osgi/core/ManifestParser.java
+++ b/src/java/org/apache/ivy/osgi/core/ManifestParser.java
@@ -204,6 +204,7 @@ public class ManifestParser {
         if (bundleClasspath != null) {
             ManifestHeaderValue bundleClasspathValue = new ManifestHeaderValue(bundleClasspath);
             bundleInfo.setClasspath(bundleClasspathValue.getValues());
+            bundleInfo.setHasInnerClasspath(true);
         }
 
         return bundleInfo;

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/src/java/org/apache/ivy/osgi/repo/AbstractOSGiResolver.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/osgi/repo/AbstractOSGiResolver.java b/src/java/org/apache/ivy/osgi/repo/AbstractOSGiResolver.java
index 6156d95..44c4e0a 100644
--- a/src/java/org/apache/ivy/osgi/repo/AbstractOSGiResolver.java
+++ b/src/java/org/apache/ivy/osgi/repo/AbstractOSGiResolver.java
@@ -234,6 +234,9 @@ public abstract class AbstractOSGiResolver extends BasicResolver {
     public ResolvedResource findResource(ResolvedResource[] rress, ResourceMDParser rmdparser,
             ModuleRevisionId mrid, Date date) {
         ResolvedResource found = super.findResource(rress, rmdparser, mrid, date);
+        if (found == null) {
+            return null;
+        }
 
         String osgiType = mrid.getOrganisation();
         // for non bundle requirement : log the selected bundle

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/src/java/org/apache/ivy/util/FileUtil.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/util/FileUtil.java b/src/java/org/apache/ivy/util/FileUtil.java
index 465abab..7a7be31 100644
--- a/src/java/org/apache/ivy/util/FileUtil.java
+++ b/src/java/org/apache/ivy/util/FileUtil.java
@@ -17,7 +17,10 @@
  */
 package org.apache.ivy.util;
 
+import java.io.BufferedInputStream;
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -36,7 +39,12 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Stack;
 import java.util.StringTokenizer;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Pack200;
+import java.util.jar.Pack200.Unpacker;
 import java.util.regex.Pattern;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.ZipInputStream;
 
 import org.apache.ivy.util.url.URLHandlerRegistry;
 
@@ -637,4 +645,87 @@ public final class FileUtil {
         return l;
     }
 
+    public static InputStream unwrapPack200(InputStream packed) throws IOException {
+        BufferedInputStream buffered = new BufferedInputStream(packed);
+        buffered.mark(4);
+        byte[] magic = new byte[4];
+        buffered.read(magic, 0, 4);
+        buffered.reset();
+
+        InputStream in = buffered;
+
+        if (magic[0] == (byte) 0x1F && magic[1] == (byte) 0x8B && magic[2]
== (byte) 0x08) {
+            // this is a gziped pack200
+            in = new GZIPInputStream(in);
+        }
+
+        Unpacker unpacker = Pack200.newUnpacker();
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        JarOutputStream jar = new JarOutputStream(baos);
+        unpacker.unpack(new UncloseInputStream(in), jar);
+        jar.close();
+        return new ByteArrayInputStream(baos.toByteArray());
+    }
+
+    /**
+     * Wrap an input stream and do not close the stream on call to close(). Used to avoid
closing a
+     * {@link ZipInputStream} used with {@link Unpacker#unpack(File, JarOutputStream)}
+     */
+    private static final class UncloseInputStream extends InputStream {
+
+        private InputStream wrapped;
+
+        public UncloseInputStream(InputStream wrapped) {
+            this.wrapped = wrapped;
+        }
+
+        public void close() throws IOException {
+            // do not close
+        }
+
+        public int read() throws IOException {
+            return wrapped.read();
+        }
+
+        public int hashCode() {
+            return wrapped.hashCode();
+        }
+
+        public int read(byte[] b) throws IOException {
+            return wrapped.read(b);
+        }
+
+        public boolean equals(Object obj) {
+            return wrapped.equals(obj);
+        }
+
+        public int read(byte[] b, int off, int len) throws IOException {
+            return wrapped.read(b, off, len);
+        }
+
+        public long skip(long n) throws IOException {
+            return wrapped.skip(n);
+        }
+
+        public String toString() {
+            return wrapped.toString();
+        }
+
+        public int available() throws IOException {
+            return wrapped.available();
+        }
+
+        public void mark(int readlimit) {
+            wrapped.mark(readlimit);
+        }
+
+        public void reset() throws IOException {
+            wrapped.reset();
+        }
+
+        public boolean markSupported() {
+            return wrapped.markSupported();
+        }
+
+    }
 }

http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/069d003e/test/java/org/apache/ivy/osgi/p2/P2DescriptorTest.java
----------------------------------------------------------------------
diff --git a/test/java/org/apache/ivy/osgi/p2/P2DescriptorTest.java b/test/java/org/apache/ivy/osgi/p2/P2DescriptorTest.java
index 1a877cd..f1766a9 100644
--- a/test/java/org/apache/ivy/osgi/p2/P2DescriptorTest.java
+++ b/test/java/org/apache/ivy/osgi/p2/P2DescriptorTest.java
@@ -195,7 +195,12 @@ public class P2DescriptorTest extends TestCase {
 
             assertEquals(artifact, ar.getArtifact());
             assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
-            assertNotNull(ar.getUnpackedLocalFile());
+            // only the binary get unpacked
+            if (ar.getArtifact().getType().equals("source")) {
+                assertNull(ar.getUnpackedLocalFile());
+            } else {
+                assertNotNull(ar.getUnpackedLocalFile());
+            }
         }
     }
 


Mime
View raw message