Browse Source
HandlerProvider is now an interface that can be used to plug in WebSocket handlers with per-connection scope semantics. There are two implementations, of the interface, one simple and a second that creates handler instances through AutowireCapableBeanFactory. HandlerProvider also provides a destroy method that is used to apply a destroy callback whenever a client connection closes.pull/292/head
34 changed files with 373 additions and 369 deletions
@ -1,88 +0,0 @@
@@ -1,88 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.websocket.server.support; |
||||
|
||||
import javax.websocket.WebSocketContainer; |
||||
|
||||
import org.springframework.beans.factory.FactoryBean; |
||||
import org.springframework.beans.factory.InitializingBean; |
||||
|
||||
|
||||
/** |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public abstract class AbstractEndpointContainerFactoryBean implements FactoryBean<WebSocketContainer>, InitializingBean { |
||||
|
||||
private WebSocketContainer container; |
||||
|
||||
|
||||
public void setAsyncSendTimeout(long timeoutInMillis) { |
||||
this.container.setAsyncSendTimeout(timeoutInMillis); |
||||
} |
||||
|
||||
public long getAsyncSendTimeout() { |
||||
return this.container.getDefaultAsyncSendTimeout(); |
||||
} |
||||
|
||||
public void setMaxSessionIdleTimeout(long timeoutInMillis) { |
||||
this.container.setDefaultMaxSessionIdleTimeout(timeoutInMillis); |
||||
} |
||||
|
||||
public long getMaxSessionIdleTimeout() { |
||||
return this.container.getDefaultMaxSessionIdleTimeout(); |
||||
} |
||||
|
||||
public void setMaxTextMessageBufferSize(int bufferSize) { |
||||
this.container.setDefaultMaxTextMessageBufferSize(bufferSize); |
||||
} |
||||
|
||||
public int getMaxTextMessageBufferSize() { |
||||
return this.container.getDefaultMaxTextMessageBufferSize(); |
||||
} |
||||
|
||||
public void setMaxBinaryMessageBufferSize(int bufferSize) { |
||||
this.container.setDefaultMaxBinaryMessageBufferSize(bufferSize); |
||||
} |
||||
|
||||
public int getMaxBinaryMessageBufferSize() { |
||||
return this.container.getDefaultMaxBinaryMessageBufferSize(); |
||||
} |
||||
|
||||
@Override |
||||
public void afterPropertiesSet() throws Exception { |
||||
this.container = getContainer(); |
||||
} |
||||
|
||||
protected abstract WebSocketContainer getContainer(); |
||||
|
||||
@Override |
||||
public WebSocketContainer getObject() throws Exception { |
||||
return this.container; |
||||
} |
||||
|
||||
@Override |
||||
public Class<?> getObjectType() { |
||||
return WebSocketContainer.class; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isSingleton() { |
||||
return true; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,94 @@
@@ -0,0 +1,94 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.websocket.support; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.springframework.beans.BeanUtils; |
||||
import org.springframework.beans.BeansException; |
||||
import org.springframework.beans.factory.BeanFactory; |
||||
import org.springframework.beans.factory.BeanFactoryAware; |
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.websocket.HandlerProvider; |
||||
|
||||
|
||||
/** |
||||
* A {@link HandlerProvider} that uses {@link AutowireCapableBeanFactory#createBean(Class) |
||||
* creating a fresh instance every time #getHandler() is called. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class BeanCreatingHandlerProvider<T> implements HandlerProvider<T>, BeanFactoryAware { |
||||
|
||||
private static final Log logger = LogFactory.getLog(BeanCreatingHandlerProvider.class); |
||||
|
||||
private final Class<? extends T> handlerClass; |
||||
|
||||
private AutowireCapableBeanFactory beanFactory; |
||||
|
||||
|
||||
public BeanCreatingHandlerProvider(Class<? extends T> handlerClass) { |
||||
Assert.notNull(handlerClass, "handlerClass is required"); |
||||
this.handlerClass = handlerClass; |
||||
} |
||||
|
||||
@Override |
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
||||
if (beanFactory instanceof AutowireCapableBeanFactory) { |
||||
this.beanFactory = (AutowireCapableBeanFactory) beanFactory; |
||||
} |
||||
} |
||||
|
||||
public boolean isSingleton() { |
||||
return false; |
||||
} |
||||
|
||||
public Class<? extends T> getHandlerType() { |
||||
return this.handlerClass; |
||||
} |
||||
|
||||
public T getHandler() { |
||||
if (logger.isTraceEnabled()) { |
||||
logger.trace("Creating instance for handler type " + this.handlerClass); |
||||
} |
||||
if (this.beanFactory == null) { |
||||
logger.warn("No BeanFactory available, attempting to use default constructor"); |
||||
return BeanUtils.instantiate(this.handlerClass); |
||||
} |
||||
else { |
||||
return this.beanFactory.createBean(this.handlerClass); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void destroy(T handler) { |
||||
if (this.beanFactory != null) { |
||||
if (logger.isTraceEnabled()) { |
||||
logger.trace("Destroying handler instance " + handler); |
||||
} |
||||
this.beanFactory.destroyBean(handler); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "BeanCreatingHandlerProvider [handlerClass=" + handlerClass + "]"; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.websocket.support; |
||||
|
||||
import org.springframework.util.ClassUtils; |
||||
import org.springframework.websocket.HandlerProvider; |
||||
|
||||
|
||||
/** |
||||
* A {@link HandlerProvider} that returns a singleton instance. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class SimpleHandlerProvider<T> implements HandlerProvider<T> { |
||||
|
||||
private final T handler; |
||||
|
||||
|
||||
public SimpleHandlerProvider(T handler) { |
||||
this.handler = handler; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isSingleton() { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public Class<?> getHandlerType() { |
||||
return ClassUtils.getUserClass(this.handler); |
||||
} |
||||
|
||||
@Override |
||||
public T getHandler() { |
||||
return this.handler; |
||||
} |
||||
|
||||
@Override |
||||
public void destroy(T handler) { |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "SimpleHandlerProvider [handler=" + handler + "]"; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue