001/*
002 * Copyright 2012-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.websocket;
018
019import java.lang.reflect.Constructor;
020
021import org.apache.catalina.Context;
022
023import org.springframework.beans.BeanUtils;
024import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
025import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
026import org.springframework.util.ClassUtils;
027import org.springframework.util.ReflectionUtils;
028
029/**
030 * {@link WebSocketContainerCustomizer} for {@link TomcatEmbeddedServletContainerFactory}.
031 *
032 * @author Dave Syer
033 * @author Phillip Webb
034 * @author Andy Wilkinson
035 * @since 1.2.0
036 */
037public class TomcatWebSocketContainerCustomizer
038                extends WebSocketContainerCustomizer<TomcatEmbeddedServletContainerFactory> {
039
040        private static final String TOMCAT_7_LISTENER_TYPE = "org.apache.catalina.deploy.ApplicationListener";
041
042        private static final String TOMCAT_8_LISTENER_TYPE = "org.apache.tomcat.util.descriptor.web.ApplicationListener";
043
044        private static final String WS_LISTENER = "org.apache.tomcat.websocket.server.WsContextListener";
045
046        @Override
047        public void doCustomize(TomcatEmbeddedServletContainerFactory tomcatContainer) {
048                tomcatContainer.addContextCustomizers(new TomcatContextCustomizer() {
049                        @Override
050                        public void customize(Context context) {
051                                addListener(context, findListenerType());
052                        }
053                });
054        }
055
056        private Class<?> findListenerType() {
057                if (ClassUtils.isPresent(TOMCAT_7_LISTENER_TYPE, null)) {
058                        return ClassUtils.resolveClassName(TOMCAT_7_LISTENER_TYPE, null);
059                }
060                if (ClassUtils.isPresent(TOMCAT_8_LISTENER_TYPE, null)) {
061                        return ClassUtils.resolveClassName(TOMCAT_8_LISTENER_TYPE, null);
062                }
063                // With Tomcat 8.0.8 ApplicationListener is not required
064                return null;
065        }
066
067        /**
068         * Instead of registering the WsSci directly as a ServletContainerInitializer, we use
069         * the ApplicationListener provided by Tomcat. Unfortunately the ApplicationListener
070         * class moved packages in Tomcat 8 and been deleted in 8.0.8 so we have to use
071         * reflection.
072         * @param context the current context
073         * @param listenerType the type of listener to add
074         */
075        private void addListener(Context context, Class<?> listenerType) {
076                Class<? extends Context> contextClass = context.getClass();
077                if (listenerType == null) {
078                        ReflectionUtils.invokeMethod(ClassUtils.getMethod(contextClass,
079                                        "addApplicationListener", String.class), context, WS_LISTENER);
080
081                }
082                else {
083                        Constructor<?> constructor = ClassUtils
084                                        .getConstructorIfAvailable(listenerType, String.class, boolean.class);
085                        Object instance = BeanUtils.instantiateClass(constructor, WS_LISTENER, false);
086                        ReflectionUtils.invokeMethod(ClassUtils.getMethod(contextClass,
087                                        "addApplicationListener", listenerType), context, instance);
088                }
089        }
090
091}