Search in sources :

Example 1 with GatewayConfigurationException

use of org.apache.geode.cache.GatewayConfigurationException in project geode by apache.

the class ConnectionFactoryImpl method createClientToServerConnection.

public Connection createClientToServerConnection(Set excludedServers) throws GemFireSecurityException {
    final Set origExcludedServers = excludedServers;
    excludedServers = new HashSet(excludedServers);
    Set blackListedServers = blackList.getBadServers();
    excludedServers.addAll(blackListedServers);
    Connection conn = null;
    // long startTime = System.currentTimeMillis();
    RuntimeException fatalException = null;
    boolean tryBlackList = true;
    do {
        ServerLocation server = source.findServer(excludedServers);
        if (server == null) {
            if (tryBlackList) {
                // Nothing worked! Let's try without the blacklist.
                tryBlackList = false;
                int size = excludedServers.size();
                excludedServers.removeAll(blackListedServers);
                // make sure we didn't remove any of the ones that the caller set not to use
                excludedServers.addAll(origExcludedServers);
                if (excludedServers.size() < size) {
                    // We are able to remove some exclusions, so lets give this another whirl.
                    continue;
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Source was unable to locate any servers");
            }
            if (fatalException != null) {
                throw fatalException;
            }
            return null;
        }
        try {
            conn = createClientToServerConnection(server, false);
        } catch (CancelException e) {
            // propagate this up immediately
            throw e;
        } catch (GemFireSecurityException e) {
            // propagate this up immediately
            throw e;
        } catch (GatewayConfigurationException e) {
            // propagate this up immediately
            throw e;
        } catch (ServerRefusedConnectionException srce) {
            fatalException = srce;
            if (logger.isDebugEnabled()) {
                logger.debug("ServerRefusedConnectionException attempting to connect to {}", server, srce);
            }
        } catch (Exception e) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.ConnectException_COULD_NOT_CONNECT_TO_0, server), e);
        }
        excludedServers.add(server);
    } while (conn == null);
    // }
    return conn;
}
Also used : GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) Set(java.util.Set) HashSet(java.util.HashSet) GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) CancelException(org.apache.geode.CancelException) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) GemFireConfigException(org.apache.geode.GemFireConfigException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) HashSet(java.util.HashSet)

Example 2 with GatewayConfigurationException

use of org.apache.geode.cache.GatewayConfigurationException in project geode by apache.

the class ConnectionFactoryImpl method createClientToServerConnection.

public Connection createClientToServerConnection(ServerLocation location, boolean forQueue) throws GemFireSecurityException {
    ConnectionImpl connection = new ConnectionImpl(this.ds, this.cancelCriterion);
    FailureTracker failureTracker = blackList.getFailureTracker(location);
    boolean initialized = false;
    try {
        HandShake connHandShake = new HandShake(handshake);
        connection.connect(endpointManager, location, connHandShake, socketBufferSize, handShakeTimeout, readTimeout, getCommMode(forQueue), this.gatewaySender, this.socketCreator);
        failureTracker.reset();
        connection.setHandShake(connHandShake);
        authenticateIfRequired(connection);
        initialized = true;
    } catch (GemFireConfigException e) {
        throw e;
    } catch (CancelException e) {
        // propagate this up, don't retry
        throw e;
    } catch (GemFireSecurityException e) {
        // propagate this up, don't retry
        throw e;
    } catch (GatewayConfigurationException e) {
        // propagate this up, don't retry
        throw e;
    } catch (ServerRefusedConnectionException src) {
        // propagate this up, don't retry
        logger.warn(LocalizedMessage.create(LocalizedStrings.AutoConnectionSourceImpl_COULD_NOT_CREATE_A_NEW_CONNECTION_TO_SERVER_0, src.getMessage()));
        testFailedConnectionToServer = true;
        throw src;
    } catch (Exception e) {
        if (e.getMessage() != null && (e.getMessage().equals("Connection refused") || e.getMessage().equals("Connection reset"))) {
            // print an exception
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to connect to {}: connection refused", location);
            }
        } else {
            // print a warning with the exception stack trace
            logger.warn(LocalizedMessage.create(LocalizedStrings.ConnectException_COULD_NOT_CONNECT_TO_0, location), e);
        }
        testFailedConnectionToServer = true;
    } finally {
        if (!initialized) {
            connection.destroy();
            failureTracker.addFailure();
            connection = null;
        }
    }
    return connection;
}
Also used : HandShake(org.apache.geode.internal.cache.tier.sockets.HandShake) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) FailureTracker(org.apache.geode.cache.client.internal.ServerBlackList.FailureTracker) GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) GemFireConfigException(org.apache.geode.GemFireConfigException) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) CancelException(org.apache.geode.CancelException) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) GemFireConfigException(org.apache.geode.GemFireConfigException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException)

Example 3 with GatewayConfigurationException

use of org.apache.geode.cache.GatewayConfigurationException in project geode by apache.

the class HandShake method handshakeWithServer.

/**
   * Client-side handshake with a Server
   */
