ConnectionState state = (ConnectionState)((DelegateSupport)cd).getState(); if(trace) { log.trace(this + " registering " + listener + " on " + cd); } state.getRemotingConnection().getInvokingClient().addConnectionListener(listener);
((ConnectionState)((DelegateSupport)cd).getState()). getRemotingConnectionListener().addDelegateListener(listener);
   public Object handleCreateConnectionDelegate(Invocation invocation) throws Throwable   {      // maintain the identity of the delegate that sends invocation through this aspect, for      // logging purposes. It makes sense, since it's an PER_INSTANCE aspect.      if (id == null)      {         id = DelegateIdentity.getIdentity(invocation);      }      cacheLocalDelegates(invocation);      if (delegates == null)      {         // not clustered, pass the invocation through         if(trace) { log.trace(this + " detecting non-clustered connection creation request, letting it pass through"); }         return invocation.invokeNext();      }      // clustered, stopping the invocation here and re-send it as non-clustered down the stack      // TODO: this should be in loop while we get exceptions creating connections, always trying      //       the next Delegate when we get an exception.      // In a clustered configuration we create connections in a round-robin fashion, contacting      // successively all available servers.      ClientConnectionFactoryDelegate cfDelegate = getDelegateRoundRobin();      if(trace) { log.trace(this + " detecting clustered connection creation request, choosing " + cfDelegate + " as target"); }      // Now create a connection delegate for this      MethodInvocation mi = (MethodInvocation)invocation;      String username = (String)mi.getArguments()[0];      String password = (String)mi.getArguments()[1];      CreateConnectionResult res = (CreateConnectionResult)cfDelegate.         createConnectionDelegate(username, password, -1);      ClientConnectionDelegate cd = (ClientConnectionDelegate)res.getDelegate();      if(trace) { log.trace(this + " got local connection delegate " + cd); }      // Add a connection listener to detect failure      ConnectionListener listener = new ConnectionFailureListener(cd);      ConnectionState state = (ConnectionState)((DelegateSupport)cd).getState();      if(trace) { log.trace(this + " registering " + listener + " on " + cd); }      state.getRemotingConnection().getInvokingClient().addConnectionListener(listener);      return new CreateConnectionResult(cd);   }