You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by jb...@apache.org on 2010/11/02 03:24:07 UTC

svn commit: r1029941 - in /incubator/aries/sandbox/jbohn/interceptor-proto: blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ blueprint/blueprint-core/src/mai...

Author: jbohn
Date: Tue Nov  2 02:24:07 2010
New Revision: 1029941

URL: http://svn.apache.org/viewvc?rev=1029941&view=rev
Log:
ARIES-420 split service registration from service reference creation, set interceptor config data from namespace handler

Modified:
    incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/Interceptor.java
    incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
    incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/QuiesceInterceptor.java
    incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java
    incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java
    incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/pom.xml
    incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxInterceptorImpl.java
    incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java
    incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml
    incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java
    incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/Interceptor.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/Interceptor.java?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/Interceptor.java (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/Interceptor.java Tue Nov  2 02:24:07 2010
@@ -75,4 +75,10 @@ public interface Interceptor {
      * @return the identity of the interceptor
      */
     public String getIdentity();
+    
+    /**
+     * Set any necessary configuration data for this Interceptor
+     *  @param config: An Object understood by the Interceptor to manage configuration information
+     */
+    public void setConfig(Object config);
 }

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java Tue Nov  2 02:24:07 2010
@@ -676,19 +676,8 @@ public class BeanRecipe extends Abstract
         String beanId = getName();
         BeanMetadata beanData = (BeanMetadata) blueprintContainer.getComponentDefinitionRegistry().getComponentDefinition(getName());
         
-        // lookup interceptors using the old mechanism
-//        List<Interceptor> interceptorsOld = reg.getInterceptors(interceptorLookupKey);
-        
         //get interceptors registered as services for this bean Id
         List<Interceptor> interceptors = InterceptorManager.getInterceptorsForBean(beanId, blueprintContainer);
-
-//        if ((interceptorsOld != null) && (interceptors != null)) {
-            // if there are interceptors using both mechanisms, then merge the lists
-//            interceptors.addAll(interceptorsOld);
-//        } else if ((interceptorsOld != null) && (interceptors == null)) {
-            // if there are only interceptors using the old mechanism then use that list
-//            interceptors = interceptorsOld;
-//        }
         
         if (interceptors != null && interceptors.size() > 0) {
             try {
@@ -731,9 +720,7 @@ public class BeanRecipe extends Abstract
         getDestroyMethod(obj);
         
         // Add partially created object to the container
-//        if (initMethod == null) {
-            addPartialObject(obj);
-//        }
+        addPartialObject(obj);
 
         // inject properties
         setProperties(obj);

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/QuiesceInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/QuiesceInterceptor.java?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/QuiesceInterceptor.java (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/QuiesceInterceptor.java Tue Nov  2 02:24:07 2010
@@ -58,4 +58,8 @@ public class QuiesceInterceptor implemen
     {
       return this.getClass().getName();
     }    
+    
+    public void setConfig(Object config) {
+        // NO-OP         
+    }
 }

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java Tue Nov  2 02:24:07 2010
@@ -36,6 +36,8 @@ import org.apache.aries.blueprint.reflec
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -56,41 +58,190 @@ public class InterceptorManager {
     private static final String FILTER_END = ")";
     private static final String FILTER_AND = "&";
     private static final String DELIM = ":";
-    
+
     private static final Map<String, Interceptor> interceptorCollection = Collections.synchronizedMap(new HashMap<String, Interceptor>());
+    private static final Map<String, List<String>> interceptorKeyCollection = Collections.synchronizedMap(new HashMap<String, List<String>>());
+    private static final Map<String, Dictionary> serviceRegistrationCollection = Collections.synchronizedMap(new HashMap<String, Dictionary>());
+    private static final Map<String, ReferenceMetadataImpl> serviceReferenceCollection = Collections.synchronizedMap(new HashMap<String, ReferenceMetadataImpl>());
+
+    public static synchronized ServiceReference registerInterceptorService(Interceptor interceptor, String beanId, long bundleId) 
+    {
+        LOGGER.debug("registerInterceptorService interceptor {} beanId {} ", interceptor, beanId);
+
+        ServiceRegistration interceptorServiceReg = null;
+        
+        Bundle interceptorBundle = FrameworkUtil.getBundle(interceptor.getClass());
+            
+        if (interceptorBundle!=null && bundleId!=0) {
+            String interceptorId = interceptor.getIdentity();
+            String interceptorKey = interceptorId + DELIM + bundleId + DELIM + beanId;
+            Dictionary properties = new Hashtable();
+            properties.put(BUNDLE_ID, bundleId);
+            properties.put(BEAN, beanId);
+            properties.put(URI, interceptorId); 
+            BundleContext interceptorBundleContext = interceptorBundle.getBundleContext();
+
+            if (serviceRegistrationCollection.get(interceptorKey) == null) {
+                // Register Service
+                LOGGER.debug("registerInterceptorService about to register interceptor service with properties {}", properties);
+                interceptorServiceReg = interceptorBundleContext.registerService(Interceptor.class.getName(), interceptor, properties);
+                
+                //Save the key for this Interceptor Service by the InterceptorId 
+                List<String> interceptorKeyList = interceptorKeyCollection.get(interceptorId);
+                if(interceptorKeyList==null){
+                    interceptorKeyList = new ArrayList<String>();
+                    interceptorKeyCollection.put(interceptorId, interceptorKeyList);
+                }
+                if(!interceptorKeyList.contains(interceptorKey)){
+                    interceptorKeyList.add(interceptorKey);
+                }
+
+                serviceRegistrationCollection.put(interceptorKey, properties);
+            }
+            else {
+                LOGGER.debug("registerInterceptorService interceptor already registered for bundle & bean " + interceptorKey );      
+            }
+        }
+        else {
+            LOGGER.debug("registerInterceptorService bundle is null - can not register interceptor service");
+        }
+
+        if (interceptorServiceReg != null) return interceptorServiceReg.getReference(); else return null;
+    }    
+
+    public static synchronized List<ServiceReference> recreateInterceptorServices(Interceptor interceptor) 
+    {
+        LOGGER.debug("recreateInterceptorServices interceptor {} ", interceptor);
+
+        List<ServiceReference> interceptorServiceRefList = new ArrayList();
+        
+        Bundle interceptorBundle = FrameworkUtil.getBundle(interceptor.getClass());
+        String interceptorId = interceptor.getIdentity();
+            
+        if (interceptorBundle!=null) {
+            
+            //get all entries for this interceptorId that were created earlier and added to the ServiceRegistry earlier            
+            List<String> interceptorKeyList = interceptorKeyCollection.get(interceptorId);
+            if(interceptorKeyList!=null){
+                
+                //For each interceptorKey pull the properties and reregister the interceptor in the service registry
+                for (String interceptorKey : interceptorKeyList) {
+                                     
+                    BundleContext interceptorBundleContext = interceptorBundle.getBundleContext();
+        
+                    Dictionary properties = serviceRegistrationCollection.get(interceptorKey);
+                    if (properties != null) {
+
+                        // Register Service
+                        LOGGER.debug("recreateInterceptorServices about to register interceptor service with properties {}", properties);
+                        ServiceRegistration interceptorServiceReg = interceptorBundleContext.registerService(Interceptor.class.getName(), interceptor, properties);
+                        
+                        if (interceptorServiceReg != null) interceptorServiceRefList.add(interceptorServiceReg.getReference());
+                   
+                        serviceRegistrationCollection.put(interceptorKey, properties);
+                    }
+                    else {
+                        LOGGER.debug("recreateInterceptorServices properties null for interceptor key " + interceptorKey );      
+                    }                      
+                }     
+            }
+            else {
+                LOGGER.debug("recreateInterceptorServices nothing to do - no existing entries for " + interceptorId);               
+            }
+
+        }
+        else {
+            LOGGER.debug("registerInterceptorService bundle is null - can not register interceptor service");
+        }
 
+        return interceptorServiceRefList; 
+    } 
+    
+    
+    public static synchronized void addServiceReference(ServiceReference interceptorReference, int availability, ParserContext pc) 
+    {
+        LOGGER.debug("addServiceReference for {} ", interceptorReference);
+
+        long parsingBundleId = (Long) interceptorReference.getProperty(BUNDLE_ID);
+        String interceptorId = interceptorReference.getProperty(URI).toString();
+        String beanId = interceptorReference.getProperty(BEAN).toString();
+        ComponentDefinitionRegistry cdr = pc.getComponentDefinitionRegistry();
+               
+        if (parsingBundleId!=0) {
+            String interceptorKey = interceptorReference.getProperty(URI) + DELIM + 
+                                    interceptorReference.getProperty(BUNDLE_ID) + DELIM + 
+                                    interceptorReference.getProperty(BEAN);
+
+            if (serviceReferenceCollection.get(interceptorKey) == null) {
+        
+                String id = pc.generateId();
+                String filter = FILTER_START + 
+                                  FILTER_AND + 
+                                   FILTER_START + 
+                                      BUNDLE_ID + "=" + parsingBundleId + 
+                                    FILTER_END + 
+                                    FILTER_START + 
+                                      BEAN + "=" + beanId + 
+                                    FILTER_END +                             
+                                    FILTER_START +                                         
+                                       URI + "=" + interceptorId +
+                                    FILTER_END +        
+                               FILTER_END;
+
+                ReferenceMetadataImpl referenceMetadata = new ReferenceMetadataImpl();
+                referenceMetadata.setId(id);
+                referenceMetadata.setAvailability(availability);
+                referenceMetadata.setRuntimeInterface(Interceptor.class);
+                referenceMetadata.setInterface(Interceptor.class.getName());
+                referenceMetadata.setFilter(filter);
+
+                LOGGER.debug("addServiceReference about to register interceptor service reference {}", referenceMetadata);
+                cdr.registerComponentDefinition(referenceMetadata); 
+                
+                serviceReferenceCollection.put(interceptorKey, referenceMetadata);
+            }
+            else {
+                LOGGER.debug("addServiceReference interceptor already registered for bundle & bean " + interceptorKey );      
+            }
+        }
+        else {
+            LOGGER.debug("addInterceptorForBean bundle is null - can not register interceptor service");
+        }
+    }
+    
     public static synchronized void addInterceptorForBean(Interceptor interceptor, int availability, String beanId, ParserContext pc) 
     {
         LOGGER.debug("addInterceptorForBean interceptor {} bean {}", interceptor, beanId);
 
-        long bundleId = 0;   
+        long parsingBundleId = 0;   
         ComponentDefinitionRegistry cdr = pc.getComponentDefinitionRegistry();
-        Bundle bundle = FrameworkUtil.getBundle(interceptor.getClass());
+        Bundle interceptorBundle = FrameworkUtil.getBundle(interceptor.getClass());
         
         if (cdr.containsComponentDefinition(BLUEPRINT_BUNDLE)) {
             PassThroughMetadata ptMetadata = (PassThroughMetadata) cdr.getComponentDefinition(BLUEPRINT_BUNDLE);
-            Bundle parsingBundle = (Bundle) ptMetadata.getObject();
-            if (parsingBundle != null) bundleId = parsingBundle.getBundleId();
+            Bundle parsingBundle = null;
+            if (ptMetadata != null) parsingBundle = (Bundle) ptMetadata.getObject();
+            if (parsingBundle != null) parsingBundleId = parsingBundle.getBundleId();
         }
         
-        if (bundle!=null && bundleId!=0) {
-        	String interceptorKey = interceptor.getIdentity() + DELIM + bundleId + DELIM + beanId;
+        if (interceptorBundle!=null && parsingBundleId!=0) {
+            String interceptorKey = interceptor.getIdentity() + DELIM + parsingBundleId + DELIM + beanId;
             Dictionary properties = new Hashtable();
-            properties.put(BUNDLE_ID, bundleId);
+            properties.put(BUNDLE_ID, parsingBundleId);
             properties.put(BEAN, beanId);
             properties.put(URI, interceptor.getIdentity()); 
-            BundleContext bundleContext = bundle.getBundleContext();
+            BundleContext interceptorBundleContext = interceptorBundle.getBundleContext();
 
             if (interceptorCollection.get(interceptorKey) == null) {
             	// Register Service
                 LOGGER.debug("addInterceptorForBean about to register interceptor service with properties {}", properties);
-                bundleContext.registerService(Interceptor.class.getName(), interceptor, properties);
+                interceptorBundleContext.registerService(Interceptor.class.getName(), interceptor, properties);
 	
                 String id = pc.generateId();
                 String filter = FILTER_START + 
                                   FILTER_AND + 
                                    FILTER_START + 
-                                      BUNDLE_ID + "=" + bundleId + 
+                                      BUNDLE_ID + "=" + parsingBundleId + 
                                     FILTER_END + 
                                     FILTER_START + 
                                       BEAN + "=" + beanId + 
@@ -119,7 +270,6 @@ public class InterceptorManager {
         else {
             LOGGER.debug("addInterceptorForBean bundle is null - can not register interceptor service");
         }
-
     }
     
     public static List<Interceptor> getInterceptorsForBean(String beanId, ExtendedBlueprintContainer blueprintContainer) 
@@ -148,6 +298,7 @@ public class InterceptorManager {
         } 
         
         // order interceptor List by service rank
+        //@JAB TODO should sort using the rank of the Interceptor service rather than Interceptor object rank (which should be removed)
         if (interceptorList != null) {
             Collections.sort(interceptorList, new Comparator<Interceptor>(){
                 public int compare(Interceptor object1, Interceptor object2) {
@@ -155,9 +306,25 @@ public class InterceptorManager {
                     return object2.getRank() - object1.getRank();
                 }
             });        	
-        }
-            
+        }  
         return interceptorList;
     }
+    
+    public static long getBundleId(ComponentDefinitionRegistry cdr) 
+    {
+        LOGGER.debug("getBundleId from CDR {}", cdr);
 
+        long bundleId = 0;   
+        
+        if (cdr.containsComponentDefinition(BLUEPRINT_BUNDLE)) {
+            PassThroughMetadata ptMetadata = (PassThroughMetadata) cdr.getComponentDefinition(BLUEPRINT_BUNDLE);
+            Bundle bundle = null;
+            if (ptMetadata != null) bundle = (Bundle) ptMetadata.getObject();
+            if (bundle != null) bundleId = bundle.getBundleId();
+        }
+        
+        LOGGER.debug("getBundleId returning {}", bundleId);
+        return bundleId;
+    }
+    
 }

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java Tue Nov  2 02:24:07 2010
@@ -105,6 +105,10 @@ public class NSHandlerTwo implements Nam
         {
           return this.getClass().getName();
         } 
+        
+        public void setConfig(Object config) {
+            // NO-OP         
+        }
     };
     
     //

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/pom.xml?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/pom.xml (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/pom.xml Tue Nov  2 02:24:07 2010
@@ -36,6 +36,7 @@
 		<aries.osgi.import>
 			org.osgi.framework;version="1.5",
 			org.osgi.service.cm;version="[1.2.0,2.0.0)",
+            org.osgi.util.tracker;version="1.4",
 			*
         </aries.osgi.import>
 		<aries.osgi.private.pkg>
@@ -54,7 +55,8 @@
 		<dependency>
 			<groupId>org.osgi</groupId>
 			<artifactId>org.osgi.compendium</artifactId>
-			<scope>test</scope>
+			<!--<scope>test</scope>-->
+			<scope>provided</scope>
 		</dependency>
 		<dependency>
 			<groupId>org.apache.geronimo.specs</groupId>

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxInterceptorImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxInterceptorImpl.java?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxInterceptorImpl.java (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxInterceptorImpl.java Tue Nov  2 02:24:07 2010
@@ -124,9 +124,13 @@ public class TxInterceptorImpl implement
     {
       tm = manager;
     }
-
-    public final void setTxMetaDataHelper(TxComponentMetaDataHelper transactionEnhancer)
-    {
-      this.metaDataHelper = transactionEnhancer;
+    
+    public void setConfig(Object config) {
+        this.metaDataHelper = (TxComponentMetaDataHelper) config;        
     }
+
+//    public final void setTxMetaDataHelper(TxComponentMetaDataHelper transactionEnhancer)
+//    {
+//      this.metaDataHelper = transactionEnhancer;
+//    }
 }

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java Tue Nov  2 02:24:07 2010
@@ -19,7 +19,6 @@
 package org.apache.aries.transaction.parsing;
 
 import java.net.URL;
-import java.util.HashSet;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -28,12 +27,17 @@ import org.apache.aries.blueprint.Compon
 import org.apache.aries.blueprint.Interceptor;
 import org.apache.aries.blueprint.NamespaceHandler;
 import org.apache.aries.blueprint.ParserContext;
-import org.apache.aries.blueprint.utils.InterceptorManager;
 import org.apache.aries.transaction.Constants;
+import org.apache.aries.blueprint.utils.InterceptorManager;
 import org.apache.aries.transaction.TxComponentMetaDataHelper;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
 import org.osgi.service.blueprint.reflect.ServiceReferenceMetadata;
 import org.osgi.service.blueprint.reflect.Metadata;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Element;
@@ -42,11 +46,30 @@ import org.w3c.dom.Node;
 public class TxElementHandler implements NamespaceHandler {
     private static final Logger LOGGER =
         LoggerFactory.getLogger(TxElementHandler.class);
-
+    private static final String BLUEPRINT_NAMESPACE = "osgi.service.blueprint.namespace";
+    private static final String filter = "(&(objectClass=" + Interceptor.class.getName() + 
+                                         ")(" +
+                                         BLUEPRINT_NAMESPACE + "=" + Constants.TRANSACTION11URI +
+                                         "))";    
+    
     private TxComponentMetaDataHelper metaDataHelper;
-    private Interceptor interceptor = null;
-
-    private Set<ComponentDefinitionRegistry> registered = new HashSet<ComponentDefinitionRegistry>();
+    private Interceptor interceptor = null;   
+    private BundleContext context;
+    private ServiceTracker interceptorTracker = null;
+
+   /**
+    * init method for NSH bean - used to initiate ServiceTracker for associated Interceptor
+    */
+   public void init() {
+       try {
+           interceptorTracker = new ServiceTracker(context, 
+                                                   context.createFilter(filter), null);
+//                                                   new InterceptorServiceTrackerCustomizer());
+           interceptorTracker.open();     
+       } catch (InvalidSyntaxException e) {
+           throw new IllegalArgumentException("Invalid filter", e);
+       }
+   }
 
     private void parseElement(Element elt, ComponentMetadata cm, ParserContext pc)
     {
@@ -56,13 +79,12 @@ public class TxElementHandler implements
         if ("transaction".equals(elt.getLocalName())) {
             if (LOGGER.isDebugEnabled())
                 LOGGER.debug("parser adding interceptor for " + elt);
-            
-            ComponentDefinitionRegistry cdr = pc.getComponentDefinitionRegistry();
-   
+              
             if (cm == null) {
                 // if the enclosing component is null, then we assume this is the top element 
                 
                 String bean = elt.getAttribute(Constants.BEAN);
+                
                 registerComponentsWithInterceptor(bean, pc);
 
                 metaDataHelper.populateBundleWideTransactionData(pc.getComponentDefinitionRegistry(), 
@@ -70,15 +92,8 @@ public class TxElementHandler implements
             } else {
                 String beanId = cm.getId(); 
                 
-//                cdr.registerInterceptorWithComponent(cm, interceptor);                
-                   
-                //Create interceptor service and reference for this bean instance
-                InterceptorManager.addInterceptorForBean(interceptor, ServiceReferenceMetadata.AVAILABILITY_MANDATORY,  beanId, pc);
-
-                if (LOGGER.isDebugEnabled())
-                    LOGGER.debug("parser setting comp trans data for " + elt.getAttribute(Constants.VALUE) + "  "
-                            + elt.getAttribute(Constants.METHOD));
-    
+                createInterceptorServiceAndReference(beanId, pc);                
+                
                 metaDataHelper.setComponentTransactionData(cm, elt.getAttribute(Constants.VALUE), elt
                         .getAttribute(Constants.METHOD));
             }
@@ -117,12 +132,17 @@ public class TxElementHandler implements
         this.metaDataHelper = transactionEnhancer;
     }
 
-    public final void setTransactionInterceptor(Interceptor itx)
+    public final void setInterceptor(Interceptor itx)
     {
         if (LOGGER.isDebugEnabled())
             LOGGER.debug("parser having interceptor set " + itx);
-        
-        this.interceptor = itx;
+        if (this.interceptor==null)
+            this.interceptor = itx;
+    }
+    
+    public final void setContext(BundleContext ctx)
+    {
+        context = ctx;
     }
 
     public Set<Class> getManagedClasses()
@@ -131,48 +151,31 @@ public class TxElementHandler implements
         return null;
     }
     
-    private boolean isRegistered(ComponentDefinitionRegistry cdr) {
-        for (ComponentDefinitionRegistry compdr : registered) {
-            if (compdr == cdr) {
-                return true;
-            }
-        }
-        
-        return false;
-    }
     
     private void registerComponentsWithInterceptor(String bean, ParserContext pc) {
 
         ComponentDefinitionRegistry cdr = pc.getComponentDefinitionRegistry();
-
-        // if it is already registered all components in the component definition registry, do nothing
-        if (isRegistered(cdr)) {
-            return;
-        }
-        
+       
         Set<String> ids = cdr.getComponentDefinitionNames();
         
         if (bean == null || bean.isEmpty()) {
             // in this case, let's attempt to register all components
             // if the component has already been registered with this interceptor,
             // the registration will be ignored.
-            for (String id : ids) {
-                InterceptorManager.addInterceptorForBean(interceptor, ServiceReferenceMetadata.AVAILABILITY_MANDATORY,  id, pc);
-//                ComponentMetadata componentMetadata = cdr.getComponentDefinition(id);
-//                cdr.registerInterceptorWithComponent(componentMetadata, interceptor);
-            }
-            synchronized (registered) {
-                registered.add(cdr);
+            for (String beanId : ids) {
+                
+                createInterceptorServiceAndReference(beanId, pc);
+
             }
         } else {
-            // register the beans specified
+            // register only the beans that match the pattern specified
             Pattern p = Pattern.compile(bean);
-            for (String id : ids) {
-                Matcher m = p.matcher(id);
+            for (String beanId : ids) {
+                Matcher m = p.matcher(beanId);
                 if (m.matches()) {
-                    InterceptorManager.addInterceptorForBean(interceptor, ServiceReferenceMetadata.AVAILABILITY_MANDATORY,  id, pc);
-//                    ComponentMetadata componentMetadata = cdr.getComponentDefinition(id);
-//                    cdr.registerInterceptorWithComponent(componentMetadata, interceptor);
+                   
+                    createInterceptorServiceAndReference(beanId, pc);
+
                 }
             }
         }
@@ -182,4 +185,55 @@ public class TxElementHandler implements
     /*private boolean includes(String beans, String id) {
         return Pattern.matches(beans, id);
     }*/
+
+    private class InterceptorServiceTrackerCustomizer implements ServiceTrackerCustomizer {
+              
+        public InterceptorServiceTrackerCustomizer() {
+
+        }
+
+        public Object addingService(ServiceReference reference) {
+            Interceptor txInterceptor = (Interceptor) context.getService(reference);
+            txInterceptor.setConfig((Object)metaDataHelper);
+            InterceptorManager.recreateInterceptorServices(txInterceptor);
+            return txInterceptor;
+        }
+        
+        public void modifiedService(ServiceReference reference, Object service) {
+            // TODO Auto-generated method stub
+        
+        }
+        
+        public void removedService(ServiceReference reference, Object service) {
+            context.ungetService(reference);
+        }
+    }
+    
+    private void createInterceptorServiceAndReference(String beanId, ParserContext pc) {
+
+        ComponentDefinitionRegistry cdr = pc.getComponentDefinitionRegistry();
+                      
+        Interceptor txInterceptor = null;
+        if (interceptor!=null) {
+            txInterceptor = interceptor;
+        }
+        else if (interceptorTracker!=null) {
+            txInterceptor = (Interceptor) interceptorTracker.getService();
+        }
+        
+        if (txInterceptor != null) {
+            
+            txInterceptor.setConfig(metaDataHelper);
+            
+            long parsingBundleId = InterceptorManager.getBundleId(cdr);
+        
+            //Create the interceptor Service for this bean instance
+            ServiceReference interceptorReference = InterceptorManager.registerInterceptorService(txInterceptor, beanId, parsingBundleId);
+            
+            //Register a Mandatory Service Reference for the interceptor Service
+            if (interceptorReference != null) InterceptorManager.addServiceReference(interceptorReference, ServiceReferenceMetadata.AVAILABILITY_MANDATORY, pc); 
+        
+        }
+    }    
+    
 }

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml Tue Nov  2 02:24:07 2010
@@ -31,10 +31,10 @@
                 </list>
             </entry>
         </service-properties>
-        <bean class="org.apache.aries.transaction.parsing.TxElementHandler">
+        <bean class="org.apache.aries.transaction.parsing.TxElementHandler" init-method="init">
             <property ref="txEnhancer" name="txMetaDataHelper"/>
+            <property ref="blueprintBundleContext" name="context" />
             <!--<property ref="txinterceptor" name="transactionInterceptor"/>-->
-            <property ref="txInterceptor" name="transactionInterceptor"/>
         </bean>
   </service>              
   
@@ -42,25 +42,29 @@
  
   <reference id="tm" interface="javax.transaction.TransactionManager"/>
   
+<!--   
   <bean id="txInterceptor" class="org.apache.aries.transaction.TxInterceptorImpl">
       <property ref="tm" name="transactionManager"/>
       <property ref="txEnhancer" name="txMetaDataHelper"/>
   </bean>
-    
-<!--   
-  <service id="txInterceptorService" ref="txInterceptor" interface="org.apache.aries.blueprint.Interceptor">
+-->    
+  
+  <service id="txInterceptorService" interface="org.apache.aries.blueprint.Interceptor">
     <service-properties>
+        <entry key="osgi.service.blueprint.namespace" value="http://aries.apache.org/xmlns/transactions/v1.1.0"/>
+<!--        
         <entry key="osgi.service.blueprint.namespace">
             <list>
                 <value>http://aries.apache.org/xmlns/transactions/v1.0.0</value>
                 <value>http://aries.apache.org/xmlns/transactions/v1.1.0</value>
             </list>
         </entry>
+-->        
     </service-properties>
     <bean class="org.apache.aries.transaction.TxInterceptorImpl">
       <property name="transactionManager" ref="tm"/>
-      <property ref="txEnhancer" name="txMetaDataHelper"/>
+      <!-- <property ref="txEnhancer" name="txMetaDataHelper"/> -->
     </bean>
   </service>
--->
+  
 </blueprint>
\ No newline at end of file

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java Tue Nov  2 02:24:07 2010
@@ -65,10 +65,12 @@ public class BundleWideNameSpaceHandlerT
         
         TxInterceptorImpl txinterceptor = new TxInterceptorImpl();
         txinterceptor.setTransactionManager(tm);
-        txinterceptor.setTxMetaDataHelper(txenhancer);
+//        txinterceptor.setConfig(txenhancer);
         
         TxElementHandler namespaceHandler = new TxElementHandler();
-        namespaceHandler.setTransactionInterceptor(txinterceptor);
+        //namespaceHandler.setContext(ctx);
+        //namespaceHandler.init();
+        namespaceHandler.setInterceptor(txinterceptor);
         namespaceHandler.setTxMetaDataHelper(txenhancer);
             
         Properties props = new Properties();

Modified: incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java?rev=1029941&r1=1029940&r2=1029941&view=diff
==============================================================================
--- incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java (original)
+++ incubator/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java Tue Nov  2 02:24:07 2010
@@ -69,10 +69,12 @@ public class NameSpaceHandlerTest {
         
         TxInterceptorImpl txinterceptor = new TxInterceptorImpl();
         txinterceptor.setTransactionManager(tm);
-        txinterceptor.setTxMetaDataHelper(txenhancer);
+//        txinterceptor.setConfig(txenhancer);
         
         TxElementHandler namespaceHandler = new TxElementHandler();
-        namespaceHandler.setTransactionInterceptor(txinterceptor);
+        //namespaceHandler.setContext(ctx);
+        //namespaceHandler.init();
+        namespaceHandler.setInterceptor(txinterceptor);
         namespaceHandler.setTxMetaDataHelper(txenhancer);
             
         Properties props = new Properties();