Program: 116440
Project: 116440
Buggy source:
private void addEventListeners(){
  topologyEventReceiver.addEventListener(new CompleteTopologyEventListener(){
    @Override protected void onEvent(    Event event){
      if (!topologyInitialized) {
        log.info("String_Node_Str" + event.getClass());
        TopologyManager.acquireReadLock();
        try {
          for (          Application application : TopologyManager.getTopology().getApplications()) {
            startApplicationMonitor(application.getUniqueIdentifier());
          }
          topologyInitialized=true;
        }
 catch (        Exception e) {
          log.error("String_Node_Str",e);
        }
 finally {
          TopologyManager.releaseReadLock();
        }
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        log.info("String_Node_Str" + event.getClass());
        ApplicationCreatedEvent applicationCreatedEvent=(ApplicationCreatedEvent)event;
        try {
          TopologyManager.acquireReadLockForApplication(applicationCreatedEvent.getApplication().getUniqueIdentifier());
          startApplicationMonitor(applicationCreatedEvent.getApplication().getUniqueIdentifier());
        }
 catch (        Exception e) {
          String msg="String_Node_Str" + e.getLocalizedMessage();
          log.error(msg,e);
        }
 finally {
          TopologyManager.releaseReadLockForApplication(applicationCreatedEvent.getApplication().getUniqueIdentifier());
        }
      }
 catch (      ClassCastException e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterActivatedEvent clusterActivatedEvent=(ClusterActivatedEvent)event;
      String appId=clusterActivatedEvent.getAppId();
      String clusterId=clusterActivatedEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      if (clusterMonitor != null) {
        clusterMonitor.setStatus(ClusterStatus.Active);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterCreatedEvent clusterCreatedEvent=(ClusterCreatedEvent)event;
      String clusterId=clusterCreatedEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      clusterMonitor.setStatus(ClusterStatus.Created);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInActivateEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInactivateEvent clusterInactivateEvent=(ClusterInactivateEvent)event;
      String appId=clusterInactivateEvent.getAppId();
      String clusterId=clusterInactivateEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      if (clusterMonitor != null) {
        clusterMonitor.setStatus(ClusterStatus.Inactive);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterTerminatingEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterTerminatingEvent clusterTerminatingEvent=(ClusterTerminatingEvent)event;
      String appId=clusterTerminatingEvent.getAppId();
      String clusterId=clusterTerminatingEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      if (clusterMonitor != null) {
        clusterMonitor.setDestroyed(true);
        clusterMonitor.terminateAllMembers();
        clusterMonitor.setStatus(ClusterStatus.Terminating);
      }
 else {
        log.warn("String_Node_Str" + clusterId);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterTerminatedEvent clusterTerminatedEvent=(ClusterTerminatedEvent)event;
      String appId=clusterTerminatedEvent.getAppId();
      String clusterId=clusterTerminatedEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      if (clusterMonitor != null) {
        clusterMonitor.setStatus(ClusterStatus.Terminated);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new GroupActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      GroupActivatedEvent groupActivatedEvent=(GroupActivatedEvent)event;
      String appId=groupActivatedEvent.getAppId();
      String groupId=groupActivatedEvent.getGroupId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      GroupMonitor monitor=(GroupMonitor)appMonitor.findGroupMonitorWithId(groupId);
      if (monitor != null) {
        monitor.setStatus(GroupStatus.Active);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new GroupInActivateEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      GroupInactivateEvent groupInactivateEvent=(GroupInactivateEvent)event;
      String appId=groupInactivateEvent.getAppId();
      String groupId=groupInactivateEvent.getGroupId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      GroupMonitor monitor=(GroupMonitor)appMonitor.findGroupMonitorWithId(groupId);
      if (monitor != null) {
        monitor.setStatus(GroupStatus.Inactive);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new GroupTerminatingEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      GroupTerminatingEvent groupTerminatingEvent=(GroupTerminatingEvent)event;
      String appId=groupTerminatingEvent.getAppId();
      String groupId=groupTerminatingEvent.getGroupId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      GroupMonitor monitor=(GroupMonitor)appMonitor.findGroupMonitorWithId(groupId);
      if (monitor != null) {
        monitor.setStatus(GroupStatus.Terminating);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new GroupTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      GroupTerminatedEvent groupTerminatedEvent=(GroupTerminatedEvent)event;
      String appId=groupTerminatedEvent.getAppId();
      String groupId=groupTerminatedEvent.getGroupId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      GroupMonitor monitor=(GroupMonitor)appMonitor.findGroupMonitorWithId(groupId);
      if (monitor != null) {
        monitor.setStatus(GroupStatus.Terminated);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ApplicationActivatedEvent applicationActivatedEvent=(ApplicationActivatedEvent)event;
      String appId=applicationActivatedEvent.getAppId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      if (appMonitor != null) {
        appMonitor.setStatus(ApplicationStatus.Active);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationUndeployedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ApplicationUndeployedEvent applicationUndeployedEvent=(ApplicationUndeployedEvent)event;
      TopologyManager.acquireReadLockForApplication(applicationUndeployedEvent.getApplicationId());
      Set<ClusterDataHolder> clusterDataHolders=applicationUndeployedEvent.getClusterData();
      if (clusterDataHolders != null) {
        for (        ClusterDataHolder clusterData : clusterDataHolders) {
          TopologyManager.acquireReadLockForCluster(clusterData.getServiceType(),clusterData.getClusterId());
        }
      }
      try {
        ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(applicationUndeployedEvent.getApplicationId());
        if (appMonitor != null) {
          appMonitor.setStatus(ApplicationStatus.Terminating);
          boolean clusterMonitorsFound=false;
          for (          ClusterDataHolder clusterData : clusterDataHolders) {
            ClusterMonitor clusterMonitor=((ClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterData.getClusterId()));
            if (clusterMonitor != null) {
              clusterMonitorsFound=true;
              clusterMonitor.setDestroyed(true);
              if (clusterMonitor.getStatus() == ClusterStatus.Active) {
                clusterMonitor.setStatus(ClusterStatus.Terminating);
                InstanceNotificationPublisher.sendInstanceCleanupEventForCluster(clusterData.getClusterId());
              }
 else {
                clusterMonitor.setStatus(ClusterStatus.Terminating);
                clusterMonitor.terminateAllMembers();
              }
            }
 else {
              log.warn("String_Node_Str" + clusterData.getClusterId());
              StatusEventPublisher.sendClusterTerminatedEvent(applicationUndeployedEvent.getApplicationId(),clusterData.getServiceType(),clusterData.getClusterId());
            }
          }
          if (!clusterMonitorsFound) {
            StatusEventPublisher.sendApplicationTerminatedEvent(applicationUndeployedEvent.getApplicationId(),clusterDataHolders);
          }
        }
 else {
          log.warn("String_Node_Str" + applicationUndeployedEvent.getApplicationId());
          StatusEventPublisher.sendApplicationTerminatedEvent(applicationUndeployedEvent.getApplicationId(),clusterDataHolders);
        }
      }
  finally {
        if (clusterDataHolders != null) {
          for (          ClusterDataHolder clusterData : clusterDataHolders) {
            TopologyManager.releaseReadLockForCluster(clusterData.getServiceType(),clusterData.getClusterId());
          }
        }
        TopologyManager.releaseReadLockForApplication(applicationUndeployedEvent.getApplicationId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationTerminatingEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ApplicationTerminatingEvent appTerminatingEvent=(ApplicationTerminatingEvent)event;
      TopologyManager.acquireReadLockForApplication(appTerminatingEvent.getAppId());
      try {
        ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appTerminatingEvent.getAppId());
        if (appMonitor != null) {
          appMonitor.setStatus(ApplicationStatus.Terminating);
        }
 else {
          log.warn("String_Node_Str" + appTerminatingEvent.getAppId());
        }
      }
  finally {
        TopologyManager.releaseReadLockForApplication(appTerminatingEvent.getAppId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ApplicationTerminatedEvent applicationRemovedEvent=(ApplicationTerminatedEvent)event;
      Set<ClusterDataHolder> clusterDataHolders=applicationRemovedEvent.getClusterData();
      try {
        ApplicationMonitor monitor=AutoscalerContext.getInstance().getAppMonitor(applicationRemovedEvent.getAppId());
        if (monitor != null) {
          for (          ClusterDataHolder clusterData : clusterDataHolders) {
            ClusterMonitor clusterMonitor=((ClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterData.getClusterId()));
            if (clusterMonitor != null) {
              clusterMonitor.setDestroyed(true);
              AutoscalerContext.getInstance().removeMonitor(clusterData.getClusterId());
            }
 else {
              log.warn("String_Node_Str" + clusterData.getClusterId() + "String_Node_Str");
            }
          }
          AutoscalerContext.getInstance().removeAppMonitor(applicationRemovedEvent.getAppId());
        }
 else {
          log.warn("String_Node_Str" + applicationRemovedEvent.getAppId() + "String_Node_Str");
        }
      }
  finally {
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberReadyToShutdownEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        MemberReadyToShutdownEvent memberReadyToShutdownEvent=(MemberReadyToShutdownEvent)event;
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        AbstractClusterMonitor monitor;
        String clusterId=memberReadyToShutdownEvent.getClusterId();
        String memberId=memberReadyToShutdownEvent.getMemberId();
        if (asCtx.monitorExist(clusterId)) {
          monitor=(AbstractClusterMonitor)asCtx.getMonitor(clusterId);
        }
 else         if (asCtx.lbMonitorExist(clusterId)) {
          monitor=asCtx.getLBMonitor(clusterId);
        }
 else {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        NetworkPartitionContext nwPartitionCtxt;
        nwPartitionCtxt=monitor.getNetworkPartitionCtxt(memberReadyToShutdownEvent.getNetworkPartitionId());
        String partitionId=monitor.getPartitionOfMember(memberId);
        PartitionContext partitionCtxt=nwPartitionCtxt.getPartitionCtxt(partitionId);
        CloudControllerClient ccClient=CloudControllerClient.getInstance();
        ccClient.terminate(memberId);
        partitionCtxt.removeActiveMemberById(memberId);
        if (log.isInfoEnabled()) {
          log.info(String.format("String_Node_Str" + "String_Node_Str",memberId,partitionId,clusterId));
        }
      }
 catch (      TerminationException e) {
        log.error(e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterRemovedEventListener(){
    @Override protected void onEvent(    Event event){
      ClusterRemovedEvent clusterRemovedEvent=null;
      try {
        clusterRemovedEvent=(ClusterRemovedEvent)event;
        TopologyManager.acquireReadLockForCluster(clusterRemovedEvent.getServiceName(),clusterRemovedEvent.getClusterId());
        String clusterId=clusterRemovedEvent.getClusterId();
        String deploymentPolicy=clusterRemovedEvent.getDeploymentPolicy();
        AbstractClusterMonitor monitor;
        if (clusterRemovedEvent.isLbCluster()) {
          DeploymentPolicy depPolicy=PolicyManager.getInstance().getDeploymentPolicy(deploymentPolicy);
          if (depPolicy != null) {
            List<NetworkPartitionLbHolder> lbHolders=PartitionManager.getInstance().getNetworkPartitionLbHolders(depPolicy);
            for (            NetworkPartitionLbHolder networkPartitionLbHolder : lbHolders) {
              boolean isRemoved=networkPartitionLbHolder.removeLbClusterId(clusterId);
              if (isRemoved) {
                log.info("String_Node_Str" + clusterId + "String_Node_Str"+ networkPartitionLbHolder.getNetworkPartitionId());
              }
              if (log.isDebugEnabled()) {
                log.debug(networkPartitionLbHolder);
              }
            }
          }
          monitor=AutoscalerContext.getInstance().removeLbMonitor(clusterId);
        }
 else {
          monitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().removeMonitor(clusterId);
        }
        if (monitor != null) {
          monitor.destroy();
          log.info(String.format("String_Node_Str",clusterId));
        }
      }
 catch (      Exception e) {
        log.error("String_Node_Str",e);
      }
 finally {
        TopologyManager.releaseReadLockForCluster(clusterRemovedEvent.getServiceName(),clusterRemovedEvent.getClusterId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberStartedEventListener(){
    @Override protected void onEvent(    Event event){
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      MemberTerminatedEvent memberTerminatedEvent=null;
      try {
        memberTerminatedEvent=(MemberTerminatedEvent)event;
        String networkPartitionId=memberTerminatedEvent.getNetworkPartitionId();
        String clusterId=memberTerminatedEvent.getClusterId();
        String partitionId=memberTerminatedEvent.getPartitionId();
        AbstractClusterMonitor monitor;
        TopologyManager.acquireReadLockForCluster(memberTerminatedEvent.getServiceName(),memberTerminatedEvent.getClusterId());
        if (AutoscalerContext.getInstance().monitorExist(clusterId)) {
          monitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
        }
 else {
          monitor=AutoscalerContext.getInstance().getLBMonitor(clusterId);
        }
        NetworkPartitionContext networkPartitionContext=monitor.getNetworkPartitionCtxt(networkPartitionId);
        PartitionContext partitionContext=networkPartitionContext.getPartitionCtxt(partitionId);
        String memberId=memberTerminatedEvent.getMemberId();
        partitionContext.removeMemberStatsContext(memberId);
        if (partitionContext.removeTerminationPendingMember(memberId)) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",memberId));
          }
        }
 else         if (partitionContext.removePendingMember(memberId)) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",memberId));
          }
        }
 else         if (partitionContext.removeActiveMemberById(memberId)) {
          log.warn(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
 else {
          log.warn(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
        if (log.isInfoEnabled()) {
          log.info(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
        StatusChecker.getInstance().onMemberTermination(clusterId);
      }
 catch (      Exception e) {
        log.error("String_Node_Str",e);
      }
 finally {
        TopologyManager.releaseReadLockForCluster(memberTerminatedEvent.getServiceName(),memberTerminatedEvent.getClusterId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      MemberActivatedEvent memberActivatedEvent=(MemberActivatedEvent)event;
      TopologyManager.acquireReadLockForCluster(memberActivatedEvent.getServiceName(),memberActivatedEvent.getClusterId());
      try {
        String networkPartitionId=memberActivatedEvent.getNetworkPartitionId();
        String clusterId=memberActivatedEvent.getClusterId();
        String partitionId=memberActivatedEvent.getPartitionId();
        String memberId=memberActivatedEvent.getMemberId();
        AbstractClusterMonitor monitor;
        if (AutoscalerContext.getInstance().monitorExist(clusterId)) {
          monitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
        }
 else {
          monitor=AutoscalerContext.getInstance().getLBMonitor(clusterId);
        }
        NetworkPartitionContext networkPartitionContext=monitor.getNetworkPartitionCtxt(networkPartitionId);
        PartitionContext partitionContext=networkPartitionContext.getPartitionCtxt(partitionId);
        partitionContext.addMemberStatsContext(new MemberStatsContext(memberId));
        if (log.isInfoEnabled()) {
          log.info(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
        partitionContext.movePendingMemberToActiveMembers(memberId);
        StatusChecker.getInstance().onMemberStatusChange(memberActivatedEvent.getClusterId());
      }
 catch (      Exception e) {
        log.error("String_Node_Str",e);
      }
 finally {
        TopologyManager.releaseReadLockForCluster(memberActivatedEvent.getServiceName(),memberActivatedEvent.getClusterId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberMaintenanceListener(){
    @Override protected void onEvent(    Event event){
      MemberMaintenanceModeEvent memberMaintenanceModeEvent=(MemberMaintenanceModeEvent)event;
      TopologyManager.acquireReadLockForCluster(memberMaintenanceModeEvent.getServiceName(),memberMaintenanceModeEvent.getClusterId());
      try {
        String memberId=memberMaintenanceModeEvent.getMemberId();
        String partitionId=memberMaintenanceModeEvent.getPartitionId();
        String networkPartitionId=memberMaintenanceModeEvent.getNetworkPartitionId();
        PartitionContext partitionContext;
        String clusterId=memberMaintenanceModeEvent.getClusterId();
        AbstractClusterMonitor monitor;
        if (AutoscalerContext.getInstance().monitorExist(clusterId)) {
          monitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
          partitionContext=monitor.getNetworkPartitionCtxt(networkPartitionId).getPartitionCtxt(partitionId);
        }
 else {
          monitor=AutoscalerContext.getInstance().getLBMonitor(clusterId);
          partitionContext=monitor.getNetworkPartitionCtxt(networkPartitionId).getPartitionCtxt(partitionId);
        }
        partitionContext.addMemberStatsContext(new MemberStatsContext(memberId));
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
        partitionContext.moveActiveMemberToTerminationPendingMembers(memberId);
      }
 catch (      Exception e) {
        log.error("String_Node_Str",e);
      }
 finally {
        TopologyManager.releaseReadLockForCluster(memberMaintenanceModeEvent.getServiceName(),memberMaintenanceModeEvent.getClusterId());
      }
    }
  }
);
}

Fixed version (ground truth):
private void addEventListeners(){
  topologyEventReceiver.addEventListener(new CompleteTopologyEventListener(){
    @Override protected void onEvent(    Event event){
      if (!topologyInitialized) {
        log.info("String_Node_Str" + event.getClass());
        TopologyManager.acquireReadLock();
        try {
          for (          Application application : TopologyManager.getTopology().getApplications()) {
            startApplicationMonitor(application.getUniqueIdentifier());
          }
          topologyInitialized=true;
        }
 catch (        Exception e) {
          log.error("String_Node_Str",e);
        }
 finally {
          TopologyManager.releaseReadLock();
        }
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        log.info("String_Node_Str" + event.getClass());
        ApplicationCreatedEvent applicationCreatedEvent=(ApplicationCreatedEvent)event;
        try {
          TopologyManager.acquireReadLockForApplication(applicationCreatedEvent.getApplication().getUniqueIdentifier());
          startApplicationMonitor(applicationCreatedEvent.getApplication().getUniqueIdentifier());
        }
 catch (        Exception e) {
          String msg="String_Node_Str" + e.getLocalizedMessage();
          log.error(msg,e);
        }
 finally {
          TopologyManager.releaseReadLockForApplication(applicationCreatedEvent.getApplication().getUniqueIdentifier());
        }
      }
 catch (      ClassCastException e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterActivatedEvent clusterActivatedEvent=(ClusterActivatedEvent)event;
      String appId=clusterActivatedEvent.getAppId();
      String clusterId=clusterActivatedEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      if (clusterMonitor != null) {
        clusterMonitor.setStatus(ClusterStatus.Active);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterCreatedEvent clusterCreatedEvent=(ClusterCreatedEvent)event;
      String clusterId=clusterCreatedEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      clusterMonitor.setStatus(ClusterStatus.Created);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInActivateEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInactivateEvent clusterInactivateEvent=(ClusterInactivateEvent)event;
      String appId=clusterInactivateEvent.getAppId();
      String clusterId=clusterInactivateEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      if (clusterMonitor != null) {
        clusterMonitor.setStatus(ClusterStatus.Inactive);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterTerminatingEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterTerminatingEvent clusterTerminatingEvent=(ClusterTerminatingEvent)event;
      String clusterId=clusterTerminatingEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      if (clusterMonitor != null) {
        if (clusterMonitor.getStatus() == ClusterStatus.Active) {
          clusterMonitor.setStatus(ClusterStatus.Terminating);
          InstanceNotificationPublisher.sendInstanceCleanupEventForCluster(clusterId);
        }
 else {
          clusterMonitor.setStatus(ClusterStatus.Terminating);
          clusterMonitor.terminateAllMembers();
        }
      }
 else {
        log.warn("String_Node_Str" + clusterId);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterTerminatedEvent clusterTerminatedEvent=(ClusterTerminatedEvent)event;
      String appId=clusterTerminatedEvent.getAppId();
      String clusterId=clusterTerminatedEvent.getClusterId();
      AbstractClusterMonitor clusterMonitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
      if (clusterMonitor != null) {
        clusterMonitor.setStatus(ClusterStatus.Terminated);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new GroupActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      GroupActivatedEvent groupActivatedEvent=(GroupActivatedEvent)event;
      String appId=groupActivatedEvent.getAppId();
      String groupId=groupActivatedEvent.getGroupId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      GroupMonitor monitor=(GroupMonitor)appMonitor.findGroupMonitorWithId(groupId);
      if (monitor != null) {
        monitor.setStatus(GroupStatus.Active);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new GroupInActivateEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      GroupInactivateEvent groupInactivateEvent=(GroupInactivateEvent)event;
      String appId=groupInactivateEvent.getAppId();
      String groupId=groupInactivateEvent.getGroupId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      GroupMonitor monitor=(GroupMonitor)appMonitor.findGroupMonitorWithId(groupId);
      if (monitor != null) {
        monitor.setStatus(GroupStatus.Inactive);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new GroupTerminatingEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      GroupTerminatingEvent groupTerminatingEvent=(GroupTerminatingEvent)event;
      String appId=groupTerminatingEvent.getAppId();
      String groupId=groupTerminatingEvent.getGroupId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      GroupMonitor monitor=(GroupMonitor)appMonitor.findGroupMonitorWithId(groupId);
      if (monitor != null) {
        monitor.setStatus(GroupStatus.Terminating);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new GroupTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      GroupTerminatedEvent groupTerminatedEvent=(GroupTerminatedEvent)event;
      String appId=groupTerminatedEvent.getAppId();
      String groupId=groupTerminatedEvent.getGroupId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      GroupMonitor monitor=(GroupMonitor)appMonitor.findGroupMonitorWithId(groupId);
      if (monitor != null) {
        monitor.setStatus(GroupStatus.Terminated);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ApplicationActivatedEvent applicationActivatedEvent=(ApplicationActivatedEvent)event;
      String appId=applicationActivatedEvent.getAppId();
      ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
      if (appMonitor != null) {
        appMonitor.setStatus(ApplicationStatus.Active);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationUndeployedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ApplicationUndeployedEvent applicationUndeployedEvent=(ApplicationUndeployedEvent)event;
      TopologyManager.acquireReadLockForApplication(applicationUndeployedEvent.getApplicationId());
      Set<ClusterDataHolder> clusterDataHolders=applicationUndeployedEvent.getClusterData();
      if (clusterDataHolders != null) {
        for (        ClusterDataHolder clusterData : clusterDataHolders) {
          TopologyManager.acquireReadLockForCluster(clusterData.getServiceType(),clusterData.getClusterId());
        }
      }
      try {
        ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(applicationUndeployedEvent.getApplicationId());
        if (appMonitor != null) {
          appMonitor.setStatus(ApplicationStatus.Terminating);
          boolean clusterMonitorsFound=false;
          for (          ClusterDataHolder clusterData : clusterDataHolders) {
            ClusterMonitor clusterMonitor=((ClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterData.getClusterId()));
            if (clusterMonitor != null) {
              clusterMonitorsFound=true;
              clusterMonitor.setDestroyed(true);
              if (clusterMonitor.getStatus() == ClusterStatus.Active) {
                clusterMonitor.setStatus(ClusterStatus.Terminating);
                InstanceNotificationPublisher.sendInstanceCleanupEventForCluster(clusterData.getClusterId());
              }
 else {
                clusterMonitor.setStatus(ClusterStatus.Terminating);
                clusterMonitor.terminateAllMembers();
              }
            }
 else {
              log.warn("String_Node_Str" + clusterData.getClusterId());
              StatusEventPublisher.sendClusterTerminatedEvent(applicationUndeployedEvent.getApplicationId(),clusterData.getServiceType(),clusterData.getClusterId());
            }
          }
          if (!clusterMonitorsFound) {
            StatusEventPublisher.sendApplicationTerminatedEvent(applicationUndeployedEvent.getApplicationId(),clusterDataHolders);
          }
        }
 else {
          log.warn("String_Node_Str" + applicationUndeployedEvent.getApplicationId());
          StatusEventPublisher.sendApplicationTerminatedEvent(applicationUndeployedEvent.getApplicationId(),clusterDataHolders);
        }
      }
  finally {
        if (clusterDataHolders != null) {
          for (          ClusterDataHolder clusterData : clusterDataHolders) {
            TopologyManager.releaseReadLockForCluster(clusterData.getServiceType(),clusterData.getClusterId());
          }
        }
        TopologyManager.releaseReadLockForApplication(applicationUndeployedEvent.getApplicationId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationTerminatingEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ApplicationTerminatingEvent appTerminatingEvent=(ApplicationTerminatingEvent)event;
      TopologyManager.acquireReadLockForApplication(appTerminatingEvent.getAppId());
      try {
        ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appTerminatingEvent.getAppId());
        if (appMonitor != null) {
          appMonitor.setStatus(ApplicationStatus.Terminating);
        }
 else {
          log.warn("String_Node_Str" + appTerminatingEvent.getAppId());
        }
      }
  finally {
        TopologyManager.releaseReadLockForApplication(appTerminatingEvent.getAppId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ApplicationTerminatedEvent applicationRemovedEvent=(ApplicationTerminatedEvent)event;
      Set<ClusterDataHolder> clusterDataHolders=applicationRemovedEvent.getClusterData();
      try {
        ApplicationMonitor monitor=AutoscalerContext.getInstance().getAppMonitor(applicationRemovedEvent.getAppId());
        if (monitor != null) {
          for (          ClusterDataHolder clusterData : clusterDataHolders) {
            ClusterMonitor clusterMonitor=((ClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterData.getClusterId()));
            if (clusterMonitor != null) {
              clusterMonitor.setDestroyed(true);
              AutoscalerContext.getInstance().removeMonitor(clusterData.getClusterId());
            }
 else {
              log.warn("String_Node_Str" + clusterData.getClusterId() + "String_Node_Str");
            }
          }
          AutoscalerContext.getInstance().removeAppMonitor(applicationRemovedEvent.getAppId());
        }
 else {
          log.warn("String_Node_Str" + applicationRemovedEvent.getAppId() + "String_Node_Str");
        }
      }
  finally {
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberReadyToShutdownEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        MemberReadyToShutdownEvent memberReadyToShutdownEvent=(MemberReadyToShutdownEvent)event;
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        AbstractClusterMonitor monitor;
        String clusterId=memberReadyToShutdownEvent.getClusterId();
        String memberId=memberReadyToShutdownEvent.getMemberId();
        if (asCtx.monitorExist(clusterId)) {
          monitor=(AbstractClusterMonitor)asCtx.getMonitor(clusterId);
        }
 else         if (asCtx.lbMonitorExist(clusterId)) {
          monitor=asCtx.getLBMonitor(clusterId);
        }
 else {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        NetworkPartitionContext nwPartitionCtxt;
        nwPartitionCtxt=monitor.getNetworkPartitionCtxt(memberReadyToShutdownEvent.getNetworkPartitionId());
        String partitionId=monitor.getPartitionOfMember(memberId);
        PartitionContext partitionCtxt=nwPartitionCtxt.getPartitionCtxt(partitionId);
        CloudControllerClient ccClient=CloudControllerClient.getInstance();
        ccClient.terminate(memberId);
        partitionCtxt.removeActiveMemberById(memberId);
        if (log.isInfoEnabled()) {
          log.info(String.format("String_Node_Str" + "String_Node_Str",memberId,partitionId,clusterId));
        }
      }
 catch (      TerminationException e) {
        log.error(e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterRemovedEventListener(){
    @Override protected void onEvent(    Event event){
      ClusterRemovedEvent clusterRemovedEvent=null;
      try {
        clusterRemovedEvent=(ClusterRemovedEvent)event;
        TopologyManager.acquireReadLockForCluster(clusterRemovedEvent.getServiceName(),clusterRemovedEvent.getClusterId());
        String clusterId=clusterRemovedEvent.getClusterId();
        String deploymentPolicy=clusterRemovedEvent.getDeploymentPolicy();
        AbstractClusterMonitor monitor;
        if (clusterRemovedEvent.isLbCluster()) {
          DeploymentPolicy depPolicy=PolicyManager.getInstance().getDeploymentPolicy(deploymentPolicy);
          if (depPolicy != null) {
            List<NetworkPartitionLbHolder> lbHolders=PartitionManager.getInstance().getNetworkPartitionLbHolders(depPolicy);
            for (            NetworkPartitionLbHolder networkPartitionLbHolder : lbHolders) {
              boolean isRemoved=networkPartitionLbHolder.removeLbClusterId(clusterId);
              if (isRemoved) {
                log.info("String_Node_Str" + clusterId + "String_Node_Str"+ networkPartitionLbHolder.getNetworkPartitionId());
              }
              if (log.isDebugEnabled()) {
                log.debug(networkPartitionLbHolder);
              }
            }
          }
          monitor=AutoscalerContext.getInstance().removeLbMonitor(clusterId);
        }
 else {
          monitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().removeMonitor(clusterId);
        }
        if (monitor != null) {
          monitor.destroy();
          log.info(String.format("String_Node_Str",clusterId));
        }
      }
 catch (      Exception e) {
        log.error("String_Node_Str",e);
      }
 finally {
        TopologyManager.releaseReadLockForCluster(clusterRemovedEvent.getServiceName(),clusterRemovedEvent.getClusterId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberStartedEventListener(){
    @Override protected void onEvent(    Event event){
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      MemberTerminatedEvent memberTerminatedEvent=null;
      try {
        memberTerminatedEvent=(MemberTerminatedEvent)event;
        String networkPartitionId=memberTerminatedEvent.getNetworkPartitionId();
        String clusterId=memberTerminatedEvent.getClusterId();
        String partitionId=memberTerminatedEvent.getPartitionId();
        AbstractClusterMonitor monitor;
        TopologyManager.acquireReadLockForCluster(memberTerminatedEvent.getServiceName(),memberTerminatedEvent.getClusterId());
        if (AutoscalerContext.getInstance().monitorExist(clusterId)) {
          monitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
        }
 else {
          monitor=AutoscalerContext.getInstance().getLBMonitor(clusterId);
        }
        NetworkPartitionContext networkPartitionContext=monitor.getNetworkPartitionCtxt(networkPartitionId);
        PartitionContext partitionContext=networkPartitionContext.getPartitionCtxt(partitionId);
        String memberId=memberTerminatedEvent.getMemberId();
        partitionContext.removeMemberStatsContext(memberId);
        if (partitionContext.removeTerminationPendingMember(memberId)) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",memberId));
          }
        }
 else         if (partitionContext.removePendingMember(memberId)) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",memberId));
          }
        }
 else         if (partitionContext.removeActiveMemberById(memberId)) {
          log.warn(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
 else {
          log.warn(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
        if (log.isInfoEnabled()) {
          log.info(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
        StatusChecker.getInstance().onMemberTermination(clusterId);
      }
 catch (      Exception e) {
        log.error("String_Node_Str",e);
      }
 finally {
        TopologyManager.releaseReadLockForCluster(memberTerminatedEvent.getServiceName(),memberTerminatedEvent.getClusterId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      MemberActivatedEvent memberActivatedEvent=(MemberActivatedEvent)event;
      TopologyManager.acquireReadLockForCluster(memberActivatedEvent.getServiceName(),memberActivatedEvent.getClusterId());
      try {
        String networkPartitionId=memberActivatedEvent.getNetworkPartitionId();
        String clusterId=memberActivatedEvent.getClusterId();
        String partitionId=memberActivatedEvent.getPartitionId();
        String memberId=memberActivatedEvent.getMemberId();
        AbstractClusterMonitor monitor;
        if (AutoscalerContext.getInstance().monitorExist(clusterId)) {
          monitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
        }
 else {
          monitor=AutoscalerContext.getInstance().getLBMonitor(clusterId);
        }
        NetworkPartitionContext networkPartitionContext=monitor.getNetworkPartitionCtxt(networkPartitionId);
        PartitionContext partitionContext=networkPartitionContext.getPartitionCtxt(partitionId);
        partitionContext.addMemberStatsContext(new MemberStatsContext(memberId));
        if (log.isInfoEnabled()) {
          log.info(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
        partitionContext.movePendingMemberToActiveMembers(memberId);
        StatusChecker.getInstance().onMemberStatusChange(memberActivatedEvent.getClusterId());
      }
 catch (      Exception e) {
        log.error("String_Node_Str",e);
      }
 finally {
        TopologyManager.releaseReadLockForCluster(memberActivatedEvent.getServiceName(),memberActivatedEvent.getClusterId());
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberMaintenanceListener(){
    @Override protected void onEvent(    Event event){
      MemberMaintenanceModeEvent memberMaintenanceModeEvent=(MemberMaintenanceModeEvent)event;
      TopologyManager.acquireReadLockForCluster(memberMaintenanceModeEvent.getServiceName(),memberMaintenanceModeEvent.getClusterId());
      try {
        String memberId=memberMaintenanceModeEvent.getMemberId();
        String partitionId=memberMaintenanceModeEvent.getPartitionId();
        String networkPartitionId=memberMaintenanceModeEvent.getNetworkPartitionId();
        PartitionContext partitionContext;
        String clusterId=memberMaintenanceModeEvent.getClusterId();
        AbstractClusterMonitor monitor;
        if (AutoscalerContext.getInstance().monitorExist(clusterId)) {
          monitor=(AbstractClusterMonitor)AutoscalerContext.getInstance().getMonitor(clusterId);
          partitionContext=monitor.getNetworkPartitionCtxt(networkPartitionId).getPartitionCtxt(partitionId);
        }
 else {
          monitor=AutoscalerContext.getInstance().getLBMonitor(clusterId);
          partitionContext=monitor.getNetworkPartitionCtxt(networkPartitionId).getPartitionCtxt(partitionId);
        }
        partitionContext.addMemberStatsContext(new MemberStatsContext(memberId));
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",memberId));
        }
        partitionContext.moveActiveMemberToTerminationPendingMembers(memberId);
      }
 catch (      Exception e) {
        log.error("String_Node_Str",e);
      }
 finally {
        TopologyManager.releaseReadLockForCluster(memberMaintenanceModeEvent.getServiceName(),memberMaintenanceModeEvent.getClusterId());
      }
    }
  }
);
}