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();
}
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()));
}
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;
}
Aggregations