diff options
Diffstat (limited to '')
12 files changed, 880 insertions, 0 deletions
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/stub/SpringXMLBeanDefinitionLoaderStub.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/stub/SpringXMLBeanDefinitionLoaderStub.java new file mode 100644 index 0000000000..f34dfb8ceb --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/stub/SpringXMLBeanDefinitionLoaderStub.java @@ -0,0 +1,92 @@ +/* + * 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.stub; + +import java.lang.reflect.Method; +import java.net.URL; +import java.util.Arrays; +import java.util.List; + +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.implementation.spring.SpringBeanElement; +import org.apache.tuscany.sca.implementation.spring.SpringSCAPropertyElement; +import org.apache.tuscany.sca.implementation.spring.SpringSCAReferenceElement; +import org.apache.tuscany.sca.implementation.spring.SpringSCAServiceElement; +import org.apache.tuscany.sca.implementation.spring.xml.SpringXMLBeanDefinitionLoader; +import org.oasisopen.sca.ServiceRuntimeException; + +/** + * This is the Tuscany side stub for the corresponding runtime tie class. + * It enables the Tuscany code to invoke methods on a Spring context without + * needing to know about any Spring classes. See the SpringContextTie class + * in the implementation-spring-runtime module for what the tie does. + */ +public class SpringXMLBeanDefinitionLoaderStub implements SpringXMLBeanDefinitionLoader { + private final static String TIE = "org.apache.tuscany.sca.implementation.spring.processor.tie.SpringXMLLoaderTie"; + + private static Method createApplicationContext; + + public SpringXMLBeanDefinitionLoaderStub() throws Exception { + synchronized (SpringXMLBeanDefinitionLoaderStub.class) { + if (createApplicationContext == null) { + Class<?> tieClass = Class.forName(TIE, false, Thread.currentThread().getContextClassLoader()); + createApplicationContext = + tieClass.getMethod("createApplicationContext", Object.class, ClassLoader.class, List.class); + } + } + } + + public Object createApplicationContext(Object scaParentContext, ClassLoader classLoader, List<URL> resources) + throws Exception { + return createApplicationContext.invoke(null, scaParentContext, classLoader, resources); + } + + public Object load(List<URL> resources, + List<SpringSCAServiceElement> serviceElements, + List<SpringSCAReferenceElement> referenceElements, + List<SpringSCAPropertyElement> propertyElements, + List<SpringBeanElement> beanElements, + ProcessorContext context) { + try { + Object appContext = + createApplicationContext(null, Thread.currentThread().getContextClassLoader(), resources); + Class<?> cls = appContext.getClass(); + Method method = cls.getMethod("getElements", Class.class); + SpringSCAServiceElement[] serviceArray = + (SpringSCAServiceElement[])method.invoke(appContext, SpringSCAServiceElement.class); + serviceElements.addAll(Arrays.asList(serviceArray)); + + SpringSCAReferenceElement[] referenceArray = + (SpringSCAReferenceElement[])method.invoke(appContext, SpringSCAReferenceElement.class); + referenceElements.addAll(Arrays.asList(referenceArray)); + + SpringSCAPropertyElement[] propertyArray = + (SpringSCAPropertyElement[])method.invoke(appContext, SpringSCAPropertyElement.class); + propertyElements.addAll(Arrays.asList(propertyArray)); + + SpringBeanElement[] beanArray = (SpringBeanElement[])method.invoke(appContext, SpringBeanElement.class); + beanElements.addAll(Arrays.asList(beanArray)); + + return appContext; + } catch (Throwable e) { + throw new ServiceRuntimeException(e); + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/ComponentTie.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/ComponentTie.java new file mode 100644 index 0000000000..882aa587e5 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/ComponentTie.java @@ -0,0 +1,41 @@ +/* + * 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.provider.stub; + +/** + * This is the Tuscany side tie for the corresponding Spring runtime side stub class. + * It enables the Tuscany code to invoke methods on a Spring context without + * needing to know about any Spring classes. See the ComponentStub class + * in the implementation-spring-runtime module for what the stub does. + */ +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +public class ComponentTie { + + private RuntimeComponent component; + + public ComponentTie(RuntimeComponent component) { + this.component = component; + } + + public Object getService(Class<?> type, String name) { + return component.getComponentContext().getService(type, name); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/PropertyValueTie.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/PropertyValueTie.java new file mode 100644 index 0000000000..5e0c5263c2 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/PropertyValueTie.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.provider.stub; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.ComponentProperty; +import org.apache.tuscany.sca.context.PropertyValueFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * This is the Tuscany side tie for the corresponding Spring runtime side stub class. + * It enables the Tuscany code to invoke methods on a Spring context without + * needing to know about any Spring classes. See the PropertyValueStub class + * in the implementation-spring-runtime module for what the stub does. + */ +public class PropertyValueTie { + + private RuntimeComponent component; + private PropertyValueFactory propertyFactory; + + public PropertyValueTie(RuntimeComponent component, PropertyValueFactory propertyFactory) { + this.component = component; + this.propertyFactory = propertyFactory; + } + + public Object getPropertyObj(Class<?> type, String name) { + List<ComponentProperty> props = component.getProperties(); + for (ComponentProperty prop : props) { + if (prop.getName().equals(name)) { + return propertyFactory.createPropertyValue(prop, type); + } + } + return null; // property name not found + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringApplicationContextAccessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringApplicationContextAccessor.java new file mode 100644 index 0000000000..84a65bcf17 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringApplicationContextAccessor.java @@ -0,0 +1,39 @@ +/* + * 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.provider.stub; + +/** + * A utility to receive the parent Spring application context + */ +public interface SpringApplicationContextAccessor { + /** + * Get the parent Spring application context for the hosting environment. This will be used as the parent + * application context for implementation.spring components + * @return The parent application context + */ + Object getParentApplicationContext(); + + /** + * 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 The parent application context + */ + void setParentApplicationContext(Object parentApplicationContext); +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringContextStub.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringContextStub.java new file mode 100644 index 0000000000..062da4b2d2 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringContextStub.java @@ -0,0 +1,142 @@ +/* + * 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.provider.stub; + +import java.lang.reflect.Constructor; +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.spring.SpringImplementation; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * This is the Tuscany side stub for the corresponding runtime tie class. + * It enables the Tuscany code to invoke methods on a Spring context without + * needing to know about any Spring classes. See the SpringContextTie class + * in the implementation-spring-runtime module for what the tie does. + */ +public class SpringContextStub { + + private static final String SPRING_IMPLEMENTATION_STUB = + "org.apache.tuscany.sca.implementation.spring.context.tie.SpringImplementationStub"; + private static final String SPRING_CONTEXT_TIE = + "org.apache.tuscany.sca.implementation.spring.context.tie.SpringContextTie"; + private Object tie; + private Method startMethod; + private Method closeMethod; + private Method getBeanMethod; + + public SpringContextStub(RuntimeComponent component, + SpringImplementation implementation, + Object parentApplicationContext, + ProxyFactory proxyService, + PropertyValueFactory propertyValueObjectFactory) { + + initTie(component, implementation, parentApplicationContext, propertyValueObjectFactory); + + } + + private void initTie(RuntimeComponent component, + SpringImplementation implementation, + Object parentApplicationContext, + PropertyValueFactory propertyValueObjectFactory) { + + // TODO: what class loader to use? + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + + try { + + Class<?> stubClass = Class.forName(SPRING_IMPLEMENTATION_STUB, true, cl); + Constructor<?> stubConstructor = stubClass.getConstructor(new Class<?>[] {Object.class}); + Object stub = + stubConstructor.newInstance(new SpringImplementationTie(implementation, parentApplicationContext, + component, propertyValueObjectFactory)); + + Class<?> tieClass = Class.forName(SPRING_CONTEXT_TIE, true, cl); + Constructor<?> tieConstructor = tieClass.getConstructor(new Class<?>[] {stubClass, List.class}); + this.tie = tieConstructor.newInstance(stub, implementation.getResource()); + + this.startMethod = tieClass.getMethod("start"); + this.closeMethod = tieClass.getMethod("close"); + this.getBeanMethod = tieClass.getMethod("getBean", String.class); + + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SecurityException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + public void start() { + try { + startMethod.invoke(tie); + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + public void close() { + try { + closeMethod.invoke(tie); + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + public Object getBean(String id) throws SpringInvocationException { + try { + + return getBeanMethod.invoke(tie, id); + + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringImplementationProvider.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringImplementationProvider.java new file mode 100644 index 0000000000..e0207b9f12 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringImplementationProvider.java @@ -0,0 +1,80 @@ +/* + * 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.provider.stub; + +import org.apache.tuscany.sca.context.PropertyValueFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.implementation.spring.SpringImplementation; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * A provider class for runtime Spring implementation instances + * @version $Rev$ $Date$ + */ +public class SpringImplementationProvider implements ImplementationProvider { + private RuntimeComponent component; + + // A Spring application context object + private SpringContextStub springContext; + + /** + * Constructor for the provider - takes a component definition and a Spring implementation + * description + * @param component - the component in the assembly + * @param implementation - the implementation + */ + public SpringImplementationProvider(RuntimeComponent component, + SpringImplementation implementation, + Object parentApplicationContext, + ProxyFactory proxyService, + PropertyValueFactory propertyValueObjectFactory) { + super(); + this.component = component; + + springContext = new SpringContextStub(component, implementation, parentApplicationContext, proxyService, propertyValueObjectFactory); + + } // end constructor + + public Invoker createInvoker(RuntimeComponentService service, Operation operation) { + return new SpringInvoker(component, springContext, service, operation); + } + + public boolean supportsOneWayInvocation() { + return false; + } + + /** + * Start this Spring implementation instance + */ + public void start() { + springContext.start(); + } + + /** + * Stop this implementation instance + */ + public void stop() { + springContext.close(); + } + +} // end class SpringImplementationProvider diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringImplementationProviderFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringImplementationProviderFactory.java new file mode 100644 index 0000000000..d402ff2eb6 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringImplementationProviderFactory.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.provider.stub; + +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.implementation.spring.SpringImplementation; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.ImplementationProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * ImplementationProviderFactory for Spring implementation type + * @version $Rev$ $Date$ + * + */ +public class SpringImplementationProviderFactory implements ImplementationProviderFactory<SpringImplementation> { + private ProxyFactory proxyFactory; + private PropertyValueFactory propertyFactory; + private SpringApplicationContextAccessor contextAccessor; + + /** + * Simple constructor + * + */ + public SpringImplementationProviderFactory(ExtensionPointRegistry registry) { + super(); + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + contextAccessor = utilities.getUtility(SpringApplicationContextAccessor.class); + proxyFactory = ExtensibleProxyFactory.getInstance(registry); + propertyFactory = utilities.getUtility(PropertyValueFactory.class); + } + + /** + * Returns a SpringImplementationProvider for a given component and Spring implementation + * @param component the component for which implementation instances are required + * @param implementation the Spring implementation with details of the component + * implementation + * @return the SpringImplementationProvider for the specified component + */ + public ImplementationProvider createImplementationProvider(RuntimeComponent component, + SpringImplementation implementation) { + Object parentApplicationContext = + (contextAccessor != null) ? contextAccessor.getParentApplicationContext() : null; + return new SpringImplementationProvider(component, implementation, parentApplicationContext, proxyFactory, + propertyFactory); + } + + /** + * Returns the class of the Spring implementation + */ + public Class<SpringImplementation> getModelType() { + return SpringImplementation.class; + } + +} // end class SpringImplementationProviderFactory diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringImplementationTie.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringImplementationTie.java new file mode 100644 index 0000000000..fc75faed21 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringImplementationTie.java @@ -0,0 +1,156 @@ +/* + * 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.provider.stub; + +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.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; + +/** + * This is the Tuscany side tie for the corresponding runtime stub class. + * It enables the Sping code in the runtime module to invoke methods on a + * Tuscany SpringImplementation without the Spring runtime module + * needing to know about any Tuscany classes. See the SpringImplementationStub class + * in the implementation-spring-runtime module for what the stub does. + */ +public class SpringImplementationTie { + + private SpringImplementation implementation; + private Object parentApplicationContext; + private RuntimeComponent component; + private PropertyValueFactory propertyFactory; + + public SpringImplementationTie(SpringImplementation implementation, + Object parentApplicationContext, + RuntimeComponent component, + PropertyValueFactory propertyFactory) { + this.implementation = implementation; + this.component = component; + this.propertyFactory = propertyFactory; + this.parentApplicationContext = parentApplicationContext; + } + + public String getURI() { + return implementation.getURI(); + } + + public String getComponentName() { + return component.getName(); + } + + /** + * Method to create a Java Bean for a Property value + * @param <B> the class type of the Bean + * @param requiredType - a Class object for the required type + * @param name - the Property name + * @return - a Bean of the specified property, with value set + */ + private <B> B getPropertyBean(Class<?> requiredType, String name) { + B propertyObject = null; + // Get the component's list of properties + 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 + propertyObject = (B) propertyFactory.createPropertyValue(prop, requiredType); + } // end if + } // end for + + return propertyObject; + } + + /** + * Creates a proxy Bean for a reference + * @param <B> the Business interface type for the reference + * @param businessInterface - the business interface as a Class + * @param referenceName - the name of the Reference + * @return an Bean of the type defined by <B> + */ + private <B> B getService(Class<B> businessInterface, String referenceName) { + return component.getComponentContext().getService(businessInterface, referenceName); + } + + /** + * Get a Bean for a reference or for a property. + * + * @param name - the name of the Bean required + * @param requiredType - the required type of the Bean (either a Java class or a Java interface) + * @return Object - a Bean which matches the requested bean + */ + public Object getBean(String name, Class<?> requiredType) { + // The expectation is that the requested Bean is either a reference or a property + // from the Spring context + for (Reference reference : implementation.getReferences()) { + if (reference.getName().equals(name)) { + // Extract the Java interface for the reference (it can't be any other interface type + // for a Spring application context) + if (requiredType == null) { + JavaInterface javaInterface = (JavaInterface)reference.getInterfaceContract().getInterface(); + requiredType = javaInterface.getJavaClass(); + } + // Create and return the proxy for the reference + return getService(requiredType, reference.getName()); + } // end if + } // end for + + // For a property, get the name and the required Java type and create a Bean + // of that type with the value inserted. + for (Property property : implementation.getProperties()) { + if (property.getName().equals(name)) { + if (requiredType == null) { + // The following code only deals with a subset of types and was superceded + // by the information from the implementation (which uses Classes as found + // in the Spring implementation itself. + //requiredType = JavaXMLMapper.getJavaType( property.getXSDType() ); + requiredType = implementation.getPropertyClass(name); + } + return getPropertyBean(requiredType, property.getName()); + } // end if + } // end for + // TODO: NoSuchBeanException + // throw new RuntimeException("Unable to find Bean with name " + name); + return null; + + } // end method getBean( String, Class ) + + public Object getComponentTie() { + return new ComponentTie(component); + } + + public Object getPropertyValueTie() { + return new PropertyValueTie(component, propertyFactory); + } + + public ClassLoader getClassLoader() { + return implementation.getClassLoader(); + } + + public Object getParentApplicationContext() { + return parentApplicationContext; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringInvocationException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringInvocationException.java new file mode 100644 index 0000000000..909a640563 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringInvocationException.java @@ -0,0 +1,40 @@ +/* + * 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.provider.stub; + +/** + * @version $Rev$ $Date$ + */ +public class SpringInvocationException extends Exception { + + private static final long serialVersionUID = -1157790036638157513L; + + public SpringInvocationException(String msg) { + super(msg); + } + + public SpringInvocationException(Throwable e) { + super(e); + } + + public SpringInvocationException(String msg, Throwable e) { + super(msg, e); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringInvoker.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringInvoker.java new file mode 100644 index 0000000000..7d73eb5e6a --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/java/org/apache/tuscany/sca/implementation/spring/provider/stub/SpringInvoker.java @@ -0,0 +1,125 @@ +/* + * 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.provider.stub; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.implementation.spring.SpringBeanElement; +import org.apache.tuscany.sca.implementation.spring.SpringImplementation; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * Initial implementation of a Spring bean invoker + * @version $Rev$ $Date$ + */ +public class SpringInvoker implements Invoker { + + private Method theMethod = null; + private Object bean; + private SpringBeanElement beanElement; + private boolean badInvoker = false; + + private SpringContextStub springContext; + private Operation operation; + + /** + * SpringInvoker constructor + * @param component - the Spring component to invoke + * @param service - the service to invoke + * @param operation - the operation to invoke + */ + public SpringInvoker(RuntimeComponent component, + SpringContextStub springContext, + RuntimeComponentService service, + Operation operation) { + + this.springContext = springContext; + this.operation = operation; + + // From the component and the service, identify the Spring Bean which is the target + SpringImplementation theImplementation = (SpringImplementation)component.getImplementation(); + beanElement = theImplementation.getBeanFromService(service.getService()); + + if (beanElement == null) { + badInvoker = true; + return; + } + + } // end constructor SpringInvoker + + // Lazy-load the method to avoid timing problems with the Spring Context + private void setupMethod() throws SpringInvocationException { + try { + bean = springContext.getBean(beanElement.getId()); + Class<?> beanClass = bean.getClass(); + theMethod = JavaInterfaceUtil.findMethod(beanClass, operation); + //System.out.println("SpringInvoker - found method " + theMethod.getName() ); + } catch (NoSuchMethodException e) { + throw new SpringInvocationException(e); + } + } + + private Object doInvoke(Object payload) throws SpringInvocationException { + if (theMethod == null) + setupMethod(); + + if (badInvoker) + throw new SpringInvocationException("Spring invoker incorrectly configured"); + // Invoke the method on the Spring bean using the payload, returning the results + try { + Object ret; + + if (payload != null && !payload.getClass().isArray()) { + ret = theMethod.invoke(bean, payload); + } else { + ret = theMethod.invoke(bean, (Object[])payload); + } + return ret; + } catch (InvocationTargetException e) { + throw new SpringInvocationException("Spring invoker invoke method '" + theMethod.getName() + "' error.", + e.getCause()); + } catch (Exception e) { + throw new SpringInvocationException("Spring invoker invoke method '" + theMethod.getName() + "' error.", e); + } + + } // end method doInvoke + + /** + * @param msg the message to invoke on the target bean + */ + public Message invoke(Message msg) { + try { + Object resp = doInvoke(msg.getBody()); + msg.setBody(resp); + } catch (SpringInvocationException e) { + msg.setFaultBody(e.getCause()); + } catch (Throwable e) { + msg.setFaultBody(e); + } + //System.out.println("Spring Invoker - invoke called"); + return msg; + } // end method invoke + +} // end class SpringInvoker diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.spring.xml.SpringXMLBeanDefinitionLoader b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.spring.xml.SpringXMLBeanDefinitionLoader new file mode 100644 index 0000000000..0a46baa13b --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.spring.xml.SpringXMLBeanDefinitionLoader @@ -0,0 +1,17 @@ +# 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. +org.apache.tuscany.sca.implementation.spring.processor.stub.SpringXMLBeanDefinitionLoaderStub;ranking=50
\ No newline at end of file diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.ImplementationProviderFactory b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.ImplementationProviderFactory new file mode 100644 index 0000000000..e037ea60f5 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-spring-stub/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.ImplementationProviderFactory @@ -0,0 +1,20 @@ +# 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 implementation extension
+org.apache.tuscany.sca.implementation.spring.provider.stub.SpringImplementationProviderFactory;model=org.apache.tuscany.sca.implementation.spring.SpringImplementation;ranking=50
+
|