Augment osgi java library bazel plugin to specify bundle classpath

Change-Id: I123064bbcb30ffb6ce60458a74356c989d3df471
diff --git a/tools/build/bazel/osgi_java_library.bzl b/tools/build/bazel/osgi_java_library.bzl
index d1b86d3..0352157 100644
--- a/tools/build/bazel/osgi_java_library.bzl
+++ b/tools/build/bazel/osgi_java_library.bzl
@@ -70,6 +70,7 @@
     version = ctx.attr.version
     license = ""
     import_packages = ctx.attr.import_packages
+    bundle_classpath = ctx.attr.bundle_classpath
     exportPackages = "*"
     include_resources = ctx.attr.include_resources
     web_context = ctx.attr.web_context
@@ -124,6 +125,7 @@
         web_xml_root_path,
         dynamicimportPackages,
         classesPath,
+        bundle_classpath,
     ]
 
     ctx.actions.run(
@@ -152,6 +154,7 @@
         "group": attr.string(),
         "source": attr.label(),
         "import_packages": attr.string(),
+        "bundle_classpath": attr.string(),
         "web_context": attr.string(),
         "web_xml": attr.label_list(allow_files = True),
         "include_resources": attr.string(),
@@ -383,7 +386,8 @@
         api_version = "",
         api_description = "",
         api_package = "",
-        import_packages = None):
+        import_packages = None,
+        bundle_classpath = ""):
     if name == None:
         name = "onos-" + native.package_name().replace("/", "-")
     if srcs == None:
@@ -460,6 +464,7 @@
         group = group,
         visibility = visibility,
         import_packages = import_packages,
+        bundle_classpath = bundle_classpath,
         web_context = web_context,
         web_xml = web_xml,
         include_resources = _include_resources_to_string(include_resources),
@@ -506,6 +511,7 @@
               For example apps/mcast/app becomes onos-apps-mcast-app
         deps: Dependencies of the generated jar file. Expressed as a list of targets
         import_packages: OSGI import list. Optional, comma separated list, defaults to "*"
+        bundle_classpath: intended for including dependencies in our bundle, so that our bundle can be deployed standalone
         group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject'
         srcs: Source file(s) to compile. Optional list of targets, defaults to src/main/java/**/*.java
         resources_root: Relative path to the root of the tree of resources for this jar. Optional, defaults to src/main/resources
@@ -540,7 +546,8 @@
         api_title = "",
         api_version = "",
         api_description = "",
-        api_package = ""):
+        api_package = "",
+        bundle_classpath = ""):
     if srcs == None:
         srcs = _all_java_sources()
     if deps == None:
@@ -569,4 +576,5 @@
         api_description = api_description,
         api_package = api_package,
         web_context = web_context,
+        bundle_classpath = 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 b5669fe..f7cf587 100644
--- a/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java
+++ b/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java
@@ -77,9 +77,11 @@
     private String webXmlRoot;
     private String destdir;
 
+    private String bundleClasspath;
+
     // FIXME should consider using Commons CLI, etc.
     public static void main(String[] args) {
-        if (args.length < 13) {
+        if (args.length < 14) {
             System.err.println("Not enough args");
             System.exit(1);
         }
@@ -98,18 +100,20 @@
         String webXmlRoot = args[11];
         String dynamicimportPackages = args[12];
         String destdir = args[13];
+        String bundleClasspath = args[14];
         String desc = Joiner.on(' ').join(Arrays.copyOfRange(args, 12, args.length));
 
         OSGiWrapper wrapper = new OSGiWrapper(jar, output, cp,
-                                              name, group,
-                                              version, license,
-                                              importPackages, exportPackages,
-                                              includeResources,
-                                              webContext,
-                                              webXmlRoot,
-                                              dynamicimportPackages,
-                                              desc,
-                                              destdir);
+                name, group,
+                version, license,
+                importPackages, exportPackages,
+                includeResources,
+                webContext,
+                webXmlRoot,
+                dynamicimportPackages,
+                desc,
+                destdir,
+                bundleClasspath);
         wrapper.log(wrapper + "\n");
         if (!wrapper.execute()) {
             System.err.printf("Error generating %s\n", name);
@@ -132,7 +136,8 @@
                        String webXmlRoot,
                        String dynamicimportPackages,
                        String bundleDescription,
-                       String destdir) {
+                       String destdir,
+                       String bundleClasspath) {
         this.inputJar = inputJar;
         this.classpath = Lists.newArrayList(classpath.split(":"));
         if (!this.classpath.contains(inputJar)) {
@@ -161,6 +166,8 @@
         this.webContext = webContext;
         this.webXmlRoot = webXmlRoot;
         this.destdir = destdir;
+
+        this.bundleClasspath = bundleClasspath;
     }
 
     private void setProperties(Analyzer analyzer) {
@@ -175,9 +182,6 @@
         //analyzer.setProperty("-provider-policy", "${range;[===,==+)}");
         //analyzer.setProperty("-consumer-policy", "${range;[===,==+)}");
 
-        // There are no good defaults so make sure you set the Import-Package
-        analyzer.setProperty(Analyzer.IMPORT_PACKAGE, importPackages);
-
         analyzer.setProperty(Analyzer.DYNAMICIMPORT_PACKAGE, dynamicimportPackages);
 
         // TODO include version in export, but not in import
@@ -188,10 +192,14 @@
             analyzer.setProperty(Analyzer.INCLUDE_RESOURCE, includeResources);
         }
 
+        // There are no good defaults so make sure you set the Import-Package
+        analyzer.setProperty(Analyzer.IMPORT_PACKAGE, importPackages);
+
         if (isWab()) {
             analyzer.setProperty(Analyzer.WAB, webXmlRoot);
             analyzer.setProperty("Web-ContextPath", webContext);
-            analyzer.setProperty(Analyzer.IMPORT_PACKAGE, "*,org.glassfish.jersey.servlet,org.jvnet.mimepull\n");
+            analyzer.setProperty(Analyzer.IMPORT_PACKAGE, importPackages +
+                    ",org.glassfish.jersey.servlet,org.jvnet.mimepull\n");
         }
     }
 
@@ -274,7 +282,7 @@
 
         log("wab %s", wab);
 
-        String specifiedClasspath = analyzer.getProperty(analyzer.BUNDLE_CLASSPATH);
+        String specifiedClasspath = this.bundleClasspath;
         String bundleClasspath = "WEB-INF/classes";
         if (specifiedClasspath != null) {
             bundleClasspath += "," + specifiedClasspath;
@@ -349,7 +357,7 @@
             walkFileTree(sourceRootPath, visitor);
         } else {
             warn("Skipping resource in bundle %s: %s (File Not Found)\n",
-                 bundleSymbolicName, sourceRoot);
+                    bundleSymbolicName, sourceRoot);
         }
     }
 
@@ -394,6 +402,7 @@
                 .add("bundleVersion", bundleVersion)
                 .add("bundleDescription", bundleDescription)
                 .add("bundleLicense", bundleLicense)
+                .add("bundleClassPath", bundleClasspath)
                 .toString();
 
     }