io.undertow.client.UndertowClient Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 io.undertow.client;
import static java.security.AccessController.doPrivileged;
import org.xnio.FutureResult;
import org.xnio.IoFuture;
import org.xnio.OptionMap;
import io.undertow.connector.ByteBufferPool;
import org.xnio.XnioIoThread;
import org.xnio.XnioWorker;
import org.xnio.ssl.XnioSsl;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
/**
* Undertow client class. This class loads {@link ClientProvider} implementations, and uses them to
* create connections to a target.
*
* @author Stuart Douglas
*/
public final class UndertowClient {
private final Map clientProviders;
private static final UndertowClient INSTANCE = new UndertowClient();
private UndertowClient() {
this(UndertowClient.class.getClassLoader());
}
private UndertowClient(final ClassLoader classLoader) {
ServiceLoader providers = doPrivileged((PrivilegedAction>)
() -> ServiceLoader.load(ClientProvider.class, classLoader));
final Map map = new HashMap<>();
for (ClientProvider provider : providers) {
for (String scheme : provider.handlesSchemes()) {
map.put(scheme, provider);
}
}
this.clientProviders = Collections.unmodifiableMap(map);
}
public IoFuture connect(final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
return connect(uri, worker, null, bufferPool, options);
}
public IoFuture connect(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
return connect(bindAddress, uri, worker, null, bufferPool, options);
}
public IoFuture connect(final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
return connect((InetSocketAddress) null, uri, worker, ssl, bufferPool, options);
}
public IoFuture connect(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
final FutureResult result = new FutureResult<>();
provider.connect(new ClientCallback() {
@Override
public void completed(ClientConnection r) {
result.setResult(r);
}
@Override
public void failed(IOException e) {
result.setException(e);
}
}, bindAddress, uri, worker, ssl, bufferPool, options);
return result.getIoFuture();
}
public IoFuture connect(final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
return connect((InetSocketAddress) null, uri, ioThread, null, bufferPool, options);
}
public IoFuture connect(InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
return connect(bindAddress, uri, ioThread, null, bufferPool, options);
}
public IoFuture connect(final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
return connect((InetSocketAddress) null, uri, ioThread, ssl, bufferPool, options);
}
public IoFuture connect(InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
final FutureResult result = new FutureResult<>();
provider.connect(new ClientCallback() {
@Override
public void completed(ClientConnection r) {
result.setResult(r);
}
@Override
public void failed(IOException e) {
result.setException(e);
}
}, bindAddress, uri, ioThread, ssl, bufferPool, options);
return result.getIoFuture();
}
public void connect(final ClientCallback listener, final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
connect(listener, uri, worker, null, bufferPool, options);
}
public void connect(final ClientCallback listener, InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
connect(listener, bindAddress, uri, worker, null, bufferPool, options);
}
public void connect(final ClientCallback listener, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, uri, worker, ssl, bufferPool, options);
}
public void connect(final ClientCallback listener, InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, bindAddress, uri, worker, ssl, bufferPool, options);
}
public void connect(final ClientCallback listener, final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
connect(listener, uri, ioThread, null, bufferPool, options);
}
public void connect(final ClientCallback listener, InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
connect(listener, bindAddress, uri, ioThread, null, bufferPool, options);
}
public void connect(final ClientCallback listener, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, uri, ioThread, ssl, bufferPool, options);
}
public void connect(final ClientCallback listener, InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, bindAddress, uri, ioThread, ssl, bufferPool, options);
}
private ClientProvider getClientProvider(URI uri) {
ClientProvider provider = clientProviders.get(uri.getScheme());
if (provider == null) {
throw UndertowClientMessages.MESSAGES.unknownScheme(uri);
}
return provider;
}
public static UndertowClient getInstance() {
return INSTANCE;
}
public static UndertowClient getInstance(final ClassLoader classLoader) {
return new UndertowClient(classLoader);
}
}