public void connect(ID targetID, IConnectContext joinContext) throws ContainerConnectException {
try {
if (isClosing)
throw new IllegalStateException("Container closing"); //$NON-NLS-1$
if (targetID == null)
throw new ContainerConnectException("targetID cannot be null"); //$NON-NLS-1$
Object response = null;
synchronized (getConnectLock()) {
// Throw if already connected
if (isConnected())
throw new IllegalStateException("Container already connected connectedID=" + getConnectedID()); //$NON-NLS-1$
// Throw if connecting
if (isConnecting())
throw new IllegalStateException("Container connecting"); //$NON-NLS-1$
// else we're entering connecting state
// first notify synchonously
final ISynchAsynchConnection aConnection = createConnection(targetID, joinContext);
setStateConnecting(aConnection);
fireContainerEvent(new ContainerConnectingEvent(this.getID(), targetID, joinContext));
final Object connectData = getConnectData(targetID, joinContext);
final int connectTimeout = getConnectTimeout();
synchronized (aConnection) {
try {
// Make connect call
response = aConnection.connect(targetID, connectData, connectTimeout);
} catch (final ECFException e) {
if (getConnection() != aConnection)
disconnect(aConnection);
else
setStateDisconnected(aConnection);
throw e;
}
// If not in correct state, disconnect and return
if (getConnection() != aConnection) {
disconnect(aConnection);
throw new IllegalStateException("Container connect failed because not in correct state"); //$NON-NLS-1$
}
ID serverID = null;
try {
serverID = handleConnectResponse(targetID, response);
} catch (final Exception e) {
setStateDisconnected(aConnection);
throw e;
}
setStateConnected(serverID, aConnection);
// notify listeners
fireContainerEvent(new ContainerConnectedEvent(this.getID(), remoteServerID));
aConnection.start();
}
}
} catch (final ContainerConnectException e) {
throw e;
} catch (final ECFException e) {
final IStatus s = e.getStatus();
throw new ContainerConnectException(s.getMessage(), s.getException());
} catch (final Exception e) {
throw new ContainerConnectException(e.getLocalizedMessage(), e);
}
}