Search in sources :

Example 1 with ConnectionBuilder

use of org.jboss.remoting3.ConnectionBuilder in project ovirt-engine-sdk-java by oVirt.

the class VmBackup method run.

/**
 * This is the non-static method equivalent to {@code main}. Its purpose is to create a connection to the server and
 * make sure that it is always closed, regardless of what happens during the execution of the actual work.
 */
private void run() throws Exception {
    // Connect to the server:
    ConnectionBuilder builder = ConnectionBuilder.connection().url(API_URL).user(API_USER).password(API_PASSWORD).trustStoreFile(API_TRUSTSTORE);
    // Do the rest of the work and always remember to close the connection:
    try (Connection connection = builder.build()) {
        log.info("Connected to the server.");
        run(connection.systemService());
    }
}
Also used : Connection(org.ovirt.engine.sdk4.Connection) ConnectionBuilder(org.ovirt.engine.sdk4.ConnectionBuilder)

Example 2 with ConnectionBuilder

use of org.jboss.remoting3.ConnectionBuilder in project jboss-remoting by jboss-remoting.

the class EndpointImpl method construct.

static EndpointImpl construct(final EndpointBuilder endpointBuilder) throws IOException {
    final String endpointName = endpointBuilder.getEndpointName();
    final List<ConnectionProviderFactoryBuilder> factoryBuilders = endpointBuilder.getConnectionProviderFactoryBuilders();
    final EndpointImpl endpoint;
    OptionMap defaultConnectionOptionMap = endpointBuilder.getDefaultConnectionOptionMap();
    final List<ConnectionBuilder> connectionBuilders = endpointBuilder.getConnectionBuilders();
    final Map<URI, OptionMap> connectionOptions = new HashMap<>();
    if (connectionBuilders != null)
        for (ConnectionBuilder connectionBuilder : connectionBuilders) {
            final URI destination = connectionBuilder.getDestination();
            final OptionMap.Builder optionBuilder = OptionMap.builder();
            if (connectionBuilder.getHeartbeatInterval() != -1) {
                optionBuilder.set(RemotingOptions.HEARTBEAT_INTERVAL, connectionBuilder.getHeartbeatInterval());
            } else {
                optionBuilder.set(RemotingOptions.HEARTBEAT_INTERVAL, defaultConnectionOptionMap.get(RemotingOptions.HEARTBEAT_INTERVAL));
            }
            if (connectionBuilder.getReadTimeout() != -1) {
                optionBuilder.set(Options.READ_TIMEOUT, connectionBuilder.getReadTimeout());
            } else {
                optionBuilder.set(Options.READ_TIMEOUT, defaultConnectionOptionMap.get(Options.READ_TIMEOUT));
            }
            if (connectionBuilder.getWriteTimeout() != -1) {
                optionBuilder.set(Options.WRITE_TIMEOUT, connectionBuilder.getWriteTimeout());
            } else {
                optionBuilder.set(Options.WRITE_TIMEOUT, defaultConnectionOptionMap.get(Options.WRITE_TIMEOUT));
            }
            if (connectionBuilder.getIPTrafficClass() != -1) {
                optionBuilder.set(Options.IP_TRAFFIC_CLASS, connectionBuilder.getIPTrafficClass());
            }
            if (connectionBuilder.isSetTcpKeepAlive()) {
                optionBuilder.set(Options.KEEP_ALIVE, connectionBuilder.isTcpKeepAlive());
            } else {
                optionBuilder.set(Options.KEEP_ALIVE, defaultConnectionOptionMap.get(Options.KEEP_ALIVE));
            }
            connectionOptions.put(destination, optionBuilder.getMap());
        }
    XnioWorker xnioWorker = endpointBuilder.getXnioWorker();
    if (xnioWorker == null) {
        final XnioWorker.Builder workerBuilder = endpointBuilder.getWorkerBuilder();
        if (workerBuilder == null) {
            xnioWorker = XnioWorker.getContextManager().get();
            endpoint = new EndpointImpl(xnioWorker, false, endpointName, connectionOptions, defaultConnectionOptionMap);
        } else {
            final AtomicReference<EndpointImpl> endpointRef = new AtomicReference<EndpointImpl>();
            workerBuilder.setDaemon(true);
            workerBuilder.setWorkerName(endpointName == null ? "Remoting (anonymous)" : "Remoting \"" + endpointName + "\"");
            workerBuilder.setTerminationTask(() -> {
                final EndpointImpl e = endpointRef.getAndSet(null);
                if (e != null) {
                    e.closeComplete();
                }
            });
            xnioWorker = workerBuilder.build();
            endpointRef.set(endpoint = new EndpointImpl(xnioWorker, true, endpointName, connectionOptions.isEmpty() ? Collections.emptyMap() : connectionOptions, defaultConnectionOptionMap));
        }
    } else {
        endpoint = new EndpointImpl(xnioWorker, false, endpointName, connectionOptions.isEmpty() ? Collections.emptyMap() : connectionOptions, defaultConnectionOptionMap);
    }
    boolean ok = false;
    try {
        if (factoryBuilders != null)
            for (ConnectionProviderFactoryBuilder factoryBuilder : factoryBuilders) {
                final String className = factoryBuilder.getClassName();
                final String moduleName = factoryBuilder.getModuleName();
                final ClassLoader classLoader;
                if (moduleName != null) {
                    classLoader = ModuleLoader.getClassLoaderFromModule(moduleName);
                } else if (className == null) {
                    throw new IllegalArgumentException("Either class or module name required for connection provider factory");
                } else {
                    classLoader = EndpointImpl.class.getClassLoader();
                }
                if (className == null) {
                    final ServiceLoader<ConnectionProviderFactory> loader = ServiceLoader.load(ConnectionProviderFactory.class, classLoader);
                    for (ConnectionProviderFactory factory : loader) {
                        endpoint.addConnectionProvider(factoryBuilder.getScheme(), factory, OptionMap.EMPTY);
                        for (String alias : factoryBuilder.getAliases()) {
                            endpoint.addConnectionProvider(alias, factory, OptionMap.EMPTY);
                        }
                    }
                } else
                    try {
                        final Class<? extends ConnectionProviderFactory> factoryClass = classLoader.loadClass(className).asSubclass(ConnectionProviderFactory.class);
                        final ConnectionProviderFactory factory = factoryClass.newInstance();
                        endpoint.addConnectionProvider(factoryBuilder.getScheme(), factory, OptionMap.EMPTY);
                        for (String alias : factoryBuilder.getAliases()) {
                            endpoint.addConnectionProvider(alias, factory, OptionMap.EMPTY);
                        }
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                        throw new IllegalArgumentException("Unable to load connection provider factory class '" + className + "'", e);
                    }
            }
        // remote (SSL is explicit in URL)
        final RemoteConnectionProviderFactory remoteConnectionProviderFactory = new RemoteConnectionProviderFactory();
        endpoint.addConnectionProvider("remote", remoteConnectionProviderFactory, OptionMap.create(Options.SSL_ENABLED, Boolean.TRUE, Options.SSL_STARTTLS, Boolean.TRUE));
        endpoint.addConnectionProvider("remote+tls", remoteConnectionProviderFactory, OptionMap.create(Options.SECURE, Boolean.TRUE));
        // old (SSL is config-based)
        endpoint.addConnectionProvider("remoting", remoteConnectionProviderFactory, OptionMap.create(Options.SSL_ENABLED, Boolean.TRUE, Options.SSL_STARTTLS, Boolean.TRUE));
        // http - SSL is handled by the HTTP layer
        final HttpUpgradeConnectionProviderFactory httpUpgradeConnectionProviderFactory = new HttpUpgradeConnectionProviderFactory();
        endpoint.addConnectionProvider("remote+http", httpUpgradeConnectionProviderFactory, OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE, Options.SSL_STARTTLS, Boolean.TRUE));
        endpoint.addConnectionProvider("remote+https", httpUpgradeConnectionProviderFactory, OptionMap.create(Options.SECURE, Boolean.TRUE));
        // old
        endpoint.addConnectionProvider("http-remoting", httpUpgradeConnectionProviderFactory, OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE, Options.SSL_STARTTLS, Boolean.TRUE));
        endpoint.addConnectionProvider("https-remoting", httpUpgradeConnectionProviderFactory, OptionMap.create(Options.SECURE, Boolean.TRUE));
        ok = true;
        return endpoint;
    } finally {
        if (!ok)
            endpoint.closeAsync();
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) XnioWorker(org.xnio.XnioWorker) URI(java.net.URI) ConnectionProviderFactory(org.jboss.remoting3.spi.ConnectionProviderFactory) HttpUpgradeConnectionProviderFactory(org.jboss.remoting3.remote.HttpUpgradeConnectionProviderFactory) RemoteConnectionProviderFactory(org.jboss.remoting3.remote.RemoteConnectionProviderFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServiceLoader(java.util.ServiceLoader) OptionMap(org.xnio.OptionMap) RemoteConnectionProviderFactory(org.jboss.remoting3.remote.RemoteConnectionProviderFactory) HttpUpgradeConnectionProviderFactory(org.jboss.remoting3.remote.HttpUpgradeConnectionProviderFactory)

Example 3 with ConnectionBuilder

use of org.jboss.remoting3.ConnectionBuilder in project jboss-ejb-client by wildfly.

the class RemotingLegacyConfiguration method getConfiguredEndpoint.

public Endpoint getConfiguredEndpoint() {
    final JBossEJBProperties properties = JBossEJBProperties.getCurrent();
    if (properties == null) {
        return null;
    }
    Logs.MAIN.legacyEJBPropertiesRemotingConfigurationInUse();
    final EndpointBuilder endpointBuilder = Endpoint.builder();
    final String endpointName = properties.getEndpointName();
    if (endpointName != null) {
        endpointBuilder.setEndpointName(endpointName);
    }
    OptionMap endpointCreationOptions = properties.getEndpointCreationOptions();
    if (endpointCreationOptions != null && endpointCreationOptions.size() > 0) {
        if (!endpointCreationOptions.contains(Options.THREAD_DAEMON)) {
            endpointCreationOptions = OptionMap.builder().addAll(endpointCreationOptions).set(Options.THREAD_DAEMON, true).getMap();
        }
        endpointBuilder.buildXnioWorker(Xnio.getInstance()).populateFromOptions(endpointCreationOptions);
    }
    final List<JBossEJBProperties.ConnectionConfiguration> connectionList = properties.getConnectionList();
    List<URI> uris = new ArrayList<URI>();
    for (JBossEJBProperties.ConnectionConfiguration connectionConfiguration : connectionList) {
        final OptionMap connectionOptions = connectionConfiguration.getConnectionOptions();
        final URI uri = CommonLegacyConfiguration.getUri(connectionConfiguration, connectionOptions);
        if (uri == null) {
            continue;
        }
        if (connectionConfiguration.isConnectEagerly()) {
            uris.add(uri);
        }
        final ConnectionBuilder connectionBuilder = endpointBuilder.addConnection(uri);
        connectionBuilder.setHeartbeatInterval(connectionOptions.get(RemotingOptions.HEARTBEAT_INTERVAL, RemotingOptions.DEFAULT_HEARTBEAT_INTERVAL));
        if (connectionOptions.get(Options.READ_TIMEOUT, -1) != -1) {
            connectionBuilder.setReadTimeout(connectionOptions.get(Options.READ_TIMEOUT, -1));
        }
        if (connectionOptions.get(Options.WRITE_TIMEOUT, -1) != -1) {
            connectionBuilder.setWriteTimeout(connectionOptions.get(Options.WRITE_TIMEOUT, -1));
        }
        connectionBuilder.setTcpKeepAlive(connectionOptions.get(Options.KEEP_ALIVE, false));
    }
    final Endpoint endpoint;
    try {
        endpoint = endpointBuilder.build();
    } catch (IOException e) {
        throw Logs.MAIN.failedToConstructEndpoint(e);
    }
    for (URI uri : uris) {
        endpoint.getConnection(uri, "ejb", "jboss");
    }
    return endpoint;
}
Also used : ArrayList(java.util.ArrayList) EndpointBuilder(org.jboss.remoting3.EndpointBuilder) ConnectionBuilder(org.jboss.remoting3.ConnectionBuilder) IOException(java.io.IOException) URI(java.net.URI) Endpoint(org.jboss.remoting3.Endpoint) OptionMap(org.xnio.OptionMap)

Aggregations

URI (java.net.URI)2 OptionMap (org.xnio.OptionMap)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 ServiceLoader (java.util.ServiceLoader)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConnectionBuilder (org.jboss.remoting3.ConnectionBuilder)1 Endpoint (org.jboss.remoting3.Endpoint)1 EndpointBuilder (org.jboss.remoting3.EndpointBuilder)1 HttpUpgradeConnectionProviderFactory (org.jboss.remoting3.remote.HttpUpgradeConnectionProviderFactory)1 RemoteConnectionProviderFactory (org.jboss.remoting3.remote.RemoteConnectionProviderFactory)1 ConnectionProviderFactory (org.jboss.remoting3.spi.ConnectionProviderFactory)1 Connection (org.ovirt.engine.sdk4.Connection)1 ConnectionBuilder (org.ovirt.engine.sdk4.ConnectionBuilder)1 XnioWorker (org.xnio.XnioWorker)1