|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.socket.server.support; |
| 18 | + |
| 19 | +import org.springframework.aot.hint.MemberCategory; |
| 20 | +import org.springframework.aot.hint.ReflectionHints; |
| 21 | +import org.springframework.aot.hint.RuntimeHints; |
| 22 | +import org.springframework.aot.hint.RuntimeHintsRegistrar; |
| 23 | +import org.springframework.aot.hint.TypeReference; |
| 24 | +import org.springframework.util.ClassUtils; |
| 25 | + |
| 26 | +/** |
| 27 | + * {@link RuntimeHintsRegistrar} implementation that registers reflection entries |
| 28 | + * for {@link AbstractHandshakeHandler}. |
| 29 | + * |
| 30 | + * @author Sebastien Deleuze |
| 31 | + * @since 6.0 |
| 32 | + */ |
| 33 | +public class HandshakeHandlerRuntimeHintsRegistrar implements RuntimeHintsRegistrar { |
| 34 | + |
| 35 | + private static final boolean tomcatWsPresent; |
| 36 | + |
| 37 | + private static final boolean jettyWsPresent; |
| 38 | + |
| 39 | + private static final boolean undertowWsPresent; |
| 40 | + |
| 41 | + private static final boolean glassfishWsPresent; |
| 42 | + |
| 43 | + private static final boolean weblogicWsPresent; |
| 44 | + |
| 45 | + private static final boolean websphereWsPresent; |
| 46 | + |
| 47 | + static { |
| 48 | + ClassLoader classLoader = AbstractHandshakeHandler.class.getClassLoader(); |
| 49 | + tomcatWsPresent = ClassUtils.isPresent( |
| 50 | + "org.apache.tomcat.websocket.server.WsHttpUpgradeHandler", classLoader); |
| 51 | + jettyWsPresent = ClassUtils.isPresent( |
| 52 | + "org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer", classLoader); |
| 53 | + undertowWsPresent = ClassUtils.isPresent( |
| 54 | + "io.undertow.websockets.jsr.ServerWebSocketContainer", classLoader); |
| 55 | + glassfishWsPresent = ClassUtils.isPresent( |
| 56 | + "org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler", classLoader); |
| 57 | + weblogicWsPresent = ClassUtils.isPresent( |
| 58 | + "weblogic.websocket.tyrus.TyrusServletWriter", classLoader); |
| 59 | + websphereWsPresent = ClassUtils.isPresent( |
| 60 | + "com.ibm.websphere.wsoc.WsWsocServerContainer", classLoader); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { |
| 65 | + ReflectionHints reflectionHints = hints.reflection(); |
| 66 | + if (tomcatWsPresent) { |
| 67 | + reflectionHints.registerType(TypeReference.of("org.springframework.web.socket.server.standard.TomcatRequestUpgradeStrategy"), |
| 68 | + builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); |
| 69 | + } |
| 70 | + else if (jettyWsPresent) { |
| 71 | + reflectionHints.registerType(TypeReference.of("org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy"), |
| 72 | + builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); |
| 73 | + } |
| 74 | + else if (undertowWsPresent) { |
| 75 | + reflectionHints.registerType(TypeReference.of("org.springframework.web.socket.server.standard.UndertowRequestUpgradeStrategy"), |
| 76 | + builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); |
| 77 | + } |
| 78 | + else if (glassfishWsPresent) { |
| 79 | + reflectionHints.registerType(TypeReference.of("org.springframework.web.socket.server.standard.GlassFishRequestUpgradeStrategy"), |
| 80 | + builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); |
| 81 | + } |
| 82 | + else if (weblogicWsPresent) { |
| 83 | + reflectionHints.registerType(TypeReference.of("org.springframework.web.socket.server.standard.WebLogicRequestUpgradeStrategy"), |
| 84 | + builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); |
| 85 | + } |
| 86 | + else if (websphereWsPresent) { |
| 87 | + reflectionHints.registerType(TypeReference.of("org.springframework.web.socket.server.standard.WebSphereRequestUpgradeStrategy"), |
| 88 | + builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments