aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jb...@apache.org
Subject svn commit: r1056367 - in /aries/sandbox/jbohn/interceptor-proto: blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ blueprint/blueprint-testbundlea/src/...
Date Fri, 07 Jan 2011 16:04:41 GMT
Author: jbohn
Date: Fri Jan  7 16:04:41 2011
New Revision: 1056367

URL: http://svn.apache.org/viewvc?rev=1056367&view=rev
Log:
use service rank and id to order interceptors - also an experiment to create the service references
in the blueprint container unforunately does not solve all of the issues

Modified:
    aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintContainerImpl.java
    aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java
    aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java
    aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java
    aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml
    aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java

Modified: aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintContainerImpl.java
URL: http://svn.apache.org/viewvc/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintContainerImpl.java?rev=1056367&r1=1056366&r2=1056367&view=diff
==============================================================================
--- aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintContainerImpl.java
(original)
+++ aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintContainerImpl.java
Fri Jan  7 16:04:41 2011
@@ -51,6 +51,7 @@ import org.apache.aries.blueprint.Bluepr
 import org.apache.aries.blueprint.ComponentDefinitionRegistryProcessor;
 import org.apache.aries.blueprint.ExtendedBeanMetadata;
 import org.apache.aries.blueprint.ExtendedBlueprintContainer;
+import org.apache.aries.blueprint.Interceptor;
 import org.apache.aries.blueprint.NamespaceHandler;
 import org.apache.aries.blueprint.Processor;
 import org.apache.aries.blueprint.di.Recipe;
@@ -60,11 +61,13 @@ import org.apache.aries.blueprint.namesp
 import org.apache.aries.blueprint.reflect.MetadataUtil;
 import org.apache.aries.blueprint.reflect.PassThroughMetadataImpl;
 import org.apache.aries.blueprint.utils.HeaderParser;
+import org.apache.aries.blueprint.utils.InterceptorManager;
 import org.apache.aries.blueprint.utils.JavaUtils;
 import org.apache.aries.blueprint.utils.HeaderParser.PathElement;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.blueprint.container.BlueprintContainer;
@@ -279,6 +282,7 @@ public class BlueprintContainerImpl impl
                         break;
                     }
                     case Populated:
+                        addInterceptorRefs();
                         getRepository();
                         trackServiceReferences();
                         Runnable r = new Runnable() {
@@ -664,6 +668,25 @@ public class BlueprintContainerImpl impl
         }
     }
 
