|
35 | 35 | import org.springframework.context.ApplicationContext;
|
36 | 36 | import org.springframework.context.ApplicationContextAware;
|
37 | 37 | import org.springframework.util.Assert;
|
38 |
| -import org.springframework.util.ClassUtils; |
39 | 38 | import org.springframework.util.ReflectionUtils;
|
40 | 39 |
|
41 | 40 | /**
|
|
58 | 57 | */
|
59 | 58 | public class ServerEndpointExporter implements InitializingBean, BeanPostProcessor, ApplicationContextAware {
|
60 | 59 |
|
61 |
| - private static final boolean isServletApiPresent = |
62 |
| - ClassUtils.isPresent("javax.servlet.ServletContext", ServerEndpointExporter.class.getClassLoader()); |
63 |
| - |
64 | 60 | private static Log logger = LogFactory.getLog(ServerEndpointExporter.class);
|
65 | 61 |
|
66 | 62 |
|
@@ -103,20 +99,25 @@ public void setApplicationContext(ApplicationContext applicationContext) {
|
103 | 99 | }
|
104 | 100 |
|
105 | 101 | protected ServerContainer getServerContainer() {
|
106 |
| - if (isServletApiPresent) { |
107 |
| - try { |
108 |
| - Method getter = ReflectionUtils.findMethod(this.applicationContext.getClass(), "getServletContext"); |
109 |
| - Object servletContext = getter.invoke(this.applicationContext); |
110 | 102 |
|
111 |
| - Method attrMethod = ReflectionUtils.findMethod(servletContext.getClass(), "getAttribute", String.class); |
112 |
| - return (ServerContainer) attrMethod.invoke(servletContext, "javax.websocket.server.ServerContainer"); |
113 |
| - } |
114 |
| - catch (Exception ex) { |
115 |
| - throw new IllegalStateException( |
116 |
| - "Failed to get javax.websocket.server.ServerContainer via ServletContext attribute", ex); |
117 |
| - } |
| 103 | + Class<?> servletContextClass; |
| 104 | + try { |
| 105 | + servletContextClass = Class.forName("javax.servlet.ServletContext"); |
| 106 | + } |
| 107 | + catch (Throwable e) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + Method getter = ReflectionUtils.findMethod(this.applicationContext.getClass(), "getServletContext"); |
| 113 | + Object servletContext = getter.invoke(this.applicationContext); |
| 114 | + Method attrMethod = ReflectionUtils.findMethod(servletContextClass, "getAttribute", String.class); |
| 115 | + return (ServerContainer) attrMethod.invoke(servletContext, "javax.websocket.server.ServerContainer"); |
| 116 | + } |
| 117 | + catch (Exception ex) { |
| 118 | + throw new IllegalStateException( |
| 119 | + "Failed to get javax.websocket.server.ServerContainer via ServletContext attribute", ex); |
118 | 120 | }
|
119 |
| - return null; |
120 | 121 | }
|
121 | 122 |
|
122 | 123 | @Override
|
|
0 commit comments