This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 4d9ba1a  Merge pull request #2126, ensure compatibility for elegant 
shutdown under servlet container.
4d9ba1a is described below

commit 4d9ba1a5372b58ce3e5eb214b48a8c06755f912a
Author: Huxing Zhang <huxing.zh...@gmail.com>
AuthorDate: Thu Jul 26 09:44:56 2018 +0800

    Merge pull request #2126, ensure compatibility for elegant shutdown under 
servlet container.
    
    Fixes #1998
---
 dubbo-config/dubbo-config-spring/pom.xml           |  7 +-
 .../DubboApplicationContextInitializer.java        | 39 ++++++++++
 .../DubboWebApplicationInitializer.java            | 40 ----------
 .../src/main/resources/META-INF/web-fragment.xml   | 22 ++++++
 .../DubboApplicationContextInitializerTest.java    | 87 ++++++++++++++++++++++
 .../src/test/resources/applicationContext.xml      |  6 ++
 .../test/resources/webapps/test/WEB-INF/web.xml    | 14 ++++
 .../test/resources/webapps/test2/WEB-INF/web.xml   | 10 +++
 .../test/resources/webapps/test3/WEB-INF/web.xml   | 10 +++
 9 files changed, 193 insertions(+), 42 deletions(-)

diff --git a/dubbo-config/dubbo-config-spring/pom.xml 
b/dubbo-config/dubbo-config-spring/pom.xml
index dc333e7..995209d 100644
--- a/dubbo-config/dubbo-config-spring/pom.xml
+++ b/dubbo-config/dubbo-config-spring/pom.xml
@@ -112,18 +112,21 @@
             <artifactId>javax.el</artifactId>
             <scope>test</scope>
         </dependency>
-
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-tx</artifactId>
             <scope>test</scope>
         </dependency>
-
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-test</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.tomcat.embed</groupId>
+            <artifactId>tomcat-embed-core</artifactId>
+            <scope>test</scope>
+        </dependency>
 
     </dependencies>
     <build>
diff --git 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/initializer/DubboApplicationContextInitializer.java
 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/initializer/DubboApplicationContextInitializer.java
new file mode 100644
index 0000000..36727e6
--- /dev/null
+++ 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/initializer/DubboApplicationContextInitializer.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.dubbo.config.spring.initializer;
+
+import org.springframework.context.ApplicationContextInitializer;
+import org.springframework.context.ConfigurableApplicationContext;
+
+/**
+ * Automatically register {@link DubboApplicationListener} to Spring context
+ * A {@link org.springframework.web.context.ContextLoaderListener} class is 
defined in
+ * src/main/resources/META-INF/web-fragment.xml
+ * In the web-fragment.xml, {@link DubboApplicationContextInitializer} is 
defined in context params.
+ * This file will be discovered if running under a servlet 3.0+ container.
+ * Even if user specifies {@link 
org.springframework.web.context.ContextLoaderListener} in web.xml,
+ * it will be merged to web.xml.
+ * If user specifies <metadata-complete="true" /> in web.xml, this will no 
take effect,
+ * unless user configures {@link DubboApplicationContextInitializer} 
explicitly in web.xml.
+ */
+public class DubboApplicationContextInitializer implements 
ApplicationContextInitializer {
+
+    @Override
+    public void initialize(ConfigurableApplicationContext applicationContext) {
+        applicationContext.addApplicationListener(new 
DubboApplicationListener());
+    }
+}
diff --git 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/initializer/DubboWebApplicationInitializer.java
 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/initializer/DubboWebApplicationInitializer.java
deleted file mode 100644
index 582535b..0000000
--- 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/initializer/DubboWebApplicationInitializer.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * 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.dubbo.config.spring.initializer;
-
-import org.springframework.web.context.AbstractContextLoaderInitializer;
-import org.springframework.web.context.WebApplicationContext;
-import org.springframework.web.context.support.XmlWebApplicationContext;
-
-/**
- * An initializer to register {@link DubboApplicationListener}
- * to the ApplicationContext seamlessly.
- */
-public class DubboWebApplicationInitializer extends 
AbstractContextLoaderInitializer {
-
-    /**
-     * This method won't be triggered if running on spring-boot.
-     * It only works when running under a servlet container.
-     * @return a WebApplicationContext with DubboApplicationListener 
registered.
-     */
-    @Override
-    protected WebApplicationContext createRootApplicationContext() {
-        XmlWebApplicationContext webApplicationContext = new 
XmlWebApplicationContext();
-        webApplicationContext.addApplicationListener(new 
DubboApplicationListener());
-        return webApplicationContext;
-    }
-}
diff --git 
a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/web-fragment.xml 
b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/web-fragment.xml
new file mode 100644
index 0000000..220874a
--- /dev/null
+++ 
b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/web-fragment.xml
@@ -0,0 +1,22 @@
+<web-fragment version="3.0" xmlns="http://java.sun.com/xml/ns/javaee";
+              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd";>
+
+    <name>dubbo-fragment</name>
+
+    <ordering>
+        <before>
+            <others/>
+        </before>
+    </ordering>
+
+    <context-param>
+        <param-name>contextInitializerClasses</param-name>
+        
<param-value>org.apache.dubbo.config.spring.initializer.DubboApplicationContextInitializer</param-value>
+    </context-param>
+
+    <listener>
+        
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
+    </listener>
+
+</web-fragment>
\ No newline at end of file
diff --git 
a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/initializer/DubboApplicationContextInitializerTest.java
 
