Search in sources :

Example 6 with ILogon

use of org.teiid.client.security.ILogon 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 7 with ILogon

use of org.teiid.client.security.ILogon 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 8 with ILogon

use of org.teiid.client.security.ILogon 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)

Aggregations

ILogon (org.teiid.client.security.ILogon)8 Test (org.junit.Test)5 CommunicationException (org.teiid.net.CommunicationException)5 IOException (java.io.IOException)3 TeiidException (org.teiid.core.TeiidException)3 ConnectionException (org.teiid.net.ConnectionException)3 UnknownHostException (java.net.UnknownHostException)2 Properties (java.util.Properties)2 InvalidSessionException (org.teiid.client.security.InvalidSessionException)2 LogonException (org.teiid.client.security.LogonException)2 TeiidComponentException (org.teiid.core.TeiidComponentException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 MalformedURLException (java.net.MalformedURLException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1