diff options
Diffstat (limited to '')
27 files changed, 1189 insertions, 22 deletions
diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/context/tie/SCAGenericApplicationContext.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/context/tie/SCAGenericApplicationContext.java new file mode 100644 index 0000000000..69e2ee2865 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/context/tie/SCAGenericApplicationContext.java @@ -0,0 +1,176 @@ +/* + * 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.context.tie; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringBeanElement; +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringConstructorArgElement; +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringElementTie; +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringPropertyElement; +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringSCAPropertyElement; +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringSCAReferenceElement; +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringSCAServiceElement; +import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.PropertyValue; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.BeanReference; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.config.ConstructorArgumentValues; +import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder; +import org.springframework.beans.factory.config.TypedStringValue; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.GenericApplicationContext; + +public class SCAGenericApplicationContext extends GenericApplicationContext { + + private ClassLoader classloader = null; + private List<SpringSCAPropertyElement> propertyElements = new ArrayList<SpringSCAPropertyElement>(); + private List<SpringSCAServiceElement> serviceElements = new ArrayList<SpringSCAServiceElement>(); + private List<SpringSCAReferenceElement> referenceElements = new ArrayList<SpringSCAReferenceElement>(); + private List<SpringBeanElement> beanElements; + + public SCAGenericApplicationContext(DefaultListableBeanFactory beanFactory, + ApplicationContext parent, + ClassLoader classloader) { + super(beanFactory, parent); + this.classloader = classloader; + } + + public SCAGenericApplicationContext(ApplicationContext parent, ClassLoader classloader) { + super(parent); + this.classloader = classloader; + } + + @Override + protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { + beanFactory.setBeanClassLoader(classloader); + } + + public void addSCAPropertyElement(SpringSCAPropertyElement propertyElement) { + propertyElements.add(propertyElement); + } + + public void addSCAServiceElement(SpringSCAServiceElement serviceElement) { + serviceElements.add(serviceElement); + } + + public void addSCAReferenceElement(SpringSCAReferenceElement referenceElement) { + referenceElements.add(referenceElement); + } + + public synchronized List<SpringBeanElement> getBeanElements() { + if (beanElements == null) { + beanElements = new ArrayList<SpringBeanElement>(); + for (String name : getBeanDefinitionNames()) { + BeanDefinition def = getBeanDefinition(name); + SpringBeanElement beanElement = new SpringBeanElement(name, def.getBeanClassName()); + beanElements.add(beanElement); + beanElement.setAbstractBean(def.isAbstract()); + beanElement.setFactoryBeanAttribute(def.getFactoryBeanName() != null); + beanElement.setFactoryMethodAttribute(def.getFactoryMethodName() != null); + beanElement.setParentAttribute(def.getParentName() != null); + beanElement.setInnerBean(beanElement.getId() == null); + + ConstructorArgumentValues args = def.getConstructorArgumentValues(); + for (Map.Entry<Integer, ValueHolder> e: args.getIndexedArgumentValues().entrySet()) { + ValueHolder holder = e.getValue(); + SpringConstructorArgElement arg = new SpringConstructorArgElement(holder.getType()); + arg.setIndex(e.getKey()); + beanElement.addCustructorArgs(arg); + } + + MutablePropertyValues values = def.getPropertyValues(); + for (PropertyValue p : values.getPropertyValueList()) { + SpringPropertyElement propertyElement = new SpringPropertyElement(p.getName()); + Object value = p.getValue(); + configurePropertyElement(propertyElement, value); + beanElement.getProperties().add(propertyElement); + } + } + } + return beanElements; + } + + public void configurePropertyElement(SpringPropertyElement propertyElement, Object value) { + if (value instanceof BeanReference) { + BeanReference beanRef = (BeanReference)value; + propertyElement.addRef(beanRef.getBeanName()); + } else if (value instanceof Collection) { + Collection collection = (Collection)value; + for (Object item : collection) { + configurePropertyElement(propertyElement, item); + } + } else if (value instanceof TypedStringValue) { + TypedStringValue stringValue = (TypedStringValue)value; + propertyElement.addValue(stringValue.getValue()); + } else { + if (value != null) { + propertyElement.addValue(value.toString()); + } + } + } + + public List<SpringSCAPropertyElement> getPropertyElements() { + return propertyElements; + } + + public List<SpringSCAServiceElement> getServiceElements() { + return serviceElements; + } + + public List<SpringSCAReferenceElement> getReferenceElements() { + return referenceElements; + } + + public <T> T[] getElements(Class<T> type) { + if (type.getSimpleName().equals(SpringSCAPropertyElement.class.getSimpleName())) { + T[] elements = (T[])Array.newInstance(type, getPropertyElements().size()); + for (int i = 0; i < elements.length; i++) { + elements[i] = SpringElementTie.copy(getPropertyElements().get(i), type, type); + } + return elements; + } else if (type.getSimpleName().equals(SpringSCAReferenceElement.class.getSimpleName())) { + T[] elements = (T[])Array.newInstance(type, getReferenceElements().size()); + for (int i = 0; i < elements.length; i++) { + elements[i] = SpringElementTie.copy(getReferenceElements().get(i), type, type); + } + return elements; + } else if (type.getSimpleName().equals(SpringSCAServiceElement.class.getSimpleName())) { + T[] elements = (T[])Array.newInstance(type, getServiceElements().size()); + for (int i = 0; i < elements.length; i++) { + elements[i] = SpringElementTie.copy(getServiceElements().get(i), type, type); + } + return elements; + } else if (type.getSimpleName().equals(SpringBeanElement.class.getSimpleName())) { + T[] elements = (T[])Array.newInstance(type, getBeanElements().size()); + for (int i = 0; i < elements.length; i++) { + elements[i] = SpringElementTie.copy(getBeanElements().get(i), type, type); + } + return elements; + } else { + throw new IllegalArgumentException(type + " is not supported"); + } + } +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SCAParentApplicationContext.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/context/tie/SCAParentApplicationContext.java index 13fe341881..d2b35ce74e 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SCAParentApplicationContext.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/context/tie/SCAParentApplicationContext.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.tuscany.sca.implementation.spring.runtime.context; +package org.apache.tuscany.sca.implementation.spring.context.tie; import java.io.IOException; import java.lang.annotation.Annotation; @@ -46,7 +46,7 @@ import org.springframework.core.io.Resource; * * @version $Rev$ $Date$ */ -class SCAParentApplicationContext implements ApplicationContext { +public class SCAParentApplicationContext implements ApplicationContext { // The Spring implementation for which this is the parent application context private SpringImplementationStub implementation; diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/context/tie/SpringContextTie.java index b3b18abbd4..8bcdc8108d 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringContextTie.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/context/tie/SpringContextTie.java @@ -17,22 +17,23 @@ * under the License. */ -package org.apache.tuscany.sca.implementation.spring.runtime.context; +package org.apache.tuscany.sca.implementation.spring.context.tie; import java.net.URL; 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.apache.tuscany.sca.implementation.spring.processor.tie.ComponentNameAnnotationProcessor; +import org.apache.tuscany.sca.implementation.spring.processor.tie.ComponentStub; +import org.apache.tuscany.sca.implementation.spring.processor.tie.ConstructorAnnotationProcessor; +import org.apache.tuscany.sca.implementation.spring.processor.tie.InitDestroyAnnotationProcessor; +import org.apache.tuscany.sca.implementation.spring.processor.tie.PropertyAnnotationProcessor; +import org.apache.tuscany.sca.implementation.spring.processor.tie.PropertyValueStub; +import org.apache.tuscany.sca.implementation.spring.processor.tie.ReferenceAnnotationProcessor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.io.UrlResource; @@ -53,6 +54,10 @@ public class SpringContextTie { SCAParentApplicationContext scaParentContext = new SCAParentApplicationContext(implementation); springContext = createApplicationContext(scaParentContext, resource); } + + public ApplicationContext getApplicationContext() { + return springContext; + } public void start() { // Do refresh here to ensure that Spring Beans are not touched before the SCA config process is complete... diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/context/tie/SpringImplementationStub.java index 20e5d6b8d2..5ce52e668c 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/context/tie/SpringImplementationStub.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.tuscany.sca.implementation.spring.runtime.context; +package org.apache.tuscany.sca.implementation.spring.context.tie; import java.lang.reflect.Method; diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringBeanElement.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringBeanElement.java index d954195980..020e001a59 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringBeanElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringBeanElement.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.tuscany.sca.implementation.spring.metadata; +package org.apache.tuscany.sca.implementation.spring.elements.tie; import java.util.ArrayList; import java.util.List; diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringConstructorArgElement.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringConstructorArgElement.java index 5571029c61..53a972b7d8 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringConstructorArgElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringConstructorArgElement.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.tuscany.sca.implementation.spring.metadata; +package org.apache.tuscany.sca.implementation.spring.elements.tie; import java.util.ArrayList; import java.util.List; diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringElementTie.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringElementTie.java index df4b077819..c088e5d0f2 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringElementTie.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringElementTie.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.tuscany.sca.implementation.spring.metadata; +package org.apache.tuscany.sca.implementation.spring.elements.tie; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringPropertyElement.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringPropertyElement.java index 585b3d3381..c0a1f2129b 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringPropertyElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringPropertyElement.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.tuscany.sca.implementation.spring.metadata; +package org.apache.tuscany.sca.implementation.spring.elements.tie; import java.util.ArrayList; import java.util.List; diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAPropertyElement.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAPropertyElement.java index f721b21042..3f9901abea 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAPropertyElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAPropertyElement.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.tuscany.sca.implementation.spring.metadata; +package org.apache.tuscany.sca.implementation.spring.elements.tie; /** * Represents an <sca:property> element in a Spring application-context diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAReferenceElement.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAReferenceElement.java index ffa91ad179..7569d2996f 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAReferenceElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAReferenceElement.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.tuscany.sca.implementation.spring.metadata; +package org.apache.tuscany.sca.implementation.spring.elements.tie; /** diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAServiceElement.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAServiceElement.java index 6dee35f46e..737abe7c04 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAServiceElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAServiceElement.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.tuscany.sca.implementation.spring.metadata; +package org.apache.tuscany.sca.implementation.spring.elements.tie; /** diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/SCANamespaceHandlerResolver.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/SCANamespaceHandlerResolver.java new file mode 100644 index 0000000000..07c7c6a454 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/SCANamespaceHandlerResolver.java @@ -0,0 +1,53 @@ +/* + * 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.namespace.tie; + +import org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver; +import org.springframework.beans.factory.xml.NamespaceHandler; + +/** + * Overrides the default Spring namespace resolver to automatically register + * {@link ScaNamespaceHandler} instead of requiring a value to be supplied in a + * Spring configuration + * + * @version $Rev$ $Date$ + */ +public class SCANamespaceHandlerResolver extends DefaultNamespaceHandlerResolver { + private static final String SCA_NAMESPACE = "http://www.springframework.org/schema/sca"; + + private ScaNamespaceHandler handler; + + public SCANamespaceHandlerResolver(ClassLoader classLoader) { + super(classLoader); + handler = new ScaNamespaceHandler(/*componentType*/); + } + + public SCANamespaceHandlerResolver(String handlerMappingsLocation, ClassLoader classLoader) { + super(classLoader, handlerMappingsLocation); + handler = new ScaNamespaceHandler(/*componentType*/); + } + + @Override + public NamespaceHandler resolve(String namespaceUri) { + if (SCA_NAMESPACE.equals(namespaceUri)) { + return handler; + } + return super.resolve(namespaceUri); + } +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaNamespaceHandler.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaNamespaceHandler.java new file mode 100644 index 0000000000..6ed49abb6f --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaNamespaceHandler.java @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2006 the original author or authors. + * + * Licensed 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.namespace.tie; + +import org.springframework.beans.factory.xml.NamespaceHandlerSupport; + +/** + * Handler for the <sca:> namespace in an application context + * + * @version $Rev$ $Date$ + */ +public class ScaNamespaceHandler extends NamespaceHandlerSupport { + + public ScaNamespaceHandler() { + } + + @Override + public void init() { + registerBeanDefinitionParser("reference", new ScaReferenceBeanDefinitionParser()); + registerBeanDefinitionParser("service", new ScaServiceBeanDefinitionParser()); + registerBeanDefinitionParser("property", new ScaPropertyBeanDefinitionParser()); + } + +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaPropertyBeanDefinitionParser.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaPropertyBeanDefinitionParser.java new file mode 100644 index 0000000000..ad11a300ae --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaPropertyBeanDefinitionParser.java @@ -0,0 +1,44 @@ +/* + * Copyright 2002-2006 the original author or authors. + * + * Licensed 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.namespace.tie; + +import org.apache.tuscany.sca.implementation.spring.context.tie.SCAGenericApplicationContext; +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringSCAPropertyElement; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.xml.BeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.w3c.dom.Element; + +/** + * Parser for the <sca:reference> element + * @version $Rev$ $Date$ + */ +public class ScaPropertyBeanDefinitionParser implements BeanDefinitionParser { + + public BeanDefinition parse(Element element, ParserContext parserContext) { + BeanDefinitionRegistry registry = parserContext.getRegistry(); + if (registry instanceof SCAGenericApplicationContext) { + SCAGenericApplicationContext context = (SCAGenericApplicationContext)registry; + SpringSCAPropertyElement propertyElement = + new SpringSCAPropertyElement(element.getAttributeNS(null, "name"), element.getAttributeNS(null, "type")); + context.addSCAPropertyElement(propertyElement); + } + // do nothing, this is handled by Tuscany + return null; + } +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaReferenceBeanDefinitionParser.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaReferenceBeanDefinitionParser.java new file mode 100644 index 0000000000..a4fefa70f1 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaReferenceBeanDefinitionParser.java @@ -0,0 +1,49 @@ +/* + * Copyright 2002-2006 the original author or authors. + * + * Licensed 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.namespace.tie; + +import org.apache.tuscany.sca.implementation.spring.context.tie.SCAGenericApplicationContext; +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringSCAReferenceElement; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.xml.BeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.w3c.dom.Element; + +/** + * Parser for the <sca:reference> element + * + * @version $Rev$ $Date$ + */ +public class ScaReferenceBeanDefinitionParser implements BeanDefinitionParser { + + public BeanDefinition parse(Element element, ParserContext parserContext) { + BeanDefinitionRegistry registry = parserContext.getRegistry(); + if (registry instanceof SCAGenericApplicationContext) { + SCAGenericApplicationContext context = (SCAGenericApplicationContext)registry; + SpringSCAReferenceElement referenceElement = + new SpringSCAReferenceElement(element.getAttributeNS(null, "name"), + element.getAttributeNS(null, "type")); + referenceElement.setDefaultBean(element.getAttributeNS(null, "default")); + context.addSCAReferenceElement(referenceElement); + } + + // do nothing, this is handled by Tuscany + return null; + } + +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaServiceBeanDefinitionParser.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaServiceBeanDefinitionParser.java new file mode 100644 index 0000000000..64f4ce20dc --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaServiceBeanDefinitionParser.java @@ -0,0 +1,48 @@ +/* + * Copyright 2002-2006 the original author or authors. + * + * Licensed 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.namespace.tie; + +import org.apache.tuscany.sca.implementation.spring.context.tie.SCAGenericApplicationContext; +import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringSCAServiceElement; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.xml.BeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.w3c.dom.Element; + +/** + * Parser for the <sca:service/> element + * + * @version $Rev$ $Date$ + */ +public class ScaServiceBeanDefinitionParser implements BeanDefinitionParser { + + public BeanDefinition parse(Element element, ParserContext parserContext) { + BeanDefinitionRegistry registry = parserContext.getRegistry(); + if (registry instanceof SCAGenericApplicationContext) { + SCAGenericApplicationContext context = (SCAGenericApplicationContext)registry; + SpringSCAServiceElement serviceElement = + new SpringSCAServiceElement(element.getAttributeNS(null, "name"), + element.getAttributeNS(null, "target")); + serviceElement.setType(element.getAttributeNS(null, "type")); + context.addSCAServiceElement(serviceElement); + } + // do nothing, handled by Tuscany + return null; + } + +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ComponentNameAnnotationProcessor.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ComponentNameAnnotationProcessor.java new file mode 100644 index 0000000000..77f9f30bfd --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ComponentNameAnnotationProcessor.java @@ -0,0 +1,151 @@ +/* + * 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.tie; + +import java.beans.PropertyDescriptor; +import java.lang.annotation.Annotation; +import java.lang.ref.Reference; +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; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.util.Assert; +import org.springframework.util.ReflectionUtils; + +public class ComponentNameAnnotationProcessor implements BeanPostProcessor { + + private Class<? extends Annotation> componentNameAnnotationType = ComponentName.class; + + private String componentName; + + public ComponentNameAnnotationProcessor(String componentName) { + this.componentName = componentName; + } + + /** + * 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 = componentName; + 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 = componentName; + 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/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentStub.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ComponentStub.java index b087c45ab4..7511ac4bd5 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/ComponentStub.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ComponentStub.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.tuscany.sca.implementation.spring.processor; +package org.apache.tuscany.sca.implementation.spring.processor.tie; import java.lang.reflect.Method; diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ConstructorAnnotationProcessor.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ConstructorAnnotationProcessor.java new file mode 100644 index 0000000000..503307cfd9 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ConstructorAnnotationProcessor.java @@ -0,0 +1,112 @@ +/* + * 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.tie; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; +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> 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/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/InitDestroyAnnotationProcessor.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/InitDestroyAnnotationProcessor.java new file mode 100644 index 0000000000..390ee040f2 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/InitDestroyAnnotationProcessor.java @@ -0,0 +1,75 @@ +/* + * 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.tie; + +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 { + + 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/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/PropertyAnnotationProcessor.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/PropertyAnnotationProcessor.java new file mode 100644 index 0000000000..ce3aafd3ef --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/PropertyAnnotationProcessor.java @@ -0,0 +1,163 @@ +/* + * 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.tie; + +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; + +public class PropertyAnnotationProcessor implements BeanPostProcessor { + + private Class<? extends Annotation> propertyAnnotationType = Property.class; + + private PropertyValueStub propertyValue; + + public PropertyAnnotationProcessor(PropertyValueStub propertyValue) { + this.propertyValue = propertyValue; + } + + /** + * 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) { + + 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) { + + 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); + } + } + }); + } + + 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); + } + } + } + +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyValueStub.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/PropertyValueStub.java index 9a95f818de..5a4b57ce81 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/PropertyValueStub.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/PropertyValueStub.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.tuscany.sca.implementation.spring.processor; +package org.apache.tuscany.sca.implementation.spring.processor.tie; import java.lang.reflect.Method; diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ReferenceAnnotationProcessor.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ReferenceAnnotationProcessor.java new file mode 100644 index 0000000000..24761d2cfa --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/ReferenceAnnotationProcessor.java @@ -0,0 +1,167 @@ +/* + * 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.tie; + +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.Reference; +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; + +public class ReferenceAnnotationProcessor implements BeanPostProcessor { + + private Class<? extends Annotation> referenceAnnotationType = Reference.class; + private ComponentStub component; + + public ReferenceAnnotationProcessor(ComponentStub 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) { + + 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) { + + 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.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); + } + } + } +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLLoaderTie.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/SpringXMLLoaderTie.java index b3f0a4a883..82aa176219 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLLoaderTie.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/tie/SpringXMLLoaderTie.java @@ -17,12 +17,12 @@ * under the License. */ -package org.apache.tuscany.sca.implementation.spring.processor; +package org.apache.tuscany.sca.implementation.spring.processor.tie; import java.net.URL; import java.util.List; -import org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext; +import org.apache.tuscany.sca.implementation.spring.context.tie.SCAGenericApplicationContext; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.ApplicationContext; import org.springframework.core.io.UrlResource; diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/resources/META-INF/spring.handlers b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/resources/META-INF/spring.handlers new file mode 100644 index 0000000000..e7b61bfe48 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/resources/META-INF/spring.handlers @@ -0,0 +1 @@ +http\://www.springframework.org/schema/sca=org.apache.tuscany.sca.implementation.spring.namespace.tie.ScaNamespaceHandler
diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/resources/META-INF/spring.schemas b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/resources/META-INF/spring.schemas new file mode 100644 index 0000000000..249cc21c13 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/resources/META-INF/spring.schemas @@ -0,0 +1 @@ +http\://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd=org/springframework/sca/xml/spring-sca.xsd diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/resources/org/springframework/sca/xml/spring-sca.xsd b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/resources/org/springframework/sca/xml/spring-sca.xsd new file mode 100644 index 0000000000..dfa0b931f6 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/resources/org/springframework/sca/xml/spring-sca.xsd @@ -0,0 +1,84 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<xsd:schema xmlns="http://www.springframework.org/schema/sca" xmlns:xsd="http://www.w3.org/2001/XMLSchema" + attributeFormDefault="unqualified" elementFormDefault="qualified" + targetNamespace="http://www.springframework.org/schema/sca"> + + <xsd:element name="reference"> + <xsd:complexType> + <xsd:attribute name="name" use="required"> + <xsd:simpleType> + <xsd:restriction base="xsd:string" /> + </xsd:simpleType> + </xsd:attribute> + <xsd:attribute name="type" use="required"> + <xsd:simpleType> + <xsd:restriction base="xsd:string" /> + </xsd:simpleType> + </xsd:attribute> + <xsd:attribute name="default" use="optional"> + <xsd:simpleType> + <xsd:restriction base="xsd:string" /> + </xsd:simpleType> + </xsd:attribute> + </xsd:complexType> + </xsd:element> + + <xsd:element name="property"> + <xsd:complexType> + <xsd:attribute name="id" use="optional"> + <xsd:simpleType> + <xsd:restriction base="xsd:string" /> + </xsd:simpleType> + </xsd:attribute> + <xsd:attribute name="name" use="required"> + <xsd:simpleType> + <xsd:restriction base="xsd:string" /> + </xsd:simpleType> + </xsd:attribute> + <xsd:attribute name="type" use="required"> + <xsd:simpleType> + <xsd:restriction base="xsd:string" /> + </xsd:simpleType> + </xsd:attribute> + </xsd:complexType> + </xsd:element> + + <xsd:element name="service"> + <xsd:complexType> + <xsd:attribute name="name" use="required"> + <xsd:simpleType> + <xsd:restriction base="xsd:string" /> + </xsd:simpleType> + </xsd:attribute> + <xsd:attribute name="type" use="required"> + <xsd:simpleType> + <xsd:restriction base="xsd:string" /> + </xsd:simpleType> + </xsd:attribute> + <xsd:attribute name="target" use="required"> + <xsd:simpleType> + <xsd:restriction base="xsd:string" /> + </xsd:simpleType> + </xsd:attribute> + </xsd:complexType> + </xsd:element> + +</xsd:schema> |