geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r766791 - in /geronimo/sandbox/blueprint/blueprint-core/src: main/java/org/apache/geronimo/blueprint/context/ main/java/org/apache/geronimo/blueprint/namespace/ test/java/org/apache/geronimo/blueprint/ test/java/org/apache/geronimo/blueprin...
Date Mon, 20 Apr 2009 18:36:03 GMT
Author: gawor
Date: Mon Apr 20 18:36:03 2009
New Revision: 766791

URL: http://svn.apache.org/viewvc?rev=766791&view=rev
Log:
support for init-method

Modified:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ReflectionUtils.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/namespace/ComponentDefinitionRegistryImpl.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-wiring.xml

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java?rev=766791&r1=766790&r2=766791&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
Mon Apr 20 18:36:03 2009
@@ -18,6 +18,11 @@
  */
 package org.apache.geronimo.blueprint.context;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+
+import org.apache.xbean.recipe.ConstructionException;
 import org.apache.xbean.recipe.ObjectRecipe;
 
 /**
@@ -28,8 +33,9 @@
 public class BlueprintObjectRecipe extends ObjectRecipe {
     
     private boolean keepRecipe = false;
+    private Method initMethod;
     
-    public BlueprintObjectRecipe(String typeName) {
+    public BlueprintObjectRecipe(Class typeName) {
         super(typeName);
     }
     
@@ -41,4 +47,28 @@
         return keepRecipe;
     }
     
+    public void setInitMethod(Method initMethod) {
+        this.initMethod = initMethod;
+    }
+    
+    public Method getInitMethod() {
+        return initMethod;
+    }
+        
+    @Override
+    protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException
{
+        Object obj = super.internalCreate(expectedType, lazyRefAllowed);
+        if (initMethod != null) {
+            try {
+                initMethod.invoke(obj, new Object[] {});
+            } catch (InvocationTargetException e) {
+                Throwable root = e.getTargetException();
+                throw new ConstructionException("init-method generated exception", root);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return obj;
+    }
+    
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java?rev=766791&r1=766790&r2=766791&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
Mon Apr 20 18:36:03 2009
@@ -18,6 +18,7 @@
  */
 package org.apache.geronimo.blueprint.context;
 
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -128,7 +129,7 @@
     private Recipe createRecipe(ComponentMetadata component) throws Exception {
         if (component instanceof LocalComponentMetadata) {
             LocalComponentMetadata local = (LocalComponentMetadata) component;
-            BundleObjectRecipe recipe = new BundleObjectRecipe(local.getClassName());
+            BlueprintObjectRecipe recipe = new BlueprintObjectRecipe(loadClass(local.getClassName()));
             recipe.allow(Option.PRIVATE_PROPERTIES);
             recipe.setName(component.getName());
             for (PropertyInjectionMetadata property : (Collection<PropertyInjectionMetadata>)
local.getPropertyInjectionMetadata()) {
@@ -138,8 +139,29 @@
             if (LocalComponentMetadata.SCOPE_PROTOTYPE.equals(local.getScope())) {
                 recipe.setKeepRecipe(true);
             }
+            // check for init-method and set it on Recipe
+            String initMethod = local.getInitMethodName();
+            if (initMethod == null) {
+                ComponentDefinitionRegistryImpl registry = 
+                    (ComponentDefinitionRegistryImpl)getComponentDefinitionRegistry();
+                Method method = ReflectionUtils.getLifecycleMethod(recipe.getType(), registry.getDefaultInitMethod());
+                recipe.setInitMethod(method);
+            } else if (initMethod.length() > 0) {
+                Method method = ReflectionUtils.getLifecycleMethod(recipe.getType(), initMethod);
+                if (method == null) {
+                    throw new ConstructionException("Component '" + component.getName() +
"' does not have init-method: " + initMethod);
+                }
+                recipe.setInitMethod(method);
+            }
+            // check for destroy-method
+            String destroyMethod = local.getDestroyMethodName();
+            if (destroyMethod != null && destroyMethod.length() > 0) {
+                Method method = ReflectionUtils.getLifecycleMethod(recipe.getType(), destroyMethod);
+                if (method == null) {
+                    throw new ConstructionException("Component '" + component.getName() +
"' does not have destroy-method: " + destroyMethod);
+                }
+            }
             // TODO: constructor args
-            // TODO: init-method
             // TODO: destroy-method
             // TODO: lazy
             // TODO: factory-method
@@ -235,14 +257,14 @@
         } else if (v instanceof TypedStringValue) {
             TypedStringValue stringValue = (TypedStringValue) v; 
             String value = stringValue.getStringValue();
-            Class type = loadType(stringValue.getTypeName());
+            Class type = loadClass(stringValue.getTypeName());
             return new ValueRecipe(getConversionService(), value, type, groupingType);
         } else if (v instanceof ReferenceValue) {
             String componentName = ((ReferenceValue) v).getComponentName();
             return new ReferenceRecipe(componentName);
         } else if (v instanceof ListValue) {
             ListValue listValue = (ListValue) v;
-            Class type = loadType(listValue.getValueType());
+            Class type = loadClass(listValue.getValueType());
             CollectionRecipe cr = new CollectionRecipe(ArrayList.class);
             for (Value lv : (List<Value>) listValue.getList()) {
                 cr.add(getValue(lv, type));
@@ -250,7 +272,7 @@
             return cr;
         } else if (v instanceof SetValue) {
             SetValue setValue = (SetValue) v;
-            Class type = loadType(setValue.getValueType());
+            Class type = loadClass(setValue.getValueType());
             CollectionRecipe cr = new CollectionRecipe(HashSet.class);
             for (Value lv : (Set<Value>) setValue.getSet()) {
                 cr.add(getValue(lv, type));
@@ -258,8 +280,8 @@
             return cr;
         } else if (v instanceof MapValue) {
             MapValue mapValue = (MapValue) v;
-            Class keyType = loadType(mapValue.getKeyType());
-            Class valueType = loadType(mapValue.getValueType());            
+            Class keyType = loadClass(mapValue.getKeyType());
+            Class valueType = loadClass(mapValue.getValueType());            
             MapRecipe mr = new MapRecipe(HashMap.class);
             for (Map.Entry<Value,Value> entry : ((Map<Value,Value>) mapValue.getMap()).entrySet())
{
                 Object key = getValue(entry.getKey(), keyType);
@@ -269,7 +291,7 @@
             return mr;
         } else if (v instanceof ArrayValue) {
             ArrayValue arrayValue = (ArrayValue) v;
-            Class type = loadType(arrayValue.getValueType());
+            Class type = loadClass(arrayValue.getValueType());
             ArrayRecipe ar = (type == null) ? new ArrayRecipe() : new ArrayRecipe(type);
             for (Value value : arrayValue.getArray()) {
                 ar.add(getValue(value, type));
@@ -294,7 +316,7 @@
         return moduleContext.getConversionService();
     }
     
-    private Class loadType(String typeName) throws ClassNotFoundException {
+    private Class loadClass(String typeName) throws ClassNotFoundException {
         if (typeName == null) {
             return null;
         }
@@ -302,34 +324,13 @@
         Class clazz = primitiveClasses.get(typeName);
         if (clazz == null) {
             if (moduleContext == null) {
-                clazz = Class.forName(typeName);            
+                ClassLoader loader = Thread.currentThread().getContextClassLoader();
+                clazz = loader.loadClass(typeName);
             } else {
                 clazz = moduleContext.getBundleContext().getBundle().loadClass(typeName);
             }
         }
         return clazz;
     }
-    
-    private class BundleObjectRecipe extends BlueprintObjectRecipe {
-
-        String typeName;
-
-        public BundleObjectRecipe(String typeName) {
-            super(typeName);
-            this.typeName = typeName;
-        }
-        
-        @Override
-        public Class getType() {
-            if (moduleContext == null) {
-                return super.getType();
-            }
-            try {
-                return moduleContext.getBundleContext().getBundle().loadClass(typeName);
-            } catch (ClassNotFoundException e) {
-                throw new ConstructionException("Type class could not be found: " + typeName);
-            }
-        }
-    }
-            
+                
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java?rev=766791&r1=766790&r2=766791&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
Mon Apr 20 18:36:03 2009
@@ -267,6 +267,10 @@
         if (root.hasAttribute(DEFAULT_AVAILABILITY_ATTRIBUTE)) {
             defaultAvailability = root.getAttribute(DEFAULT_AVAILABILITY_ATTRIBUTE);
         }
+        
+        registry.setDefaultInitMethod(defaultInitMethod);
+        registry.setDefaultDestroyMethod(defaultDestroyMethod);
+        
         /*
         // Parse custom attributes
         NamedNodeMap attributes = root.getAttributes();
@@ -375,20 +379,10 @@
             metadata.setExplicitDependencies(parseListAsSet(element.getAttribute(DEPENDS_ON_ATTRIBUTE)));
         }
         if (element.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
-            String method = element.getAttribute(INIT_METHOD_ATTRIBUTE);
-            if (method.trim().length() != 0) { 
-                metadata.setInitMethodName(method);
-            }
-        } else {
-            metadata.setInitMethodName(defaultInitMethod);
+            metadata.setInitMethodName(element.getAttribute(INIT_METHOD_ATTRIBUTE));
         }
         if (element.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
-            String method = element.getAttribute(DESTROY_METHOD_ATTRIBUTE);
-            if (method.trim().length() != 0) {
-                metadata.setDestroyMethodName(method);
-            }
-        } else {
-            metadata.setDestroyMethodName(defaultDestroyMethod);
+            metadata.setDestroyMethodName(element.getAttribute(DESTROY_METHOD_ATTRIBUTE));
         }
 
         // Parse elements

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ReflectionUtils.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ReflectionUtils.java?rev=766791&r1=766790&r2=766791&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ReflectionUtils.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ReflectionUtils.java
Mon Apr 20 18:36:03 2009
@@ -50,6 +50,18 @@
         return classes;
     }
     
+    public static Method getLifecycleMethod(Class clazz, String name) {
+        try {
+            Method method = clazz.getMethod(name, new Class[] {});
+            if (Void.TYPE.equals(method.getReturnType())) {
+                return method;
+            }
+        } catch (NoSuchMethodException e) {
+            // fall thru
+        }
+        return null;
+    }
+    
     public static Method findMethod(Class clazz, String name, Class[] paramTypes) {
         try {
             return clazz.getMethod(name, paramTypes);

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/namespace/ComponentDefinitionRegistryImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/namespace/ComponentDefinitionRegistryImpl.java?rev=766791&r1=766790&r2=766791&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/namespace/ComponentDefinitionRegistryImpl.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/namespace/ComponentDefinitionRegistryImpl.java
Mon Apr 20 18:36:03 2009
@@ -42,6 +42,8 @@
 
     private final Map<String, ComponentMetadata> components;
     private final List<Value> typeConverters;
+    private String defaultInitMethod;
+    private String defaultDestroyMethod;
 
     public ComponentDefinitionRegistryImpl() {
         components = new HashMap<String, ComponentMetadata>();
@@ -96,4 +98,20 @@
         return names;
     }
 
+    public void setDefaultInitMethod(String method) {
+        defaultInitMethod = method;
+    }
+    
+    public String getDefaultInitMethod() {
+        return defaultInitMethod;
+    }
+    
+    public void setDefaultDestroyMethod(String method) {
+        defaultDestroyMethod = method;
+    }
+    
+    public String getDefaultDestroyMethod() {
+        return defaultDestroyMethod;
+    }
+    
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java?rev=766791&r1=766790&r2=766791&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java
Mon Apr 20 18:36:03 2009
@@ -122,8 +122,8 @@
         assertTrue(param.getValue() instanceof ReferenceValue);
         assertEquals("pojoB", ((ReferenceValue) param.getValue()).getComponentName());
         
-        assertEquals("init", local.getInitMethodName());
-        assertEquals("destroy", local.getDestroyMethodName());
+        assertEquals(null, local.getInitMethodName());
+        assertEquals(null, local.getDestroyMethodName());
                 
         // test pojoB
         ComponentMetadata pojoB = registry.getComponentDefinition("pojoB");
@@ -132,7 +132,7 @@
         assertTrue(pojoB instanceof LocalComponentMetadata);
         LocalComponentMetadata pojoBLocal = (LocalComponentMetadata) pojoB;
         assertEquals("initPojo", pojoBLocal.getInitMethodName());
-        assertNull(pojoBLocal.getDestroyMethodName());
+        assertEquals("", pojoBLocal.getDestroyMethodName());
     }
 
     public void testParse() throws Exception {

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java?rev=766791&r1=766790&r2=766791&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
Mon Apr 20 18:36:03 2009
@@ -90,10 +90,15 @@
         assertEquals(new BigInteger("50"), pojoa.getNumberArray()[1]);
         assertEquals(new Long(100), pojoa.getNumberArray()[2]);
         
+        // test init-method
+        assertEquals(true, pojob.getInitCalled());
+        
+        // test service
         Object obj3 = graph.create("service1");
         assertNotNull(obj3);
         assertTrue(obj3 instanceof ServiceRegistration);    
         
+        // tests 'prototype' scope
         Object obj4 = graph.create("pojoC");
         assertNotNull(obj4);
         assertTrue(obj4 != graph.create("pojoC"));

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java?rev=766791&r1=766790&r2=766791&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java
Mon Apr 20 18:36:03 2009
@@ -23,6 +23,7 @@
 public class PojoB {
 
     private URI uri;
+    private boolean initCalled;
 
     public PojoB() {
     }
@@ -38,4 +39,12 @@
     public void setUri(URI uri) {
         this.uri = uri;
     }
+    
+    public void init() {
+        initCalled = true;
+    }
+    
+    public boolean getInitCalled() {
+        return initCalled;
+    }
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-wiring.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-wiring.xml?rev=766791&r1=766790&r2=766791&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-wiring.xml (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-wiring.xml Mon Apr 20
18:36:03 2009
@@ -2,10 +2,12 @@
 <components xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 ../../main/resources/org/apache/felix/blueprint/blueprint.xsd"
-            default-availability="mandatory">
+            default-availability="mandatory"
+            default-init-method = "init"
+            default-destroy-method = "destroy" >
 
     <type-converters>
-            <component id="converter1" class="org.apache.geronimo.blueprint.convert.TestConverter">
+            <component id="converter1" class="org.apache.geronimo.blueprint.pojos.ConverterA">
                 <property name="bundle" ref="bundle" />
             </component>
     </type-converters>
@@ -21,7 +23,7 @@
         <property name="uri" value="urn:myuri" />
     </component>
     
-    <component id="pojoB" class="org.apache.geronimo.blueprint.pojos.PojoB">
+    <component id="pojoB" class="org.apache.geronimo.blueprint.pojos.PojoB" init-method
= "init">
         <property name="uri" value="urn:myuri" />
     </component>
 



Mime
View raw message