From 49a28fd5795d7ed1d155a2ee571a98e009214281 Mon Sep 17 00:00:00 2001 From: rfeng Date: Thu, 29 Jul 2010 00:14:45 +0000 Subject: Add the parent delegation for the SCA spring application context Improve the sample to demonstrate the child bean wired to a bean in the parent context (WebApplicationContext) git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@980272 13f79535-47bb-0310-9956-ffa450edef68 --- .../runtime/context/SCAParentApplicationContext.java | 12 ++++++++++-- .../spring/runtime/context/SpringImplementationStub.java | 7 +++---- .../spring/invocation/SpringImplementationTie.java | 3 ++- .../src/main/java/sample/DateServiceImpl.java | 1 + .../src/main/java/sample/HelloworldImpl.java | 14 +++++++------- .../src/main/resources/helloworld-context.xml | 4 +--- .../src/main/resources/helloworld.composite | 8 ++++++-- .../trunk/samples/webapps/helloworld-spring/pom.xml | 7 ++++++- 8 files changed, 36 insertions(+), 20 deletions(-) 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-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SCAParentApplicationContext.java index 5b4ef9e130..13fe341881 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-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SCAParentApplicationContext.java @@ -69,7 +69,15 @@ class SCAParentApplicationContext implements ApplicationContext { * @return Object - a Bean which matches the requested bean */ public Object getBean(String name, Class requiredType) throws BeansException { - return implementation.getBean(name, requiredType); + Object bean = implementation.getBean(name, requiredType); + if (bean == null && getParent() != null) { + bean = getParent().getBean(name, requiredType); + } + if (bean == null) { + throw new NoSuchBeanDefinitionException("Unable to find Bean with name " + name); + } else { + return bean; + } } // end method getBean( String, Class ) public Object getBean(String name, Object[] args) throws BeansException { @@ -77,7 +85,7 @@ class SCAParentApplicationContext implements ApplicationContext { } public T getBean(Class clazz) throws BeansException { - return clazz.cast(implementation.getBean(clazz.getName(), clazz)); + return clazz.cast(getBean(clazz.getName(), clazz)); } public Map getBeansWithAnnotation(Class clazz) throws BeansException { 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-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java index bf46765f13..20e5d6b8d2 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-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/runtime/context/SpringImplementationStub.java @@ -22,7 +22,6 @@ package org.apache.tuscany.sca.implementation.spring.runtime.context; import java.lang.reflect.Method; import org.springframework.beans.BeansException; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; /** @@ -80,9 +79,9 @@ public class SpringImplementationStub { try { Object bean = getBean.invoke(tie, new Object[] {name, requiredType}); - if (bean == null) { - throw new NoSuchBeanDefinitionException("Unable to find Bean with name " + name); - } +// if (bean == null) { +// throw new NoSuchBeanDefinitionException("Unable to find Bean with name " + name); +// } return bean; } catch (Exception e) { 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 81cbed28dc..d8b7d0c285 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 @@ -132,7 +132,8 @@ public class SpringImplementationTie { } // end if } // end for // TODO: NoSuchBeanException - throw new RuntimeException("Unable to find Bean with name " + name); + // throw new RuntimeException("Unable to find Bean with name " + name); + return null; } // end method getBean( String, Class ) diff --git a/sca-java-2.x/trunk/samples/helloworld-spring/src/main/java/sample/DateServiceImpl.java b/sca-java-2.x/trunk/samples/helloworld-spring/src/main/java/sample/DateServiceImpl.java index 2b3c412de3..64bdd86f7c 100644 --- a/sca-java-2.x/trunk/samples/helloworld-spring/src/main/java/sample/DateServiceImpl.java +++ b/sca-java-2.x/trunk/samples/helloworld-spring/src/main/java/sample/DateServiceImpl.java @@ -30,6 +30,7 @@ import org.oasisopen.sca.annotation.Service; public class DateServiceImpl implements DateService { public Date getDate() { + System.out.println("DateServiceImpl.getDate()"); return new Date(); } diff --git a/sca-java-2.x/trunk/samples/helloworld-spring/src/main/java/sample/HelloworldImpl.java b/sca-java-2.x/trunk/samples/helloworld-spring/src/main/java/sample/HelloworldImpl.java index a44dcacc3a..125c333ddc 100644 --- a/sca-java-2.x/trunk/samples/helloworld-spring/src/main/java/sample/HelloworldImpl.java +++ b/sca-java-2.x/trunk/samples/helloworld-spring/src/main/java/sample/HelloworldImpl.java @@ -18,17 +18,17 @@ */ package sample; -import org.oasisopen.sca.annotation.Reference; public class HelloworldImpl implements Helloworld { - @Reference(required = false) - private DateService dateService; + + public HelloworldImpl() { + super(); + System.out.println("HelloworldImpl()"); + } public String sayHello(String name) { - if (dateService == null) { - return "Hello " + name; - } - return "[" + dateService.getDate() + "] Hello " + name; + System.out.println("HelloworldImpl.sayHello(" + name + ")"); + return "Hello " + name; } } diff --git a/sca-java-2.x/trunk/samples/helloworld-spring/src/main/resources/helloworld-context.xml b/sca-java-2.x/trunk/samples/helloworld-spring/src/main/resources/helloworld-context.xml index b5fba07d66..9ecd09974e 100644 --- a/sca-java-2.x/trunk/samples/helloworld-spring/src/main/resources/helloworld-context.xml +++ b/sca-java-2.x/trunk/samples/helloworld-spring/src/main/resources/helloworld-context.xml @@ -24,9 +24,7 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/sca http://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd"> - + - - \ No newline at end of file diff --git a/sca-java-2.x/trunk/samples/helloworld-spring/src/main/resources/helloworld.composite b/sca-java-2.x/trunk/samples/helloworld-spring/src/main/resources/helloworld.composite index 0900c63a84..6d356758ae 100644 --- a/sca-java-2.x/trunk/samples/helloworld-spring/src/main/resources/helloworld.composite +++ b/sca-java-2.x/trunk/samples/helloworld-spring/src/main/resources/helloworld.composite @@ -19,10 +19,14 @@ --> - - + + + + + diff --git a/sca-java-2.x/trunk/samples/webapps/helloworld-spring/pom.xml b/sca-java-2.x/trunk/samples/webapps/helloworld-spring/pom.xml index e184a014ae..59f3a25696 100644 --- a/sca-java-2.x/trunk/samples/webapps/helloworld-spring/pom.xml +++ b/sca-java-2.x/trunk/samples/webapps/helloworld-spring/pom.xml @@ -69,7 +69,12 @@ 4.8.1 test - + + httpunit + httpunit + 1.6.1 + test + -- cgit v1.2.3