ServerEndpoint实例化
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
ConfiguredServerEndpoint config = HandshakeUtil.getConfig(channel);
try {
if(container.isClosed()) {
//if the underlying container is closed we just reject
channel.sendClose();
channel.resumeReceives();
return;
}
InstanceFactory<?> endpointFactory = config.getEndpointFactory();
ServerEndpointConfig.Configurator configurator = config.getEndpointConfiguration().getConfigurator();
final InstanceHandle<?> instance;
DefaultContainerConfigurator.setCurrentInstanceFactory(endpointFactory);
final Object instanceFromConfigurator = configurator.getEndpointInstance(config.getEndpointConfiguration().getEndpointClass());
final InstanceHandle<?> factoryInstance = DefaultContainerConfigurator.clearCurrentInstanceFactory();
if (factoryInstance == null) {
instance = new ImmediateInstanceHandle<>(instanceFromConfigurator);
} else if (factoryInstance.getInstance() == instanceFromConfigurator) {
instance = factoryInstance;
} else {
//the default instance has been wrapped
instance = new InstanceHandle<Object>() {
@Override
public Object getInstance() {
return instanceFromConfigurator;
}
@Override
public void release() {
factoryInstance.release();
}
};
}
ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
Principal principal = exchange.getAttachment(HandshakeUtil.PRINCIPAL);
if(principal == null) {
if(src.getServletRequest() instanceof HttpServletRequest) {
principal = ((HttpServletRequest)src.getServletRequest()).getUserPrincipal();
} else {
principal = src.getOriginalRequest().getUserPrincipal();
}
}
final InstanceHandle<Endpoint> endpointInstance;
if(config.getAnnotatedEndpointFactory() != null) {
final AnnotatedEndpoint annotated = config.getAnnotatedEndpointFactory().createInstance(instance);
endpointInstance = new InstanceHandle<Endpoint>() {
@Override
public Endpoint getInstance() {
return annotated;
}
@Override
public void release() {
instance.release();
}
};
} else {
endpointInstance = (InstanceHandle<Endpoint>) instance;
}
UndertowSession session = new UndertowSession(channel, URI.create(exchange.getRequestURI()), exchange.getAttachment(HandshakeUtil.PATH_PARAMS), exchange.getRequestParameters(), this, principal, endpointInstance, config.getEndpointConfiguration(), exchange.getQueryString(), config.getEncodingFactory().createEncoding(config.getEndpointConfiguration()), config, channel.getSubProtocol(), Collections.<Extension>emptyList(), null);
config.addOpenSession(session);
session.setMaxBinaryMessageBufferSize(getContainer().getDefaultMaxBinaryMessageBufferSize());
session.setMaxTextMessageBufferSize(getContainer().getDefaultMaxTextMessageBufferSize());
session.setMaxIdleTimeout(getContainer().getDefaultMaxSessionIdleTimeout());
session.getAsyncRemote().setSendTimeout(getContainer().getDefaultAsyncSendTimeout());
try {
endpointInstance.getInstance().onOpen(session, config.getEndpointConfiguration());
} catch (Exception e) {
endpointInstance.getInstance().onError(session, e);
IoUtils.safeClose(session);
}
channel.resumeReceives();
} catch (Exception e) {
JsrWebSocketLogger.REQUEST_LOGGER.endpointCreationFailed(e);
IoUtils.safeClose(channel);
}
}
@Override
public <T> T getEndpointInstance(final Class<T> endpointClass) throws InstantiationException {
InstanceFactory<?> factory = currentInstanceFactory.get();
if(factory != null) {
InstanceHandle<?> instance = factory.createInstance();
currentInstanceHandle.set(instance);
return (T) instance.getInstance();
}
try {
return endpointClass.newInstance();
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public <T> T getEndpointInstance(Class<T> clazz)
throws InstantiationException {
try {
return clazz.getConstructor().newInstance();
} catch (InstantiationException e) {
throw e;
} catch (ReflectiveOperationException e) {
InstantiationException ie = new InstantiationException();
ie.initCause(e);
throw ie;
}
}
注意:本文归作者所有,未经作者允许,不得转载