001/*
002 * Copyright 2012-2016 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 org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
020import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
021import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
022import org.springframework.core.Ordered;
023import org.springframework.core.ResolvableType;
024
025/**
026 * {@link EmbeddedServletContainerCustomizer} to configure websockets for a given
027 * {@link EmbeddedServletContainerFactory}.
028 *
029 * @param <T> the embedded servlet container factory
030 * @author Dave Syer
031 * @author Phillip Webb
032 * @author Andy Wilkinson
033 * @since 1.2.0
034 */
035public abstract class WebSocketContainerCustomizer<T extends EmbeddedServletContainerFactory>
036                implements EmbeddedServletContainerCustomizer, Ordered {
037
038        @Override
039        public int getOrder() {
040                return 0;
041        }
042
043        @SuppressWarnings("unchecked")
044        @Override
045        public void customize(ConfigurableEmbeddedServletContainer container) {
046                if (getContainerType().isAssignableFrom(container.getClass())) {
047                        doCustomize((T) container);
048                }
049        }
050
051        protected Class<?> getContainerType() {
052                return ResolvableType.forClass(WebSocketContainerCustomizer.class, getClass())
053                                .resolveGeneric();
054        }
055
056        protected abstract void doCustomize(T container);
057
058}