Example usage for java.net Socket isClosed

List of usage examples for java.net Socket isClosed

Introduction

In this page you can find the example usage for java.net Socket isClosed.

Prototype

public boolean isClosed() 

Source Link

Document

Returns the closed state of the socket.

Usage

From source file:org.apache.hadoop.hbase.ipc.TestRpcClientLeaks.java

@Test(expected = RetriesExhaustedException.class)
public void testSocketClosed() throws IOException, InterruptedException {
    String tableName = "testSocketClosed";
    TableName name = TableName.valueOf(tableName);
    UTIL.createTable(name, fam1).close();

    Configuration conf = new Configuration(UTIL.getConfiguration());
    conf.set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY, MyRpcClientImpl.class.getName());
    conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
    Connection connection = ConnectionFactory.createConnection(conf);
    Table table = connection.getTable(TableName.valueOf(tableName));
    table.get(new Get("asd".getBytes()));
    connection.close();/*from www.  ja  v a 2  s .c  o m*/
    for (Socket socket : MyRpcClientImpl.savedSockets) {
        assertTrue("Socket + " + socket + " is not closed", socket.isClosed());
    }
}

From source file:org.mule.transport.legstar.tcp.LegstarTcpSocketFactory.java

/**
 * {@inheritDoc}/*from   w  w w. j ava  2  s  .co  m*/
 * When a socket is returned to the pool in opened state, we need to commit the
 * mainframe transaction (otherwise the transaction would never end).
 */
public void passivateObject(final Object key, final Object object) throws Exception {
    super.passivateObject(key, object);
    Socket socket = (Socket) object;
    if (!socket.isClosed()) {
        exchangeCommitMessage((Socket) object);
    }
}

From source file:org.openhab.binding.pilight.internal.PilightListener.java

@Override
public void run() {
    while (running) {
        try {//www .  j a  v a  2 s  .  c o m
            Socket socket = connection.getSocket();
            if (!socket.isClosed()) {
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line = in.readLine();
                while (running && line != null) {
                    if (!StringUtils.isEmpty(line)) {
                        if (line.startsWith("{\"config\":{")) {
                            connection.setConfig(getInputMapper().readValue(line, Config.class));
                            configAction(ConfigModifyAction.ConfigReceived, null);
                        } else if (line.equals("1")) {
                            // pilight stopping
                            connection.getSocket().close();
                            throw new IOException("Connection to pilight lost");
                        } else {
                            Status status = getInputMapper().readValue(line, Status.class);
                            callback.messageReceived(connection, status);
                        }
                    }
                    line = in.readLine();
                }
            }
        } catch (IOException e) {
            logger.error("Error in pilight listener thread", e);
        }

        // empty line received (socket closed) or pilight stopped, try to reconnect
        reconnect();
    }

    cleanup();
}

From source file:org.darkphoenixs.pool.socket.SocketConnectionFactory.java

@Override
public boolean validateObject(PooledObject<Socket> p) {

    Socket socket = p.getObject();

    if (socket != null)

        return (socket.isConnected()) && (!socket.isClosed());

    return false;
}

From source file:org.mule.transport.legstar.tcp.LegstarTcpConnector.java

/**
 * Lookup a socket in the list of dispatcher sockets but don't create a new
 * socket./*from w  w w  .j  a  v  a  2  s.c  o  m*/
 * <p/>
 * This code is almost identical to TcpConnector#getSocket but there are no
 * other way we can substitute our own socket key.
 * 
 * @param event the mule event that triggered the need for a socket
 * @param endpoint the mule endpoint
 * @return a socket
 * @throws Exception if socket cannot be retrieved
 */
protected Socket getSocket(final MuleEvent event, final ImmutableEndpoint endpoint) throws Exception {
    HostCredentials hostCredentials = getHostCredentials(event.getMessage());
    LegstarTcpSocketKey socketKey = new LegstarTcpSocketKey(endpoint, hostCredentials);
    if (logger.isDebugEnabled()) {
        logger.debug("borrowing socket for " + socketKey + "/" + socketKey.hashCode());
    }
    Socket socket = (Socket) _socketsPool.borrowObject(socketKey);
    if (logger.isDebugEnabled()) {
        logger.debug("borrowed socket, " + (socket.isClosed() ? "closed" : "open") + "; debt "
                + _socketsPool.getNumActive());
    }
    return socket;
}

