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