[ 
https://issues.apache.org/jira/browse/FELIX-4317?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13825250#comment-13825250
 ] 

Guillaume Nodet commented on FELIX-4317:
----------------------------------------

{code}
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 315137c..4823629 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
@@ -56,6 +56,9 @@ public class BundleComponentActivator implements Logger
     // 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;
 
@@ -93,6 +96,7 @@ public class BundleComponentActivator implements Logger
         m_componentRegistry = componentRegistry;
         m_componentActor = componentActor;
         m_context = context;
+        m_bundle = context.getBundle();
 
         // mark this instance active
         m_active = true;
@@ -103,10 +107,10 @@ public class BundleComponentActivator implements Logger
         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" );
@@ -128,7 +132,7 @@ public class BundleComponentActivator implements Logger
     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, ", " );
@@ -137,7 +141,7 @@ public class BundleComponentActivator implements Logger
         {
             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
@@ -157,19 +161,19 @@ public class BundleComponentActivator implements Logger
         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 );
             }
         }
     }
@@ -232,7 +236,7 @@ public class BundleComponentActivator implements Logger
             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 );
@@ -250,7 +254,7 @@ public class BundleComponentActivator implements Logger
                     // 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
@@ -264,7 +268,7 @@ public class BundleComponentActivator implements Logger
                     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 )
@@ -326,7 +330,7 @@ public class BundleComponentActivator implements Logger
             m_active = 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 )
         {
@@ -343,14 +347,14 @@ public class BundleComponentActivator implements Logger
             }
             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();
 
@@ -474,7 +478,7 @@ public class BundleComponentActivator implements Logger
             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 };
{code}

> SCR implementation should avoid using bundleContext.getBundle()
> ---------------------------------------------------------------
>
>                 Key: FELIX-4317
>                 URL: https://issues.apache.org/jira/browse/FELIX-4317
>             Project: Felix
>          Issue Type: Improvement
>          Components: Declarative Services (SCR)
>    Affects Versions: scr-1.8.0
>            Reporter: Guillaume Nodet
>            Assignee: Guillaume Nodet
>             Fix For: scr-1.8.2
>
>
> The reason is that the bundle context can be invalidated by the framework, so 
> keeping the bundle as a member property avoids running into such exceptions



--
This message was sent by Atlassian JIRA
(v6.1#6144)

Reply via email to