diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-07-28 21:01:41 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-07-28 21:01:41 +0000 |
commit | 9dafe3e2c4d2476cb3ea7789e3f3063418340059 (patch) | |
tree | fa543a6cc40ea6ec3ad43ee7f7bf0478685db344 /sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/implementation | |
parent | 83d18d1113e87315181b6d9f1daba6146ad12cc9 (diff) |
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
Diffstat (limited to 'sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/implementation')
2 files changed, 105 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/implementation/spring/webapp/ApplicationContextAccessor.java b/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/implementation/spring/webapp/ApplicationContextAccessor.java new file mode 100644 index 0000000000..e2f748aba8 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/implementation/spring/webapp/ApplicationContextAccessor.java @@ -0,0 +1,43 @@ +/* + * 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.webapp; + +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.support.ApplicationObjectSupport; + +/** + * A singleton utility class that can be added to the Spring application context to receive injection of the containing + * Spring application context. + * + * The Spring bean definition file is: org/apache/tuscany/sca/implementation/spring/webapp/spring-webapp-context.xml + */ +public class ApplicationContextAccessor extends ApplicationObjectSupport implements ApplicationContextAware { + public final static String BEAN_ID = "tuscanySpringApplicationContextAccessor"; + private final static ApplicationContextAccessor INSTANCE = new ApplicationContextAccessor(); + + private ApplicationContextAccessor() { + } + + // Spring static factory method to create the singleton instance + public static ApplicationContextAccessor getInstance() { + return INSTANCE; + } + +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/implementation/spring/webapp/SpringWebModuleActivator.java b/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/implementation/spring/webapp/SpringWebModuleActivator.java new file mode 100644 index 0000000000..062e6105a2 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/implementation/spring/webapp/SpringWebModuleActivator.java @@ -0,0 +1,62 @@ +/* + * 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.webapp; + +import java.util.logging.Logger; + +import javax.servlet.ServletContext; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.host.http.ExtensibleServletHost; +import org.apache.tuscany.sca.implementation.spring.invocation.SpringApplicationContextHelper; +import org.springframework.context.ApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + +public class SpringWebModuleActivator implements ModuleActivator { + private static Logger log = Logger.getLogger(SpringWebModuleActivator.class.getName()); + private ExtensionPointRegistry registry; + + public SpringWebModuleActivator(ExtensionPointRegistry registry) { + super(); + this.registry = registry; + } + + @Override + public void start() { + ExtensibleServletHost servletHost = ExtensibleServletHost.getInstance(registry); + SpringApplicationContextHelper contextHelper = SpringApplicationContextHelper.getInstance(registry); + + ServletContext servletContext = servletHost.getServletContext(); + if (servletContext != null) { + ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext); + if (context != null) { + log.info("Spring WebApplicationContext is now injected on Tuscany"); + contextHelper.setParentApplicationContext(context); + } + } + } + + @Override + public void stop() { + // NO-OP + } + +} |