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/20 22:09:58 UTC
svn commit: r766843 - in
/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context:
BlueprintObjectRecipe.java BundleScopeServiceFactory.java Instanciator.java
ModuleContextImpl.java ReflectionUtils.java
Author: gawor
Date: Mon Apr 20 20:09:58 2009
New Revision: 766843
URL: http://svn.apache.org/viewvc?rev=766843&view=rev
Log:
refactory Instatiator a bit and support destroy-method callback for bundle scope
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/BundleScopeServiceFactory.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/ModuleContextImpl.java
geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ReflectionUtils.java
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=766843&r1=766842&r2=766843&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 20:09:58 2009
@@ -34,6 +34,7 @@
private boolean keepRecipe = false;
private Method initMethod;
+ private Method destroyMethod;
public BlueprintObjectRecipe(Class typeName) {
super(typeName);
@@ -54,6 +55,14 @@
public Method getInitMethod() {
return initMethod;
}
+
+ public void setDestroyMethod(Method destroyMethod) {
+ this.destroyMethod = destroyMethod;
+ }
+
+ public Method getDestroyMethod() {
+ return destroyMethod;
+ }
@Override
protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException {
@@ -71,4 +80,16 @@
return obj;
}
+ public void destroyInstance(Object obj) {
+ if (!getType().equals(obj.getClass())) {
+ throw new RuntimeException("");
+ }
+ if (destroyMethod != null) {
+ try {
+ destroyMethod.invoke(obj, new Object[] {});
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
}
Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BundleScopeServiceFactory.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BundleScopeServiceFactory.java?rev=766843&r1=766842&r2=766843&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BundleScopeServiceFactory.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BundleScopeServiceFactory.java Mon Apr 20 20:09:58 2009
@@ -25,8 +25,6 @@
import java.util.Set;
import org.apache.xbean.recipe.ObjectGraph;
-import org.apache.xbean.recipe.ObjectRecipe;
-import org.apache.xbean.recipe.Recipe;
import org.apache.xbean.recipe.Repository;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceFactory;
@@ -38,10 +36,10 @@
public class BundleScopeServiceFactory implements ServiceFactory {
private ModuleContextImpl moduleContext;
- private Recipe serviceRecipe;
+ private BlueprintObjectRecipe serviceRecipe;
private Map<Bundle, Entry> instanceMap = Collections.synchronizedMap(new HashMap<Bundle, Entry>());
- public BundleScopeServiceFactory(ModuleContextImpl moduleContext, Recipe serviceRecipe) {
+ public BundleScopeServiceFactory(ModuleContextImpl moduleContext, BlueprintObjectRecipe serviceRecipe) {
this.moduleContext = moduleContext;
this.serviceRecipe = serviceRecipe;
}
@@ -83,11 +81,11 @@
}
private void destroyInstance(Object instance) {
- // TODO: call destroy method if any
+ serviceRecipe.destroyInstance(instance);
}
protected Class getServiceClass() {
- return ((ObjectRecipe) serviceRecipe).getType();
+ return serviceRecipe.getType();
}
private static class Entry {
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=766843&r1=766842&r2=766843&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 20:09:58 2009
@@ -128,73 +128,9 @@
private Recipe createRecipe(ComponentMetadata component) throws Exception {
if (component instanceof LocalComponentMetadata) {
- LocalComponentMetadata local = (LocalComponentMetadata) component;
- BlueprintObjectRecipe recipe = new BlueprintObjectRecipe(loadClass(local.getClassName()));
- recipe.allow(Option.PRIVATE_PROPERTIES);
- recipe.setName(component.getName());
- for (PropertyInjectionMetadata property : (Collection<PropertyInjectionMetadata>) local.getPropertyInjectionMetadata()) {
- Object value = getValue(property.getValue(), null);
- recipe.setProperty(property.getName(), value);
- }
- 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: destroy-method
- // TODO: lazy
- // TODO: factory-method
- // TODO: factory-component
- return recipe;
+ return createComponentRecipe( (LocalComponentMetadata) component);
} else if (component instanceof ServiceExportComponentMetadata) {
- ServiceExportComponentMetadata serviceExport = (ServiceExportComponentMetadata) component;
- ObjectRecipe recipe = new ObjectRecipe(ServiceRegistrationProxy.class);
- recipe.allow(Option.PRIVATE_PROPERTIES);
- recipe.setName(component.getName());
- recipe.setProperty("moduleContext", moduleContext);
- LocalComponentMetadata exportedComponent = getLocalServiceComponent(serviceExport.getExportedComponent());
- if (exportedComponent != null && LocalComponentMetadata.SCOPE_BUNDLE.equals(exportedComponent.getScope())) {
- Recipe exportedComponentRecipe = createRecipe(exportedComponent);
- recipe.setProperty("service", new BundleScopeServiceFactory(moduleContext, exportedComponentRecipe));
- } else {
- recipe.setProperty("service", getValue(serviceExport.getExportedComponent(), null));
- }
- recipe.setProperty("metadata", component);
- if (component instanceof ServiceExportComponentMetadataImpl) {
- ServiceExportComponentMetadataImpl impl = (ServiceExportComponentMetadataImpl) component;
- if (impl.getServicePropertiesValue() != null) {
- recipe.setProperty("serviceProperties", getValue(impl.getServicePropertiesValue(), null));
- }
- }
- if (serviceExport.getRegistrationListeners() != null) {
- CollectionRecipe cr = new CollectionRecipe(ArrayList.class);;
- for (RegistrationListenerMetadata listener : (Collection<RegistrationListenerMetadata>)serviceExport.getRegistrationListeners()) {
- cr.add(createRecipe(listener));
- }
- recipe.setProperty("listeners", cr);
- }
- return recipe;
+ return createServiceRecipe( (ServiceExportComponentMetadata) component);
} else if (component instanceof UnaryServiceReferenceComponentMetadata) {
UnaryServiceReferenceComponentMetadata metadata = (UnaryServiceReferenceComponentMetadata) component;
CollectionRecipe cr = null;
@@ -218,6 +154,78 @@
}
}
+ private ObjectRecipe createServiceRecipe(ServiceExportComponentMetadata serviceExport) throws Exception {
+ ObjectRecipe recipe = new ObjectRecipe(ServiceRegistrationProxy.class);
+ recipe.allow(Option.PRIVATE_PROPERTIES);
+ recipe.setName(serviceExport.getName());
+ recipe.setProperty("moduleContext", moduleContext);
+ LocalComponentMetadata exportedComponent = getLocalServiceComponent(serviceExport.getExportedComponent());
+ if (exportedComponent != null && LocalComponentMetadata.SCOPE_BUNDLE.equals(exportedComponent.getScope())) {
+ BlueprintObjectRecipe exportedComponentRecipe = createComponentRecipe(exportedComponent);
+ recipe.setProperty("service", new BundleScopeServiceFactory(moduleContext, exportedComponentRecipe));
+ } else {
+ recipe.setProperty("service", getValue(serviceExport.getExportedComponent(), null));
+ }
+ recipe.setProperty("metadata", serviceExport);
+ if (serviceExport instanceof ServiceExportComponentMetadataImpl) {
+ ServiceExportComponentMetadataImpl impl = (ServiceExportComponentMetadataImpl) serviceExport;
+ if (impl.getServicePropertiesValue() != null) {
+ recipe.setProperty("serviceProperties", getValue(impl.getServicePropertiesValue(), null));
+ }
+ }
+ if (serviceExport.getRegistrationListeners() != null) {
+ CollectionRecipe cr = new CollectionRecipe(ArrayList.class);;
+ for (RegistrationListenerMetadata listener : (Collection<RegistrationListenerMetadata>)serviceExport.getRegistrationListeners()) {
+ cr.add(createRecipe(listener));
+ }
+ recipe.setProperty("listeners", cr);
+ }
+ return recipe;
+ }
+
+ private BlueprintObjectRecipe createComponentRecipe(LocalComponentMetadata local) throws Exception {
+ BlueprintObjectRecipe recipe = new BlueprintObjectRecipe(loadClass(local.getClassName()));
+ recipe.allow(Option.PRIVATE_PROPERTIES);
+ recipe.setName(local.getName());
+ for (PropertyInjectionMetadata property : (Collection<PropertyInjectionMetadata>) local.getPropertyInjectionMetadata()) {
+ Object value = getValue(property.getValue(), null);
+ recipe.setProperty(property.getName(), value);
+ }
+ if (LocalComponentMetadata.SCOPE_PROTOTYPE.equals(local.getScope())) {
+ recipe.setKeepRecipe(true);
+ }
+ ComponentDefinitionRegistryImpl registry = (ComponentDefinitionRegistryImpl)getComponentDefinitionRegistry();
+ // 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.getName() + "' 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.getName() + "' does not have destroy-method: " + destroyMethod);
+ }
+ recipe.setDestroyMethod(method);
+ }
+ // TODO: constructor args
+ // TODO: lazy
+ // TODO: factory-method
+ // TODO: factory-component
+ return recipe;
+ }
+
private Recipe createRecipe(RegistrationListenerMetadata listener) throws Exception {
ObjectRecipe recipe = new ObjectRecipe(ServiceRegistrationProxy.Listener.class);
recipe.allow(Option.PRIVATE_PROPERTIES);
Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ModuleContextImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ModuleContextImpl.java?rev=766843&r1=766842&r2=766843&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ModuleContextImpl.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ModuleContextImpl.java Mon Apr 20 20:09:58 2009
@@ -18,6 +18,7 @@
*/
package org.apache.geronimo.blueprint.context;
+import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
@@ -35,6 +36,7 @@
import org.apache.geronimo.blueprint.NamespaceHandlerRegistry;
import org.apache.geronimo.blueprint.convert.ConversionServiceImpl;
import org.apache.geronimo.blueprint.namespace.ComponentDefinitionRegistryImpl;
+import org.apache.xbean.recipe.ConstructionException;
import org.apache.xbean.recipe.ObjectGraph;
import org.apache.xbean.recipe.Repository;
import org.osgi.framework.Bundle;
@@ -227,7 +229,7 @@
public BundleContext getBundleContext() {
return bundleContext;
}
-
+
public void destroy() {
if (registration != null) {
registration.unregister();
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=766843&r1=766842&r2=766843&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 20:09:58 2009
@@ -51,13 +51,15 @@
}
public static Method getLifecycleMethod(Class clazz, String name) {
- try {
- Method method = clazz.getMethod(name, new Class[] {});
- if (Void.TYPE.equals(method.getReturnType())) {
- return method;
+ if (name != null) {
+ try {
+ Method method = clazz.getMethod(name, new Class[] {});
+ if (Void.TYPE.equals(method.getReturnType())) {
+ return method;
+ }
+ } catch (NoSuchMethodException e) {
+ // fall thru
}
- } catch (NoSuchMethodException e) {
- // fall thru
}
return null;
}