summaryrefslogtreecommitdiffstats
path: root/java/sca
diff options
context:
space:
mode:
authorantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-05-01 14:36:14 +0000
committerantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-05-01 14:36:14 +0000
commit4e695e520ba966d5d004d24d1f7b16140d02605e (patch)
treef0fd4e80e4192147650ee76381aa80c73339ef31 /java/sca
parent27fdbbe851e772a8772f22c94956a1fe6a0408dd (diff)
Start bringing up the spring annotation support
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@770692 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca')
-rw-r--r--java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentNameAnnotationProcessor.java7
-rw-r--r--java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentStub.java60
-rw-r--r--java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ConstructorAnnotationProcessor.java9
-rw-r--r--java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/InitDestroyAnnotationProcessor.java47
-rw-r--r--java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyAnnotationProcessor.java169
-rw-r--r--java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyValueStub.java49
-rw-r--r--java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ReferenceAnnotationProcessor.java154
-rw-r--r--java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java40
-rw-r--r--java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java12
9 files changed, 300 insertions, 247 deletions
diff --git a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentNameAnnotationProcessor.java b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentNameAnnotationProcessor.java
index 53c1988a79..1e5eb19634 100644
--- a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentNameAnnotationProcessor.java
+++ b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentNameAnnotationProcessor.java
@@ -25,6 +25,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import org.oasisopen.sca.annotation.ComponentName;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
@@ -34,13 +35,11 @@ import org.springframework.util.ReflectionUtils;
public class ComponentNameAnnotationProcessor implements BeanPostProcessor {
-// private Class<? extends Annotation> componentNameAnnotationType = ComponentName.class;
- private Class<? extends Annotation> componentNameAnnotationType;
+ private Class<? extends Annotation> componentNameAnnotationType = ComponentName.class;
private String componentName;
- public ComponentNameAnnotationProcessor (Class<? extends Annotation> componentNameAnnotationType,String componentName) {
- this.componentNameAnnotationType = componentNameAnnotationType;
+ public ComponentNameAnnotationProcessor (String componentName) {
this.componentName = componentName;
}
diff --git a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentStub.java b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentStub.java
new file mode 100644
index 0000000000..f78f28edd5
--- /dev/null
+++ b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentStub.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.implementation.spring.processor;
+
+import java.lang.reflect.Method;
+
+public class ComponentStub {
+
+ private Object tie;
+ private Method getService;
+ private Method getReference;
+
+ public ComponentStub(Object tie) {
+ this.tie = tie;
+ Class<?> tieClass = tie.getClass();
+ try {
+ getService = tieClass.getMethod("getService", new Class<?>[]{Class.class, String.class});
+ getReference = tieClass.getMethod("getReference", new Class<?>[]{Class.class, String.class});
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ public Object getService(Class<?> type, String name) {
+ try {
+
+ return getService.invoke(tie, type, name);
+
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Object getReference(Class<?> type, String name) {
+ try {
+
+ return getReference.invoke(tie, type, name);
+
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
diff --git a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ConstructorAnnotationProcessor.java b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ConstructorAnnotationProcessor.java
index bdb5c01ae2..91c89f2af1 100644
--- a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ConstructorAnnotationProcessor.java
+++ b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ConstructorAnnotationProcessor.java
@@ -28,14 +28,13 @@ import org.springframework.util.Assert;
public class ConstructorAnnotationProcessor extends InstantiationAwareBeanPostProcessorAdapter {
-// private Class<? extends Annotation> constructorAnnotationType
-// = org.oasisopen.sca.annotation.Constructor.class;
- private Class<? extends Annotation> constructorAnnotationType;
+ private Class<? extends Annotation> constructorAnnotationType
+ = org.oasisopen.sca.annotation.Constructor.class;
private Class<? extends Annotation> autowiredAnnotationType = Autowired.class;
- public ConstructorAnnotationProcessor (Class<? extends Annotation> annotation) {
- this.constructorAnnotationType = annotation;
+ public ConstructorAnnotationProcessor () {
+ // Default constructor.
}
/**
diff --git a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/InitDestroyAnnotationProcessor.java b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/InitDestroyAnnotationProcessor.java
index 80d67f578f..e05b782926 100644
--- a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/InitDestroyAnnotationProcessor.java
+++ b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/InitDestroyAnnotationProcessor.java
@@ -20,22 +20,17 @@ package org.apache.tuscany.sca.implementation.spring.processor;
import java.lang.annotation.Annotation;
+import org.oasisopen.sca.annotation.Destroy;
+import org.oasisopen.sca.annotation.Init;
import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
-public class InitDestroyAnnotationProcessor extends InitDestroyAnnotationBeanPostProcessor {
-
+public class InitDestroyAnnotationProcessor extends InitDestroyAnnotationBeanPostProcessor {
+
private static final long serialVersionUID = 0;
-
-// private Class<? extends Annotation> initAnnotationType = Init.class;
-// private Class<? extends Annotation> destroyAnnotationType = Destroy.class;
- private Class<? extends Annotation> initAnnotationType;
- private Class<? extends Annotation> destroyAnnotationType;
-
- public InitDestroyAnnotationProcessor(Class<? extends Annotation> initAnnotationType, Class<? extends Annotation> destroyAnnotationType) {
- this.initAnnotationType = initAnnotationType;
- this.destroyAnnotationType = destroyAnnotationType;
- }
-
+
+ private Class<? extends Annotation> initAnnotationType = Init.class;
+ private Class<? extends Annotation> destroyAnnotationType = Destroy.class;
+
/**
* Gets init annotation type.
*/
@@ -46,11 +41,13 @@ public class InitDestroyAnnotationProcessor extends InitDestroyAnnotationBeanPos
/**
* Sets init annotation type.
*/
- /*public void setInitAnnotationType(Class<? extends Annotation> initAnnotationType) {
- Assert.notNull(initAnnotationType, "Init annotation type must not be null.");
- this.initAnnotationType = initAnnotationType;
- }*/
-
+ /*
+ * public void setInitAnnotationType(Class<? extends Annotation>
+ * initAnnotationType) { Assert.notNull(initAnnotationType,
+ * "Init annotation type must not be null."); this.initAnnotationType =
+ * initAnnotationType; }
+ */
+
/**
* Gets destroy annotation type.
*/
@@ -61,15 +58,17 @@ public class InitDestroyAnnotationProcessor extends InitDestroyAnnotationBeanPos
/**
* Sets destroy annotation type.
*/
- /*public void setDestroyAnnotationType(Class<? extends Annotation> destroyAnnotationType) {
- Assert.notNull(destroyAnnotationType, "Destroy annotation type must not be null.");
- this.destroyAnnotationType = destroyAnnotationType;
- }*/
+ /*
+ * public void setDestroyAnnotationType(Class<? extends Annotation>
+ * destroyAnnotationType) { Assert.notNull(destroyAnnotationType,
+ * "Destroy annotation type must not be null."); this.destroyAnnotationType
+ * = destroyAnnotationType; }
+ */
- public InitDestroyAnnotationProcessor () {
+ public InitDestroyAnnotationProcessor() {
// Set the @Init annotation type
setInitAnnotationType(initAnnotationType);
-
+
// Set the @Destroy annotation type
setDestroyAnnotationType(destroyAnnotationType);
}
diff --git a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyAnnotationProcessor.java b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyAnnotationProcessor.java
index a797f8f4b9..d87ea5e4c7 100644
--- a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyAnnotationProcessor.java
+++ b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyAnnotationProcessor.java
@@ -22,28 +22,24 @@ import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import org.oasisopen.sca.annotation.Property;
+import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
-import com.sun.xml.internal.bind.v2.runtime.property.Property;
-
public class PropertyAnnotationProcessor implements BeanPostProcessor {
-// private Class<? extends Annotation> propertyAnnotationType = Property.class;
- private Class<? extends Annotation> propertyAnnotationType;
+ private Class<? extends Annotation> propertyAnnotationType = Property.class;
-// private RuntimeComponent component;
-//
-// private JavaPropertyValueObjectFactory propertyFactory;
+ private PropertyValueStub propertyValue;
- public PropertyAnnotationProcessor (Class<? extends Annotation> propertyAnnotationType) {
- this.propertyAnnotationType = propertyAnnotationType;
-// this.propertyFactory = propertyFactory;
-// this.component = component;
+ public PropertyAnnotationProcessor (PropertyValueStub propertyValue) {
+ this.propertyValue = propertyValue;
}
/**
@@ -91,87 +87,66 @@ public class PropertyAnnotationProcessor implements BeanPostProcessor {
ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
public void doWith(Method method) {
-// //Annotation annotation = method.getAnnotation(getPropertyAnnotationType());
-// Property annotation = (Property) method.getAnnotation(getPropertyAnnotationType());
-//
-// if (annotation != null) {
-// if (Modifier.isStatic(method.getModifiers())) {
-// throw new IllegalStateException("Property annotation is not supported on static methods");
-// }
-//
-// if (Modifier.isPrivate(method.getModifiers())) {
-// throw new IllegalStateException("Property annotation is not supported on private methods");
-// }
-//
-// if (method.getParameterTypes().length == 0) {
-// throw new IllegalStateException("Property annotation requires at least one argument: " + method);
-// }
-//
-// PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
-// if (pd != null) {
-// String propName = annotation.name();
-// if ("".equals(propName)) {
-// injectProperty(bean, pd, getPropertyObj(pd.getPropertyType(), pd.getName()));
-// } else {
-// injectProperty(bean, pd, getPropertyObj(pd.getPropertyType(), propName));
-// }
-// }
-// }
+
+ Property annotation = (Property) method.getAnnotation(getPropertyAnnotationType());
+
+ if (annotation != null) {
+ if (Modifier.isStatic(method.getModifiers())) {
+ throw new IllegalStateException("Property annotation is not supported on static methods");
+ }
+
+ if (Modifier.isPrivate(method.getModifiers())) {
+ throw new IllegalStateException("Property annotation is not supported on private methods");
+ }
+
+ if (method.getParameterTypes().length == 0) {
+ throw new IllegalStateException("Property annotation requires at least one argument: " + method);
+ }
+
+ PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
+ if (pd != null) {
+ String propName = annotation.name();
+ if ("".equals(propName)) {
+ injectProperty(bean, pd, propertyValue.getPropertyObj(pd.getPropertyType(), pd.getName()));
+ } else {
+ injectProperty(bean, pd, propertyValue.getPropertyObj(pd.getPropertyType(), propName));
+ }
+ }
+ }
}
});
ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() {
public void doWith(Field field) {
- //Annotation annotation = field.getAnnotation(getPropertyAnnotationType());
-// Property annotation = (Property) field.getAnnotation(getPropertyAnnotationType());
-//
-// if (annotation != null) {
-// if (Modifier.isStatic(field.getModifiers())) {
-// throw new IllegalStateException("Property annotation is not supported on static fields");
-// }
-//
-// if (Modifier.isPrivate(field.getModifiers())) {
-// throw new IllegalStateException("Property annotation is not supported on private fields");
-// }
-//
-// ReflectionUtils.makeAccessible(field);
-//
-// Object propertyObj = null;
-// String propName = annotation.name();
-// if ("".equals(propName)) {
-// propertyObj = getPropertyObj(field.getType(), field.getName());
-// } else {
-// propertyObj = getPropertyObj(field.getType(), propName);
-// }
-//
-// if (propertyObj != null)
-// ReflectionUtils.setField(field, bean, propertyObj);
-// }
+
+ Property annotation = (Property) field.getAnnotation(getPropertyAnnotationType());
+
+ if (annotation != null) {
+ if (Modifier.isStatic(field.getModifiers())) {
+ throw new IllegalStateException("Property annotation is not supported on static fields");
+ }
+
+ if (Modifier.isPrivate(field.getModifiers())) {
+ throw new IllegalStateException("Property annotation is not supported on private fields");
+ }
+
+ ReflectionUtils.makeAccessible(field);
+
+ Object propertyObj = null;
+ String propName = annotation.name();
+ if ("".equals(propName)) {
+ propertyObj = propertyValue.getPropertyObj(field.getType(), field.getName());
+ } else {
+ propertyObj = propertyValue.getPropertyObj(field.getType(), propName);
+ }
+
+ if (propertyObj != null)
+ ReflectionUtils.setField(field, bean, propertyObj);
+ }
}
});
}
- /**
- * Processes a property descriptor to inject a service.
- */
- public Object getPropertyObj(Class requiredType, String name) {
-
- Object propertyObj = null;
-
-// List<ComponentProperty> props = component.getProperties();
-// for (ComponentProperty prop : props) {
-// if (prop.getName().equals(name)) {
-// // On finding the property, create a factory for it and create a Bean using
-// // the factory
-// ObjectFactory factory = propertyFactory.createValueFactory(prop, prop.getValue(), requiredType);
-// propertyObj = factory.getInstance();
-// } // end if
-// } // end for
-
- return propertyObj;
- }
-
-
public void injectProperty(Object bean, PropertyDescriptor pd, Object propertyObj) {
if (propertyObj != null) {
@@ -183,32 +158,4 @@ public class PropertyAnnotationProcessor implements BeanPostProcessor {
}
}
- /**
- * Processes a property descriptor to inject a service.
- */
- /*public void injectMethod(Object bean, Method method) {
- PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
-
- if (pd != null) {
- Object propertyObj = null;
-
- List<ComponentProperty> props = component.getProperties();
- for (ComponentProperty prop : props) {
- if (prop.getName().equals(pd.getName())) {
- // On finding the property, create a factory for it and create a Bean using
- // the factory
- ObjectFactory factory = propertyFactory.createValueFactory(prop, prop.getValue(), pd.getPropertyType());
- propertyObj = factory.getInstance();
- } // end if
- } // end for
-
- if (propertyObj != null) {
- try {
- pd.getWriteMethod().invoke(bean, new Object[] { propertyObj });
- } catch (Throwable e) {
- throw new FatalBeanException("Problem injecting property: " + e.getMessage(), e);
- }
- }
- }
- }*/
}
diff --git a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyValueStub.java b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyValueStub.java
new file mode 100644
index 0000000000..59ffd44e64
--- /dev/null
+++ b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyValueStub.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.implementation.spring.processor;
+
+import java.lang.reflect.Method;
+
+public class PropertyValueStub {
+
+ private Object tie;
+ private Method getPropertyObj;
+
+ public PropertyValueStub(Object tie) {
+ this.tie = tie;
+ Class<?> tieClass = tie.getClass();
+ try {
+ getPropertyObj = tieClass.getMethod("getPropertyObj", new Class<?>[]{Class.class, String.class});
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Object getPropertyObj(Class<?> propertyType, String name) {
+ try {
+
+ return getPropertyObj.invoke(tie, propertyType, name);
+
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
diff --git a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ReferenceAnnotationProcessor.java b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ReferenceAnnotationProcessor.java
index e1aab78ff5..d86d074a54 100644
--- a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ReferenceAnnotationProcessor.java
+++ b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ReferenceAnnotationProcessor.java
@@ -22,9 +22,10 @@ import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
-import javax.naming.Reference;
-
+import org.oasisopen.sca.annotation.Reference;
+import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.config.BeanPostProcessor;
@@ -33,13 +34,11 @@ import org.springframework.util.ReflectionUtils;
public class ReferenceAnnotationProcessor implements BeanPostProcessor {
-// private Class<? extends Annotation> referenceAnnotationType = Reference.class;
- private Class<? extends Annotation> referenceAnnotationType;
-
-// private RuntimeComponent component;
+ private Class<? extends Annotation> referenceAnnotationType = Reference.class;
+ private ComponentStub component;
- public ReferenceAnnotationProcessor (Class<? extends Annotation> referenceAnnotationType, Object component) {
- this.referenceAnnotationType = referenceAnnotationType;
+ public ReferenceAnnotationProcessor (ComponentStub component) {
+ this.component = component;
}
/**
@@ -87,98 +86,79 @@ public class ReferenceAnnotationProcessor implements BeanPostProcessor {
ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
public void doWith(Method method) {
- //Annotation annotation = method.getAnnotation(getReferenceAnnotationType());
-// Reference annotation = (Reference) method.getAnnotation(getReferenceAnnotationType());
-//
-// if (annotation != null) {
-// if (Modifier.isStatic(method.getModifiers())) {
-// throw new IllegalStateException("Reference annotation is not supported on static methods");
-// }
-//
-// if (Modifier.isPrivate(method.getModifiers())) {
-// throw new IllegalStateException("Reference annotation is not supported on private methods");
-// }
-//
-// if (method.getParameterTypes().length == 0) {
-// throw new IllegalStateException("Reference annotation requires at least one argument: " + method);
-// }
-//
-// PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
-// if (pd != null) {
-// String refName = annotation.name();
-// if ("".equals(refName)) {
-// injectReference(bean, pd, pd.getName());
-// } else {
-// injectReference(bean, pd, refName);
-// }
-// }
-// }
+
+ Reference annotation = (Reference) method.getAnnotation(getReferenceAnnotationType());
+
+ if (annotation != null) {
+ if (Modifier.isStatic(method.getModifiers())) {
+ throw new IllegalStateException("Reference annotation is not supported on static methods");
+ }
+
+ if (Modifier.isPrivate(method.getModifiers())) {
+ throw new IllegalStateException("Reference annotation is not supported on private methods");
+ }
+
+ if (method.getParameterTypes().length == 0) {
+ throw new IllegalStateException("Reference annotation requires at least one argument: " + method);
+ }
+
+ PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
+ if (pd != null) {
+ String refName = annotation.name();
+ if ("".equals(refName)) {
+ injectReference(bean, pd, pd.getName());
+ } else {
+ injectReference(bean, pd, refName);
+ }
+ }
+ }
}
});
ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() {
public void doWith(Field field) {
- //Annotation annotation = field.getAnnotation(getReferenceAnnotationType());
-// Reference annotation = (Reference) field.getAnnotation(getReferenceAnnotationType());
-//
-// if (annotation != null) {
-// if (Modifier.isStatic(field.getModifiers())) {
-// throw new IllegalStateException("Reference annotation is not supported on static fields");
-// }
-//
-// if (Modifier.isPrivate(field.getModifiers())) {
-// throw new IllegalStateException("Reference annotation is not supported on private fields");
-// }
-//
-// ReflectionUtils.makeAccessible(field);
-//
-// Object referenceObj = null;
-// String refName = annotation.name();
-// if ("".equals(refName)) {
-// referenceObj = component.getComponentContext().getService(field.getType(), field.getName());
-// } else {
-// referenceObj = component.getComponentContext().getService(field.getType(), refName);
-// }
-//
-// if (referenceObj != null)
-// ReflectionUtils.setField(field, bean, referenceObj);
-// }
+
+ Reference annotation = (Reference) field.getAnnotation(getReferenceAnnotationType());
+
+ if (annotation != null) {
+ if (Modifier.isStatic(field.getModifiers())) {
+ throw new IllegalStateException("Reference annotation is not supported on static fields");
+ }
+
+ if (Modifier.isPrivate(field.getModifiers())) {
+ throw new IllegalStateException("Reference annotation is not supported on private fields");
+ }
+
+ ReflectionUtils.makeAccessible(field);
+
+ Object referenceObj = null;
+ String refName = annotation.name();
+ if ("".equals(refName)) {
+ referenceObj = component.getService(field.getType(), field.getName());
+ } else {
+ referenceObj = component.getService(field.getType(), refName);
+ }
+
+ if (referenceObj != null)
+ ReflectionUtils.setField(field, bean, referenceObj);
+ }
}
});
}
-
+
/**
* Processes a property descriptor to inject a service.
*/
public void injectReference(Object bean, PropertyDescriptor pd, String name) {
-// Object referenceObj = component.getComponentContext().getService(pd.getPropertyType(), name);
-//
-// if (referenceObj != null) {
-// try {
-// pd.getWriteMethod().invoke(bean, new Object[] { referenceObj });
-// } catch (Throwable e) {
-// throw new FatalBeanException("Problem injecting reference: " + e.getMessage(), e);
-// }
-// }
- }
-
- /**
- * Processes a property descriptor to inject a service.
- */
- /*public void injectMethod(Object bean, Method method) {
- PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
-
- if (pd != null) {
- Object referenceObj = component.getComponentContext().getService(pd.getPropertyType(), pd.getName());
-
- if (referenceObj != null) {
- try {
- pd.getWriteMethod().invoke(bean, new Object[] { referenceObj });
- } catch (Throwable e) {
- throw new FatalBeanException("Problem injecting reference: " + e.getMessage(), e);
- }
+ Object referenceObj = component.getReference(pd.getPropertyType(), name);
+
+ if (referenceObj != null) {
+ try {
+ pd.getWriteMethod().invoke(bean, new Object[] { referenceObj });
+ } catch (Throwable e) {
+ throw new FatalBeanException("Problem injecting reference: " + e.getMessage(), e);
}
}
- }*/
+ }
}
diff --git a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java
index 18d04a56f1..b12a851dec 100644
--- a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java
+++ b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java
@@ -23,8 +23,13 @@ import java.net.URL;
import java.util.Iterator;
import java.util.List;
+import org.apache.tuscany.sca.implementation.spring.processor.ComponentNameAnnotationProcessor;
+import org.apache.tuscany.sca.implementation.spring.processor.ComponentStub;
import org.apache.tuscany.sca.implementation.spring.processor.ConstructorAnnotationProcessor;
import org.apache.tuscany.sca.implementation.spring.processor.InitDestroyAnnotationProcessor;
+import org.apache.tuscany.sca.implementation.spring.processor.PropertyAnnotationProcessor;
+import org.apache.tuscany.sca.implementation.spring.processor.PropertyValueStub;
+import org.apache.tuscany.sca.implementation.spring.processor.ReferenceAnnotationProcessor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -47,10 +52,12 @@ import org.springframework.core.io.UrlResource;
public class SpringContextTie {
private AbstractApplicationContext springContext;
+ private SpringImplementationStub implementation;
public SpringContextTie(SpringImplementationStub implementation, URL resource) {
SCAParentApplicationContext scaParentContext = new SCAParentApplicationContext(implementation);
- springContext = createApplicationContext(scaParentContext, resource);
+ springContext = createApplicationContext(scaParentContext, resource);
+ this.implementation = implementation;
}
public void start() {
@@ -107,11 +114,11 @@ public class SpringContextTie {
if (beanClassName.indexOf(".ClassPathXmlApplicationContext") != -1) {
appContext = new ClassPathXmlApplicationContext(listValues, false, scaParentContext);
- //includeAnnotationProcessors(appContext.getBeanFactory());
+ includeAnnotationProcessors(appContext.getBeanFactory());
return appContext;
} else {
appContext = new FileSystemXmlApplicationContext(listValues, false, scaParentContext);
- //includeAnnotationProcessors(appContext.getBeanFactory());
+ includeAnnotationProcessors(appContext.getBeanFactory());
return appContext;
}
}
@@ -136,25 +143,26 @@ public class SpringContextTie {
private void includeAnnotationProcessors(ConfigurableListableBeanFactory beanFactory) {
// Processor to deal with @Init and @Destroy SCA Annotations
-// BeanPostProcessor initDestroyProcessor = new InitDestroyAnnotationProcessor();
-// beanFactory.addBeanPostProcessor(initDestroyProcessor);
+ BeanPostProcessor initDestroyProcessor = new InitDestroyAnnotationProcessor();
+ beanFactory.addBeanPostProcessor(initDestroyProcessor);
-// TODO: implement passing the component and property factory
-// // Processor to deal with @Reference SCA Annotations
+ ComponentStub component = null; //TODO
+ // Processor to deal with @Reference SCA Annotations
// BeanPostProcessor referenceProcessor = new ReferenceAnnotationProcessor(component);
// beanFactory.addBeanPostProcessor(referenceProcessor);
-//
-// // Processor to deal with @Property SCA Annotations
-// BeanPostProcessor propertyProcessor = new PropertyAnnotationProcessor(propertyValueObjectFactory, component);
+
+ PropertyValueStub pvs = null; //TODO
+ // Processor to deal with @Property SCA Annotations
+// BeanPostProcessor propertyProcessor = new PropertyAnnotationProcessor(pvs);
// beanFactory.addBeanPostProcessor(propertyProcessor);
-//
-// // Processor to deal with @ComponentName SCA Annotations
-// BeanPostProcessor componentNameProcessor = new ComponentNameAnnotationProcessor(component);
-// beanFactory.addBeanPostProcessor(componentNameProcessor);
+
+ // Processor to deal with @ComponentName SCA Annotations
+ BeanPostProcessor componentNameProcessor = new ComponentNameAnnotationProcessor(implementation.getComponentName());
+ beanFactory.addBeanPostProcessor(componentNameProcessor);
// Processor to deal with @Constructor SCA Annotations
-// BeanPostProcessor constructorProcessor = new ConstructorAnnotationProcessor();
-// beanFactory.addBeanPostProcessor(constructorProcessor);
+ BeanPostProcessor constructorProcessor = new ConstructorAnnotationProcessor();
+ beanFactory.addBeanPostProcessor(constructorProcessor);
}
}
diff --git a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java
index 28d40453e9..57948938e3 100644
--- a/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java
+++ b/java/sca/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java
@@ -36,6 +36,7 @@ public class SpringImplementationStub {
Object tie;
Method getURI;
Method getBean;
+ Method getComponentName;
public SpringImplementationStub(Object tie) {
this.tie = tie;
@@ -43,6 +44,7 @@ public class SpringImplementationStub {
try {
getURI = tieClass.getMethod("getURI", new Class<?>[]{});
getBean = tieClass.getMethod("getBean", new Class<?>[]{String.class, Class.class});
+ getComponentName = tieClass.getMethod("getComponentName");
} catch (Exception e) {
throw new RuntimeException(e);
}
@@ -77,6 +79,16 @@ public class SpringImplementationStub {
} catch (Exception e) {
throw new RuntimeException(e);
}
+ }
+
+ public String getComponentName() {
+ try {
+
+ return (String)getComponentName.invoke(tie);
+
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
}
}