FELIX-4737 : Provide an option to use system bundle context to get bundles/services (Support for subsystems)

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1648389 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundleContextUtil.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundleContextUtil.java
new file mode 100644
index 0000000..f76581a
--- /dev/null
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundleContextUtil.java
@@ -0,0 +1,46 @@
+/*
+ * 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.felix.webconsole.internal.core;
+
+
+import org.osgi.framework.BundleContext;
+
+
+/**
+ * The <code>BundleContextUtil</code> class.
+ */
+public class BundleContextUtil
+{
+    /**
+     * If this property is specified (regardless of it's value), the system bundle is used
+     * as a working bundle context. Otherwise the web console bundle context is used.
+     */
+    public static final String FWK_PROP_USE_SYSTEM_BUNDLE = "webconsole.use.systembundle";
+
+    /**
+     * Get the working bundle context: the bundle context to lookup bundles and
+     * services.
+     */
+    public static BundleContext getWorkingBundleContext( final BundleContext bc)
+    {
+        if ( bc.getProperty(FWK_PROP_USE_SYSTEM_BUNDLE) != null )
+        {
+            return bc.getBundle(0).getBundleContext();
+        }
+        return bc;
+    }
+}
\ No newline at end of file
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesConfigurationPrinter.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesConfigurationPrinter.java
index b7cd16c..d96d72c 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesConfigurationPrinter.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesConfigurationPrinter.java
@@ -19,10 +19,14 @@
 
 import java.io.PrintWriter;
 import java.text.MessageFormat;
-import java.util.*;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.TreeMap;
 
 import org.apache.felix.webconsole.internal.AbstractConfigurationPrinter;
-import org.osgi.framework.*;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 import org.osgi.service.packageadmin.PackageAdmin;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -101,7 +105,7 @@
      */
     public void printConfiguration( final PrintWriter pw )
     {
-        final Bundle[] bundles = this.getBundleContext().getBundles();
+        final Bundle[] bundles = BundleContextUtil.getWorkingBundleContext(this.getBundleContext()).getBundles();
         // create a map for sorting first
         final TreeMap bundlesMap = new TreeMap();
         int active = 0, installed = 0, resolved = 0, fragments = 0;
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
index 7b60b9f..605fb09 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
@@ -474,7 +474,7 @@
             final long bundleId = Long.parseLong( pathInfo );
             if ( bundleId >= 0 )
             {
-                return getBundleContext().getBundle( bundleId );
+                return BundleContextUtil.getWorkingBundleContext(this.getBundleContext()).getBundle( bundleId );
             }
         }
         catch ( NumberFormatException nfe )
@@ -492,7 +492,7 @@
             }
 
             // search
-            final Bundle[] bundles = getBundleContext().getBundles();
+            final Bundle[] bundles = BundleContextUtil.getWorkingBundleContext(this.getBundleContext()).getBundles();
             for(int i=0; i<bundles.length; i++)
             {
                 final Bundle bundle = bundles[i];
@@ -751,7 +751,7 @@
 
     private final Bundle[] getBundles()
     {
-        return getBundleContext().getBundles();
+        return BundleContextUtil.getWorkingBundleContext(this.getBundleContext()).getBundles();
     }
 
 
@@ -1672,7 +1672,7 @@
             }
             else
             {
-                Bundle[] bundles = getBundleContext().getBundles();
+                Bundle[] bundles = BundleContextUtil.getWorkingBundleContext(this.getBundleContext()).getBundles();
                 for ( int i = 0; i < bundles.length; i++ )
                 {
                     if ( ( bundles[i].getLocation() != null && bundles[i].getLocation().equals( location ) )
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesConfigurationPrinter.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesConfigurationPrinter.java
index be41582..7c3d160 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesConfigurationPrinter.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesConfigurationPrinter.java
@@ -30,10 +30,9 @@
 import org.osgi.framework.Constants;

 import org.osgi.framework.InvalidSyntaxException;

 import org.osgi.framework.ServiceReference;

-import org.osgi.service.packageadmin.ExportedPackage;

 

 /**

- * ServicesConfigurationPrinter provides a configuration printer for inspecting the 

+ * ServicesConfigurationPrinter provides a configuration printer for inspecting the

  * registered services.

  */

 public class ServicesConfigurationPrinter extends AbstractConfigurationPrinter implements Constants

@@ -131,7 +130,7 @@
         ServiceReference[] refs = null;

         try

         {

-            refs = getBundleContext().getAllServiceReferences(null, null);

+            refs = BundleContextUtil.getWorkingBundleContext(getBundleContext()).getAllServiceReferences(null, null);

         }

         catch (InvalidSyntaxException e)

         {

diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
index 146381d..e6efe0a 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
@@ -152,7 +152,7 @@
         String filterStr = filter.toString();
         try
         {
-            ServiceReference[] refs = getBundleContext().getAllServiceReferences( null, filterStr );
+            ServiceReference[] refs = BundleContextUtil.getWorkingBundleContext(this.getBundleContext()).getAllServiceReferences( null, filterStr );
             if ( refs == null || refs.length != 1 )
             {
                 return null;
@@ -176,7 +176,7 @@
         }
         try
         {
-            final ServiceReference[] refs = getBundleContext().getAllServiceReferences( null, filter );
+            final ServiceReference[] refs = BundleContextUtil.getWorkingBundleContext(this.getBundleContext()).getAllServiceReferences( null, filter );
             if ( refs != null )
             {
                 return refs;
@@ -420,5 +420,4 @@
 
         response.getWriter().print( TEMPLATE );
     }
-
 }