openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gerdo...@apache.org
Subject svn commit: r925749 - in /openwebbeans/trunk: webbeans-impl/src/main/java/org/apache/webbeans/context/creational/ webbeans-impl/src/main/java/org/apache/webbeans/inject/ webbeans-impl/src/main/java/org/apache/webbeans/intercept/ webbeans-tomcat/src/mai...
Date Sun, 21 Mar 2010 10:42:05 GMT
Author: gerdogdu
Date: Sun Mar 21 10:42:04 2010
New Revision: 925749

URL: http://svn.apache.org/viewvc?rev=925749&view=rev
Log:
Destroy javaee components instances after their owner has destroyed by the container

Added:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/EjbInterceptorContext.java
  (with props)
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/CreationalContextImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorDataImpl.java
    openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatAnnotProcessor.java
    openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/CreationalContextImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/CreationalContextImpl.java?rev=925749&r1=925748&r2=925749&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/CreationalContextImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/CreationalContextImpl.java
Sun Mar 21 10:42:04 2010
@@ -50,7 +50,7 @@ public class CreationalContextImpl<T> im
     private CreationalContextImpl<?> ownerCreational = null;
     
     /**Ejb interceptors*/
-    private Map<Class<?>, Object> ejbInterceptors = new HashMap<Class<?>,
Object>();
+    private Map<Class<?>, EjbInterceptorContext> ejbInterceptors = new HashMap<Class<?>,
EjbInterceptorContext>();
     
     /**
      * Package private
@@ -65,7 +65,7 @@ public class CreationalContextImpl<T> im
      * @param clazz interceptor class
      * @param instance interceptor instance
      */
-    public void addEjbInterceptor(Class<?> clazz, Object instance)
+    public void addEjbInterceptor(Class<?> clazz, EjbInterceptorContext instance)
     {
         this.ejbInterceptors.put(clazz, instance);
     }
@@ -75,7 +75,7 @@ public class CreationalContextImpl<T> im
      * @param clazz interceptor class
      * @return interceptor instance
      */
-    public Object getEjbInterceptor(Class<?> clazz)
+    public EjbInterceptorContext getEjbInterceptor(Class<?> clazz)
     {
         return this.ejbInterceptors.get(clazz);
     }
@@ -263,6 +263,16 @@ public class CreationalContextImpl<T> im
         }
         
         this.dependentObjects.clear();
+        
+        Collection<EjbInterceptorContext> interceptors = this.ejbInterceptors.values();
+        if(interceptors != null)
+        {
+            for(EjbInterceptorContext intereptor : interceptors)
+            {
+                intereptor.getInjectorInstance().destroy();
+            }
+        }
+        
         this.ejbInterceptors.clear();
     }
     

Added: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/EjbInterceptorContext.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/EjbInterceptorContext.java?rev=925749&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/EjbInterceptorContext.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/EjbInterceptorContext.java
Sun Mar 21 10:42:04 2010
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.context.creational;
+
+import org.apache.webbeans.inject.OWBInjector;
+
+public class EjbInterceptorContext
+{
+    private Object interceptorInstance;
+    
+    private OWBInjector injectorInstance;
+    
+    public EjbInterceptorContext()
+    {
+        
+    }
+
+    /**
+     * @return the interceptorInstance
+     */
+    public Object getInterceptorInstance()
+    {
+        return interceptorInstance;
+    }
+
+    /**
+     * @param interceptorInstance the interceptorInstance to set
+     */
+    public void setInterceptorInstance(Object interceptorInstance)
+    {
+        this.interceptorInstance = interceptorInstance;
+    }
+
+    /**
+     * @return the injectorInstance
+     */
+    public OWBInjector getInjectorInstance()
+    {
+        return injectorInstance;
+    }
+
+    /**
+     * @param injectorInstance the injectorInstance to set
+     */
+    public void setInjectorInstance(OWBInjector injectorInstance)
+    {
+        this.injectorInstance = injectorInstance;
+    }
+    
+    
+}

Propchange: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/creational/EjbInterceptorContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java?rev=925749&r1=925748&r2=925749&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java
Sun Mar 21 10:42:04 2010
@@ -24,7 +24,6 @@ import java.lang.reflect.Method;
 import java.util.Set;
 
 import javax.enterprise.inject.spi.AnnotatedType;
-import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.InjectionTarget;
@@ -61,7 +60,7 @@ public final class OWBInjector
     }
     
     @SuppressWarnings("unchecked")
-    public static void inject(Object javaEeComponentInstance) throws Exception
+    public  OWBInjector inject(Object javaEeComponentInstance) throws Exception
     {
         BeanManager beanManager = BeanManagerImpl.getManager();
         try
@@ -72,7 +71,7 @@ public final class OWBInjector
             Set<InjectionPoint> injectionPoints = injectionTarget.getInjectionPoints();
             if(injectionPoints != null && injectionPoints.size() > 0)
             {
-                CreationalContextImpl<?> ownerCreationalContext = (CreationalContextImpl<?>)
beanManager.createCreationalContext((Bean<?>)injectionTarget);
+                this.ownerCreationalContext = (CreationalContextImpl<?>) beanManager.createCreationalContext(null);
                 
                 for(InjectionPoint injectionPoint : injectionPoints)
                 {
@@ -88,6 +87,8 @@ public final class OWBInjector
                         ClassUtil.setField(javaEeComponentInstance, field, object);
                     }
                 }
+                
+                return this;
             }
             
             
@@ -95,6 +96,8 @@ public final class OWBInjector
         {
             throw e;
         }
