From 9dafe3e2c4d2476cb3ea7789e3f3063418340059 Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 28 Jul 2010 21:01:41 +0000 Subject: Allow Spring WebApplicationContext to be used by Tuscany Bring up a sample web application which demonstrates the integration between Spring and Tuscany git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@980218 13f79535-47bb-0310-9956-ffa450edef68 --- .../spring/invocation/PropertyValueTie.java | 8 ++-- .../invocation/SpringApplicationContextHelper.java | 56 ++++++++++++++++++++++ .../spring/invocation/SpringContextStub.java | 12 +++-- .../invocation/SpringImplementationProvider.java | 13 ++--- .../SpringImplementationProviderFactory.java | 22 ++++----- .../spring/invocation/SpringImplementationTie.java | 17 ++++--- .../spring/xml/SpringImplementationProcessor.java | 2 +- 7 files changed, 94 insertions(+), 36 deletions(-) create mode 100644 sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringApplicationContextHelper.java (limited to 'sca-java-2.x/trunk/modules/implementation-spring/src') diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/PropertyValueTie.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/PropertyValueTie.java index 6289ec8626..2a1df9bcb5 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/PropertyValueTie.java +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/PropertyValueTie.java @@ -22,7 +22,7 @@ package org.apache.tuscany.sca.implementation.spring.invocation; import java.util.List; import org.apache.tuscany.sca.assembly.ComponentProperty; -import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; +import org.apache.tuscany.sca.context.PropertyValueFactory; import org.apache.tuscany.sca.runtime.RuntimeComponent; /** @@ -34,9 +34,9 @@ import org.apache.tuscany.sca.runtime.RuntimeComponent; public class PropertyValueTie { private RuntimeComponent component; - private JavaPropertyValueObjectFactory propertyFactory; + private PropertyValueFactory propertyFactory; - public PropertyValueTie(RuntimeComponent component, JavaPropertyValueObjectFactory propertyFactory) { + public PropertyValueTie(RuntimeComponent component, PropertyValueFactory propertyFactory) { this.component = component; this.propertyFactory = propertyFactory; } @@ -45,7 +45,7 @@ public class PropertyValueTie { List props = component.getProperties(); for (ComponentProperty prop : props) { if (prop.getName().equals(name)) { - return propertyFactory.createValueFactory(prop, prop.getValue(), type).getInstance(); + return propertyFactory.createPropertyValue(prop, type); } } return null; // property name not found diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringApplicationContextHelper.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringApplicationContextHelper.java new file mode 100644 index 0000000000..f1a1d7e1e7 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringApplicationContextHelper.java @@ -0,0 +1,56 @@ +/* + * 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.invocation; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; + +/** + * A utility to receive the parent Spring application context + */ +public class SpringApplicationContextHelper { + private Object parentApplicationContext; + + public SpringApplicationContextHelper(ExtensionPointRegistry registry) { + } + + public static SpringApplicationContextHelper getInstance(ExtensionPointRegistry registry) { + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + return utilities.getUtility(SpringApplicationContextHelper.class); + } + + /** + * Get the parent Spring application context for the hosting environment. This will be used as the parent + * application context for implementation.spring components + * @return + */ + public Object getParentApplicationContext() { + return parentApplicationContext; + } + + /** + * Set the root Spring application context. This is particually useful for Spring web integration where Spring + * creates WebApplicationContext and keeps it in the ServletContext + * @param parentApplicationContext + */ + public void setParentApplicationContext(Object parentApplicationContext) { + this.parentApplicationContext = parentApplicationContext; + } +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringContextStub.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringContextStub.java index 6980b81e5d..2b5b5821c7 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringContextStub.java +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringContextStub.java @@ -24,8 +24,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; +import org.apache.tuscany.sca.context.PropertyValueFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactory; -import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; import org.apache.tuscany.sca.implementation.spring.SpringImplementation; import org.apache.tuscany.sca.runtime.RuntimeComponent; @@ -44,16 +44,18 @@ public class SpringContextStub { public SpringContextStub(RuntimeComponent component, SpringImplementation implementation, + Object parentApplicationContext, ProxyFactory proxyService, - JavaPropertyValueObjectFactory propertyValueObjectFactory) { + PropertyValueFactory propertyValueObjectFactory) { - initTie(component, implementation, propertyValueObjectFactory); + initTie(component, implementation, parentApplicationContext, propertyValueObjectFactory); } private void initTie(RuntimeComponent component, SpringImplementation implementation, - JavaPropertyValueObjectFactory propertyValueObjectFactory) { + Object parentApplicationContext, + PropertyValueFactory propertyValueObjectFactory) { // TODO: what class loader to use? ClassLoader cl = Thread.currentThread().getContextClassLoader(); @@ -66,7 +68,7 @@ public class SpringContextStub { cl); Constructor stubConstructor = stubClass.getConstructor(new Class[] {Object.class}); Object stub = - stubConstructor.newInstance(new SpringImplementationTie(implementation, component, + stubConstructor.newInstance(new SpringImplementationTie(implementation, parentApplicationContext, component, propertyValueObjectFactory)); Class tieClass = diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationProvider.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationProvider.java index 304f3e2c1a..1d71514721 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationProvider.java +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationProvider.java @@ -18,8 +18,8 @@ */ package org.apache.tuscany.sca.implementation.spring.invocation; +import org.apache.tuscany.sca.context.PropertyValueFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactory; -import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; import org.apache.tuscany.sca.implementation.spring.SpringImplementation; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Invoker; @@ -37,10 +37,6 @@ public class SpringImplementationProvider implements ImplementationProvider { // A Spring application context object private SpringContextStub springContext; - private SpringImplementation implementation; - - private JavaPropertyValueObjectFactory propertyValueObjectFactory; - /** * Constructor for the provider - takes a component definition and a Spring implementation * description @@ -49,14 +45,13 @@ public class SpringImplementationProvider implements ImplementationProvider { */ public SpringImplementationProvider(RuntimeComponent component, SpringImplementation implementation, + Object parentApplicationContext, ProxyFactory proxyService, - JavaPropertyValueObjectFactory propertyValueObjectFactory) { + PropertyValueFactory propertyValueObjectFactory) { super(); - this.implementation = implementation; this.component = component; - this.propertyValueObjectFactory = propertyValueObjectFactory; - springContext = new SpringContextStub(component, implementation, proxyService, propertyValueObjectFactory); + springContext = new SpringContextStub(component, implementation, parentApplicationContext, proxyService, propertyValueObjectFactory); } // end constructor diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationProviderFactory.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationProviderFactory.java index 5d9d6b31c4..46d49dbe1a 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationProviderFactory.java +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationProviderFactory.java @@ -18,11 +18,11 @@ */ package org.apache.tuscany.sca.implementation.spring.invocation; +import org.apache.tuscany.sca.context.PropertyValueFactory; import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactory; -import org.apache.tuscany.sca.databinding.impl.MediatorImpl; -import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; import org.apache.tuscany.sca.implementation.spring.SpringImplementation; import org.apache.tuscany.sca.provider.ImplementationProvider; import org.apache.tuscany.sca.provider.ImplementationProviderFactory; @@ -34,21 +34,19 @@ import org.apache.tuscany.sca.runtime.RuntimeComponent; * */ public class SpringImplementationProviderFactory implements ImplementationProviderFactory { - private ProxyFactory proxyFactory; - private JavaPropertyValueObjectFactory propertyFactory; + private PropertyValueFactory propertyFactory; + private SpringApplicationContextHelper contextHelper; /** * Simple constructor * */ - public SpringImplementationProviderFactory(ExtensionPointRegistry extensionPoints) { + public SpringImplementationProviderFactory(ExtensionPointRegistry registry) { super(); - - proxyFactory = ExtensibleProxyFactory.getInstance(extensionPoints); - - // TODO: could the runtime have a default PropertyValueObjectFactory? - propertyFactory = new JavaPropertyValueObjectFactory(new MediatorImpl(extensionPoints)); + contextHelper = SpringApplicationContextHelper.getInstance(registry); + proxyFactory = ExtensibleProxyFactory.getInstance(registry); + propertyFactory = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(PropertyValueFactory.class); } /** @@ -60,7 +58,8 @@ public class SpringImplementationProviderFactory implements ImplementationProvid */ public ImplementationProvider createImplementationProvider(RuntimeComponent component, SpringImplementation implementation) { - return new SpringImplementationProvider(component, implementation, proxyFactory, propertyFactory); + Object parentApplicationContext = contextHelper.getParentApplicationContext(); + return new SpringImplementationProvider(component, implementation, parentApplicationContext, proxyFactory, propertyFactory); } /** @@ -69,4 +68,5 @@ public class SpringImplementationProviderFactory implements ImplementationProvid public Class getModelType() { return SpringImplementation.class; } + } // end class SpringImplementationProviderFactory diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationTie.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationTie.java index 749081e95b..81cbed28dc 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationTie.java +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/invocation/SpringImplementationTie.java @@ -24,8 +24,7 @@ import java.util.List; import org.apache.tuscany.sca.assembly.ComponentProperty; import org.apache.tuscany.sca.assembly.Property; import org.apache.tuscany.sca.assembly.Reference; -import org.apache.tuscany.sca.core.factory.ObjectFactory; -import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; +import org.apache.tuscany.sca.context.PropertyValueFactory; import org.apache.tuscany.sca.implementation.spring.SpringImplementation; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.runtime.RuntimeComponent; @@ -40,15 +39,18 @@ import org.apache.tuscany.sca.runtime.RuntimeComponent; public class SpringImplementationTie { private SpringImplementation implementation; + private Object parentApplicationContext; private RuntimeComponent component; - private JavaPropertyValueObjectFactory propertyFactory; + private PropertyValueFactory propertyFactory; public SpringImplementationTie(SpringImplementation implementation, + Object parentApplicationContext, RuntimeComponent component, - JavaPropertyValueObjectFactory propertyFactory) { + PropertyValueFactory propertyFactory) { this.implementation = implementation; this.component = component; this.propertyFactory = propertyFactory; + this.parentApplicationContext = parentApplicationContext; } public String getURI() { @@ -74,8 +76,7 @@ public class SpringImplementationTie { 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); - propertyObject = (B)factory.getInstance(); + propertyObject = (B) propertyFactory.createPropertyValue(prop, requiredType); } // end if } // end for @@ -146,5 +147,9 @@ public class SpringImplementationTie { public ClassLoader getClassLoader() { return implementation.getClassLoader(); } + + public Object getParentApplicationContext() { + return parentApplicationContext; + } } diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java index 086dd3b5cf..af3a16cd5d 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java @@ -129,7 +129,7 @@ public class SpringImplementationProcessor extends BaseStAXArtifactProcessor imp * 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. + * in the META-INF/spring directory. */ public SpringImplementation read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { -- cgit v1.2.3