Search in sources :

Example 1 with CloseFuture

use of org.apache.mina.core.future.CloseFuture in project camel by apache.

the class Mina2Producer method closeSessionIfNeededAndAwaitCloseInHandler.

private void closeSessionIfNeededAndAwaitCloseInHandler(IoSession sessionToBeClosed) throws InterruptedException {
    closeLatch = new CountDownLatch(1);
    if (!sessionToBeClosed.isClosing()) {
        CloseFuture closeFuture = sessionToBeClosed.closeNow();
        closeFuture.await(timeout, TimeUnit.MILLISECONDS);
        closeLatch.await(timeout, TimeUnit.MILLISECONDS);
    }
}
Also used : CloseFuture(org.apache.mina.core.future.CloseFuture) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with CloseFuture

use of org.apache.mina.core.future.CloseFuture in project camel by apache.

the class Mina2Consumer method doStop.

@Override
protected void doStop() throws Exception {
    if (configuration.isClientMode() && configuration.getProtocol().equals("tcp")) {
        LOG.info("Disconnect from server address: {} using connector: {}", address, connector);
        if (session != null) {
            CloseFuture closeFuture = session.closeNow();
            closeFuture.awaitUninterruptibly();
        }
        connector.dispose(true);
    } else {
        LOG.info("Unbinding from server address: {} using acceptor: {}", address, acceptor);
        if (address instanceof InetSocketAddress) {
            // need to check if the address is IPV4 all network address
            if ("0.0.0.0".equals(((InetSocketAddress) address).getAddress().getHostAddress())) {
                LOG.info("Unbind the server address {}", acceptor.getLocalAddresses());
                acceptor.unbind(acceptor.getLocalAddresses());
            } else {
                acceptor.unbind(address);
            }
        } else {
            acceptor.unbind(address);
        }
    }
    super.doStop();
}
Also used : CloseFuture(org.apache.mina.core.future.CloseFuture) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with CloseFuture

use of org.apache.mina.core.future.CloseFuture in project bigbluebutton by bigbluebutton.

the class BlockStreamEventMessageHandler method closeSession.

private void closeSession(IoSession session) {
    String room = (String) session.getAttribute(ROOM, null);
    if (room != null) {
        log.info("Closing session [" + room + "]. ");
    } else {
        log.info("Cannot determine session to close.");
    }
    CloseFuture future = session.close(true);
}
Also used : CloseFuture(org.apache.mina.core.future.CloseFuture)

Example 4 with CloseFuture

use of org.apache.mina.core.future.CloseFuture in project cxf by apache.

the class IoSessionOutputStream method close.

@Override
public void close() throws IOException {
    try {
        flush();
    } finally {
        CloseFuture future = session.closeOnFlush();
        future.awaitUninterruptibly();
    }
}
Also used : CloseFuture(org.apache.mina.core.future.CloseFuture)

Example 5 with CloseFuture

use of org.apache.mina.core.future.CloseFuture in project directory-ldap-api by apache.

the class LdapNetworkConnection method connect.

// -------------------------- The methods ---------------------------//
/**
 * {@inheritDoc}
 */
