From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../spring/webapp/ScaWebApplicationContext.java | 130 +++++++++++++++++++++ .../spring/webapp/SpringWebappRuntimeInfo.java | 41 +++++++ 2 files changed, 171 insertions(+) create mode 100644 branches/sca-java-M2/sca/services/containers/container.spring/src/main/java/org/apache/tuscany/container/spring/webapp/ScaWebApplicationContext.java create mode 100644 branches/sca-java-M2/sca/services/containers/container.spring/src/main/java/org/apache/tuscany/container/spring/webapp/SpringWebappRuntimeInfo.java (limited to 'branches/sca-java-M2/sca/services/containers/container.spring/src/main/java/org/apache/tuscany/container/spring/webapp') diff --git a/branches/sca-java-M2/sca/services/containers/container.spring/src/main/java/org/apache/tuscany/container/spring/webapp/ScaWebApplicationContext.java b/branches/sca-java-M2/sca/services/containers/container.spring/src/main/java/org/apache/tuscany/container/spring/webapp/ScaWebApplicationContext.java new file mode 100644 index 0000000000..2284d32201 --- /dev/null +++ b/branches/sca-java-M2/sca/services/containers/container.spring/src/main/java/org/apache/tuscany/container/spring/webapp/ScaWebApplicationContext.java @@ -0,0 +1,130 @@ +/* + * 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.container.spring.webapp; + +import java.io.File; +import java.net.URL; + +import javax.servlet.ServletContext; + +import org.apache.tuscany.api.TuscanyRuntimeException; +import org.apache.tuscany.container.spring.config.SCANamespaceHandlerResolver; +import org.apache.tuscany.container.spring.impl.SpringScaAdapter; +import org.apache.tuscany.container.spring.model.SpringComponentType; +import org.apache.tuscany.runtime.webapp.WebappRuntime; +import org.apache.tuscany.runtime.webapp.WebappUtil; +import org.apache.tuscany.runtime.webapp.WebappUtilImpl; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.core.io.Resource; +import org.springframework.sca.ScaAdapterAware; +import org.springframework.sca.ScaAdapterPostProcessor; +import org.springframework.util.Assert; +import org.springframework.web.context.ConfigurableWebApplicationContext; +import org.springframework.web.context.support.XmlWebApplicationContext; + +/** + * WebApplicationContext implementation that understands SCA extensions. + * This class is not very complicated, the key issue is getting hold of a reference + * to the Tuscany runtime which the webapp is using. + * + * @author Andy Piper + */ +public class ScaWebApplicationContext extends XmlWebApplicationContext + implements ConfigurableWebApplicationContext { + private WebappRuntime runtime; + private SpringComponentType componentType; + + public ScaWebApplicationContext() { + } + + protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) { +// beanDefinitionReader.setEntityResolver(null); + beanDefinitionReader + .setNamespaceHandlerResolver(new SCANamespaceHandlerResolver(getClassLoader(), componentType)); + } + + public Resource getResource(String location) { + Assert.notNull(location, "location is required"); + return super.getResource(location); + } + + /* (non-Javadoc) + * @see org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) + */ + protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + super.postProcessBeanFactory(beanFactory); + beanFactory.addBeanPostProcessor(new ScaAdapterPostProcessor + (new SpringScaAdapter(componentType))); + beanFactory.ignoreDependencyInterface(ScaAdapterAware.class); + } + + protected void onRefresh() { + if (runtime != null) { // egregious hack to prevent recursion in refresh() + return; + } + ServletContext servletContext = getServletContext(); + WebappUtil utils = getUtils(servletContext); + + try { + ClassLoader webappClassLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader bootClassLoader = utils.getBootClassLoader(webappClassLoader); + runtime = utils.getRuntime(bootClassLoader); + URL systemScdl = utils.getSystemScdl(bootClassLoader); + URL applicationScdl = utils.getApplicationScdl(webappClassLoader); + + runtime.setMonitorFactory(runtime.createDefaultMonitorFactory()); + runtime.setApplicationName(utils.getApplicationName()); + runtime.setServletContext(servletContext); + runtime.setHostClassLoader(webappClassLoader); + runtime.setSystemScdl(systemScdl); + runtime.setApplicationScdl(applicationScdl); + runtime.setRuntimeInfo(new SpringWebappRuntimeInfo(getApplicationRootDirectory(), this)); + runtime.initialize(); + } catch (TuscanyRuntimeException e) { + servletContext.log(e.getMessage(), e); + throw e; + } + } + + protected WebappUtil getUtils(ServletContext servletContext) { + return new WebappUtilImpl(servletContext); + } + + protected void onClose() { + if (runtime != null) { + runtime.destroy(); + runtime = null; + } + } + + /** + * What does this do and why to we need it? + * @return + */ + private File getApplicationRootDirectory() { + String property = System.getProperty("tuscany.applicationRootDir"); + if (property != null) { + return new File(property); + } + + return new File(System.getProperty("user.dir")); + } +} diff --git a/branches/sca-java-M2/sca/services/containers/container.spring/src/main/java/org/apache/tuscany/container/spring/webapp/SpringWebappRuntimeInfo.java b/branches/sca-java-M2/sca/services/containers/container.spring/src/main/java/org/apache/tuscany/container/spring/webapp/SpringWebappRuntimeInfo.java new file mode 100644 index 0000000000..a860049462 --- /dev/null +++ b/branches/sca-java-M2/sca/services/containers/container.spring/src/main/java/org/apache/tuscany/container/spring/webapp/SpringWebappRuntimeInfo.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.container.spring.webapp; + +import java.io.File; +import javax.servlet.ServletContext; + +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.XmlWebApplicationContext; + +import org.apache.tuscany.container.spring.impl.SpringRuntimeInfo; +import org.apache.tuscany.runtime.webapp.WebappRuntimeInfo; + +/** + * @author Andy Piper + */ +public class SpringWebappRuntimeInfo extends SpringRuntimeInfo implements WebappRuntimeInfo { + public SpringWebappRuntimeInfo(File appRootDir, XmlWebApplicationContext applicationContext) { + super(appRootDir, applicationContext); + } + + public ServletContext getServletContext() { + return ((WebApplicationContext) getApplicationContext()).getServletContext(); + } +} -- cgit v1.2.3