From source file:VASSAL.tools.io.IOUtilsTest.java

@Test
public void testCloseQuietlySocketOpen() {
    final Socket s = new Socket();
    assertFalse(s.isClosed());
    IOUtils.closeQuietly(s);/*from ww w  .jav a2s .  c  o  m*/
    assertTrue(s.isClosed());
}

From source file:VASSAL.tools.io.IOUtilsTest.java

@Test
public void testCloseQuietlySocketClosed() throws IOException {
    final Socket s = new Socket();
    s.close();// w ww  .  j a  va 2s.  co  m
    assertTrue(s.isClosed());
    IOUtils.closeQuietly(s);
    assertTrue(s.isClosed());
}

From source file:org.eclipse.mylyn.internal.commons.repositories.http.core.PollingSslProtocolSocketFactory.java

public boolean isSecure(Socket socket) throws IllegalArgumentException {
    Assert.isNotNull(socket);/*from   ww w  . j  a v a2 s.  co m*/
    if (!(socket instanceof SSLSocket)) {
        throw new IllegalArgumentException("Socket is not secure: " + socket.getClass()); //$NON-NLS-1$
    }
    if (socket.isClosed()) {
        throw new IllegalArgumentException("Socket is closed"); //$NON-NLS-1$
    }
    return true;
}

From source file:com.googlecode.jmxtrans.connections.SocketFactory.java

/**
 * Validates that the socket is good./*from   ww  w  .  j  a v a2  s .c o m*/
 */
@Override
public boolean validateObject(InetSocketAddress address, Socket socket) {
    if (socket == null) {
        log.error("Socket is null [{}]", address);
        return false;
    }

    if (!socket.isBound()) {
        log.error("Socket is not bound [{}]", address);
        return false;
    }
    if (socket.isClosed()) {
        log.error("Socket is closed [{}]", address);
        return false;
    }
    if (!socket.isConnected()) {
        log.error("Socket is not connected [{}]", address);
        return false;
    }
    if (socket.isInputShutdown()) {
        log.error("Socket input is shutdown [{}]", address);
        return false;
    }
    if (socket.isOutputShutdown()) {
        log.error("Socket output is shutdown [{}]", address);
        return false;
    }
    return true;
}

From source file:org.openhab.binding.pilight.internal.PilightConnector.java

@Override
public void run() {
    reconnect();//from  w w w . j  a v a 2 s  . co  m

    while (running) {
        try {
            Socket socket = connection.getSocket();
            if (!socket.isClosed()) {
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line = in.readLine();
                while (running && line != null) {
                    if (!StringUtils.isEmpty(line)) {
                        logger.debug("Received from pilight: {}", line);
                        if (line.startsWith("{\"message\":\"config\"")) {
                            // Configuration received
                            connection.setConfig(inputMapper.readValue(line, Message.class).getConfig());
                            configAction(ConfigModifyAction.ConfigReceived, null);
                        } else if (line.startsWith("{\"status\":")) {
                            // Status message, we're not using this for now. 
                            Response response = inputMapper.readValue(line, Response.class);
                            logger.trace("Response success: " + response.isSuccess());
                        } else if (line.equals("1")) {
                            // pilight stopping
                            connection.getSocket().close();
                            throw new IOException("Connection to pilight lost");
                        } else {
                            logger.debug(line);
                            Status status = inputMapper.readValue(line, Status.class);
                            callback.messageReceived(connection, status);
                        }
                    }
                    line = in.readLine();
                }
            }
        } catch (IOException e) {
            logger.error("Error in pilight listener thread", e);
        }

        // empty line received (socket closed) or pilight stopped, try to reconnect
        reconnect();
    }

    cleanup();
}