[webbeans-commits] Webbeans SVN: r275 - in ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans: model/bean and 1 other directory.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Fri Nov 7 12:49:25 EST 2008


Author: gavin.king at jboss.com
Date: 2008-11-07 12:49:25 -0500 (Fri, 07 Nov 2008)
New Revision: 275

Modified:
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/model/bean/SimpleBeanModel.java
Log:
@PostConstruct/@PreDestroy for simple web beans

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java	2008-11-07 17:39:32 UTC (rev 274)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java	2008-11-07 17:49:25 UTC (rev 275)
@@ -3,6 +3,7 @@
 import org.jboss.webbeans.ManagerImpl;
 import org.jboss.webbeans.injectable.InjectableField;
 import org.jboss.webbeans.injectable.InjectableMethod;
+import org.jboss.webbeans.introspector.AnnotatedMethod;
 import org.jboss.webbeans.model.bean.SimpleBeanModel;
 
 public class SimpleBean<T> extends AbstractBean<T>
@@ -25,9 +26,48 @@
       injectEjbAndCommonFields();
       injectBoundFields(instance);
       callInitializers(instance);
+      callPostConstruct(instance);
       return instance;
    }
    
+   @Override
+   public void destroy(T instance)
+   {
+      callPreDestroy(instance);
+   }
+   
+   protected void callPreDestroy(T instance)
+   {
+      AnnotatedMethod<Object> preDestroy = getModel().getPreDestroy();
+      if (preDestroy!=null)
+      {
+         try
+         {
+            preDestroy.getAnnotatedMethod().invoke(instance);
+         }
+         catch (Exception e) 
+         {
+            throw new RuntimeException("Unable to invoke " + preDestroy + " on " + instance, e);
+         }
+     }
+   }
+
+   protected void callPostConstruct(T instance)
+   {
+      AnnotatedMethod<Object> postConstruct = getModel().getPostConstruct();
+      if (postConstruct!=null)
+      {
+         try
+         {
+            postConstruct.getAnnotatedMethod().invoke(instance);
+         }
+         catch (Exception e) 
+         {
+            throw new RuntimeException("Unable to invoke " + postConstruct + " on " + instance, e);
+         }
+      }
+   }
+
    protected void callInitializers(T instance)
    {
       for (InjectableMethod<Object> initializer : model.getInitializerMethods())

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/model/bean/SimpleBeanModel.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/model/bean/SimpleBeanModel.java	2008-11-07 17:39:32 UTC (rev 274)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/model/bean/SimpleBeanModel.java	2008-11-07 17:49:25 UTC (rev 275)
@@ -4,6 +4,8 @@
 import java.util.Set;
 import java.util.logging.Logger;
 
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import javax.webbeans.DefinitionException;
 import javax.webbeans.Initializer;
 
@@ -12,6 +14,7 @@
 import org.jboss.webbeans.injectable.SimpleConstructor;
 import org.jboss.webbeans.introspector.AnnotatedClass;
 import org.jboss.webbeans.introspector.AnnotatedConstructor;
+import org.jboss.webbeans.introspector.AnnotatedMethod;
 import org.jboss.webbeans.introspector.AnnotatedParameter;
 import org.jboss.webbeans.introspector.impl.SimpleAnnotatedClass;
 import org.jboss.webbeans.util.LoggerUtil;
@@ -25,6 +28,8 @@
    private static Set<Class<?>> NO_ARGUMENTS = Collections.emptySet();
    
    private SimpleConstructor<T> constructor;
+   private AnnotatedMethod<Object> postConstruct;
+   private AnnotatedMethod<Object> preDestroy;
 
    private String location;
    
@@ -47,6 +52,8 @@
       initConstructor();
       checkType(getType());
       initInjectionPoints();
+      initPostConstruct();
+      initPreDestroy();
       // TODO Interceptors
    }
    
@@ -78,10 +85,7 @@
       log.finest("Found " + initializerAnnotatedConstructors + " constructors annotated with @Initializer for " + getType());
       if (initializerAnnotatedConstructors.size() > 1)
       {
-         if (initializerAnnotatedConstructors.size() > 1)
-         {
-            throw new DefinitionException("Cannot have more than one constructor annotated with @Initializer for " + getType());
-         }
+         throw new DefinitionException("Cannot have more than one constructor annotated with @Initializer for " + getType());
       }
       else if (initializerAnnotatedConstructors.size() == 1)
       {
@@ -89,24 +93,70 @@
          log.finest("Exactly one constructor (" + constructor +") annotated with @Initializer defined, using it as the bean constructor for " + getType());
          return;
       }
-         
-      if (getAnnotatedItem().getConstructor(NO_ARGUMENTS) != null)
+      else if (getAnnotatedItem().getConstructor(NO_ARGUMENTS) != null)
       {
          
          this.constructor = new SimpleConstructor<T>(getAnnotatedItem().getConstructor(NO_ARGUMENTS));
          log.finest("Exactly one constructor (" + constructor +") defined, using it as the bean constructor for " + getType());
          return;
       }
-      
-      throw new DefinitionException("Cannot determine constructor to use for " + getType());
+      else {
+         throw new DefinitionException("Cannot determine constructor to use for " + getType());
+      }
    }
+   
+   protected void initPostConstruct()
+   {
+      Set<AnnotatedMethod<Object>> postConstructMethods = getAnnotatedItem().getAnnotatedMethods(PostConstruct.class);
+      log.finest("Found " + postConstructMethods + " constructors annotated with @Initializer for " + getType());
+      if (postConstructMethods.size() > 1)
+      {
+         //TODO: actually this is wrong, in EJB you can have @PostConstruct methods on the superclass,
+         //      though the Web Beans spec is silent on the issue
+         throw new DefinitionException("Cannot have more than one post construct method annotated with @Initializer for " + getType());
+      }
+      else if (postConstructMethods.size() == 1)
+      {
+         this.postConstruct = postConstructMethods.iterator().next();
+         log.finest("Exactly one post construct method (" + postConstruct +") for " + getType());
+        return;
+      }
+   }
 
+   protected void initPreDestroy()
+   {
+      Set<AnnotatedMethod<Object>> preDestroyMethods = getAnnotatedItem().getAnnotatedMethods(PreDestroy.class);
+      log.finest("Found " + preDestroyMethods + " constructors annotated with @Initializer for " + getType());
+      if (preDestroyMethods.size() > 1)
+      {
+         //TODO: actually this is wrong, in EJB you can have @PreDestroy methods on the superclass,
+         //      though the Web Beans spec is silent on the issue
+         throw new DefinitionException("Cannot have more than one pre destroy method annotated with @Initializer for " + getType());
+      }
+      else if (preDestroyMethods.size() == 1)
+      {
+         this.preDestroy = preDestroyMethods.iterator().next();
+         log.finest("Exactly one post construct method (" + preDestroy +") for " + getType());
+        return;
+      }
+   }
+
    public SimpleConstructor<T> getConstructor()
    {
       return constructor;
    }
    
+   public AnnotatedMethod<Object> getPostConstruct() 
+   {
+      return postConstruct;
+   }
+   
 
+   public AnnotatedMethod<Object> getPreDestroy() 
+   {
+      return preDestroy;
+   }
+   
    @Override
    public String toString()
    {




More information about the weld-commits mailing list