diff options
author | ramkumar <ramkumar@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-28 10:23:01 +0000 |
---|---|---|
committer | ramkumar <ramkumar@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-28 10:23:01 +0000 |
commit | ee2e5557540acf796dcfb13533be69ea85f55dd6 (patch) | |
tree | 411cbc40c15e8377e40a5b32cf3a0e64ad8c82f1 /java/sca/modules/implementation-spring | |
parent | 639513a345113497a52f703c62a0c16dbdf97522 (diff) |
Refactoring Spring Modules
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@769323 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/implementation-spring')
9 files changed, 277 insertions, 748 deletions
diff --git a/java/sca/modules/implementation-spring/pom.xml b/java/sca/modules/implementation-spring/pom.xml index 5908e77720..2a270e8e5b 100644 --- a/java/sca/modules/implementation-spring/pom.xml +++ b/java/sca/modules/implementation-spring/pom.xml @@ -59,6 +59,11 @@ <artifactId>tuscany-implementation-java-runtime</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-implementation-spring-runtime</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
<!-- Spring dependencies... -->
<dependency>
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentNameAnnotationProcessor.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentNameAnnotationProcessor.java deleted file mode 100644 index 6e0987791b..0000000000 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentNameAnnotationProcessor.java +++ /dev/null @@ -1,151 +0,0 @@ -/*
- * 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.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.beans.PropertyDescriptor;
-import java.lang.annotation.Annotation;
-
-import org.springframework.util.Assert;
-import org.springframework.beans.BeanUtils;
-import org.springframework.util.ReflectionUtils;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.FatalBeanException;
-import org.springframework.beans.factory.config.BeanPostProcessor;
-
-import org.oasisopen.sca.annotation.ComponentName;
-import org.apache.tuscany.sca.runtime.RuntimeComponent;
-
-public class ComponentNameAnnotationProcessor implements BeanPostProcessor {
-
- private Class<? extends Annotation> componentNameAnnotationType = ComponentName.class;
-
- private RuntimeComponent component;
-
- public ComponentNameAnnotationProcessor (RuntimeComponent component) {
- this.component = component;
- }
-
- /**
- * Gets componentName annotation type.
- */
- protected Class<? extends Annotation> getComponentNameAnnotationType() {
- return this.componentNameAnnotationType;
- }
-
- /**
- * Sets componentName annotation type.
- */
- public void setComponentNameAnnotationType(Class<? extends Annotation> componentNameAnnotationType) {
- Assert.notNull(componentNameAnnotationType, "'componentNameAnnotationType' type must not be null.");
- this.componentNameAnnotationType = componentNameAnnotationType;
- }
-
- /**
- * This method is used to execute before a bean's initialization callback.
- *
- * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
- */
- public Object postProcessBeforeInitialization(Object bean, String beanName)
- throws BeansException {
- processAnnotation(bean);
- return bean;
- }
-
- /**
- * This method is used to execute after a bean's initialization callback.
- *
- * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
- */
- public Object postProcessAfterInitialization(Object bean, String beanName)
- throws BeansException {
- return bean;
- }
-
- /**
- * <p>Processes a beans fields for injection if it has a {@link Reference} annotation.</p>
- */
- protected void processAnnotation(final Object bean) {
-
- final Class<?> clazz = bean.getClass();
-
- ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() {
- public void doWith(Field field) {
- Annotation annotation = field.getAnnotation(getComponentNameAnnotationType());
-
- if (annotation != null) {
- if (Modifier.isStatic(field.getModifiers())) {
- throw new IllegalStateException("ComponentName annotation is not supported on static fields");
- }
-
- if (Modifier.isPrivate(field.getModifiers())) {
- throw new IllegalStateException("ComponentName annotation is not supported on private fields");
- }
-
- ReflectionUtils.makeAccessible(field);
-
- if (field.getType().getName().equals("java.lang.String")) {
- Object nameObj = component.getName();
- if (nameObj != null)
- ReflectionUtils.setField(field, bean, nameObj);
- } else {
- throw new IllegalStateException("ComponentName annotation is supported only on java.lang.String field type.");
- }
- }
- }
- });
-
- ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
- public void doWith(Method method) {
- Annotation annotation = method.getAnnotation(getComponentNameAnnotationType());
-
- if (annotation != null) {
- if (Modifier.isStatic(method.getModifiers())) {
- throw new IllegalStateException("ComponentName annotation is not supported on static methods");
- }
-
- if (Modifier.isPrivate(method.getModifiers())) {
- throw new IllegalStateException("ComponentName annotation is not supported on private methods");
- }
-
- if (method.getParameterTypes().length == 0) {
- throw new IllegalStateException("ComponentName annotation requires at least one argument: " + method);
- }
-
- PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
-
- if (pd.getPropertyType().getName().equals("java.lang.String")) {
- Object nameObj = component.getName();
- if (nameObj != null) {
- try {
- pd.getWriteMethod().invoke(bean, new Object[] { nameObj });
- } catch (Throwable e) {
- throw new FatalBeanException("Problem injecting reference: " + e.getMessage(), e);
- }
- }
- } else {
- throw new IllegalStateException("ComponentName annotation is supported only on java.lang.String field type.");
- }
- }
- }
- });
- }
-}
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ConstructorAnnotationProcessor.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ConstructorAnnotationProcessor.java deleted file mode 100644 index 689ac27f0f..0000000000 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ConstructorAnnotationProcessor.java +++ /dev/null @@ -1,119 +0,0 @@ -/*
- * 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;
-import java.lang.reflect.Modifier;
-import java.lang.reflect.Constructor;
-import java.lang.annotation.Annotation;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.springframework.util.Assert;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
-import org.springframework.beans.factory.annotation.Autowired;
-
-public class ConstructorAnnotationProcessor extends InstantiationAwareBeanPostProcessorAdapter {
-
- private Class<? extends Annotation> constructorAnnotationType
- = org.oasisopen.sca.annotation.Constructor.class;
-
- private Class<? extends Annotation> autowiredAnnotationType = Autowired.class;
-
- public ConstructorAnnotationProcessor () {
- // Default constructor.
- }
-
- /**
- * Set the 'autowired' annotation type, to be used on constructors, fields,
- * setter methods and arbitrary config methods.
- */
- public void setAutowiredAnnotationType(Class<? extends Annotation> autowiredAnnotationType) {
- Assert.notNull(autowiredAnnotationType, "'autowiredAnnotationType' must not be null");
- this.autowiredAnnotationType = autowiredAnnotationType;
- }
-
- /**
- * Return the 'autowired' annotation type.
- */
- protected Class<? extends Annotation> getAutowiredAnnotationType() {
- return this.autowiredAnnotationType;
- }
-
- /**
- * Return the 'constructor' annotation type.
- */
- protected Class<? extends Annotation> getConstructorAnnotationType() {
- return this.constructorAnnotationType;
- }
-
- /**
- * Sets the 'constructor' annotation type.
- */
- public void setConstructorAnnotationType(Class<? extends Annotation> constructorAnnotationType) {
- Assert.notNull(constructorAnnotationType, "'constructorAnnotationType' type must not be null.");
- this.constructorAnnotationType = constructorAnnotationType;
- }
-
- /**
- * This method is used to execute before a bean's initialization callback.
- *
- * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
- */
- public Object postProcessBeforeInitialization(Object bean, String beanName)
- throws BeansException {
- return bean;
- }
-
- /**
- * This method is used to execute after a bean's initialization callback.
- *
- * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
- */
- public Object postProcessAfterInitialization(Object bean, String beanName)
- throws BeansException {
- return bean;
- }
-
- public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
- /*Constructor[] declaredConstructors = beanClass.getDeclaredConstructors();
- Method[] declaredMethods = beanClass.getDeclaredMethods();
- List candidates = new ArrayList(declaredConstructors.length);
-
- for (int i = 0; i < declaredMethods.length; i++) {
- Method method = declaredMethods[i];
- Annotation annotation = method.getAnnotation(getConstructorAnnotationType());
- if (annotation != null) {
- if (Modifier.isStatic(method.getModifiers())) {
- throw new IllegalStateException("Constructor annotation is not supported on static methods");
- }
-
- if (candidates.size() == 1) {
- throw new IllegalStateException("Only one method is allowed to have constructor annotation in a bean: " + method);
- }
-
- candidates.add(method);
- }
- }
-
- return (Constructor[]) candidates.toArray(new Constructor[candidates.size()]);*/
- return null;
- }
-}
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/InitDestroyAnnotationProcessor.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/InitDestroyAnnotationProcessor.java deleted file mode 100644 index 129a15e72e..0000000000 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/InitDestroyAnnotationProcessor.java +++ /dev/null @@ -1,72 +0,0 @@ -/*
- * 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.annotation.Annotation;
-import org.oasisopen.sca.annotation.Init;
-import org.oasisopen.sca.annotation.Destroy;
-import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
-import org.springframework.util.Assert;
-
-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;
-
- /**
- * Gets init annotation type.
- */
- protected Class<? extends Annotation> getInitAnnotationType() {
- return this.initAnnotationType;
- }
-
- /**
- * Sets init annotation type.
- */
- /*public void setInitAnnotationType(Class<? extends Annotation> initAnnotationType) {
- Assert.notNull(initAnnotationType, "Init annotation type must not be null.");
- this.initAnnotationType = initAnnotationType;
- }*/
-
- /**
- * Gets destroy annotation type.
- */
- protected Class<? extends Annotation> getDestroyAnnotationType() {
- return this.destroyAnnotationType;
- }
-
- /**
- * 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 InitDestroyAnnotationProcessor () {
- // Set the @Init annotation type
- setInitAnnotationType(initAnnotationType);
-
- // Set the @Destroy annotation type
- setDestroyAnnotationType(destroyAnnotationType);
- }
-}
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyAnnotationProcessor.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyAnnotationProcessor.java deleted file mode 100644 index fbc0e2b36f..0000000000 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyAnnotationProcessor.java +++ /dev/null @@ -1,220 +0,0 @@ -/*
- * 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.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.beans.PropertyDescriptor;
-import java.lang.annotation.Annotation;
-import java.util.List;
-
-import org.springframework.util.Assert;
-import org.springframework.beans.BeanUtils;
-import org.springframework.util.ReflectionUtils;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.FatalBeanException;
-import org.springframework.beans.factory.config.BeanPostProcessor;
-
-import org.oasisopen.sca.annotation.Property;
-import org.apache.tuscany.sca.assembly.ComponentProperty;
-import org.apache.tuscany.sca.core.factory.ObjectFactory;
-import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory;
-import org.apache.tuscany.sca.runtime.RuntimeComponent;
-
-public class PropertyAnnotationProcessor implements BeanPostProcessor {
-
- private Class<? extends Annotation> propertyAnnotationType = Property.class;
-
- private RuntimeComponent component;
-
- private JavaPropertyValueObjectFactory propertyFactory;
-
- public PropertyAnnotationProcessor (JavaPropertyValueObjectFactory propertyFactory,
- RuntimeComponent component) {
- this.propertyFactory = propertyFactory;
- this.component = component;
- }
-
- /**
- * Gets property annotation type.
- */
- protected Class<? extends Annotation> getPropertyAnnotationType() {
- return this.propertyAnnotationType;
- }
-
- /**
- * Sets property annotation type.
- */
- public void setPropertyAnnotationType(Class<? extends Annotation> propertyAnnotationType) {
- Assert.notNull(propertyAnnotationType, "'propertyAnnotationType' type must not be null.");
- this.propertyAnnotationType = propertyAnnotationType;
- }
-
- /**
- * This method is used to execute before a bean's initialization callback.
- *
- * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
- */
- public Object postProcessBeforeInitialization(Object bean, String beanName)
- throws BeansException {
- processAnnotation(bean);
- return bean;
- }
-
- /**
- * This method is used to execute after a bean's initialization callback.
- *
- * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
- */
- public Object postProcessAfterInitialization(Object bean, String beanName)
- throws BeansException {
- return bean;
- }
-
- /**
- * <p>Processes a beans fields for injection if it has a {@link Property} annotation.</p>
- */
- protected void processAnnotation(final Object bean) {
-
- final Class<?> clazz = bean.getClass();
-
- 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));
- }
- }
- }
- }
- });
-
- 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);
- }
- }
- });
- }
-
- /**
- * 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) {
- try {
- pd.getWriteMethod().invoke(bean, new Object[] { propertyObj });
- } catch (Throwable e) {
- throw new FatalBeanException("Problem injecting property: " + 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 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/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ReferenceAnnotationProcessor.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ReferenceAnnotationProcessor.java deleted file mode 100644 index 48c727baab..0000000000 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ReferenceAnnotationProcessor.java +++ /dev/null @@ -1,186 +0,0 @@ -/*
- * 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.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.beans.PropertyDescriptor;
-import java.lang.annotation.Annotation;
-
-import org.springframework.util.Assert;
-import org.springframework.beans.BeanUtils;
-import org.springframework.util.ReflectionUtils;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.FatalBeanException;
-import org.springframework.beans.factory.config.BeanPostProcessor;
-
-import org.oasisopen.sca.annotation.Reference;
-import org.apache.tuscany.sca.runtime.RuntimeComponent;
-
-public class ReferenceAnnotationProcessor implements BeanPostProcessor {
-
- private Class<? extends Annotation> referenceAnnotationType = Reference.class;
-
- private RuntimeComponent component;
-
- public ReferenceAnnotationProcessor (RuntimeComponent component) {
- this.component = component;
- }
-
- /**
- * Gets referece annotation type.
- */
- protected Class<? extends Annotation> getReferenceAnnotationType() {
- return this.referenceAnnotationType;
- }
-
- /**
- * Sets referece annotation type.
- */
- public void setReferenceAnnotationType(Class<? extends Annotation> referenceAnnotationType) {
- Assert.notNull(referenceAnnotationType, "'referenceAnnotationType' type must not be null.");
- this.referenceAnnotationType = referenceAnnotationType;
- }
-
- /**
- * This method is used to execute before a bean's initialization callback.
- *
- * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
- */
- public Object postProcessBeforeInitialization(Object bean, String beanName)
- throws BeansException {
- processAnnotation(bean);
- return bean;
- }
-
- /**
- * This method is used to execute after a bean's initialization callback.
- *
- * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
- */
- public Object postProcessAfterInitialization(Object bean, String beanName)
- throws BeansException {
- return bean;
- }
-
- /**
- * <p>Processes a beans fields for injection if it has a {@link Reference} annotation.</p>
- */
- protected void processAnnotation(final Object bean) {
-
- final Class<?> clazz = bean.getClass();
-
- 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);
- }
- }
- }
- }
- });
-
- 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);
- }
- }
- });
- }
-
- /**
- * 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);
- }
- }
- }
- }*/
-}
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java new file mode 100644 index 0000000000..f461667b91 --- /dev/null +++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java @@ -0,0 +1,230 @@ +/*
+ * 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.xml;
+
+import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.ComponentType;
+import org.apache.tuscany.sca.assembly.xml.Constants;
+import org.apache.tuscany.sca.assembly.xml.PolicySubjectProcessor;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
+import org.apache.tuscany.sca.contribution.processor.ContributionResolveException;
+import org.apache.tuscany.sca.contribution.processor.ContributionWriteException;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
+import org.apache.tuscany.sca.monitor.Monitor;
+import org.apache.tuscany.sca.monitor.Problem;
+import org.apache.tuscany.sca.monitor.Problem.Severity;
+import org.apache.tuscany.sca.policy.PolicyFactory;
+import org.apache.tuscany.sca.implementation.spring.introspect.SpringXMLComponentTypeLoader;
+import org.apache.tuscany.sca.implementation.spring.SpringImplementation;
+
+/**
+ * SpringArtifactProcessor is responsible for processing the XML of an <implementation.spring.../>
+ * element in an SCA SCDL file.
+ *
+ * @version $Rev: 511195 $ $Date: 2007-02-24 02:29:46 +0000 (Sat, 24 Feb 2007) $
+ */
+public class SpringImplementationProcessor implements StAXArtifactProcessor<SpringImplementation> {
+
+ private static final String LOCATION = "location";
+ private static final String IMPLEMENTATION_SPRING = "implementation.spring";
+ private static final QName IMPLEMENTATION_SPRING_QNAME = new QName(Constants.SCA11_NS, IMPLEMENTATION_SPRING);
+ private static final String MSG_LOCATION_MISSING = "Reading implementation.spring - location attribute missing";
+
+ private AssemblyFactory assemblyFactory;
+ private JavaInterfaceFactory javaFactory;
+ private PolicyFactory policyFactory;
+ private PolicySubjectProcessor policyProcessor;
+ private Monitor monitor;
+
+ public SpringImplementationProcessor(FactoryExtensionPoint modelFactories, Monitor monitor) {
+ this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class);
+ this.javaFactory = modelFactories.getFactory(JavaInterfaceFactory.class);
+ this.policyFactory = modelFactories.getFactory(PolicyFactory.class);
+ this.policyProcessor = new PolicySubjectProcessor(policyFactory);
+ this.monitor = monitor;
+ }
+
+ /**
+ * Report a exception.
+ *
+ * @param problems
+ * @param message
+ * @param model
+ */
+ private void error(String message, Object model, Exception ex) {
+ if (monitor != null) {
+ Problem problem = monitor.createProblem(this.getClass().getName(), "impl-spring-validation-messages", Severity.ERROR, model, message, ex);
+ monitor.problem(problem);
+ }
+ }
+
+ /**
+ * Report a error.
+ *
+ * @param problems
+ * @param message
+ * @param model
+ */
+ private void error(String message, Object model, Object... messageParameters) {
+ if (monitor != null) {
+ Problem problem = monitor.createProblem(this.getClass().getName(), "impl-spring-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters);
+ monitor.problem(problem);
+ }
+ }
+
+ /*
+ * Read the XML and parse out the attributes.
+ *
+ * <implementation.spring.../> has a single required attribute:
+ * "location" - which is the target URI of of an archive file or a directory that contains the Spring
+ * application context files.
+ * If the resource identified by the location attribute is an archive file, then the file
+ * META-INF/MANIFEST.MF is read from the archive.
+ * If the location URI identifies a directory, then META-INF/MANIFEST.MF must exist
+ * underneath that directory.
+ * If the manifest file contains a header "Spring-Context" of the format:
+ * Spring-Context ::= path ( ';' path )*
+ *
+ * Where path is a relative path with respect to the location URI, then the set of paths
+ * specified in the header identify the context configuration files.
+ * If there is no MANIFEST.MF file or no Spring-Context header within that file,
+ * then the default behaviour is to build an application context using all the *.xml files
+ * in the METAINF/spring directory.
+ */
+ public SpringImplementation read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
+
+ // Create the Spring implementation
+ SpringImplementation springImplementation = null;
+
+ // Read the location attribute for the spring implementation
+ String springLocation = reader.getAttributeValue(null, LOCATION);
+ if (springLocation != null) {
+ springImplementation = new SpringImplementation();
+ springImplementation.setLocation(springLocation);
+ springImplementation.setUnresolved(true);
+ processComponentType(springImplementation);
+ } else {
+ error("LocationAttributeMissing", reader);
+ //throw new ContributionReadException(MSG_LOCATION_MISSING);
+ }
+
+ // Read policies
+ policyProcessor.readPolicies(springImplementation, reader);
+
+ // Skip to end element
+ while (reader.hasNext()) {
+ if (reader.next() == END_ELEMENT && IMPLEMENTATION_SPRING_QNAME.equals(reader.getName())) {
+ break;
+ }
+ } // end while
+
+ return springImplementation;
+ } // end read
+
+ /*
+ * Handles the component type for the Spring implementation
+ * @param springImplementation - a Spring implementation. The component type information
+ * is created for this implementation
+ *
+ */
+ private void processComponentType(SpringImplementation springImplementation) {
+
+ // Create a ComponentType and mark it unresolved
+ ComponentType componentType = assemblyFactory.createComponentType();
+ componentType.setUnresolved(true);
+ springImplementation.setComponentType(componentType);
+ } // end processComponentType
+
+ /*
+ * Write out the XML representation of the Spring implementation
+ * <implementation.spring location="..." />
+ */
+ public void write(SpringImplementation springImplementation, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException {
+
+ // Write <implementation.spring>
+ policyProcessor.writePolicyPrefixes(springImplementation, writer);
+ writer.writeStartElement(Constants.SCA11_NS, IMPLEMENTATION_SPRING);
+ policyProcessor.writePolicyAttributes(springImplementation, writer);
+
+ if (springImplementation.getLocation() != null) {
+ writer.writeAttribute(LOCATION, springImplementation.getLocation());
+ }
+
+ writer.writeEndElement();
+
+ } // end write
+
+ /**
+ * Resolves the Spring implementation - loads the Spring application-context XML and
+ * derives the spring implementation componentType from it
+ */
+ public void resolve(SpringImplementation springImplementation, ModelResolver resolver)
+ throws ContributionResolveException {
+
+ if (springImplementation == null)
+ return;
+
+ /* Load the Spring component type by reading the Spring application context */
+ SpringXMLComponentTypeLoader springLoader =
+ new SpringXMLComponentTypeLoader(assemblyFactory, javaFactory, policyFactory);
+ try {
+ // Load the Spring Implementation information from its application context file...
+ springLoader.load(springImplementation);
+ } catch (ContributionReadException e) {
+ ContributionResolveException ce = new ContributionResolveException(e);
+ error("ContributionResolveException", resolver, ce);
+ throw ce;
+ }
+
+ ComponentType ct = springImplementation.getComponentType();
+ if (ct.isUnresolved()) {
+ // If the introspection fails to resolve, try to find a side file...
+ ComponentType componentType = resolver.resolveModel(ComponentType.class, ct);
+ if (componentType.isUnresolved()) {
+ error("UnableToResolveComponentType", resolver);
+ //throw new ContributionResolveException("SpringArtifactProcessor: unable to resolve componentType for Spring component");
+ } else {
+ springImplementation.setComponentType(componentType);
+ springImplementation.setUnresolved(false);
+ }
+
+ } // end if
+
+ } // end method resolve
+
+ public QName getArtifactType() {
+ return IMPLEMENTATION_SPRING_QNAME;
+ }
+
+ public Class<SpringImplementation> getModelType() {
+ return SpringImplementation.class;
+ }
+
+} // end class SpringArtifactProcessor
diff --git a/java/sca/modules/implementation-spring/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/java/sca/modules/implementation-spring/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor new file mode 100644 index 0000000000..52fe975f14 --- /dev/null +++ b/java/sca/modules/implementation-spring/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -0,0 +1,19 @@ +# 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.
+
+# Implementation class for the artifact processor extension
+org.apache.tuscany.sca.implementation.spring.xml.SpringImplementationProcessor;qname=http://docs.oasis-open.org/ns/opencsa/sca/200903#implementation.spring,model=org.apache.tuscany.sca.implementation.spring.SpringImplementation
diff --git a/java/sca/modules/implementation-spring/src/main/resources/impl-spring-validation-messages.properties b/java/sca/modules/implementation-spring/src/main/resources/impl-spring-validation-messages.properties new file mode 100644 index 0000000000..a348225d31 --- /dev/null +++ b/java/sca/modules/implementation-spring/src/main/resources/impl-spring-validation-messages.properties @@ -0,0 +1,23 @@ +#
+#
+# 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.
+#
+#
+LocationAttributeMissing = Reading implementation.spring - location attribute missing
+ContributionResolveException = ContributionResolveException occured due to:
+UnableToResolveComponentType = SpringArtifactProcessor: unable to resolve componentType for Spring component
\ No newline at end of file |