Skip to content

Commit 7507560

Browse files
committed
Reimplemented ServerEndpointExporter to avoid BeanPostProcessor role
Issue: SPR-12340 (cherry picked from commit 10328f1)
1 parent a1c0905 commit 7507560

File tree

3 files changed

+69
-73
lines changed

3 files changed

+69
-73
lines changed

Diff for: spring-websocket/src/main/java/org/springframework/web/socket/server/standard/ServerEndpointExporter.java

+44-59
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Arrays;
2020
import java.util.LinkedHashSet;
2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.Set;
2324
import javax.servlet.ServletContext;
2425
import javax.websocket.DeploymentException;
@@ -27,7 +28,6 @@
2728
import javax.websocket.server.ServerEndpointConfig;
2829

2930
import org.springframework.beans.factory.InitializingBean;
30-
import org.springframework.beans.factory.config.BeanPostProcessor;
3131
import org.springframework.context.ApplicationContext;
3232
import org.springframework.util.Assert;
3333
import org.springframework.web.context.support.WebApplicationObjectSupport;
@@ -41,7 +41,7 @@
4141
*
4242
* <p>When this class is used, by declaring it in Spring configuration, it should be
4343
* possible to turn off a Servlet container's scan for WebSocket endpoints. This can be
44-
* done with the help of the {@code <absolute-ordering>} element in web.xml.
44+
* done with the help of the {@code <absolute-ordering>} element in {@code web.xml}.
4545
*
4646
* @author Rossen Stoyanchev
4747
* @author Juergen Hoeller
@@ -50,19 +50,26 @@
5050
* @see SpringConfigurator
5151
* @see ServletServerContainerFactoryBean
5252
*/
53-
public class ServerEndpointExporter extends WebApplicationObjectSupport implements BeanPostProcessor, InitializingBean {
54-
55-
private ServerContainer serverContainer;
53+
public class ServerEndpointExporter extends WebApplicationObjectSupport implements InitializingBean {
5654

5755
private List<Class<?>> annotatedEndpointClasses;
5856

59-
private Set<Class<?>> annotatedEndpointBeanTypes;
57+
private ServerContainer serverContainer;
58+
6059

60+
/**
61+
* Explicitly list annotated endpoint types that should be registered on startup. This
62+
* can be done if you wish to turn off a Servlet container's scan for endpoints, which
63+
* goes through all 3rd party jars in the, and rely on Spring configuration instead.
64+
* @param annotatedEndpointClasses {@link ServerEndpoint}-annotated types
65+
*/
66+
public void setAnnotatedEndpointClasses(Class<?>... annotatedEndpointClasses) {
67+
this.annotatedEndpointClasses = Arrays.asList(annotatedEndpointClasses);
68+
}
6169

6270
/**
6371
* Set the JSR-356 {@link ServerContainer} to use for endpoint registration.
6472
* If not set, the container is going to be retrieved via the {@code ServletContext}.
65-
* @since 4.1
6673
*/
6774
public void setServerContainer(ServerContainer serverContainer) {
6875
this.serverContainer = serverContainer;
@@ -75,33 +82,6 @@ protected ServerContainer getServerContainer() {
7582
return this.serverContainer;
7683
}
7784

78-
/**
79-
* Explicitly list annotated endpoint types that should be registered on startup. This
80-
* can be done if you wish to turn off a Servlet container's scan for endpoints, which
81-
* goes through all 3rd party jars in the, and rely on Spring configuration instead.
82-
* @param annotatedEndpointClasses {@link ServerEndpoint}-annotated types
83-
*/
84-
public void setAnnotatedEndpointClasses(Class<?>... annotatedEndpointClasses) {
85-
this.annotatedEndpointClasses = Arrays.asList(annotatedEndpointClasses);
86-
}
87-
88-
@Override
89-
protected void initApplicationContext(ApplicationContext context) {
90-
// Initializes ServletContext given a WebApplicationContext
91-
super.initApplicationContext(context);
92-
93-
// Retrieve beans which are annotated with @ServerEndpoint
94-
this.annotatedEndpointBeanTypes = new LinkedHashSet<Class<?>>();
95-
String[] beanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class);
96-
for (String beanName : beanNames) {
97-
Class<?> beanType = context.getType(beanName);
98-
if (logger.isInfoEnabled()) {
99-
logger.info("Detected @ServerEndpoint bean '" + beanName + "', registering it as an endpoint by type");
100-
}
101-
this.annotatedEndpointBeanTypes.add(beanType);
102-
}
103-
}
104-
10585
@Override
10686
protected void initServletContext(ServletContext servletContext) {
10787
if (this.serverContainer == null) {
@@ -110,64 +90,69 @@ protected void initServletContext(ServletContext servletContext) {
11090
}
11191
}
11292

93+
@Override
94+
protected boolean isContextRequired() {
95+
return false;
96+
}
11397

11498
@Override
11599
public void afterPropertiesSet() {
116100
Assert.state(getServerContainer() != null, "javax.websocket.server.ServerContainer not available");
117101
registerEndpoints();
118102
}
119103

104+
120105
/**
121106
* Actually register the endpoints. Called by {@link #afterPropertiesSet()}.
122-
* @since 4.1
123107
*/
124108
protected void registerEndpoints() {
125109
Set<Class<?>> endpointClasses = new LinkedHashSet<Class<?>>();
126110
if (this.annotatedEndpointClasses != null) {
127111
endpointClasses.addAll(this.annotatedEndpointClasses);
128112
}
129-
if (this.annotatedEndpointBeanTypes != null) {
130-
endpointClasses.addAll(this.annotatedEndpointBeanTypes);
113+
114+
ApplicationContext context = getApplicationContext();
115+
if (context != null) {
116+
String[] endpointBeanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class);
117+
for (String beanName : endpointBeanNames) {
118+
endpointClasses.add(context.getType(beanName));
119+
}
131120
}
121+
132122
for (Class<?> endpointClass : endpointClasses) {
133123
registerEndpoint(endpointClass);
134124
}
125+
126+
if (context != null) {
127+
Map<String, ServerEndpointConfig> endpointConfigMap = context.getBeansOfType(ServerEndpointConfig.class);
128+
for (ServerEndpointConfig endpointConfig : endpointConfigMap.values()) {
129+
registerEndpoint(endpointConfig);
130+
}
131+
}
135132
}
136133

137134
private void registerEndpoint(Class<?> endpointClass) {
138135
try {
139136
if (logger.isInfoEnabled()) {
140-
logger.info("Registering @ServerEndpoint type: " + endpointClass);
137+
logger.info("Registering @ServerEndpoint class: " + endpointClass);
141138
}
142139
getServerContainer().addEndpoint(endpointClass);
143140
}
144141
catch (DeploymentException ex) {
145-
throw new IllegalStateException("Failed to register @ServerEndpoint type " + endpointClass, ex);
142+
throw new IllegalStateException("Failed to register @ServerEndpoint class: " + endpointClass, ex);
146143
}
147144
}
148145

149-
150-
@Override
151-
public Object postProcessBeforeInitialization(Object bean, String beanName) {
152-
return bean;
153-
}
154-
155-
@Override
156-
public Object postProcessAfterInitialization(Object bean, String beanName) {
157-
if (bean instanceof ServerEndpointConfig) {
158-
ServerEndpointConfig endpointConfig = (ServerEndpointConfig) bean;
159-
try {
160-
if (logger.isInfoEnabled()) {
161-
logger.info("Registering bean '" + beanName +
162-
"' as javax.websocket.Endpoint under path " + endpointConfig.getPath());
163-
}
164-
getServerContainer().addEndpoint(endpointConfig);
165-
}
166-
catch (DeploymentException ex) {
167-
throw new IllegalStateException("Failed to deploy Endpoint bean with name '" + bean + "'", ex);
146+
private void registerEndpoint(ServerEndpointConfig endpointConfig) {
147+
try {
148+
if (logger.isInfoEnabled()) {
149+
logger.info("Registering ServerEndpointConfig: " + endpointConfig);
168150
}
151+
getServerContainer().addEndpoint(endpointConfig);
152+
}
153+
catch (DeploymentException ex) {
154+
throw new IllegalStateException("Failed to register ServerEndpointConfig: " + endpointConfig, ex);
169155
}
170-
return bean;
171156
}
172157

173158
}

Diff for: spring-websocket/src/main/java/org/springframework/web/socket/server/standard/ServerEndpointRegistration.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -73,7 +73,7 @@ public class ServerEndpointRegistration extends ServerEndpointConfig.Configurato
7373

7474
/**
7575
* Create a new {@link ServerEndpointRegistration} instance from an
76-
* {@code javax.webscoket.Endpoint} class.
76+
* {@code javax.websocket.Endpoint} class.
7777
* @param path the endpoint path
7878
* @param endpointClass the endpoint class
7979
*/
@@ -202,4 +202,9 @@ public List<Extension> getNegotiatedExtensions(List<Extension> installed, List<E
202202
return super.getNegotiatedExtensions(installed, requested);
203203
}
204204

205+
206+
@Override
207+
public String toString() {
208+
return "ServerEndpointRegistration for path '" + getPath() + "': " + getEndpointClass();
209+
}
205210
}

Diff for: spring-websocket/src/test/java/org/springframework/web/socket/server/standard/ServerEndpointExporterTests.java

+18-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -67,7 +67,7 @@ public void setup() {
6767

6868

6969
@Test
70-
public void addAnnotatedEndpointBeans() throws Exception {
70+
public void addAnnotatedEndpointClasses() throws Exception {
7171
this.exporter.setAnnotatedEndpointClasses(AnnotatedDummyEndpoint.class);
7272
this.exporter.setApplicationContext(this.webAppContext);
7373
this.exporter.afterPropertiesSet();
@@ -77,7 +77,7 @@ public void addAnnotatedEndpointBeans() throws Exception {
7777
}
7878

7979
@Test
80-
public void addAnnotatedEndpointBeansWithServletContextOnly() throws Exception {
80+
public void addAnnotatedEndpointClassesWithServletContextOnly() throws Exception {
8181
this.exporter.setAnnotatedEndpointClasses(AnnotatedDummyEndpoint.class, AnnotatedDummyEndpointBean.class);
8282
this.exporter.setServletContext(this.servletContext);
8383
this.exporter.afterPropertiesSet();
@@ -87,7 +87,7 @@ public void addAnnotatedEndpointBeansWithServletContextOnly() throws Exception {
8787
}
8888

8989
@Test
90-
public void addAnnotatedEndpointBeansWithServerContainerOnly() throws Exception {
90+
public void addAnnotatedEndpointClassesWithExplicitServerContainerOnly() throws Exception {
9191
this.exporter.setAnnotatedEndpointClasses(AnnotatedDummyEndpoint.class, AnnotatedDummyEndpointBean.class);
9292
this.exporter.setServerContainer(this.serverContainer);
9393
this.exporter.afterPropertiesSet();
@@ -98,31 +98,37 @@ public void addAnnotatedEndpointBeansWithServerContainerOnly() throws Exception
9898

9999
@Test
100100
public void addServerEndpointConfigBean() throws Exception {
101+
ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration("/dummy", new DummyEndpoint());
102+
this.webAppContext.getBeanFactory().registerSingleton("dummyEndpoint", endpointRegistration);
103+
101104
this.exporter.setApplicationContext(this.webAppContext);
102105
this.exporter.afterPropertiesSet();
103106

104-
ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration("/dummy", new DummyEndpoint());
105-
this.exporter.postProcessAfterInitialization(endpointRegistration, "dummyEndpoint");
106107
verify(this.serverContainer).addEndpoint(endpointRegistration);
107108
}
108109

109110
@Test
110-
public void addServerEndpointConfigBeanWithServletContextOnly() throws Exception {
111+
public void addServerEndpointConfigBeanWithExplicitServletContext() throws Exception {
112+
ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration("/dummy", new DummyEndpoint());
113+
this.webAppContext.getBeanFactory().registerSingleton("dummyEndpoint", endpointRegistration);
114+
111115
this.exporter.setServletContext(this.servletContext);
116+
this.exporter.setApplicationContext(this.webAppContext);
112117
this.exporter.afterPropertiesSet();
113118

114-
ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration("/dummy", new DummyEndpoint());
115-
this.exporter.postProcessAfterInitialization(endpointRegistration, "dummyEndpoint");
116119
verify(this.serverContainer).addEndpoint(endpointRegistration);
117120
}
118121

119122
@Test
120-
public void addServerEndpointConfigBeanWithServerContainerOnly() throws Exception {
123+
public void addServerEndpointConfigBeanWithExplicitServerContainer() throws Exception {
124+
ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration("/dummy", new DummyEndpoint());
125+
this.webAppContext.getBeanFactory().registerSingleton("dummyEndpoint", endpointRegistration);
126+
this.servletContext.removeAttribute("javax.websocket.server.ServerContainer");
127+
121128
this.exporter.setServerContainer(this.serverContainer);
129+
this.exporter.setApplicationContext(this.webAppContext);
122130
this.exporter.afterPropertiesSet();
123131

124-
ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration("/dummy", new DummyEndpoint());
125-
this.exporter.postProcessAfterInitialization(endpointRegistration, "dummyEndpoint");
126132
verify(this.serverContainer).addEndpoint(endpointRegistration);
127133
}
128134

0 commit comments

Comments
 (0)