b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/initializer/DubboApplicationContextInitializerTest.java
new file mode 100644
index 0000000..2c84095
--- /dev/null
+++ 
b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/initializer/DubboApplicationContextInitializerTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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.dubbo.config.spring.initializer;
+
+import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.startup.ContextConfig;
+import org.apache.catalina.startup.Tomcat;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.web.context.ContextLoaderListener;
+
+
+public class DubboApplicationContextInitializerTest {
+
+    @Test
+    public void testSpringContextLoaderListenerInWebXml() throws Exception {
+        Tomcat tomcat = new Tomcat();
+        tomcat.setBaseDir("src/test/resources");
+        tomcat.setPort(12345);
+        StandardContext context = new StandardContext();
+        context.setName("test");
+        context.setDocBase("test");
+        context.setPath("/test");
+        context.addLifecycleListener(new ContextConfig());
+        tomcat.getHost().addChild(context);
+        tomcat.start();
+        // there should be 1 application listener
+        Assert.assertEquals(1, 
context.getApplicationLifecycleListeners().length);
+        // the first one should be Spring's built in ContextLoaderListener.
+        Assert.assertTrue(context.getApplicationLifecycleListeners()[0] 
instanceof ContextLoaderListener);
+        tomcat.stop();
+        tomcat.destroy();
+    }
+
+    @Test
+    public void testNoListenerInWebXml() throws Exception {
+        Tomcat tomcat = new Tomcat();
+        tomcat.setBaseDir("src/test/resources");
+        tomcat.setPort(12345);
+        StandardContext context = new StandardContext();
+        context.setName("test2");
+        context.setDocBase("test2");
+        context.setPath("/test2");
+        context.addLifecycleListener(new ContextConfig());
+        tomcat.getHost().addChild(context);
+        tomcat.start();
+        // there should be 1 application listener
+        Assert.assertEquals(1, 
context.getApplicationLifecycleListeners().length);
+        // the first one should be Spring's built in ContextLoaderListener.
+        Assert.assertTrue(context.getApplicationLifecycleListeners()[0] 
instanceof ContextLoaderListener);
+        tomcat.stop();
+        tomcat.destroy();
+    }
+
+    @Test
+    public void testMetadataComplete() throws Exception {
+        Tomcat tomcat = new Tomcat();
+        tomcat.setBaseDir("src/test/resources");
+        tomcat.setPort(12345);
+        StandardContext context = new StandardContext();
+        context.setName("test3");
+        context.setDocBase("test3");
+        context.setPath("/test3");
+        context.addLifecycleListener(new ContextConfig());
+        tomcat.getHost().addChild(context);
+        tomcat.start();
+        // there should be no application listeners
+        Assert.assertEquals(0, 
context.getApplicationLifecycleListeners().length);
+        tomcat.stop();
+        tomcat.destroy();
+    }
+
+}
diff --git 
a/dubbo-config/dubbo-config-spring/src/test/resources/applicationContext.xml 
b/dubbo-config/dubbo-config-spring/src/test/resources/applicationContext.xml
new file mode 100644
index 0000000..977a8a4
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/resources/applicationContext.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
+       http://www.springframework.org/schema/beans/spring-beans.xsd";>
+</beans>
\ No newline at end of file
diff --git 
a/dubbo-config/dubbo-config-spring/src/test/resources/webapps/test/WEB-INF/web.xml
 
b/dubbo-config/dubbo-config-spring/src/test/resources/webapps/test/WEB-INF/web.xml
new file mode 100644
index 0000000..25214e2
--- /dev/null
+++ 
b/dubbo-config/dubbo-config-spring/src/test/resources/webapps/test/WEB-INF/web.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="3.1">
+    <display-name>dubbo-demo</display-name>
+
+    <context-param>
+        <param-name>contextConfigLocation</param-name>
+        <param-value>classpath:applicationContext.xml</param-value>
+    </context-param>
+
+    <listener>
+        
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
+    </listener>
+
+</web-app>
diff --git 
a/dubbo-config/dubbo-config-spring/src/test/resources/webapps/test2/WEB-INF/web.xml
 
b/dubbo-config/dubbo-config-spring/src/test/resources/webapps/test2/WEB-INF/web.xml
new file mode 100644
index 0000000..0da8d8a
--- /dev/null
+++ 
b/dubbo-config/dubbo-config-spring/src/test/resources/webapps/test2/WEB-INF/web.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="3.1">
+    <display-name>dubbo-demo</display-name>
+
+    <context-param>
+        <param-name>contextConfigLocation</param-name>
+        <param-value>classpath:applicationContext.xml</param-value>
+    </context-param>
+
+</web-app>
diff --git 
a/dubbo-config/dubbo-config-spring/src/test/resources/webapps/test3/WEB-INF/web.xml
 
b/dubbo-config/dubbo-config-spring/src/test/resources/webapps/test3/WEB-INF/web.xml
new file mode 100644
index 0000000..81a5a13
--- /dev/null
+++ 
b/dubbo-config/dubbo-config-spring/src/test/resources/webapps/test3/WEB-INF/web.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="3.1" metadata-complete="true">
+    <display-name>dubbo-demo</display-name>
+
+    <context-param>
+        <param-name>contextConfigLocation</param-name>
+        <param-value>classpath:applicationContext.xml</param-value>
+    </context-param>
+
+</web-app>

Reply via email to