Fixing BND OSGI wrapp for Bazel build.

Change-Id: I7d70f79eb7ce79e5cca65659e096062fb469f18c
diff --git a/tools/build/bazel/osgi_java_library.bzl b/tools/build/bazel/osgi_java_library.bzl
index 7df0ec8..03913f4 100644
--- a/tools/build/bazel/osgi_java_library.bzl
+++ b/tools/build/bazel/osgi_java_library.bzl
@@ -86,23 +86,11 @@
     for dep in ctx.attr.deps:
         if java_common.provider in dep:
             file = dep.files.to_list()[0]
-
             if cp:
                 cp += ":"
             cp += file.path
             inputDependencies = inputDependencies + [file]
 
-    # extract the class files for use by bnd
-    classes = ctx.actions.declare_file("classes" + ctx.label.name.replace("/", "-"))
-    classesPath = classes.path
-    jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath)
-    ctx.actions.run_shell(
-        inputs = inputDependencies,
-        outputs = [classes],
-        command = jarCommand,
-        progress_message = "Expanding jar file: %s" % jar,
-    )
-    inputDependencies += [classes]
     web_xml_root_path = ""
     if len(web_xml) != 0:
         web_xml_root = web_xml[0].files.to_list()[0]
@@ -124,7 +112,7 @@
         web_context,
         web_xml_root_path,
         dynamicimportPackages,
-        classesPath,
+        "classes",
         bundle_classpath,
     ]
 
diff --git a/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java b/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java
index f7cf587..8a8d16f 100644
--- a/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java
+++ b/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java
@@ -28,9 +28,11 @@
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
+import com.google.common.io.ByteStreams;
 import org.apache.felix.scrplugin.bnd.SCRDescriptorBndPlugin;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.file.FileVisitResult;
 import java.nio.file.FileVisitor;
@@ -44,8 +46,12 @@
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
 import java.util.jar.Manifest;
 
+import static com.google.common.io.Files.createParentDirs;
+import static com.google.common.io.Files.write;
 import static java.nio.file.Files.walkFileTree;
 
 /**
@@ -206,6 +212,8 @@
     public boolean execute() {
         Analyzer analyzer = new Builder();
         try {
+            // Extract the input jar contents into the specified output directory
+            expandJar(inputJar, new File(destdir));
 
             Jar jar = new Jar(new File(inputJar));  // where our data is
             analyzer.setJar(jar);                   // give bnd the contents
@@ -217,9 +225,6 @@
 
             setProperties(analyzer);
 
-//            analyzer.setProperty("DESTDIR");
-//            analyzer.setBase();
-
             // ------------- let's begin... -------------------------
 
             // Analyze the target JAR first
@@ -243,9 +248,6 @@
 
             // Calculate the manifest
             Manifest manifest = analyzer.calcManifest();
-            //OutputStream s = new FileOutputStream("/tmp/foo2.txt");
-            //manifest.write(s);
-            //s.close();
 
             if (analyzer.isOk()) {
                 analyzer.getJar().setManifest(manifest);
@@ -269,6 +271,26 @@
         }
     }
 
+    // Expands the specified jar file into the given directory
+    private void expandJar(String inputJar, File intoDir) throws IOException {
+        try (JarInputStream jis = new JarInputStream(new FileInputStream(inputJar))) {
+            JarEntry entry;
+            while ((entry = jis.getNextJarEntry()) != null) {
+                if (!entry.isDirectory()) {
+                    byte[] data = ByteStreams.toByteArray(jis);
+                    jis.closeEntry();
+                    if (!entry.getName().contains("..")) {
+                        File file = new File(intoDir, entry.getName());
+                        createParentDirs(file);
+                        write(data, file);
+                    } else {
+                        throw new IOException("Corrupt jar file");
+                    }
+                }
+            }
+        }
+    }
+
     private boolean isWab() {
         return !Objects.equals(webContext, NONE);
     }