public ServerQueueStatus handshakeWithServer(Connection conn, ServerLocation location, byte communicationMode) throws IOException, AuthenticationRequiredException, AuthenticationFailedException, ServerRefusedConnectionException {
    try {
        ServerQueueStatus serverQStatus = null;
        Socket sock = conn.getSocket();
        DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
        final InputStream in = sock.getInputStream();
        DataInputStream dis = new DataInputStream(in);
        DistributedMember member = getIDForSocket(sock);
        // if running in a loner system, use the new port number in the ID to
        // help differentiate from other clients
        DM dm = ((InternalDistributedSystem) this.system).getDistributionManager();
        InternalDistributedMember idm = dm.getDistributionManagerId();
        synchronized (idm) {
            if (idm.getPort() == 0 && dm instanceof LonerDistributionManager) {
                int port = sock.getLocalPort();
                ((LonerDistributionManager) dm).updateLonerPort(port);
                updateProxyID(dm.getDistributionManagerId());
            }
        }
        if (communicationMode == Acceptor.GATEWAY_TO_GATEWAY) {
            this.credentials = getCredentials(member);
        }
        byte intermediateAcceptanceCode = write(dos, dis, communicationMode, REPLY_OK, this.clientReadTimeout, null, this.credentials, member, false);
        String authInit = this.system.getProperties().getProperty(SECURITY_CLIENT_AUTH_INIT);
        if (communicationMode != Acceptor.GATEWAY_TO_GATEWAY && intermediateAcceptanceCode != REPLY_AUTH_NOT_REQUIRED && (authInit != null && authInit.length() != 0)) {
            location.compareAndSetRequiresCredentials(true);
        }
        // Read the acceptance code
        byte acceptanceCode = dis.readByte();
        if (acceptanceCode == (byte) 21 && !(sock instanceof SSLSocket)) {
            // SSL
            throw new AuthenticationRequiredException(LocalizedStrings.HandShake_SERVER_EXPECTING_SSL_CONNECTION.toLocalizedString());
        }
        if (acceptanceCode == REPLY_SERVER_IS_LOCATOR) {
            throw new GemFireConfigException("Improperly configured client detected.  " + "Server at " + location + " is actually a locator.  Use addPoolLocator to configure locators.");
        }
        // Successful handshake for GATEWAY_TO_GATEWAY mode sets the peer version in connection
        if (communicationMode == Acceptor.GATEWAY_TO_GATEWAY && !(acceptanceCode == REPLY_EXCEPTION_AUTHENTICATION_REQUIRED || acceptanceCode == REPLY_EXCEPTION_AUTHENTICATION_FAILED)) {
            short wanSiteVersion = Version.readOrdinal(dis);
            conn.setWanSiteVersion(wanSiteVersion);
            // establish a versioned stream for the other site, if necessary
            if (wanSiteVersion < Version.CURRENT_ORDINAL) {
                dis = new VersionedDataInputStream(dis, Version.fromOrdinalOrCurrent(wanSiteVersion));
            }
        }
        // No need to check for return value since DataInputStream already throws
        // EOFException in case of EOF
        byte epType = dis.readByte();
        int qSize = dis.readInt();
        // Read the server member
        member = readServerMember(dis);
        serverQStatus = new ServerQueueStatus(epType, qSize, member);
        // Read the message (if any)
        readMessage(dis, dos, acceptanceCode, member);
        // DSes with different values of this. It shoule be a member variable.
        if (communicationMode != Acceptor.GATEWAY_TO_GATEWAY && currentClientVersion.compareTo(Version.GFE_61) >= 0) {
            deltaEnabledOnServer = dis.readBoolean();
        }
        // validate that the remote side has a different distributed system id.
        if (communicationMode == Acceptor.GATEWAY_TO_GATEWAY && Version.GFE_66.compareTo(conn.getWanSiteVersion()) <= 0 && currentClientVersion.compareTo(Version.GFE_66) >= 0) {
            int remoteDistributedSystemId = in.read();
            int localDistributedSystemId = ((InternalDistributedSystem) system).getDistributionManager().getDistributedSystemId();
            if (localDistributedSystemId >= 0 && localDistributedSystemId == remoteDistributedSystemId) {
                throw new GatewayConfigurationException("Remote WAN site's distributed system id " + remoteDistributedSystemId + " matches this sites distributed system id " + localDistributedSystemId);
            }
        }
        // Read the PDX registry size from the remote size
        if (communicationMode == Acceptor.GATEWAY_TO_GATEWAY && Version.GFE_80.compareTo(conn.getWanSiteVersion()) <= 0 && currentClientVersion.compareTo(Version.GFE_80) >= 0) {
            int remotePdxSize = dis.readInt();
            serverQStatus.setPdxSize(remotePdxSize);
        }
        return serverQStatus;
    } catch (IOException ex) {
        CancelCriterion stopper = this.system.getCancelCriterion();
        stopper.checkCancelInProgress(null);
        throw ex;
    }
}
Also used : GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) DataOutputStream(java.io.DataOutputStream) VersionedDataOutputStream(org.apache.geode.internal.VersionedDataOutputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) CancelCriterion(org.apache.geode.CancelCriterion) DM(org.apache.geode.distributed.internal.DM) AuthenticationRequiredException(org.apache.geode.security.AuthenticationRequiredException) IOException(java.io.IOException) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) DataInputStream(java.io.DataInputStream) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) GemFireConfigException(org.apache.geode.GemFireConfigException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) LonerDistributionManager(org.apache.geode.distributed.internal.LonerDistributionManager) SSLSocket(javax.net.ssl.SSLSocket) Socket(java.net.Socket) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream)

Aggregations

GemFireConfigException (org.apache.geode.GemFireConfigException)3 GatewayConfigurationException (org.apache.geode.cache.GatewayConfigurationException)3 CancelException (org.apache.geode.CancelException)2 ServerRefusedConnectionException (org.apache.geode.cache.client.ServerRefusedConnectionException)2 GemFireSecurityException (org.apache.geode.security.GemFireSecurityException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Socket (java.net.Socket)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 SSLSocket (javax.net.ssl.SSLSocket)1 CancelCriterion (org.apache.geode.CancelCriterion)1 FailureTracker (org.apache.geode.cache.client.internal.ServerBlackList.FailureTracker)1 DistributedMember (org.apache.geode.distributed.DistributedMember)1 DM (org.apache.geode.distributed.internal.DM)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1