001/*
002 * Copyright 2002-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 *      https://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.web.socket.server.standard;
018
019import java.util.Arrays;
020import java.util.Map;
021import java.util.concurrent.ConcurrentHashMap;
022import javax.websocket.server.ServerEndpoint;
023import javax.websocket.server.ServerEndpointConfig.Configurator;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027
028import org.springframework.core.annotation.AnnotationUtils;
029import org.springframework.stereotype.Component;
030import org.springframework.util.ClassUtils;
031import org.springframework.util.ObjectUtils;
032import org.springframework.web.context.ContextLoader;
033import org.springframework.web.context.WebApplicationContext;
034
035/**
036 * A {@link javax.websocket.server.ServerEndpointConfig.Configurator} for initializing
037 * {@link ServerEndpoint}-annotated classes through Spring.
038 *
039 * <p>
040 * <pre class="code">
041 * &#064;ServerEndpoint(value = "/echo", configurator = SpringConfigurator.class)
042 * public class EchoEndpoint {
043 *     // ...
044 * }
045 * </pre>
046 *
047 * @author Rossen Stoyanchev
048 * @since 4.0
049 * @see ServerEndpointExporter
050 */
051public class SpringConfigurator extends Configurator {
052
053        private static final String NO_VALUE = ObjectUtils.identityToString(new Object());
054
055        private static final Log logger = LogFactory.getLog(SpringConfigurator.class);
056
057        private static final Map<String, Map<Class<?>, String>> cache =
058                        new ConcurrentHashMap<String, Map<Class<?>, String>>();
059
060
061        @SuppressWarnings("unchecked")
062        @Override
063        public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
064                WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
065                if (wac == null) {
066                        String message = "Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?";
067                        logger.error(message);
068                        throw new IllegalStateException(message);
069                }
070
071                String beanName = ClassUtils.getShortNameAsProperty(endpointClass);
072                if (wac.containsBean(beanName)) {
073                        T endpoint = wac.getBean(beanName, endpointClass);
074                        if (logger.isTraceEnabled()) {
075                                logger.trace("Using @ServerEndpoint singleton " + endpoint);
076                        }
077                        return endpoint;
078                }
079
080                Component ann = AnnotationUtils.findAnnotation(endpointClass, Component.class);
081                if (ann != null && wac.containsBean(ann.value())) {
082                        T endpoint = wac.getBean(ann.value(), endpointClass);
083                        if (logger.isTraceEnabled()) {
084                                logger.trace("Using @ServerEndpoint singleton " + endpoint);
085                        }
086                        return endpoint;
087                }
088
089                beanName = getBeanNameByType(wac, endpointClass);
090                if (beanName != null) {
091                        return (T) wac.getBean(beanName);
092                }
093
094                if (logger.isTraceEnabled()) {
095                        logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass);
096                }
097                return wac.getAutowireCapableBeanFactory().createBean(endpointClass);
098        }
099
100        private String getBeanNameByType(WebApplicationContext wac, Class<?> endpointClass) {
101                String wacId = wac.getId();
102
103                Map<Class<?>, String> beanNamesByType = cache.get(wacId);
104                if (beanNamesByType == null) {
105                        beanNamesByType = new ConcurrentHashMap<Class<?>, String>();
106                        cache.put(wacId, beanNamesByType);
107                }
108
109                if (!beanNamesByType.containsKey(endpointClass)) {
110                        String[] names = wac.getBeanNamesForType(endpointClass);
111                        if (names.length == 1) {
112                                beanNamesByType.put(endpointClass, names[0]);
113                        }
114                        else {
115                                beanNamesByType.put(endpointClass, NO_VALUE);
116                                if (names.length > 1) {
117                                        throw new IllegalStateException("Found multiple @ServerEndpoint's of type [" +
118                                                        endpointClass.getName() + "]: bean names " + Arrays.asList(names));
119                                }
120                        }
121                }
122
123                String beanName = beanNamesByType.get(endpointClass);
124                return (NO_VALUE.equals(beanName) ? null : beanName);
125        }
126
127}