Example usage for org.apache.thrift.transport TTransport isOpen

List of usage examples for org.apache.thrift.transport TTransport isOpen

Introduction

In this page you can find the example usage for org.apache.thrift.transport TTransport isOpen.

Prototype

public abstract boolean isOpen();

Source Link

Document

Queries whether the transport is open.

Usage

From source file:alluxio.network.connection.ThriftClientPool.java

License:Apache License

/**
 * A helper function to close thrift clients.
 *
 * @param client the thrift client to close
 * @param <C> the thrift client type
 *//*from  w  w w . ja  v  a  2 s  . c  om*/
public static <C extends AlluxioService.Client> void closeThriftClient(C client) {
    // Note that the input and output protocol is the same in Alluxio.
    TTransport transport = client.getOutputProtocol().getTransport();
    if (transport.isOpen()) {
        transport.close();
    }
}

From source file:alluxio.network.connection.ThriftClientPool.java

License:Apache License

/**
 * Creates a thrift client instance./*www . j  a  v a2 s  .c  om*/
 *
 * @return the thrift client created
 * @throws IOException if it fails to create a thrift client
 */
@Override
protected T createNewResource() throws IOException {
    TTransport transport = mTransportProvider.getClientTransport(mAddress);
    TProtocol binaryProtocol = new TBinaryProtocol(transport);
    T client = createThriftClient(new TMultiplexedProtocol(binaryProtocol, mServiceName));

    RetryPolicy retry = new ExponentialBackoffRetry(CONNECTION_OPEN_RETRY_BASE_SLEEP_MS, Constants.SECOND_MS,
            CONNECTION_OPEN_RETRY_MAX);
    while (true) {
        try {
            if (!transport.isOpen()) {
                transport.open();
            }
            if (transport.isOpen()) {
                checkVersion(client);
            }
        } catch (TTransportException e) {
            LOG.error("Failed to connect (" + retry.getRetryCount() + ") to " + getServiceNameForLogging()
                    + " @ " + mAddress, e);
            if (e.getCause() instanceof java.net.SocketTimeoutException) {
                // Do not retry if socket timeout.
                String message = "Thrift transport open times out. Please check whether the "
                        + "authentication types match between client and server. Note that NOSASL client "
                        + "is not able to connect to servers with SIMPLE security mode.";
                throw new IOException(message, e);
            }
            if (!retry.attemptRetry()) {
                throw new IOException(e);
            }
        }
        break;
    }
    LOG.info("Created a new thrift client {}", client.toString());
    return client;
}

From source file:alluxio.security.authentication.AuthenticationUtilsTest.java

License:Apache License

/**
 * In NOSASL mode, the TTransport used should be the same as Alluxio original code.
 *
 * @throws Exception thrown when the server cannot be started
 *//*from www. ja va 2s  .  c o  m*/
@Test
public void nosaslAuthenticationTest() throws Exception {
    mConfiguration.set(Constants.SECURITY_AUTHENTICATION_TYPE, "NOSASL");

    // start server
    startServerThread();

    // create client and connect to server
    TTransport client = AuthenticationUtils.getClientTransport(mConfiguration, mServerAddress);
    client.open();
    Assert.assertTrue(client.isOpen());

    // clean up
    client.close();
    mServer.stop();
}

From source file:alluxio.security.authentication.AuthenticationUtilsTest.java

License:Apache License

/**
 * In SIMPLE mode, the TTransport mechanism is PLAIN. When server authenticate the connected
 * client user, it use {@link SimpleAuthenticationProvider}.
 *
 * @throws Exception thrown when the server cannot be started or the retrieval of the plain client
 *                   transport fails/*from  w ww  .ja va  2  s  . co m*/
 */
@Test
public void simpleAuthenticationTest() throws Exception {
    mConfiguration.set(Constants.SECURITY_AUTHENTICATION_TYPE, "SIMPLE");

    // start server
    startServerThread();

    // when connecting, authentication happens. It is a no-op in Simple mode.
    TTransport client = PlainSaslUtils.getPlainClientTransport("anyone", "whatever", mClientTSocket);
    client.open();
    Assert.assertTrue(client.isOpen());

    // clean up
    client.close();
    mServer.stop();
}

From source file:alluxio.security.authentication.AuthenticationUtilsTest.java

License:Apache License