+        
+        return null;
     }
     
     public static boolean checkInjectionPointForInterceptorPassivation(Class<?> clazz)

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorDataImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorDataImpl.java?rev=925749&r1=925748&r2=925749&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorDataImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorDataImpl.java
Sun Mar 21 10:42:04 2010
@@ -24,9 +24,11 @@ import javax.interceptor.AroundInvoke;
 
 import org.apache.webbeans.container.BeanManagerImpl;
 import org.apache.webbeans.context.creational.CreationalContextImpl;
+import org.apache.webbeans.context.creational.EjbInterceptorContext;
 import org.apache.webbeans.decorator.WebBeansDecoratorInterceptor;
 import org.apache.webbeans.inject.OWBInjector;
 import org.apache.webbeans.intercept.webbeans.WebBeansInterceptor;
+import org.apache.webbeans.logger.WebBeansLogger;
 import org.apache.webbeans.util.WebBeansUtil;
 
 /**
@@ -35,6 +37,9 @@ import org.apache.webbeans.util.WebBeans
  */
 public class InterceptorDataImpl implements InterceptorData
 {
+    //Logger instance
+    private static final WebBeansLogger logger = WebBeansLogger.getLogger(InterceptorDataImpl.class);
+    
     /** Around invokes method */
     private Method aroundInvoke = null;
 
@@ -324,26 +329,35 @@ public class InterceptorDataImpl impleme
         }
         else
         {
+            EjbInterceptorContext ctx = null;
             Object interceptor = null;       
             //control for this InterceptorData is defined by interceptor class
             if(this.definedInInterceptorClass)
             {
-                interceptor = ownerCreationalContext.getEjbInterceptor(this.interceptorClass);
-                
-                if(interceptor == null)
-                {
+                ctx = ownerCreationalContext.getEjbInterceptor(this.interceptorClass);  
             
+                if(ctx == null)
+                {                    
                     interceptor = WebBeansUtil.newInstanceForced(this.interceptorClass);
                     try
                     {
-                        OWBInjector.inject(interceptor);
+                        OWBInjector injector = new OWBInjector();
+                        injector.inject(interceptor);
+                        
+                        ctx = new EjbInterceptorContext();
+                        ctx.setInjectorInstance(injector);
+                        ctx.setInterceptorInstance(interceptor);
                     }
                     catch (Exception e)
                     {
-                        //No-op
+                        logger.error("Unable to inject dependencies of EJB interceptor instance
with class : " + interceptorClass,e);
                     }          
                     
-                    ownerCreationalContext.addEjbInterceptor(interceptorClass, interceptor);
-                }                
+                    ownerCreationalContext.addEjbInterceptor(interceptorClass, ctx);
+                }
+                else
+                {
+                    interceptor = ctx.getInterceptorInstance();
+                }
             }
 
             return interceptor; 

Modified: openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatAnnotProcessor.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatAnnotProcessor.java?rev=925749&r1=925748&r2=925749&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatAnnotProcessor.java
(original)
+++ openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatAnnotProcessor.java
Sun Mar 21 10:42:04 2010
@@ -19,6 +19,8 @@
 package org.apache.webbeans.web.tomcat;
 
 import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.naming.NamingException;
 
@@ -30,6 +32,8 @@ public class TomcatAnnotProcessor implem
 
     private ClassLoader loader;
     
+    private Map<Object, Object> objects = new ConcurrentHashMap<Object, Object>();
+    
     public TomcatAnnotProcessor(ClassLoader loader, AnnotationProcessor processor)
     {
         this.processor = processor;
@@ -45,6 +49,18 @@ public class TomcatAnnotProcessor implem
     @Override
     public void preDestroy(Object obj) throws IllegalAccessException, InvocationTargetException
     {
+        Object injectorInstance = objects.get(obj);
+        if(injectorInstance != null)
+        {
+            try
+            {
+                TomcatUtil.destroy(injectorInstance, loader);
+            }
+            catch (Exception e)
+            {
+                e.printStackTrace();
+            }
+        }
         processor.preDestroy(obj);
     }
 
@@ -54,7 +70,11 @@ public class TomcatAnnotProcessor implem
         processor.processAnnotations(obj);
         try
         {
-            TomcatUtil.inject(obj, loader);
+           Object injectorInstance = TomcatUtil.inject(obj, loader);
+           if(injectorInstance != null)
+           {
+               objects.put(obj, injectorInstance);   
+           }
         }
         catch (Exception e)
         {

Modified: openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java?rev=925749&r1=925748&r2=925749&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java
(original)
+++ openwebbeans/trunk/webbeans-tomcat/src/main/java/org/apache/webbeans/web/tomcat/TomcatUtil.java
Sun Mar 21 10:42:04 2010
@@ -22,10 +22,21 @@ import java.lang.reflect.Method;
 
 public class TomcatUtil
 {
-    public static void inject(Object object, ClassLoader loader) throws Exception
+    public static Object inject(Object object, ClassLoader loader) throws Exception
     {
         Class<?> injector = loader.loadClass("org.apache.webbeans.inject.OWBInjector");
+        Object injectorInstance = injector.newInstance();
         Method method = injector.getDeclaredMethod("inject", new Class<?>[]{Object.class});
-        method.invoke(null, new Object[]{object});        
+        injectorInstance = method.invoke(injectorInstance, new Object[]{object});       
+        
+        return injectorInstance;
     }
+    
+    public static void destroy(Object injectorInstance, ClassLoader loader) throws Exception
+    {
+        Class<?> injector = loader.loadClass("org.apache.webbeans.inject.OWBInjector");
+        Method method = injector.getDeclaredMethod("destroy", new Class<?>[]{});
+        method.invoke(injectorInstance, new Object[]{});               
+    }
+
 }



Mime
View raw message