You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2019/10/26 17:50:56 UTC
[cxf] branch master updated: CXF-8130: Since Spring 5.1.5 Release
CXFServlet application listener dissapears by context refresh
This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/master by this push:
new a3849c8 CXF-8130: Since Spring 5.1.5 Release CXFServlet application listener dissapears by context refresh
a3849c8 is described below
commit a3849c89cb29e53b336e2e7c1f41752fcc136617
Author: reta <dr...@gmail.com>
AuthorDate: Sat Oct 26 13:49:47 2019 -0400
CXF-8130: Since Spring 5.1.5 Release CXFServlet application listener dissapears by context refresh
---
.../apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java | 1 +
.../apache/cxf/transport/servlet/CXFServlet.java | 23 +++++
.../systest/servlet/SpringServletContextTest.java | 102 +++++++++++++++++++++
.../cxf/systest/servlet/web-spring-context.xml | 43 +++++++++
4 files changed, 169 insertions(+)
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java
index e60b9b0..7ffc5b1 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java
@@ -144,6 +144,7 @@ public class AbstractJAXRSFactoryBean extends AbstractEndpointFactory {
setTransportId(transportId);
+ //EndpointInfo ei = new EndpointInfo(service.getServiceInfos().get(0), transportId);
EndpointInfo ei = new EndpointInfo();
ei.setTransportId(transportId);
ei.setName(serviceFactory.getService().getName());
diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFServlet.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFServlet.java
index 6caa5c9..610fe20 100644
--- a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFServlet.java
+++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFServlet.java
@@ -20,6 +20,7 @@ package org.apache.cxf.transport.servlet;
import java.io.IOException;
import java.io.InputStream;
+import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
@@ -35,6 +36,7 @@ import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.io.Resource;
+import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
@@ -85,6 +87,27 @@ public class CXFServlet extends CXFNonSpringServlet
}
protected void addListener(AbstractApplicationContext wac) {
+ /**
+ * The change in the way application listeners are maintained during the context refresh
+ * since Spring Framework 5.1.5 (https://github.com/spring-projects/spring-framework/issues/22325). The
+ * CXF adds listener **after** the context has been refreshed, not much control we have over it, but
+ * it does matter now: the listeners registered after the context refresh disappear when
+ * context is refreshed. The ugly hack here, to stay in the loop, is to add CXF servlet
+ * to "earlyApplicationListeners" set, only than it will be kept between refreshes.
+ */
+ try {
+ final Field f = ReflectionUtils.findField(wac.getClass(), "earlyApplicationListeners");
+
+ if (f != null) {
+ Collection<Object> c = CastUtils.cast((Collection<?>)ReflectionUtil.setAccessible(f).get(wac));
+ if (c != null) {
+ c.add(this);
+ }
+ }
+ } catch (SecurityException | IllegalAccessException e) {
+ //ignore.
+ }
+
try {
//spring 2 vs spring 3 return type is different
Method m = wac.getClass().getMethod("getApplicationListeners");
diff --git a/systests/transports/src/test/java/org/apache/cxf/systest/servlet/SpringServletContextTest.java b/systests/transports/src/test/java/org/apache/cxf/systest/servlet/SpringServletContextTest.java
new file mode 100644
index 0000000..720a407
--- /dev/null
+++ b/systests/transports/src/test/java/org/apache/cxf/systest/servlet/SpringServletContextTest.java
@@ -0,0 +1,102 @@
+/**
+ * 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.cxf.systest.servlet;
+
+import java.util.Arrays;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+import com.meterware.httpunit.WebRequest;
+import com.meterware.httpunit.WebResponse;
+import com.meterware.servletunit.ServletUnitClient;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusException;
+import org.apache.cxf.bus.spring.SpringBus;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.DependsOn;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+public class SpringServletContextTest extends AbstractServletTest {
+ @Configuration
+ public static class AppConfig {
+ @Bean(destroyMethod = "shutdown")
+ public SpringBus cxf() {
+ return new SpringBus();
+ }
+
+ @Bean @DependsOn("cxf")
+ public Server jaxRsServer() {
+ final JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
+ factory.setServiceBeans(Arrays.asList(contextRestService()));
+ factory.setAddress("/services/context");
+ return factory.create();
+ }
+
+ @Bean
+ public ContextRestService contextRestService() {
+ return new ContextRestService();
+ }
+ }
+
+ @Path("/")
+ public static class ContextRestService {
+ @Autowired
+ private ConfigurableApplicationContext context;
+
+ @GET
+ @Path("/refresh")
+ public void refresh() {
+ context.refresh();
+ }
+ }
+
+ @Override
+ protected String getConfiguration() {
+ return "/org/apache/cxf/systest/servlet/web-spring-context.xml";
+ }
+
+ @Override
+ protected Bus createBus() throws BusException {
+ // don't set up the bus, let the servlet do it
+ return null;
+ }
+
+ @Test
+ public void testContextRefresh() throws Exception {
+ ServletUnitClient client = newClient();
+ client.setExceptionsThrownOnErrorStatus(true);
+
+ for (int i = 0; i < 5; ++i) {
+ final WebRequest request = new GetMethodQueryWebRequest(CONTEXT_URL + "/services/context/refresh");
+ final WebResponse response = client.getResponse(request);
+ assertThat(response.getResponseCode(), equalTo(204));
+ }
+ }
+}
\ No newline at end of file
diff --git a/systests/transports/src/test/resources/org/apache/cxf/systest/servlet/web-spring-context.xml b/systests/transports/src/test/resources/org/apache/cxf/systest/servlet/web-spring-context.xml
new file mode 100644
index 0000000..281bea6
--- /dev/null
+++ b/systests/transports/src/test/resources/org/apache/cxf/systest/servlet/web-spring-context.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
+<!--
+ 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.
+-->
+<web-app>
+ <context-param>
+ <param-name>contextClass</param-name>
+ <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
+ </context-param>
+ <context-param>
+ <param-name>contextConfigLocation</param-name>
+ <param-value>org.apache.cxf.systest.servlet.SpringServletContextTest$AppConfig</param-value>
+ </context-param>
+ <listener>
+ <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
+ </listener>
+ <servlet>
+ <servlet-name>CXFServlet</servlet-name>
+ <display-name>CXF Servlet</display-name>
+ <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+ <servlet-mapping>
+ <servlet-name>CXFServlet</servlet-name>
+ <url-pattern>/*</url-pattern>
+ </servlet-mapping>
+</web-app>