public void elect() throws InterruptedException, ActiveElectionException {
  boolean isException=false;
  if (getState() != State.PENDING)   return;
  try {
    while (!isTerminate()) {
      try {
        if (findActive() != null) {
          return;
        }
 else         if (pollStateUntilAvailable(State.PREPARE) != null) {
          return;
        }
        if (findActive() != null) {
          return;
        }
 else         if (!isAdvanceToStateAllowed(State.PENDING,State.PREPARE)) {
          continue;
        }
 else         if (!doChangeState(State.PENDING,State.PREPARE)) {
          return;
        }
        if (findActive() != null) {
          return;
        }
 else         if (!isAdvanceToStateAllowed(State.PREPARE,State.ACTIVE)) {
          continue;
        }
 else         if (!doChangeState(State.PREPARE,State.ACTIVE)) {
          return;
        }
        notifyListenerOnActive(new ServiceItem(_electTemplate.getServiceID(),_electTemplate.getService(),null));
        return;
      }
 catch (      DirectPersistencyRecoveryException ex) {
        try {
          if (_logger.isLoggable(Level.WARNING)) {
            if (ex instanceof DirectPersistencyAttributeStoreException) {
              _logger.log(Level.WARNING,"Failed to set or get last primary state using AttributeStore, will try to reelect...",ex);
            }
 else {
              _logger.log(Level.WARNING,"Failed to elect as primary",ex);
            }
          }
          isException=true;
          changeState(getState(),State.PENDING,true);
        }
 catch (        RemoteException e) {
          isException=true;
          String exMsg="Failed to communicate with [" + _namingService.getName() + "] naming service while changing state from "+ getState()+ " to PENDING. ";
          throw new ActiveElectionException(exMsg,ex);
        }
      }
catch (      RemoteException ex) {
        isException=true;
        String exMsg="Failed to communicate with [" + _namingService.getName() + "] naming service. ";
        throw new ActiveElectionException(exMsg,ex);
      }
    }
  }
  finally {
    if (!isException) {
      try {
        if (getState() == State.PREPARE) {
          changeState(State.PREPARE,State.PENDING,true);
        }
      }
 catch (      RemoteException ex) {
        throw new ActiveElectionException("Force change state failed.",ex);
      }
      try {
        startSplitBrainController();
      }
 catch (      Exception ex) {
        throw new ActiveElectionException("Failed to start SplitBrainController.",ex);
      }
    }
  }
}

public void elect ( ) throws InterruptedException , ActiveElectionException { boolean isException = false ; if ( getState ( ) != State . PENDING ) return ; try { while ( ! isTerminate ( ) ) { try { if ( findActive ( ) != null ) { return ; } else if ( pollStateUntilAvailable ( State . PREPARE ) != null ) { return ; } if ( findActive ( ) != null ) { return ; } else if ( ! isAdvanceToStateAllowed ( State . PENDING , State . PREPARE ) ) { continue ; } else if ( ! doChangeState ( State . PENDING , State . PREPARE ) ) { return ; } if ( findActive ( ) != null ) { return ; } else if ( ! isAdvanceToStateAllowed ( State . PREPARE , State . ACTIVE ) ) { continue ; } else if ( ! doChangeState ( State . PREPARE , State . ACTIVE ) ) { return ; } notifyListenerOnActive ( new ServiceItem ( _electTemplate . getServiceID ( ) , _electTemplate . getService ( ) , null ) ) ; return ; } catch ( DirectPersistencyRecoveryException ex ) { try { if ( _logger . isLoggable ( Level . WARNING ) ) { if ( ex instanceof DirectPersistencyAttributeStoreException ) { _logger . log ( Level . WARNING , "Failed to set or get last primary state using AttributeStore, will try to reelect..." , ex ) ; } else { _logger . log ( Level . WARNING , "Failed to elect as primary" , ex ) ; } } isException = true ; changeState ( getState ( ) , State . PENDING , true ) ; } catch ( RemoteException e ) { isException = true ; String exMsg = "Failed to communicate with [" + _namingService . getName ( ) + "] naming service while changing state from " + getState ( ) + " to PENDING. " ; throw new ActiveElectionException ( exMsg , ex ) ; } } catch ( RemoteException ex ) { isException = true ; String exMsg = "Failed to communicate with [" + _namingService . getName ( ) + "] naming service. " ; throw new ActiveElectionException ( exMsg , ex ) ; } } } finally { if ( ! isException ) { try { if ( getState ( ) == State . PREPARE ) { changeState ( State . PREPARE , State . PENDING , true ) ; } } catch ( RemoteException ex ) { throw new ActiveElectionException ( "Force change state failed." , ex ) ; } try { startSplitBrainController ( ) ; } catch ( Exception ex ) { throw new ActiveElectionException ( "Failed to start SplitBrainController." , ex ) ; } } } }
elect the active service , this method will be blocked while the active service will be elected .