/**
 * In CUSTOM mode, the TTransport mechanism is PLAIN. When server authenticate the connected
 * client user, it use configured AuthenticationProvider. If the username:password pair matches, a
 * connection should be built.//from   w  ww  .  j  ava 2 s . co  m
 *
 * @throws Exception thrown when the server cannot be started or the retrieval of the plain client
 *                   transport fails
 */
@Test
public void customAuthenticationExactNamePasswordMatchTest() throws Exception {
    mConfiguration.set(Constants.SECURITY_AUTHENTICATION_TYPE, "CUSTOM");
    mConfiguration.set(Constants.SECURITY_AUTHENTICATION_CUSTOM_PROVIDER,
            ExactlyMatchAuthenticationProvider.class.getName());

    // start server
    startServerThread();

    // when connecting, authentication happens. User's name:pwd pair matches and auth pass.
    TTransport client = PlainSaslUtils.getPlainClientTransport("alluxio", "correct-password", mClientTSocket);
    client.open();
    Assert.assertTrue(client.isOpen());

    // clean up
    client.close();
    mServer.stop();
}

From source file:alluxio.security.authentication.TransportProviderTest.java

License:Apache License

/**
 * In NOSASL mode, the TTransport used should be the same as Alluxio original code.
 *//*from w w w .  j  a va  2s.  c  om*/
@Test
public void nosaslAuthentrication() throws Exception {
    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_TYPE, AuthType.NOSASL.getAuthName());
    mTransportProvider = TransportProvider.Factory.create();

    // start server
    startServerThread();

    // create client and connect to server
    TTransport client = mTransportProvider.getClientTransport(mServerAddress);
    client.open();
    Assert.assertTrue(client.isOpen());

    // clean up
    client.close();
    mServer.stop();
}

From source file:alluxio.security.authentication.TransportProviderTest.java

License:Apache License

/**
 * In SIMPLE mode, the TTransport mechanism is PLAIN. When server authenticate the connected
 * client user, it use {@link SimpleAuthenticationProvider}.
 */// w ww.j a v  a  2s. c  o m
@Test
public void simpleAuthentication() throws Exception {
    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_TYPE, AuthType.SIMPLE.getAuthName());
    mTransportProvider = TransportProvider.Factory.create();

    // start server
    startServerThread();

    // when connecting, authentication happens. It is a no-op in Simple mode.
    TTransport client = mTransportProvider.getClientTransport(mServerAddress);
    client.open();
    Assert.assertTrue(client.isOpen());

    // clean up
    client.close();
    mServer.stop();
}

From source file:alluxio.security.authentication.TransportProviderTest.java

License:Apache License

/**
 * In CUSTOM mode, the TTransport mechanism is PLAIN. When server authenticate the connected
 * client user, it use configured AuthenticationProvider. If the username:password pair matches, a
 * connection should be built./*ww w . jav a 2 s  . c  o m*/
 */
@Test
public void customAuthenticationExactNamePasswordMatch() throws Exception {
    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_TYPE, AuthType.CUSTOM.getAuthName());
    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_CUSTOM_PROVIDER_CLASS,
            ExactlyMatchAuthenticationProvider.class.getName());
    mTransportProvider = TransportProvider.Factory.create();

    // start server
    startServerThread();

    // when connecting, authentication happens. User's name:pwd pair matches and auth pass.
    TTransport client = ((PlainSaslTransportProvider) mTransportProvider).getClientTransport(
            ExactlyMatchAuthenticationProvider.USERNAME, ExactlyMatchAuthenticationProvider.PASSWORD,
            mServerAddress);
    client.open();
    Assert.assertTrue(client.isOpen());

    // clean up
    client.close();
    mServer.stop();
}

From source file:client.IrcClient.java

private static void closeSocket(TTransport transport) {
    if (transport.isOpen())
        transport.close();
}

From source file:com.abiquo.nodecollector.aim.impl.AimCollectorImpl.java

License:Open Source License

@Override
public void pingAIM() throws AimException {
    TTransport transport = new TSocket(host, aimport);

    try {/*from  w w  w. j  a v a2  s .  com*/
        transport.open();

        if (transport.isOpen()) {
            transport.close();

            LOG.debug("AIM service running at [{}:{}]", host, aimport);
        } else {
            LOG.error(MessageValues.AIM_NO_PING);
            throw new AimException(MessageValues.AIM_NO_PING);
        }
    } catch (TTransportException e) {
        LOG.error(MessageValues.AIM_NO_PING, e);
        throw new AimException(MessageValues.AIM_NO_PING, e);
    }
}