[FELIX-4317] SCR implementation should avoid using bundleContext.getBundle()

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1543754 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/Activator.java b/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
index 9ea1d8a..ebfb0d6 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
@@ -56,6 +56,9 @@
     // this bundle's context
     private static BundleContext m_context;
 
+    // this bundle
+    private static Bundle m_bundle;
+
     // the log service to log messages to
     private static volatile ServiceTracker m_logService;
 
@@ -85,6 +88,7 @@
     public void start( BundleContext context ) throws Exception
     {
         m_context = context;
+        m_bundle = context.getBundle();
         super.start(context);
     }
 
@@ -101,8 +105,8 @@
         m_configuration.start( m_context );
 
         // log SCR startup
-        log( LogService.LOG_INFO, m_context.getBundle(), " Version = {0}",
-            new Object[] {m_context.getBundle().getHeaders().get( Constants.BUNDLE_VERSION )}, null );
+        log( LogService.LOG_INFO, m_bundle, " Version = {0}",
+            new Object[] {m_bundle.getHeaders().get( Constants.BUNDLE_VERSION )}, null );
 
         // create and start the component actor
         m_componentActor = new ComponentActorThread();
@@ -188,7 +192,7 @@
             try {
                 this.started.await(m_configuration.stopTimeout(), TimeUnit.MILLISECONDS);
             } catch (InterruptedException e) {
-                log( LogService.LOG_WARNING, m_context.getBundle(), "The wait for bundle {0}/{1} being started before destruction has been interrupted.",
+                log( LogService.LOG_WARNING, m_bundle, "The wait for bundle {0}/{1} being started before destruction has been interrupted.",
                         new Object[] {bundle.getSymbolicName(), bundle.getBundleId()}, e );
             }
             disposeComponents( this.bundle );
@@ -216,7 +220,7 @@
         BundleContext context = bundle.getBundleContext();
         if ( context == null )
         {
-            log( LogService.LOG_ERROR, m_context.getBundle(), "Cannot get BundleContext of bundle {0}/{1}",
+            log( LogService.LOG_ERROR, m_bundle, "Cannot get BundleContext of bundle {0}/{1}",
                 new Object[] {bundle.getSymbolicName(), bundle.getBundleId()}, null );
             return;
         }
@@ -244,7 +248,7 @@
         // terminate if already loaded (or currently being loaded)
         if ( loaded )
         {
-            log( LogService.LOG_DEBUG, m_context.getBundle(), "Components for bundle {0}/{1} already loaded. Nothing to do.",
+            log( LogService.LOG_DEBUG, m_bundle, "Components for bundle {0}/{1} already loaded. Nothing to do.",
                 new Object[] {bundle.getSymbolicName(), bundle.getBundleId()}, null );
             return;
         }
@@ -273,14 +277,14 @@
             {
                 log(
                     LogService.LOG_DEBUG,
-                    m_context.getBundle(),
+                    m_bundle,
                     "Bundle {0}/{1} has been stopped while trying to activate its components. Trying again when the bundles gets started again.",
                 new Object[] {bundle.getSymbolicName(), bundle.getBundleId()},
                     e );
             }
             else
             {
-                log( LogService.LOG_ERROR, m_context.getBundle(), "Error while loading components of bundle {0}/{1}",
+                log( LogService.LOG_ERROR, m_bundle, "Error while loading components of bundle {0}/{1}",
                 new Object[] {bundle.getSymbolicName(), bundle.getBundleId()}, e );
             }
         }
@@ -310,7 +314,7 @@
             }
             catch ( Exception e )
             {
-                log( LogService.LOG_ERROR, m_context.getBundle(), "Error while disposing components of bundle {0}/{1}",
+                log( LogService.LOG_ERROR, m_bundle, "Error while disposing components of bundle {0}/{1}",
                     new Object[] {bundle.getSymbolicName(), bundle.getBundleId()}, e );
             }
         }
@@ -328,7 +332,7 @@
 
     @Override
     protected void error(String msg, Throwable t) {
-        log( LogService.LOG_DEBUG, m_context.getBundle(), msg, t );
+        log( LogService.LOG_DEBUG, m_bundle, msg, t );
     }
 
     public static void log( int level, Bundle bundle, String pattern, Object[] arguments, Throwable ex )
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java b/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java
index c33620a..91a9b16 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java
@@ -59,6 +59,9 @@
     // global component registration
     private final ComponentRegistry m_componentRegistry;
 
+    // The bundle owning the registered component
+    private final Bundle m_bundle;
+
     // The bundle context owning the registered component
     private final BundleContext m_context;
 
@@ -97,6 +100,7 @@
         m_componentRegistry = componentRegistry;
         m_componentActor = componentActor;
         m_context = context;
+        m_bundle = context.getBundle();
 
         // have the LogService handy (if available)
         m_logService = new ServiceTracker( context, Activator.LOGSERVICE_CLASS, null );
@@ -104,10 +108,10 @@
         m_configuration = configuration;
 
         log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] active",
-                new Object[] {m_context.getBundle().getBundleId()}, null, null, null );
+                new Object[] {m_bundle.getBundleId()}, null, null, null );
 
         // Get the Metadata-Location value from the manifest
