Search in sources :

Example 11 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class TestSocketServerConnection method testImmediateFail1.

@Test(expected = CommunicationException.class)
public void testImmediateFail1() throws Exception {
    SocketServerConnection connection = createConnection(new CommunicationException());
    connection.setFailOver(true);
    ILogon logon = connection.getService(ILogon.class);
    logon.ping();
}
Also used : CommunicationException(org.teiid.net.CommunicationException) ILogon(org.teiid.client.security.ILogon) Test(org.junit.Test)

Example 12 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class SocketServerConnection method selectServerInstance.

/**
 * Implements a sticky random selection policy
 * TODO: make this customizable
 * TODO: put more information on hostinfo as to process response time, last successful connect, etc.
 * @throws ConnectionException
 */
public synchronized SocketServerInstance selectServerInstance(boolean logoff) throws CommunicationException, ConnectionException {
    if (closed) {
        throw new CommunicationException(JDBCPlugin.Event.TEIID20016, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20016));
    }
    if (this.serverInstance != null && (!failOver || this.serverInstance.isOpen())) {
        return this.serverInstance;
    }
    List<HostInfo> hostKeys = new ArrayList<HostInfo>(this.serverDiscovery.getKnownHosts(logonResult, null));
    boolean discoverHosts = true;
    closeServerInstance();
    List<HostInfo> hostCopy = new ArrayList<HostInfo>(hostKeys);
    int knownHosts = hostKeys.size();
    while (hostKeys.size() > 0) {
        HostInfo hostInfo = this.serverDiscovery.selectNextInstance(hostKeys);
        Exception ex = null;
        try {
            if (!hostInfo.isResolved()) {
                InetAddress inetAddress = hostInfo.getInetAddress();
                if (!hostInfo.isResolved()) {
                    // create a resolved version
                    hostInfo = new HostInfo(hostInfo.getHostName(), new InetSocketAddress(inetAddress, hostInfo.getPortNumber()));
                }
            }
            ILogon newLogon = connect(hostInfo);
            if (this.logonResult == null) {
                try {
                    logon(newLogon, logoff);
                    this.serverDiscovery.connectionSuccessful(hostInfo);
                    if (discoverHosts) {
                        List<HostInfo> updatedHosts = this.serverDiscovery.getKnownHosts(logonResult, this.serverInstance);
                        if (updatedHosts.size() > 1 && new HashSet<HostInfo>(updatedHosts).size() > new HashSet<HostInfo>(hostCopy).size()) {
                            hostKeys = updatedHosts;
                            closeServerInstance();
                            discoverHosts = false;
                            continue;
                        }
                    }
                } catch (LogonException e) {
                    // to give to the user
                    throw new ConnectionException(e);
                } catch (TeiidComponentException e) {
                    if (e.getCause() instanceof CommunicationException) {
                        throw (CommunicationException) e.getCause();
                    }
                    throw new CommunicationException(JDBCPlugin.Event.TEIID20018, e, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20018));
                }
            }
            return this.serverInstance;
        } catch (IOException e) {
            ex = e;
        } catch (SingleInstanceCommunicationException e) {
            ex = e;
        }
        this.serverDiscovery.markInstanceAsBad(hostInfo);
        if (knownHosts == 1) {
            // just a single host, use the exception
            if (ex instanceof UnknownHostException) {
                throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20019, ex, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20019, hostInfo.getHostName()));
            }
            throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20020, ex, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20020, hostInfo.getHostName(), String.valueOf(hostInfo.getPortNumber()), ex.getMessage()));
        }
        // $NON-NLS-1$
        log.log(Level.FINE, "Unable to connect to host", ex);
    }
    throw new CommunicationException(JDBCPlugin.Event.TEIID20021, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20021, hostCopy.toString()));
}
Also used : CommunicationException(org.teiid.net.CommunicationException) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InvalidSessionException(org.teiid.client.security.InvalidSessionException) TeiidComponentException(org.teiid.core.TeiidComponentException) LogonException(org.teiid.client.security.LogonException) SocketException(java.net.SocketException) CommunicationException(org.teiid.net.CommunicationException) TeiidException(org.teiid.core.TeiidException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ConnectionException(org.teiid.net.ConnectionException) ILogon(org.teiid.client.security.ILogon) LogonException(org.teiid.client.security.LogonException) TeiidComponentException(org.teiid.core.TeiidComponentException) HostInfo(org.teiid.net.HostInfo) InetAddress(java.net.InetAddress) ConnectionException(org.teiid.net.ConnectionException)

Example 13 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class SocketServerConnectionFactory method getServerInstance.

@Override
public SocketServerInstance getServerInstance(HostInfo info) throws CommunicationException, IOException {
    CachedInstance key = null;
    boolean useCache = this.maxCachedInstances > 0;
    if (useCache) {
        CachedInstance instance = null;
        key = new CachedInstance(info);
        synchronized (instancePool) {
            instance = instancePool.remove(key);
        }
        if (instance != null) {
            ILogon logon = instance.actual.getService(ILogon.class);
            boolean valid = false;
            try {
                Future<?> success = logon.ping();
                success.get(this.channelFactory.getSoTimeout(), TimeUnit.MILLISECONDS);
                valid = true;
            } catch (Exception e) {
                // $NON-NLS-1$
                log.log(Level.FINE, "Error performing ping, will select another instance", e);
            }
            if (valid) {
                return instance.proxy;
            }
            instance.actual.shutdown();
            // technically we only want to remove instances with the same inetaddress
            while (true) {
                CachedInstance invalid = null;
                synchronized (instancePool) {
                    invalid = instancePool.remove(key);
                }
                if (invalid == null) {
                    break;
                }
                invalid.actual.shutdown();
            }
        }
    }
    SocketServerInstanceImpl ssii = new SocketServerInstanceImpl(info, getSynchronousTtl(), this.channelFactory.getSoTimeout());
    ssii.connect(this.channelFactory);
    if (useCache) {
        key.actual = ssii;
        key.instance = instanceCount.getAndIncrement();
        // create a proxied socketserverinstance that will pool itself on shutdown
        key.proxy = (SocketServerInstance) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { SocketServerInstance.class }, new ShutdownHandler(key));
        return key.proxy;
    }
    return ssii;
}
Also used : ILogon(org.teiid.client.security.ILogon) MalformedURLException(java.net.MalformedURLException) CommunicationException(org.teiid.net.CommunicationException) TeiidException(org.teiid.core.TeiidException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConnectionException(org.teiid.net.ConnectionException)

Example 14 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class SocketServerInstanceImpl method doHandshake.

private void doHandshake() throws IOException, CommunicationException {
    Handshake handshake = null;
    boolean sentInit = false;
    long handShakeRetries = 1;
    if (this.soTimeout > 0) {
        handShakeRetries = Math.max(1, synchTimeout / this.soTimeout);
    }
    for (int i = 0; i < handShakeRetries; i++) {
        try {
            Object obj = this.socketChannel.read();
            if (!(obj instanceof Handshake)) {
                throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20009, null, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20009));
            }
            handshake = (Handshake) obj;
            break;
        } catch (ClassNotFoundException e1) {
            throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20010, e1, e1.getMessage());
        } catch (SocketTimeoutException e) {
            if (!sentInit && !this.info.isSsl()) {
                // write a dummy initialization value - if the server is actually ssl, this can cause the server side handshake to fail, otherwise it's ignored
                // TODO: could always do this initialization in the non-ssl case and not wait for a timeout
                this.socketChannel.write(null);
                sentInit = true;
            }
            if (i == handShakeRetries - 1) {
                throw e;
            }
        } catch (IOException e) {
            if (sentInit && !this.info.isSsl()) {
                throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20032, e, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20032));
            }
            throw e;
        }
    }
    try {
        /*if (!getVersionInfo().equals(handshake.getVersion())) {
                 throw new CommunicationException(JDBCPlugin.Event.TEIID20011, NetPlugin.Util.getString(JDBCPlugin.Event.TEIID20011, getVersionInfo(), handshake.getVersion()));
            }*/
        serverVersion = handshake.getVersion();
        handshake.setVersion();
        byte[] serverPublicKey = handshake.getPublicKey();
        byte[] serverPublicKeyLarge = handshake.getPublicKeyLarge();
        if (serverPublicKey != null) {
            DhKeyGenerator keyGen = new DhKeyGenerator();
            boolean large = false;
            if (serverPublicKeyLarge != null) {
                try {
                    byte[] publicKey = keyGen.createPublicKey(true);
                    handshake.setPublicKey(null);
                    handshake.setPublicKeyLarge(publicKey);
                    serverPublicKey = serverPublicKeyLarge;
                    large = true;
                } catch (CryptoException e) {
                // not supported on this platform
                }
            }
            if (!large) {
                byte[] publicKey = keyGen.createPublicKey(false);
                handshake.setPublicKey(publicKey);
                handshake.setPublicKeyLarge(null);
            }
            boolean useCbc = handshake.isCbc();
            // $NON-NLS-1$
            this.cryptor = keyGen.getSymmetricCryptor(serverPublicKey, "08.03".compareTo(serverVersion) > 0, this.getClass().getClassLoader(), large, useCbc);
        } else {
            this.cryptor = new NullCryptor();
        }
        this.socketChannel.write(handshake);
    } catch (CryptoException e) {
        throw new CommunicationException(JDBCPlugin.Event.TEIID20012, e, e.getMessage());
    }
}
Also used : CommunicationException(org.teiid.net.CommunicationException) IOException(java.io.IOException) NullCryptor(org.teiid.core.crypto.NullCryptor) SocketTimeoutException(java.net.SocketTimeoutException) DhKeyGenerator(org.teiid.core.crypto.DhKeyGenerator) CryptoException(org.teiid.core.crypto.CryptoException)

