| | |
| | | import org.forgerock.opendj.ldap.responses.ExtendedResult; |
| | | import org.forgerock.opendj.ldap.responses.Result; |
| | | import org.glassfish.grizzly.CompletionHandler; |
| | | import org.glassfish.grizzly.Connection; |
| | | import org.glassfish.grizzly.EmptyCompletionHandler; |
| | | import org.glassfish.grizzly.filterchain.DefaultFilterChain; |
| | | import org.glassfish.grizzly.filterchain.FilterChain; |
| | | import org.glassfish.grizzly.filterchain.TransportFilter; |
| | | import org.glassfish.grizzly.nio.transport.TCPNIOTransport; |
| | | |
| | | import com.forgerock.opendj.util.AbstractConnectionFactory; |
| | | import com.forgerock.opendj.util.CompletedFutureResult; |
| | | import com.forgerock.opendj.util.FutureResultTransformer; |
| | | import com.forgerock.opendj.util.RecursiveFutureResult; |
| | |
| | | /** |
| | | * LDAP connection factory implementation. |
| | | */ |
| | | public final class LDAPConnectionFactoryImpl extends AbstractConnectionFactory |
| | | implements ConnectionFactory |
| | | public final class LDAPConnectionFactoryImpl implements ConnectionFactory |
| | | { |
| | | |
| | | @SuppressWarnings("rawtypes") |
| | | private final class FutureResultImpl implements CompletionHandler<Connection> |
| | | private final class FutureResultImpl implements |
| | | CompletionHandler<org.glassfish.grizzly.Connection> |
| | | { |
| | | private final FutureResultTransformer<Result, AsynchronousConnection> futureStartTLSResult; |
| | | |
| | | private final FutureResultTransformer<Result, Connection> futureStartTLSResult; |
| | | private final RecursiveFutureResult<LDAPConnection, ExtendedResult> futureConnectionResult; |
| | | |
| | | private LDAPConnection connection; |
| | | |
| | | |
| | | |
| | | private FutureResultImpl( |
| | | final ResultHandler<? super AsynchronousConnection> handler) |
| | | private FutureResultImpl(final ResultHandler<? super Connection> handler) |
| | | { |
| | | this.futureStartTLSResult = new FutureResultTransformer<Result, AsynchronousConnection>( |
| | | this.futureStartTLSResult = new FutureResultTransformer<Result, Connection>( |
| | | handler) |
| | | { |
| | | |
| | |
| | | { |
| | | final StartTLSExtendedRequest startTLS = Requests |
| | | .newStartTLSExtendedRequest(options.getSSLContext()); |
| | | startTLS.addEnabledCipherSuite( |
| | | options.getEnabledCipherSuites().toArray( |
| | | new String[options.getEnabledCipherSuites().size()])); |
| | | startTLS.addEnabledProtocol( |
| | | options.getEnabledProtocols().toArray( |
| | | new String[options.getEnabledProtocols().size()])); |
| | | return connection.extendedRequest(startTLS, handler); |
| | | startTLS.addEnabledCipherSuite(options.getEnabledCipherSuites() |
| | | .toArray(new String[options.getEnabledCipherSuites().size()])); |
| | | startTLS.addEnabledProtocol(options.getEnabledProtocols().toArray( |
| | | new String[options.getEnabledProtocols().size()])); |
| | | return connection.extendedRequestAsync(startTLS, null, handler); |
| | | } |
| | | |
| | | if (options.getSSLContext() != null) |
| | |
| | | } |
| | | catch (final IOException ioe) |
| | | { |
| | | throw newErrorResult( |
| | | ResultCode.CLIENT_SIDE_CONNECT_ERROR, |
| | | throw newErrorResult(ResultCode.CLIENT_SIDE_CONNECT_ERROR, |
| | | ioe.getMessage(), ioe); |
| | | } |
| | | } |
| | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | @Override |
| | | public void cancelled() |
| | | { |
| | | // Ignore this. |
| | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public void completed(final Connection connection) |
| | | @Override |
| | | public void completed(final org.glassfish.grizzly.Connection connection) |
| | | { |
| | | futureConnectionResult.handleResult(adaptConnection(connection)); |
| | | } |
| | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | @Override |
| | | public void failed(final Throwable throwable) |
| | | { |
| | | futureConnectionResult |
| | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public void updated(final Connection connection) |
| | | @Override |
| | | public void updated(final org.glassfish.grizzly.Connection connection) |
| | | { |
| | | // Ignore this. |
| | | } |
| | |
| | | |
| | | |
| | | private final SocketAddress socketAddress; |
| | | |
| | | private final TCPNIOTransport transport; |
| | | |
| | | private final FilterChain defaultFilterChain; |
| | | |
| | | private final LDAPClientFilter clientFilter; |
| | | |
| | | private final LDAPOptions options; |
| | | |
| | | |
| | |
| | | * {@inheritDoc} |
| | | */ |
| | | @Override |
| | | public FutureResult<AsynchronousConnection> getAsynchronousConnection( |
| | | final ResultHandler<? super AsynchronousConnection> handler) |
| | | public Connection getConnection() throws ErrorResultException, |
| | | InterruptedException |
| | | { |
| | | return getConnectionAsync(null).get(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | @Override |
| | | public FutureResult<Connection> getConnectionAsync( |
| | | final ResultHandler<? super Connection> handler) |
| | | { |
| | | final FutureResultImpl future = new FutureResultImpl(handler); |
| | | |
| | |
| | | catch (final IOException e) |
| | | { |
| | | final ErrorResultException result = adaptConnectionException(e); |
| | | return new CompletedFutureResult<AsynchronousConnection>(result); |
| | | return new CompletedFutureResult<Connection>(result); |
| | | } |
| | | } |
| | | |
| | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | @Override |
| | | public String toString() |
| | | { |
| | | final StringBuilder builder = new StringBuilder(); |
| | |
| | | |
| | | |
| | | |
| | | private LDAPConnection adaptConnection(final Connection<?> connection) |
| | | private LDAPConnection adaptConnection( |
| | | final org.glassfish.grizzly.Connection<?> connection) |
| | | { |
| | | // Test shows that its much faster with non block writes but risk |
| | | // running out of memory if the server is slow. |
| | |
| | | t = t.getCause(); |
| | | } |
| | | |
| | | return newErrorResult(ResultCode.CLIENT_SIDE_CONNECT_ERROR, |
| | | t.getMessage(), t); |
| | | return newErrorResult(ResultCode.CLIENT_SIDE_CONNECT_ERROR, t.getMessage(), |
| | | t); |
| | | } |
| | | } |