@Override
public boolean connect() throws LdapException {
    if ((ldapSession != null) && connected.get()) {
        // No need to connect if we already have a connected session
        return true;
    }
    // Create the connector if needed
    if (connector == null) {
        createConnector();
    }
    // Build the connection address
    SocketAddress address = new InetSocketAddress(config.getLdapHost(), config.getLdapPort());
    // And create the connection future
    timeout = config.getTimeout();
    long maxRetry = System.currentTimeMillis() + timeout;
    ConnectFuture connectionFuture = null;
    boolean interrupted = false;
    while (maxRetry > System.currentTimeMillis() && !interrupted) {
        connectionFuture = connector.connect(address);
        boolean result = false;
        // Wait until it's established
        try {
            result = connectionFuture.await(timeout);
        } catch (InterruptedException e) {
            connector.dispose();
            connector = null;
            LOG.debug(I18n.msg(I18n.MSG_03221_INTERRUPTED_WAITING_FOR_CONNECTION, config.getLdapHost(), config.getLdapPort()), e);
            interrupted = true;
            throw new LdapOtherException(e.getMessage(), e);
        } finally {
            if (result) {
                boolean isConnected = connectionFuture.isConnected();
                if (!isConnected) {
                    Throwable connectionException = connectionFuture.getException();
                    if ((connectionException instanceof ConnectException) || (connectionException instanceof UnresolvedAddressException)) {
                        // We know that there was a permanent error such as "connection refused".
                        if (LOG.isDebugEnabled()) {
                            LOG.debug(I18n.msg(I18n.MSG_03245_CONNECTION_ERROR, connectionFuture.getException().getMessage()));
                        }
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(I18n.msg(I18n.MSG_03244_CONNECTION_RETRYING));
                    }
                    // Wait 500 ms and retry
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        connector = null;
                        LOG.debug(I18n.msg(I18n.MSG_03221_INTERRUPTED_WAITING_FOR_CONNECTION, config.getLdapHost(), config.getLdapPort()), e);
                        interrupted = true;
                        throw new LdapOtherException(e.getMessage(), e);
                    }
                } else {
                    break;
                }
            }
        }
    }
    if (connectionFuture == null) {
        connector.dispose();
        throw new InvalidConnectionException("Cannot connect");
    }
    boolean isConnected = connectionFuture.isConnected();
    if (!isConnected) {
        // disposing connector if not connected
        try {
            close();
        } catch (IOException ioe) {
        // Nothing to do
        }
        Throwable e = connectionFuture.getException();
        if (e != null) {
            StringBuilder message = new StringBuilder("Cannot connect to the server: ");
            // (most of the time no message is associated with this exception)
            if ((e instanceof UnresolvedAddressException) && (e.getMessage() == null)) {
                message.append("Hostname '");
                message.append(config.getLdapHost());
                message.append("' could not be resolved.");
                throw new InvalidConnectionException(message.toString(), e);
            }
            // Default case
            message.append(e.getMessage());
            throw new InvalidConnectionException(message.toString(), e);
        }
        return false;
    }
    // Get the close future for this session
    CloseFuture closeFuture = connectionFuture.getSession().getCloseFuture();
    // Add a listener to close the session in the session.
    closeFuture.addListener(new IoFutureListener<IoFuture>() {

        @Override
        public void operationComplete(IoFuture future) {
            // Process all the waiting operations and cancel them
            LOG.debug(I18n.msg(I18n.MSG_03238_NOD_RECEIVED));
            for (ResponseFuture<?> responseFuture : futureMap.values()) {
                LOG.debug(I18n.msg(I18n.MSG_03235_CLOSING, responseFuture));
                responseFuture.cancel();
                try {
                    if (responseFuture instanceof AddFuture) {
                        ((AddFuture) responseFuture).set(AddNoDResponse.PROTOCOLERROR);
                    } else if (responseFuture instanceof BindFuture) {
                        ((BindFuture) responseFuture).set(BindNoDResponse.PROTOCOLERROR);
                    } else if (responseFuture instanceof CompareFuture) {
                        ((CompareFuture) responseFuture).set(CompareNoDResponse.PROTOCOLERROR);
                    } else if (responseFuture instanceof DeleteFuture) {
                        ((DeleteFuture) responseFuture).set(DeleteNoDResponse.PROTOCOLERROR);
                    } else if (responseFuture instanceof ExtendedFuture) {
                        ((ExtendedFuture) responseFuture).set(ExtendedNoDResponse.PROTOCOLERROR);
                    } else if (responseFuture instanceof ModifyFuture) {
                        ((ModifyFuture) responseFuture).set(ModifyNoDResponse.PROTOCOLERROR);
                    } else if (responseFuture instanceof ModifyDnFuture) {
                        ((ModifyDnFuture) responseFuture).set(ModifyDnNoDResponse.PROTOCOLERROR);
                    } else if (responseFuture instanceof SearchFuture) {
                        ((SearchFuture) responseFuture).set(SearchNoDResponse.PROTOCOLERROR);
                    }
                } catch (InterruptedException e) {
                    LOG.error(I18n.err(I18n.ERR_03202_ERROR_PROCESSING_NOD, responseFuture), e);
                }
                futureMap.remove(messageId.get());
            }
            futureMap.clear();
        }
    });
    // Get back the session
    ldapSession = connectionFuture.getSession();
    connected.set(true);
    // Store the container into the session if we don't have one
    @SuppressWarnings("unchecked") LdapMessageContainer<MessageDecorator<? extends Message>> container = (LdapMessageContainer<MessageDecorator<? extends Message>>) ldapSession.getAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR);
    if (container != null) {
        if ((schemaManager != null) && !(container.getBinaryAttributeDetector() instanceof SchemaBinaryAttributeDetector)) {
            container.setBinaryAttributeDetector(new SchemaBinaryAttributeDetector(schemaManager));
        }
    } else {
        BinaryAttributeDetector atDetector = new DefaultConfigurableBinaryAttributeDetector();
        if (schemaManager != null) {
            atDetector = new SchemaBinaryAttributeDetector(schemaManager);
        }
        ldapSession.setAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR, new LdapMessageContainer<MessageDecorator<? extends Message>>(codec, atDetector));
    }
    // Initialize the MessageId
    messageId.set(0);
    // And return
    return true;
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) ModifyFuture(org.apache.directory.ldap.client.api.future.ModifyFuture) Message(org.apache.directory.api.ldap.model.message.Message) InetSocketAddress(java.net.InetSocketAddress) IoFuture(org.apache.mina.core.future.IoFuture) ConnectFuture(org.apache.mina.core.future.ConnectFuture) BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) ModifyDnFuture(org.apache.directory.ldap.client.api.future.ModifyDnFuture) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) DefaultConfigurableBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) CompareFuture(org.apache.directory.ldap.client.api.future.CompareFuture) SchemaBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.SchemaBinaryAttributeDetector) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ConnectException(java.net.ConnectException) CloseFuture(org.apache.mina.core.future.CloseFuture) ResponseFuture(org.apache.directory.ldap.client.api.future.ResponseFuture) ExtendedFuture(org.apache.directory.ldap.client.api.future.ExtendedFuture) IOException(java.io.IOException) SchemaBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.SchemaBinaryAttributeDetector) DefaultConfigurableBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector) BinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.BinaryAttributeDetector) DeleteFuture(org.apache.directory.ldap.client.api.future.DeleteFuture) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) AddFuture(org.apache.directory.ldap.client.api.future.AddFuture) SearchFuture(org.apache.directory.ldap.client.api.future.SearchFuture)

Aggregations

CloseFuture (org.apache.mina.core.future.CloseFuture)6 InetSocketAddress (java.net.InetSocketAddress)2 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 SocketAddress (java.net.SocketAddress)1 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 BinaryAttributeDetector (org.apache.directory.api.ldap.codec.api.BinaryAttributeDetector)1 DefaultConfigurableBinaryAttributeDetector (org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector)1 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)1 MessageDecorator (org.apache.directory.api.ldap.codec.api.MessageDecorator)1 SchemaBinaryAttributeDetector (org.apache.directory.api.ldap.codec.api.SchemaBinaryAttributeDetector)1 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)1 Message (org.apache.directory.api.ldap.model.message.Message)1 InvalidConnectionException (org.apache.directory.ldap.client.api.exception.InvalidConnectionException)1 AddFuture (org.apache.directory.ldap.client.api.future.AddFuture)1 BindFuture (org.apache.directory.ldap.client.api.future.BindFuture)1 CompareFuture (org.apache.directory.ldap.client.api.future.CompareFuture)1 DeleteFuture (org.apache.directory.ldap.client.api.future.DeleteFuture)1 ExtendedFuture (org.apache.directory.ldap.client.api.future.ExtendedFuture)1