incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From o...@apache.org
Subject svn commit: r881291 - in /incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint: ComponentDefinitionRegistry.java container/BeanRecipe.java namespace/ComponentDefinitionRegistryImpl.java
Date Tue, 17 Nov 2009 13:54:37 GMT
Author: ozzy
Date: Tue Nov 17 13:54:37 2009
New Revision: 881291

URL: http://svn.apache.org/viewvc?rev=881291&view=rev
Log:
ARIES-47 Add interceptor concept to blueprint

Modified:
    incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ComponentDefinitionRegistry.java
    incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
    incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/ComponentDefinitionRegistryImpl.java

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ComponentDefinitionRegistry.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ComponentDefinitionRegistry.java?rev=881291&r1=881290&r2=881291&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ComponentDefinitionRegistry.java
(original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ComponentDefinitionRegistry.java
Tue Nov 17 13:54:37 2009
@@ -37,5 +37,9 @@
     void registerTypeConverter(Target component);
 
     List<Target> getTypeConverters();
+    
+    void registerInterceptorWithComponent(ComponentMetadata component, Interceptor interceptor);
+    
+    List<Interceptor> getInterceptors(ComponentMetadata component);
 
 }

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java?rev=881291&r1=881290&r2=881291&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
(original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
Tue Nov 17 13:54:37 2009
@@ -33,13 +33,19 @@
 import java.util.Map;
 
 import org.apache.aries.blueprint.BeanProcessor;
+import org.apache.aries.blueprint.ComponentDefinitionRegistry;
 import org.apache.aries.blueprint.ExtendedBlueprintContainer;
+import org.apache.aries.blueprint.Interceptor;
+import org.apache.aries.blueprint.container.AbstractServiceReferenceRecipe.CgLibProxyFactory;
+import org.apache.aries.blueprint.container.AbstractServiceReferenceRecipe.JdkProxyFactory;
 import org.apache.aries.blueprint.di.AbstractRecipe;
 import org.apache.aries.blueprint.di.Recipe;
+import org.apache.aries.blueprint.proxy.CgLibInterceptorWrapper;
 import org.apache.aries.blueprint.utils.ReflectionUtils;
 import org.osgi.service.blueprint.container.ComponentDefinitionException;
 import org.osgi.service.blueprint.container.ReifiedType;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
+import org.osgi.service.blueprint.reflect.ComponentMetadata;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -594,6 +600,32 @@
         }
         return obj;
     }    
+    
+    private Object addInterceptors(Object original) throws ComponentDefinitionException{
+        
+        try {
+            // Try load load a cglib class (to make sure it's actually available
+            getClass().getClassLoader().loadClass("net.sf.cglib.proxy.Enhancer");
+        } catch (Throwable t) {
+            throw new ComponentDefinitionException("Interceptors have been configured but
cglib can not be used", t);
+        }
+        
+        Object intercepted = null;
+        String beanName = getName();
+        ComponentDefinitionRegistry reg = blueprintContainer.getComponentDefinitionRegistry();
+        ComponentMetadata metaData = reg.getComponentDefinition(beanName);
+        List<Interceptor> interceptors = reg.getInterceptors(metaData); 
+        if(interceptors!=null && interceptors.size()>0){
+            intercepted = CgLibInterceptorWrapper.createProxyObject(original.getClass().getClassLoader(),

+                                                                metaData, 
+                                                                interceptors, 
+                                                                original, 
+                                                                original.getClass().getInterfaces());
+        }else{
+            intercepted = original;
+        }
+        return intercepted;
+    }
         
     @Override
     protected Object internalCreate() throws ComponentDefinitionException {
@@ -622,6 +654,8 @@
         
         obj = runBeanProcPostInit(obj);
         
+        obj = addInterceptors(obj);
+        
         return obj;
     }
     

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/ComponentDefinitionRegistryImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/ComponentDefinitionRegistryImpl.java?rev=881291&r1=881290&r2=881291&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/ComponentDefinitionRegistryImpl.java
(original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/ComponentDefinitionRegistryImpl.java
Tue Nov 17 13:54:37 2009
@@ -18,7 +18,10 @@
  */
 package org.apache.aries.blueprint.namespace;
 
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -27,6 +30,7 @@
 
 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
 import org.apache.aries.blueprint.ComponentNameAlreadyInUseException;
+import org.apache.aries.blueprint.Interceptor;
 import org.apache.aries.blueprint.reflect.PassThroughMetadataImpl;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
 import org.osgi.service.blueprint.reflect.Target;
@@ -43,11 +47,13 @@
 
     private final Map<String, ComponentMetadata> components;
     private final List<Target> typeConverters;
+    private final Map<ComponentMetadata, List<Interceptor>> interceptors;
 
     public ComponentDefinitionRegistryImpl() {
         // Use a linked hash map to keep the declaration order 
         components = Collections.synchronizedMap(new LinkedHashMap<String, ComponentMetadata>());
         typeConverters = new CopyOnWriteArrayList<Target>();
+        interceptors = Collections.synchronizedMap(new HashMap<ComponentMetadata, List<Interceptor>>());
     }
 
     public boolean containsComponentDefinition(String name) {
@@ -79,7 +85,10 @@
     }
 
     public void removeComponentDefinition(String name) {
-        components.remove(name);
+        ComponentMetadata removed = components.remove(name);
+        if(removed!=null){
+            interceptors.remove(removed);
+        }
     }
 
     public void registerTypeConverter(Target component) {
@@ -92,5 +101,28 @@
     public List<Target> getTypeConverters() {
         return typeConverters;
     }
+
+    public void registerInterceptorWithComponent(ComponentMetadata component, Interceptor
interceptor) {
+        if(interceptor!=null){
+            List<Interceptor> componentInterceptorList = interceptors.get(component);
+            if(componentInterceptorList==null){
+                componentInterceptorList = new ArrayList<Interceptor>();
+                interceptors.put(component, componentInterceptorList);
+            }
+            if(!componentInterceptorList.contains(interceptor)){
+                componentInterceptorList.add(interceptor);
+                Collections.sort(componentInterceptorList, new Comparator<Interceptor>(){
+                    public int compare(Interceptor object1, Interceptor object2) {
+                        //invert the order so higher ranks are sorted 1st
+                        return object2.getRank() - object1.getRank();
+                    }
+                });
+            }
+        }
+    }
+
+    public List<Interceptor> getInterceptors(ComponentMetadata component) {
+        return interceptors.get(component);
+    }
     
 }



Mime
View raw message