You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2009/04/27 22:06:35 UTC
svn commit: r769138 - in /geronimo/sandbox/blueprint/blueprint-core/src:
main/java/org/apache/geronimo/blueprint/context/ test/resources/
Author: gawor
Date: Mon Apr 27 20:06:35 2009
New Revision: 769138
URL: http://svn.apache.org/viewvc?rev=769138&view=rev
Log:
check for lifecycle methods on the instantiated beans
Modified:
geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java
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/test/resources/test-constructor.xml
Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java?rev=769138&r1=769137&r2=769138&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java Mon Apr 27 20:06:35 2009
@@ -435,6 +435,10 @@
return conversionService;
}
+ protected ComponentDefinitionRegistryImpl getComponentDefinitionRegistry() {
+ return componentDefinitionRegistry;
+ }
+
public BundleContext getBundleContext() {
return bundleContext;
}
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=769138&r1=769137&r2=769138&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 27 20:06:35 2009
@@ -33,8 +33,10 @@
import org.apache.xbean.recipe.RecipeHelper;
import org.apache.xbean.recipe.ReferenceRecipe;
import org.apache.geronimo.blueprint.Destroyable;
+import org.apache.geronimo.blueprint.namespace.ComponentDefinitionRegistryImpl;
import org.apache.geronimo.blueprint.utils.ArgumentsMatch;
import org.apache.geronimo.blueprint.utils.ArgumentsMatcher;
+import org.apache.geronimo.blueprint.utils.ReflectionUtils;
import org.osgi.service.blueprint.reflect.BeanArgument;
import org.osgi.service.blueprint.reflect.Metadata;
import org.osgi.service.blueprint.reflect.ValueMetadata;
@@ -48,8 +50,8 @@
private final BlueprintContextImpl blueprintContext;
private boolean keepRecipe = false;
- private Method initMethod;
- private Method destroyMethod;
+ private String initMethod;
+ private String destroyMethod;
private List<String> explicitDependencies;
private Object factory; // could be Recipe or actual object
@@ -91,19 +93,19 @@
return keepRecipe;
}
- public void setInitMethod(Method initMethod) {
+ public void setInitMethod(String initMethod) {
this.initMethod = initMethod;
}
- public Method getInitMethod() {
+ public String getInitMethod() {
return initMethod;
}
- public void setDestroyMethod(Method destroyMethod) {
+ public void setDestroyMethod(String destroyMethod) {
this.destroyMethod = destroyMethod;
}
- public Method getDestroyMethod() {
+ public String getDestroyMethod() {
return destroyMethod;
}
@@ -261,6 +263,42 @@
return instance;
}
+
+ /**
+ * Returns init method (if any). Throws exception if the init-method was set explicitly on the bean
+ * and the method is not found on the instance.
+ */
+ private Method getInitMethod(Object instance) throws ConstructionException {
+ Method method = null;
+ if (initMethod == null) {
+ ComponentDefinitionRegistryImpl registry = blueprintContext.getComponentDefinitionRegistry();
+ method = ReflectionUtils.getLifecycleMethod(instance.getClass(), registry.getDefaultInitMethod());
+ } else if (initMethod.length() > 0) {
+ method = ReflectionUtils.getLifecycleMethod(instance.getClass(), initMethod);
+ if (method == null) {
+ throw new ConstructionException("Component '" + getName() + "' does not have init-method: " + initMethod);
+ }
+ }
+ return method;
+ }
+
+ /**
+ * Returns destroy method (if any). Throws exception if the destroy-method was set explicitly on the bean
+ * and the method is not found on the instance.
+ */
+ private Method getDestroyMethod(Object instance) throws ConstructionException {
+ Method method = null;
+ if (destroyMethod == null) {
+ ComponentDefinitionRegistryImpl registry = blueprintContext.getComponentDefinitionRegistry();
+ method = ReflectionUtils.getLifecycleMethod(instance.getClass(), registry.getDefaultDestroyMethod());
+ } else if (destroyMethod.length() > 0) {
+ method = ReflectionUtils.getLifecycleMethod(instance.getClass(), destroyMethod);
+ if (method == null) {
+ throw new ConstructionException("Component '" + getName() + "' does not have destroy-method: " + destroyMethod);
+ }
+ }
+ return method;
+ }
@Override
public List<Recipe> getConstructorRecipes() {
@@ -272,13 +310,16 @@
final Object obj = getInstance(lazyRefAllowed);
+ // check for init lifecycle method (if any)
+ Method initMethod = getInitMethod(obj);
+
+ // check for destroy lifecycle method (if any)
+ getDestroyMethod(obj);
+
// inject properties
setProperties(obj);
- if (getName() != null) {
- ExecutionContext.getContext().addObject(getName(), obj);
- }
-
+ // call init method
if (initMethod != null) {
try {
initMethod.invoke(obj);
@@ -289,6 +330,11 @@
e.printStackTrace();
}
}
+
+ if (getName() != null) {
+ ExecutionContext.getContext().addObject(getName(), obj);
+ }
+
if (destroyMethod != null && blueprintContext != null) {
Destroyable d = new Destroyable() {
public void destroy() {
@@ -297,6 +343,7 @@
};
blueprintContext.addDestroyable(getName(), d);
}
+
return obj;
}
@@ -304,12 +351,13 @@
if (!getType().equals(obj.getClass())) {
throw new RuntimeException("");
}
- if (destroyMethod != null) {
- try {
- destroyMethod.invoke(obj);
- } catch (Exception e) {
- e.printStackTrace();
+ try {
+ Method method = getDestroyMethod(obj);
+ if (method != null) {
+ method.invoke(obj);
}
+ } catch (Exception e) {
+ e.printStackTrace();
}
}
}
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=769138&r1=769137&r2=769138&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 27 20:06:35 2009
@@ -208,30 +208,8 @@
if (BeanMetadata.SCOPE_PROTOTYPE.equals(local.getScope())) {
recipe.setKeepRecipe(true);
}
- // check for init-method and set it on Recipe
- String initMethod = local.getInitMethodName();
- if (initMethod == null) {
- 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 '" + local.getId() + "' does not have init-method: " + initMethod);
- }
- recipe.setInitMethod(method);
- }
- // check for destroy-method and set it on Recipe
- String destroyMethod = local.getDestroyMethodName();
- if (destroyMethod == null) {
- Method method = ReflectionUtils.getLifecycleMethod(recipe.getType(), registry.getDefaultDestroyMethod());
- recipe.setDestroyMethod(method);
- } else if (destroyMethod.length() > 0) {
- Method method = ReflectionUtils.getLifecycleMethod(recipe.getType(), destroyMethod);
- if (method == null) {
- throw new ConstructionException("Component '" + local.getId() + "' does not have destroy-method: " + destroyMethod);
- }
- recipe.setDestroyMethod(method);
- }
+ recipe.setInitMethod(local.getInitMethodName());
+ recipe.setDestroyMethod(local.getDestroyMethodName());
List<BeanArgument> beanArguments = local.getArguments();
if (beanArguments != null && !beanArguments.isEmpty()) {
boolean hasIndex = (beanArguments.get(0).getIndex() >= 0);
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=769138&r1=769137&r2=769138&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 27 20:06:35 2009
@@ -433,6 +433,14 @@
metadata.setFactoryMethodName(factoryMethod);
}
+ // Do some validation
+ if (metadata.getClassName() == null && metadata.getFactoryComponent() == null) {
+ throw new ComponentDefinitionException("Bean class or factory-component must be specified");
+ }
+ if (metadata.getFactoryComponent() != null && metadata.getFactoryMethodName() == null) {
+ throw new ComponentDefinitionException("factory-method is required when factory-component is set");
+ }
+
// Parse elements
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml?rev=769138&r1=769137&r2=769138&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml Mon Apr 27 20:06:35 2009
@@ -28,9 +28,7 @@
<argument value="urn:myuri" index = "0" />
</bean>
- <!-- TODO: class is not needed when factory-component is specified -->
- <bean id="pojoE" class="org.apache.geronimo.blueprint.pojos.PojoB"
- factory-method = "createDynamic" factory-component = "pojoB" >
+ <bean id="pojoE" factory-method = "createDynamic" factory-component = "pojoB" >
<argument><value type="int">20</value></argument>
<argument value="urn:myuri" />
</bean>