-        String descriptorLocations = ( String ) m_context.getBundle().getHeaders().get( "Service-Component" );
+        String descriptorLocations = ( String ) m_bundle.getHeaders().get( "Service-Component" );
         if ( descriptorLocations == null )
         {
             throw new ComponentException( "Service-Component entry not found in the manifest" );
@@ -129,7 +133,7 @@
     private void initialize( String descriptorLocations )
     {
         log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] descriptor locations {1}",
-                new Object[] {m_context.getBundle().getBundleId(), descriptorLocations}, null, null, null );
+                new Object[] {m_bundle.getBundleId(), descriptorLocations}, null, null, null );
 
         // 112.4.1: The value of the the header is a comma separated list of XML entries within the Bundle
         StringTokenizer st = new StringTokenizer( descriptorLocations, ", " );
@@ -138,7 +142,7 @@
         {
             String descriptorLocation = st.nextToken();
 
-            URL[] descriptorURLs = findDescriptors( m_context.getBundle(), descriptorLocation );
+            URL[] descriptorURLs = findDescriptors( m_bundle, descriptorLocation );
             if ( descriptorURLs.length == 0 )
             {
                 // 112.4.1 If an XML document specified by the header cannot be located in the bundle and its attached
@@ -158,19 +162,19 @@
         for ( ComponentHolder componentHolder : m_managers )
         {
             log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] May enable component holder {1}",
-                    new Object[] {m_context.getBundle().getBundleId(), componentHolder.getComponentMetadata().getName()}, null, null, null );
+                    new Object[] {m_bundle.getBundleId(), componentHolder.getComponentMetadata().getName()}, null, null, null );
 
             if ( componentHolder.getComponentMetadata().isEnabled() )
             {
                 log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] Enabling component holder {1}",
-                        new Object[] {m_context.getBundle().getBundleId(), componentHolder.getComponentMetadata().getName()}, null, null, null );
+                        new Object[] {m_bundle.getBundleId(), componentHolder.getComponentMetadata().getName()}, null, null, null );
 
                 componentHolder.enableComponents( false );
             }
             else
             {
                 log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] Will not enable component holder {1}",
-                        new Object[] {m_context.getBundle().getBundleId(), componentHolder.getComponentMetadata().getName()}, null, null, null );
+                        new Object[] {m_bundle.getBundleId(), componentHolder.getComponentMetadata().getName()}, null, null, null );
             }
         }
     }
@@ -233,7 +237,7 @@
             stream = descriptorURL.openStream();
 
             BufferedReader in = new BufferedReader( new InputStreamReader( stream, "UTF-8" ) );
-            XmlHandler handler = new XmlHandler( m_context.getBundle(), this );
+            XmlHandler handler = new XmlHandler( m_bundle, this );
             KXml2SAXParser parser;
 
             parser = new KXml2SAXParser( in );
@@ -251,7 +255,7 @@
                     // check and reserve the component name (if not null)
                     if ( metadata.getName() != null )
                     {
-                        key = m_componentRegistry.checkComponentName( m_context.getBundle(), metadata.getName() );
+                        key = m_componentRegistry.checkComponentName( m_bundle, metadata.getName() );
                     }
 
                     // validate the component metadata
@@ -265,7 +269,7 @@
                     m_managers.add( holder );
 
                     log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] ComponentHolder created for {1}",
-                            new Object[] {m_context.getBundle().getBundleId(), metadata.getName()}, null, null, null );
+                            new Object[] {m_bundle.getBundleId(), metadata.getName()}, null, null, null );
 
                 }
                 catch ( Throwable t )
@@ -320,7 +324,7 @@
         if ( m_active.compareAndSet( true, false ))
         {
             log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] will destroy {1} instances", new Object[]
-                    { m_context.getBundle().getBundleId(), m_managers.size() }, null, null, null );
+                    { m_bundle.getBundleId(), m_managers.size() }, null, null, null );
 
             while ( m_managers.size() != 0 )
             {
@@ -337,14 +341,14 @@
                 }
                 finally
                 {
-                    m_componentRegistry.unregisterComponentHolder( m_context.getBundle(), holder.getComponentMetadata()
+                    m_componentRegistry.unregisterComponentHolder( m_bundle, holder.getComponentMetadata()
                             .getName() );
                 }
 
             }
 
             log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] STOPPED", new Object[]
-                    {m_context.getBundle().getBundleId()}, null, null, null );
+                    {m_bundle.getBundleId()}, null, null, null );
 
             m_logService.close();
             m_closeLatch.countDown();
@@ -481,7 +485,7 @@
             return m_managers.toArray( new ComponentHolder[m_managers.size()] );
         }
 
-        ComponentHolder componentHolder = m_componentRegistry.getComponentHolder( m_context.getBundle(), name );
+        ComponentHolder componentHolder = m_componentRegistry.getComponentHolder( m_bundle, name );
         if (componentHolder != null)
         {
             return new ComponentHolder[] { componentHolder };
@@ -609,7 +613,7 @@
                 Object logger = logService.getService();
                 if ( logger == null )
                 {
-                    Activator.log( level, getBundleContext().getBundle(), message, ex );
+                    Activator.log( level, m_bundle, message, ex );
                 }
                 else
                 {