You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xbean-scm@geronimo.apache.org by dj...@apache.org on 2010/02/09 20:57:57 UTC

svn commit: r908185 - /geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java

Author: djencks
Date: Tue Feb  9 19:57:57 2010
New Revision: 908185

URL: http://svn.apache.org/viewvc?rev=908185&view=rev
Log:
XBEAN-142 Scan for @PostConstruct and @PreDestroy annotations

Modified:
    geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java

Modified: geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java?rev=908185&r1=908184&r2=908185&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java (original)
+++ geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java Tue Feb  9 19:57:57 2010
@@ -28,6 +28,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.Collection;
 import java.util.HashMap;
@@ -36,6 +37,9 @@
 import java.util.Properties;
 import java.util.Set;
 
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+
 import org.apache.aries.blueprint.NamespaceHandler;
 import org.apache.aries.blueprint.ParserContext;
 import org.apache.aries.blueprint.mutable.MutableBeanMetadata;
@@ -129,12 +133,15 @@
 
     private static Set<Class> managedClassesFromProperties(ClassLoader cl, Properties properties) {
         Set<Class> managedClasses = new HashSet<Class>();
+        Properties methods = new Properties();
         for (Map.Entry entry : properties.entrySet()) {
             String key = (String) entry.getKey();
             if (key.indexOf(".") < 0) {
                 String className = (String) entry.getValue();
                 try {
-                    managedClasses.add(cl.loadClass(className));
+                    Class<?> beanClass = cl.loadClass(className);
+                    managedClasses.add(beanClass);
+                    findAnnotations(key, beanClass, methods);
                 } catch (NoClassDefFoundError e) {
                     LOGGER.warn("Could not load class: {} due to {}",className, e.getMessage());
                 } catch (ClassNotFoundException e) {
@@ -142,17 +149,21 @@
                 }
             }
         }
+        properties.putAll(methods);
         return managedClasses;
     }
 
     private static Set<Class> managedClassesFromProperties(Bundle bundle, Properties properties) {
         Set<Class> managedClasses = new HashSet<Class>();
+        Properties methods = new Properties();
         for (Map.Entry entry : properties.entrySet()) {
             String key = (String) entry.getKey();
             if (key.indexOf(".") < 0) {
                 String className = (String) entry.getValue();
                 try {
-                    managedClasses.add(bundle.loadClass(className));
+                    Class<?> beanClass = bundle.loadClass(className);
+                    managedClasses.add(beanClass);
+                    findAnnotations(key, beanClass, methods);
                 } catch (NoClassDefFoundError e) {
                     LOGGER.warn("Could not load class: {} due to {}",className, e.getMessage());
                 } catch (ClassNotFoundException e) {
@@ -160,9 +171,21 @@
                 }
             }
         }
+        properties.putAll(methods);
         return managedClasses;
     }
 
+    private static void findAnnotations(String key, Class<?> beanClass, Properties methods) {
+        for (Method m: beanClass.getMethods()) {
+            if (m.isAnnotationPresent(PostConstruct.class)) {
+                methods.put(key  + ".initMethod", m.getName());
+            }
+            if (m.isAnnotationPresent(PreDestroy.class)) {
+                methods.put(key + ".destroyMethod", m.getName());
+            }
+        }
+    }
+
     private Map<String, Class<? extends PropertyEditor>> propertyEditorsFromProperties(Bundle bundle, Properties properties) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
         Map<String, Class<? extends PropertyEditor>> propertyEditors = new HashMap<String, Class<? extends PropertyEditor>>();
         for (Map.Entry entry : properties.entrySet()) {