Example 15 with CommunicationException

use of org.teiid.net.CommunicationException in project teiid by teiid.

the class TransportService method start.

@Override
public void start(StartContext context) throws StartException {
    this.setVDBRepository(this.getVdbRepository());
    SessionService ss = sessionServiceInjector.getValue();
    this.setSecurityHelper(ss.getSecurityHelper());
    // create the necessary services
    // $NON-NLS-1$
    this.logon = new LogonImpl(ss, "teiid-cluster");
    DQP dqpProxy = proxyService(DQP.class, getDQP(), LogConstants.CTX_DQP);
    this.registerClientService(ILogon.class, logon, LogConstants.CTX_SECURITY);
    this.registerClientService(DQP.class, dqpProxy, LogConstants.CTX_DQP);
    this.setAuthenticationType(ss.getDefaultAuthenticationType());
    if (this.socketConfig != null) {
        /*
    		try {
				// this is to show the bound socket port in the JMX console
				SocketBinding socketBinding = getSocketBindingInjector().getValue();
				ManagedServerSocketBinding ss = (ManagedServerSocketBinding)socketBinding.getSocketBindings().getServerSocketFactory().createServerSocket(socketBinding.getName());
				socketBinding.getSocketBindings().getNamedRegistry().registerBinding(ss);
			}  catch (IOException e) {
				throw new StartException(IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50013));
			}
    		*/
        this.address = getSocketBindingInjector().getValue().getSocketAddress();
        this.socketConfig.setBindAddress(this.address.getHostName());
        this.socketConfig.setPortNumber(this.address.getPort());
        boolean sslEnabled = false;
        if (this.socketConfig.getSSLConfiguration() != null) {
            sslEnabled = this.socketConfig.getSSLConfiguration().isSslEnabled();
        }
        if (socketConfig.getProtocol() == WireProtocol.teiid) {
            this.socketListener = new SocketListener(address, this.socketConfig, this, getBufferManagerInjector().getValue());
            // $NON-NLS-1$ //$NON-NLS-2$
            LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50012, this.transportName, address.getHostName(), String.valueOf(address.getPort()), (sslEnabled ? "ON" : "OFF")));
        } else if (socketConfig.getProtocol() == WireProtocol.pg) {
            TeiidDriver driver = new TeiidDriver();
            driver.setLocalProfile(new ConnectionProfile() {

                @Override
                public ConnectionImpl connect(String url, Properties info) throws TeiidSQLException {
                    try {
                        LocalServerConnection sc = new LocalServerConnection(info, true) {

                            @Override
                            protected ClientServiceRegistry getClientServiceRegistry(String name) {
                                return TransportService.this;
                            }
                        };
                        return new ConnectionImpl(sc, info, url);
                    } catch (CommunicationException e) {
                        throw TeiidSQLException.create(e);
                    } catch (ConnectionException e) {
                        throw TeiidSQLException.create(e);
                    }
                }
            });
            ODBCSocketListener odbc = new ODBCSocketListener(address, this.socketConfig, this, getBufferManagerInjector().getValue(), getMaxODBCLobSizeAllowed(), this.logon, driver);
            this.socketListener = odbc;
            // $NON-NLS-1$ //$NON-NLS-2$
            LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50037, this.transportName, address.getHostName(), String.valueOf(address.getPort()), (sslEnabled ? "ON" : "OFF")));
        } else {
            throw new StartException(IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50013));
        }
    } else {
        LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50038, LocalServerConnection.jndiNameForRuntime(transportName)));
    }
}
Also used : DQP(org.teiid.client.DQP) CommunicationException(org.teiid.net.CommunicationException) ConnectionProfile(org.teiid.jdbc.ConnectionProfile) ConnectionImpl(org.teiid.jdbc.ConnectionImpl) Properties(java.util.Properties) SessionService(org.teiid.dqp.service.SessionService) LogonImpl(org.teiid.transport.LogonImpl) SocketListener(org.teiid.transport.SocketListener) ODBCSocketListener(org.teiid.transport.ODBCSocketListener) TeiidDriver(org.teiid.jdbc.TeiidDriver) StartException(org.jboss.msc.service.StartException) LocalServerConnection(org.teiid.transport.LocalServerConnection) ODBCSocketListener(org.teiid.transport.ODBCSocketListener) ConnectionException(org.teiid.net.ConnectionException)

Aggregations

CommunicationException (org.teiid.net.CommunicationException)20 Test (org.junit.Test)9 ConnectionException (org.teiid.net.ConnectionException)9 IOException (java.io.IOException)7 ILogon (org.teiid.client.security.ILogon)6 LogonException (org.teiid.client.security.LogonException)6 TeiidComponentException (org.teiid.core.TeiidComponentException)6 Properties (java.util.Properties)5 TeiidException (org.teiid.core.TeiidException)5 UnknownHostException (java.net.UnknownHostException)4 InvalidSessionException (org.teiid.client.security.InvalidSessionException)4 SocketException (java.net.SocketException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 CryptoException (org.teiid.core.crypto.CryptoException)3 NullCryptor (org.teiid.core.crypto.NullCryptor)3 TeiidURL (org.teiid.net.TeiidURL)3 SocketServerConnection (org.teiid.net.socket.SocketServerConnection)3 ConnectException (java.net.ConnectException)2 MalformedURLException (java.net.MalformedURLException)2 DQP (org.teiid.client.DQP)2