+    private void addInterceptorRefs() {
+        long bundleId = bundleContext.getBundle().getBundleId();                        
 
+        String filter = "(bundle-id" + "=" + bundleId + ")";
+        //lookup all interceptor services for this bundle
+        try {
+            ServiceReference[] interceptors = bundleContext.getServiceReferences(Interceptor.class.getName(),
filter);
+            //create a service reference for each interceptor service
+            if (interceptors != null) {
+                for (ServiceReference interceptorRef : interceptors) {
+                    //@JAB  How do I know if it should be mandatory or optional? 
+                    //@JAB  Also inherent problem with adding references here - a Mandatory
service may not be available but we wouldn't know.
+                    InterceptorManager.addServiceReference(interceptorRef, ServiceReferenceMetadata.AVAILABILITY_MANDATORY,
componentDefinitionRegistry); 
+                }
+            }        
+        } catch (InvalidSyntaxException e) {
+            throw new RuntimeException(e);
+        }
+    }
+    
     protected void unregisterServices() {
         if (repository != null) {
             List<ServiceRecipe> recipes = this.services;

Modified: aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java
URL: http://svn.apache.org/viewvc/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java?rev=1056367&r1=1056366&r2=1056367&view=diff
==============================================================================
--- aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java
(original)
+++ aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/InterceptorManager.java
Fri Jan  7 16:04:41 2011
@@ -28,6 +28,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
+import org.apache.aries.blueprint.container.BlueprintRepository;
 import org.apache.aries.blueprint.ExtendedBlueprintContainer;
 import org.apache.aries.blueprint.Interceptor;
 import org.apache.aries.blueprint.ParserContext;
@@ -35,9 +36,11 @@ import org.apache.aries.blueprint.PassTh
 import org.apache.aries.blueprint.reflect.ReferenceMetadataImpl;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.blueprint.container.ReifiedType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -63,7 +66,7 @@ public class InterceptorManager {
     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) 
+    public static synchronized ServiceReference registerInterceptorService(Interceptor interceptor,
Integer ranking, String beanId, long bundleId) 
     {
         LOGGER.debug("registerInterceptorService interceptor {} beanId {} ", interceptor,
beanId);
 
@@ -78,6 +81,7 @@ public class InterceptorManager {
             properties.put(BUNDLE_ID, bundleId);
             properties.put(BEAN, beanId);
             properties.put(URI, interceptorId); 
+            properties.put(Constants.SERVICE_RANKING, ranking);
             BundleContext interceptorBundleContext = interceptorBundle.getBundleContext();
 
             if (serviceRegistrationCollection.get(interceptorKey) == null) {
@@ -167,9 +171,8 @@ public class InterceptorManager {
         ComponentDefinitionRegistry cdr = pc.getComponentDefinitionRegistry();
                
         if (parsingBundleId!=0) {
-            String interceptorKey = interceptorReference.getProperty(URI) + DELIM + 
-                                    interceptorReference.getProperty(BUNDLE_ID) + DELIM +

-                                    interceptorReference.getProperty(BEAN);
+
+            String interceptorKey = interceptorId + DELIM + parsingBundleId + DELIM + beanId;
 
             if (serviceReferenceCollection.get(interceptorKey) == null) {
         
@@ -204,15 +207,65 @@ public class InterceptorManager {
             }
         }
         else {
-            LOGGER.debug("addInterceptorForBean bundle is null - can not register interceptor
service");
+            LOGGER.debug("addServiceReference bundleId is null - can not register interceptor
service");
+        }
+    }
+    
+    public static synchronized void addServiceReference(ServiceReference interceptorReference,
int availability, ComponentDefinitionRegistry cdr) 
+    {
+        LOGGER.debug("addServiceReference for {} ", interceptorReference);
+
+        long parsingBundleId = (Long) interceptorReference.getProperty(BUNDLE_ID);
+        String interceptorId = interceptorReference.getProperty(URI).toString();
+        String beanId = interceptorReference.getProperty(BEAN).toString();
+               
+        if (parsingBundleId!=0) {
+
+            String interceptorKey = interceptorId + DELIM + parsingBundleId + DELIM + beanId;
+
+            if (serviceReferenceCollection.get(interceptorKey) == null) {
+        
+                String id = interceptorKey;
+                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("addServiceReference bundleId is null - can not register interceptor
service");
         }
     }
     
-    public static List<Interceptor> getInterceptorsForBean(String beanId, ExtendedBlueprintContainer
blueprintContainer) 
+    public static List<Interceptor> getInterceptorsForBean(String beanId, final ExtendedBlueprintContainer
blueprintContainer) 
     {
         LOGGER.debug("getInterceptorsForBean {}", beanId);
 
         List<Interceptor> interceptorList = null;
+        List<InterceptorManager.InterceptorElement> interceptorElementList = null;
         
         LOGGER.debug("getInterceptorsForBean searching for interceptors for bean {} ", beanId);
 
@@ -224,25 +277,39 @@ public class InterceptorManager {
             	String refFilter = interceptorRefMetadata.getFilter();
                 if (refFilter != null) { 
                     if (refFilter.contains(FILTER_START + BEAN + "=" + beanId + FILTER_END))
{
-                        LOGGER.debug("getInterceptorsForBean matching reference found, about
to get instance {}", interceptorRefMetadata);       	
-                        Interceptor interceptor = (Interceptor) blueprintContainer.getComponentInstance(interceptorRefMetadata.getId());
-                        if(interceptorList==null) interceptorList = new ArrayList<Interceptor>();
-                        interceptorList.add(interceptor);                            	
+                        LOGGER.debug("getInterceptorsForBean matching reference found, about
to get instance {}", interceptorRefMetadata); 
+                        
+                        InterceptorManager.InterceptorElement element = new InterceptorManager.InterceptorElement(interceptorRefMetadata,
blueprintContainer);
+                        if (interceptorElementList==null) interceptorElementList = new ArrayList<InterceptorElement>();
+                        interceptorElementList.add(element);
                     } 
                 }
             } 
         } 
-        
-        // 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) {
-                    //invert the order so higher ranks are sorted 1st
-                    return object2.getRank() - object1.getRank();
+  
+        //Sort Inteceptor Elements honoring rank first and service id if rank is equal 
+        if (interceptorElementList != null) {
+            Collections.sort(interceptorElementList, new Comparator<InterceptorElement>(){
+                public int compare(InterceptorElement element1, InterceptorElement element2)
+                {
+                  int res = 0;
+                  res = element2.getRank().compareTo(element1.getRank());
+                  if (res == 0) {
+                      res = element1.getId().compareTo(element2.getId());
+                  }                      
+                  return res;
                 }
-            });        	
-        }  
+            });
+        }
+        
+        // Create a list of just the interceptors
+        if (interceptorElementList != null) {
+            if(interceptorList==null) interceptorList = new ArrayList<Interceptor>();
+            for (InterceptorElement interceptorElement: interceptorElementList) {
+                interceptorList.add((Interceptor) interceptorElement.getInterceptor()); 
       
+            }            
+        }
+  
         return interceptorList;
     }
     
@@ -263,4 +330,38 @@ public class InterceptorManager {
         return bundleId;
     }
     
+    private static class InterceptorElement {
+
+        Interceptor interceptor;
+        Integer     rank;
+        Long        id;
+        
+        public InterceptorElement(ReferenceMetadataImpl interceptorRefMetadata, ExtendedBlueprintContainer
blueprintContainer)
+        {
+            try {              
+                BlueprintRepository repository = (BlueprintRepository) blueprintContainer.getRepository();
+                interceptor = (Interceptor) blueprintContainer.getComponentInstance(interceptorRefMetadata.getId());
 
+                ServiceReference serviceRef = (ServiceReference) repository.convert(repository.getInstance(interceptorRefMetadata.getId()),
new ReifiedType(ServiceReference.class));
+                rank = (Integer) serviceRef.getProperty(Constants.SERVICE_RANKING);
+                if (rank == null) rank = 0;
+                id = (Long) serviceRef.getProperty(Constants.SERVICE_ID);               

+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+        
+        public Interceptor getInterceptor() {
+            return interceptor;
+        }
+        
+        public Integer getRank() {
+            return rank;
+        }
+        
+        public Long getId() {
+            return id;
+        }
+        
+    }
+    
 }

Modified: aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java
URL: http://svn.apache.org/viewvc/aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java?rev=1056367&r1=1056366&r2=1056367&view=diff
==============================================================================
--- aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java
(original)
+++ aries/sandbox/jbohn/interceptor-proto/blueprint/blueprint-testbundlea/src/main/java/org/apache/aries/blueprint/testbundlea/NSHandlerTwo.java
Fri Jan  7 16:04:41 2011
@@ -121,7 +121,7 @@ public class NSHandlerTwo implements Nam
             long parsingBundleId = InterceptorManager.getBundleId(context.getComponentDefinitionRegistry());
         
             //Create the interceptor Service for this bean instance
-            ServiceReference interceptorReference = InterceptorManager.registerInterceptorService(tracker,
component.getId(), parsingBundleId);
+            ServiceReference interceptorReference = InterceptorManager.registerInterceptorService(tracker,
100, component.getId(), parsingBundleId);
             
             //Register a Mandatory Service Reference for the interceptor Service
             if (interceptorReference != null) InterceptorManager.addServiceReference(interceptorReference,
ServiceReferenceMetadata.AVAILABILITY_MANDATORY, context);

Modified: aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java
URL: http://svn.apache.org/viewvc/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java?rev=1056367&r1=1056366&r2=1056367&view=diff
==============================================================================
--- aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java
(original)
+++ aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java
Fri Jan  7 16:04:41 2011
@@ -53,7 +53,8 @@ public class TxElementHandler implements
                                          "))";    
     
     private TxComponentMetaDataHelper metaDataHelper;
-    private Interceptor interceptor = null;   
+    private Interceptor interceptor = null;  
+    private Integer interceptorRanking = 0;
     private BundleContext context;
     private ServiceTracker interceptorTracker = null;
 
@@ -140,6 +141,11 @@ public class TxElementHandler implements
             this.interceptor = itx;
     }
     
+    public final void setInterceptorRanking(Integer ranking)
+    {
+        interceptorRanking = ranking;
+    }
+    
     public final void setContext(BundleContext ctx)
     {
         context = ctx;
@@ -218,7 +224,9 @@ public class TxElementHandler implements
             txInterceptor = interceptor;
         }
         else if (interceptorTracker!=null) {
-            txInterceptor = (Interceptor) interceptorTracker.getService();
+            ServiceReference interceptorReference = interceptorTracker.getServiceReference();
+            txInterceptor = (Interceptor) interceptorTracker.getService(interceptorReference);
+            interceptorRanking = (Integer) interceptorReference.getProperty("service.ranking");
         }
         
         if (txInterceptor != null) {
@@ -228,10 +236,11 @@ public class TxElementHandler implements
             long parsingBundleId = InterceptorManager.getBundleId(cdr);
         
             //Create the interceptor Service for this bean instance
-            ServiceReference interceptorReference = InterceptorManager.registerInterceptorService(txInterceptor,
beanId, parsingBundleId);
+            ServiceReference interceptorReference = InterceptorManager.registerInterceptorService(txInterceptor,
interceptorRanking, beanId, parsingBundleId);
             
             //Register a Mandatory Service Reference for the interceptor Service
-            if (interceptorReference != null) InterceptorManager.addServiceReference(interceptorReference,
ServiceReferenceMetadata.AVAILABILITY_MANDATORY, pc); 
+            // @JAB temporarily remove to try lookup/add in Blueprint Container
+            // if (interceptorReference != null) InterceptorManager.addServiceReference(interceptorReference,
ServiceReferenceMetadata.AVAILABILITY_MANDATORY, pc); 
         
         }
     }    

Modified: aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml
URL: http://svn.apache.org/viewvc/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml?rev=1056367&r1=1056366&r2=1056367&view=diff
==============================================================================
--- aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml
(original)
+++ aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml
Fri Jan  7 16:04:41 2011
@@ -34,7 +34,6 @@
         <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"/>-->
         </bean>
   </service>              
   
@@ -42,28 +41,12 @@
  
   <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" interface="org.apache.aries.blueprint.Interceptor">
+  <service id="txInterceptorService" interface="org.apache.aries.blueprint.Interceptor"
ranking="500">
     <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>
--->        
+        <entry key="osgi.service.blueprint.namespace" value="http://aries.apache.org/xmlns/transactions/v1.1.0"/>
    
     </service-properties>
     <bean class="org.apache.aries.transaction.TxInterceptorImpl">
       <property name="transactionManager" ref="tm"/>
-      <!-- <property ref="txEnhancer" name="txMetaDataHelper"/> -->
     </bean>
   </service>
   

Modified: aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java?rev=1056367&r1=1056366&r2=1056367&view=diff
==============================================================================
--- aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java
(original)
+++ aries/sandbox/jbohn/interceptor-proto/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java
Fri Jan  7 16:04:41 2011
@@ -72,6 +72,7 @@ public class BundleWideNameSpaceHandlerT
         //namespaceHandler.init();
         namespaceHandler.setInterceptor(txinterceptor);
         namespaceHandler.setTxMetaDataHelper(txenhancer);
+        namespaceHandler.setInterceptorRanking(100);
             
         Properties props = new Properties();
         props.put("osgi.service.blueprint.namespace", new String[]{"http://aries.apache.org/xmlns/transactions/v1.0.0",
"http://aries.apache.org/xmlns/transactions/v1.1.0"});



Mime
View raw message