Program: 115580
Project: 115580
Buggy source:
private void addEventListeners(){
  topologyEventReceiver.addEventListener(new CompleteTopologyEventListener(){
    @Override protected void onEvent(    Event event){
      if (!topologyInitialized) {
        log.info("String_Node_Str" + event.getClass());
        try {
          ApplicationHolder.acquireReadLock();
          Applications applications=ApplicationHolder.getApplications();
          if (applications != null) {
            for (            Application application : applications.getApplications().values()) {
              ApplicationContext applicationContext=AutoscalerContext.getInstance().getApplicationContext(application.getUniqueIdentifier());
              if (applicationContext != null && applicationContext.getStatus().equals(ApplicationContext.STATUS_DEPLOYED)) {
                if (AutoscalerUtil.allClustersInitialized(application)) {
                  AutoscalerUtil.getInstance().startApplicationMonitor(application.getUniqueIdentifier());
                }
 else {
                  log.error("String_Node_Str" + "String_Node_Str");
                }
              }
 else {
                log.info("String_Node_Str" + "String_Node_Str" + application.getUniqueIdentifier());
              }
            }
            topologyInitialized=true;
          }
 else {
            log.info("String_Node_Str");
          }
        }
 catch (        Exception e) {
          log.error("String_Node_Str",e);
        }
 finally {
          ApplicationHolder.releaseReadLock();
        }
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationClustersCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        log.info("String_Node_Str" + event.getClass());
        ApplicationClustersCreatedEvent applicationClustersCreatedEvent=(ApplicationClustersCreatedEvent)event;
        String appId=applicationClustersCreatedEvent.getAppId();
        boolean appMonitorCreationTriggered=false;
        int retries=5;
        while (!appMonitorCreationTriggered && retries > 0) {
          try {
            ApplicationHolder.acquireReadLock();
            ApplicationContext applicationContext=AutoscalerContext.getInstance().getApplicationContext(appId);
            if (applicationContext != null && applicationContext.getStatus().equals(ApplicationContext.STATUS_DEPLOYED)) {
              if (!AutoscalerContext.getInstance().containsApplicationPendingMonitor(appId)) {
                appMonitorCreationTriggered=true;
                AutoscalerUtil.getInstance().startApplicationMonitor(appId);
                break;
              }
            }
          }
 catch (          Exception e) {
            String msg="String_Node_Str" + e.getLocalizedMessage();
            log.error(msg,e);
          }
 finally {
            ApplicationHolder.releaseReadLock();
          }
          try {
            retries--;
            Thread.sleep(1000);
          }
 catch (          InterruptedException e) {
          }
        }
        if (!appMonitorCreationTriggered) {
          String msg=String.format("String_Node_Str" + "String_Node_Str" + "String_Node_Str",appId,ApplicationContext.STATUS_DEPLOYED);
          log.warn(msg);
        }
      }
 catch (      ClassCastException e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInstanceActivatedEvent clusterActivatedEvent=(ClusterInstanceActivatedEvent)event;
      String clusterId=clusterActivatedEvent.getClusterId();
      String instanceId=clusterActivatedEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      monitor=asCtx.getClusterMonitor(clusterId);
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        return;
      }
      monitor.notifyParentMonitor(ClusterStatus.Active,instanceId);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterResetEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterResetEvent clusterResetEvent=(ClusterResetEvent)event;
      String clusterId=clusterResetEvent.getClusterId();
      String instanceId=clusterResetEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      monitor=asCtx.getClusterMonitor(clusterId);
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        return;
      }
      monitor.destroy();
      monitor.notifyParentMonitor(ClusterStatus.Created,instanceId);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceInactivateEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInstanceInactivateEvent clusterInactivateEvent=(ClusterInstanceInactivateEvent)event;
      String clusterId=clusterInactivateEvent.getClusterId();
      String instanceId=clusterInactivateEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      monitor=asCtx.getClusterMonitor(clusterId);
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        return;
      }
      monitor.notifyParentMonitor(ClusterStatus.Inactive,instanceId);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceTerminatingEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInstanceTerminatingEvent clusterTerminatingEvent=(ClusterInstanceTerminatingEvent)event;
      String clusterId=clusterTerminatingEvent.getClusterId();
      String clusterInstanceId=clusterTerminatingEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      monitor=asCtx.getClusterMonitor(clusterId);
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        ClusterStatusEventPublisher.sendClusterTerminatedEvent(clusterTerminatingEvent.getAppId(),clusterTerminatingEvent.getServiceName(),clusterId,clusterInstanceId);
        return;
      }
      ClusterInstance clusterInstance=(ClusterInstance)monitor.getInstance(clusterInstanceId);
      if (clusterInstance.getPreviousState() == ClusterStatus.Active) {
        monitor.notifyParentMonitor(ClusterStatus.Terminating,clusterInstanceId);
        InstanceNotificationPublisher.getInstance().sendInstanceCleanupEventForCluster(clusterId,clusterInstanceId);
        monitor.terminatePendingMembers(clusterInstanceId,clusterInstance.getNetworkPartitionId());
      }
 else {
        monitor.notifyParentMonitor(ClusterStatus.Terminating,clusterInstanceId);
        monitor.terminateAllMembers(clusterInstanceId,clusterInstance.getNetworkPartitionId());
      }
      ServiceReferenceHolder.getInstance().getClusterStatusProcessorChain().process("String_Node_Str",clusterId,clusterInstanceId);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInstanceTerminatedEvent clusterTerminatedEvent=(ClusterInstanceTerminatedEvent)event;
      String clusterId=clusterTerminatedEvent.getClusterId();
      String instanceId=clusterTerminatedEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      ApplicationMonitor appMonitor=null;
      monitor=asCtx.getClusterMonitor(clusterId);
      appMonitor=AutoscalerContext.getInstance().getAppMonitor(clusterTerminatedEvent.getAppId());
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        appMonitor=AutoscalerContext.getInstance().getAppMonitor(clusterTerminatedEvent.getAppId());
        if (appMonitor != null) {
          appMonitor.onChildStatusEvent(new ClusterStatusEvent(ClusterStatus.Terminated,clusterId,instanceId));
        }
        return;
      }
      monitor.notifyParentMonitor(ClusterStatus.Terminated,instanceId);
      ClusterInstance instance=(ClusterInstance)monitor.getInstance(instanceId);
      monitor.getClusterContext().getNetworkPartitionCtxt(instance.getNetworkPartitionId()).removeInstanceContext(instanceId);
      monitor.removeInstance(instanceId);
      if (!monitor.hasInstance() && appMonitor.isTerminating()) {
        monitor.destroy();
        AutoscalerContext.getInstance().removeClusterMonitor(clusterId);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberReadyToShutdownEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        log.info("String_Node_Str" + event.getClass());
        MemberReadyToShutdownEvent memberReadyToShutdownEvent=(MemberReadyToShutdownEvent)event;
        String clusterId=memberReadyToShutdownEvent.getClusterId();
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        ClusterMonitor monitor;
        monitor=asCtx.getClusterMonitor(clusterId);
        if (null == monitor) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        monitor.handleMemberReadyToShutdownEvent(memberReadyToShutdownEvent);
      }
 catch (      Exception e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberStartedEventListener(){
    @Override protected void onEvent(    Event event){
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        MemberTerminatedEvent memberTerminatedEvent=(MemberTerminatedEvent)event;
        String clusterId=memberTerminatedEvent.getClusterId();
        ClusterMonitor monitor;
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        monitor=asCtx.getClusterMonitor(clusterId);
        if (null == monitor) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        monitor.handleMemberTerminatedEvent(memberTerminatedEvent);
      }
 catch (      Exception e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        MemberActivatedEvent memberActivatedEvent=(MemberActivatedEvent)event;
        String clusterId=memberActivatedEvent.getClusterId();
        log.info("String_Node_Str" + memberActivatedEvent.toString());
        ClusterMonitor monitor;
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        monitor=asCtx.getClusterMonitor(clusterId);
        if (null == monitor) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        monitor.handleMemberActivatedEvent(memberActivatedEvent);
      }
 catch (      Exception e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberMaintenanceListener(){
    @Override protected void onEvent(    Event event){
      try {
        MemberMaintenanceModeEvent maintenanceModeEvent=(MemberMaintenanceModeEvent)event;
        String clusterId=maintenanceModeEvent.getClusterId();
        ClusterMonitor monitor;
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        monitor=asCtx.getClusterMonitor(clusterId);
        if (null == monitor) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        monitor.handleMemberMaintenanceModeEvent(maintenanceModeEvent);
      }
 catch (      Exception e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      ClusterInstanceCreatedEvent clusterInstanceCreatedEvent=(ClusterInstanceCreatedEvent)event;
      ClusterMonitor clusterMonitor=AutoscalerContext.getInstance().getClusterMonitor(clusterInstanceCreatedEvent.getClusterId());
      ClusterInstance clusterInstance=((ClusterInstanceCreatedEvent)event).getClusterInstance();
      String instanceId=clusterInstance.getInstanceId();
      if (clusterMonitor != null) {
        TopologyManager.acquireReadLockForCluster(clusterInstanceCreatedEvent.getServiceName(),clusterInstanceCreatedEvent.getClusterId());
        try {
          Service service=TopologyManager.getTopology().getService(clusterInstanceCreatedEvent.getServiceName());
          if (service != null) {
            Cluster cluster=service.getCluster(clusterInstanceCreatedEvent.getClusterId());
            if (cluster != null) {
              try {
                ClusterContext clusterContext=(ClusterContext)clusterMonitor.getClusterContext();
                if (clusterContext == null) {
                  clusterContext=ClusterContextFactory.getVMClusterContext(instanceId,cluster,clusterMonitor.hasScalingDependents(),clusterMonitor.getDeploymentPolicyId());
                  clusterMonitor.setClusterContext(clusterContext);
                }
                log.info("String_Node_Str" + "String_Node_Str" + clusterMonitor.hasScalingDependents() + "String_Node_Str");
                clusterContext.addInstanceContext(instanceId,cluster,clusterMonitor.hasScalingDependents(),clusterMonitor.groupScalingEnabledSubtree());
                if (clusterMonitor.getInstance(instanceId) == null) {
                  ClusterInstance clusterInstance1=cluster.getInstanceContexts(instanceId);
                  clusterMonitor.addInstance(clusterInstance1);
                }
                if (clusterMonitor.hasMonitoringStarted().compareAndSet(false,true)) {
                  clusterMonitor.startScheduler();
                  log.info("String_Node_Str" + clusterInstanceCreatedEvent.getClusterId() + "String_Node_Str");
                }
 else {
                  ((ClusterMonitor)clusterMonitor).monitor();
                }
              }
 catch (              PolicyValidationException e) {
                log.error(e.getMessage(),e);
              }
catch (              PartitionValidationException e) {
                log.error(e.getMessage(),e);
              }
            }
          }
 else {
            log.error("String_Node_Str" + clusterInstanceCreatedEvent.getServiceName() + "String_Node_Str"+ clusterInstanceCreatedEvent.getClusterId());
          }
        }
  finally {
          TopologyManager.releaseReadLockForCluster(clusterInstanceCreatedEvent.getServiceName(),clusterInstanceCreatedEvent.getClusterId());
        }
      }
 else {
        log.error("String_Node_Str" + clusterInstanceCreatedEvent.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());
        try {
          ApplicationHolder.acquireReadLock();
          Applications applications=ApplicationHolder.getApplications();
          if (applications != null) {
            for (            Application application : applications.getApplications().values()) {
              ApplicationContext applicationContext=AutoscalerContext.getInstance().getApplicationContext(application.getUniqueIdentifier());
              if (applicationContext != null && applicationContext.getStatus().equals(ApplicationContext.STATUS_DEPLOYED)) {
                if (AutoscalerUtil.allClustersInitialized(application)) {
                  AutoscalerUtil.getInstance().startApplicationMonitor(application.getUniqueIdentifier());
                }
 else {
                  log.error("String_Node_Str" + "String_Node_Str");
                }
              }
 else {
                log.info("String_Node_Str" + "String_Node_Str" + application.getUniqueIdentifier());
              }
            }
            topologyInitialized=true;
          }
 else {
            log.info("String_Node_Str");
          }
        }
 catch (        Exception e) {
          log.error("String_Node_Str",e);
        }
 finally {
          ApplicationHolder.releaseReadLock();
        }
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ApplicationClustersCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        log.info("String_Node_Str" + event.getClass());
        ApplicationClustersCreatedEvent applicationClustersCreatedEvent=(ApplicationClustersCreatedEvent)event;
        String appId=applicationClustersCreatedEvent.getAppId();
        boolean appMonitorCreationTriggered=false;
        int retries=5;
        while (!appMonitorCreationTriggered && retries > 0) {
          try {
            ApplicationHolder.acquireReadLock();
            ApplicationContext applicationContext=AutoscalerContext.getInstance().getApplicationContext(appId);
            if (applicationContext != null && applicationContext.getStatus().equals(ApplicationContext.STATUS_DEPLOYED)) {
              if (!AutoscalerContext.getInstance().containsApplicationPendingMonitor(appId)) {
                appMonitorCreationTriggered=true;
                AutoscalerUtil.getInstance().startApplicationMonitor(appId);
                break;
              }
            }
          }
 catch (          Exception e) {
            String msg="String_Node_Str" + e.getLocalizedMessage();
            log.error(msg,e);
          }
 finally {
            ApplicationHolder.releaseReadLock();
          }
          try {
            retries--;
            Thread.sleep(1000);
          }
 catch (          InterruptedException e) {
          }
        }
        if (!appMonitorCreationTriggered) {
          String msg=String.format("String_Node_Str" + "String_Node_Str" + "String_Node_Str",appId,ApplicationContext.STATUS_DEPLOYED);
          log.warn(msg);
        }
      }
 catch (      ClassCastException e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInstanceActivatedEvent clusterActivatedEvent=(ClusterInstanceActivatedEvent)event;
      String clusterId=clusterActivatedEvent.getClusterId();
      String instanceId=clusterActivatedEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      monitor=asCtx.getClusterMonitor(clusterId);
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        return;
      }
      monitor.notifyParentMonitor(ClusterStatus.Active,instanceId);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterResetEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterResetEvent clusterResetEvent=(ClusterResetEvent)event;
      String clusterId=clusterResetEvent.getClusterId();
      String instanceId=clusterResetEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      monitor=asCtx.getClusterMonitor(clusterId);
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        return;
      }
      monitor.destroy();
      monitor.notifyParentMonitor(ClusterStatus.Created,instanceId);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceInactivateEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInstanceInactivateEvent clusterInactivateEvent=(ClusterInstanceInactivateEvent)event;
      String clusterId=clusterInactivateEvent.getClusterId();
      String instanceId=clusterInactivateEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      monitor=asCtx.getClusterMonitor(clusterId);
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        return;
      }
      monitor.notifyParentMonitor(ClusterStatus.Inactive,instanceId);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceTerminatingEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInstanceTerminatingEvent clusterTerminatingEvent=(ClusterInstanceTerminatingEvent)event;
      String clusterId=clusterTerminatingEvent.getClusterId();
      String clusterInstanceId=clusterTerminatingEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      monitor=asCtx.getClusterMonitor(clusterId);
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        ClusterStatusEventPublisher.sendClusterTerminatedEvent(clusterTerminatingEvent.getAppId(),clusterTerminatingEvent.getServiceName(),clusterId,clusterInstanceId);
        return;
      }
      ClusterInstance clusterInstance=(ClusterInstance)monitor.getInstance(clusterInstanceId);
      if (clusterInstance.getPreviousState() == ClusterStatus.Active) {
        monitor.notifyParentMonitor(ClusterStatus.Terminating,clusterInstanceId);
        InstanceNotificationPublisher.getInstance().sendInstanceCleanupEventForCluster(clusterId,clusterInstanceId);
        monitor.terminatePendingMembers(clusterInstanceId,clusterInstance.getNetworkPartitionId());
        monitor.moveMembersToTerminatingPending(clusterInstanceId,clusterInstance.getNetworkPartitionId());
      }
 else {
        monitor.notifyParentMonitor(ClusterStatus.Terminating,clusterInstanceId);
        monitor.terminateAllMembers(clusterInstanceId,clusterInstance.getNetworkPartitionId());
      }
      ServiceReferenceHolder.getInstance().getClusterStatusProcessorChain().process("String_Node_Str",clusterId,clusterInstanceId);
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      log.info("String_Node_Str" + event.getClass());
      ClusterInstanceTerminatedEvent clusterTerminatedEvent=(ClusterInstanceTerminatedEvent)event;
      String clusterId=clusterTerminatedEvent.getClusterId();
      String instanceId=clusterTerminatedEvent.getInstanceId();
      AutoscalerContext asCtx=AutoscalerContext.getInstance();
      ClusterMonitor monitor;
      ApplicationMonitor appMonitor=null;
      monitor=asCtx.getClusterMonitor(clusterId);
      appMonitor=AutoscalerContext.getInstance().getAppMonitor(clusterTerminatedEvent.getAppId());
      if (null == monitor) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
        }
        appMonitor=AutoscalerContext.getInstance().getAppMonitor(clusterTerminatedEvent.getAppId());
        if (appMonitor != null) {
          appMonitor.onChildStatusEvent(new ClusterStatusEvent(ClusterStatus.Terminated,clusterId,instanceId));
        }
        return;
      }
      monitor.notifyParentMonitor(ClusterStatus.Terminated,instanceId);
      ClusterInstance instance=(ClusterInstance)monitor.getInstance(instanceId);
      monitor.getClusterContext().getNetworkPartitionCtxt(instance.getNetworkPartitionId()).removeInstanceContext(instanceId);
      monitor.removeInstance(instanceId);
      if (!monitor.hasInstance() && appMonitor.isTerminating()) {
        monitor.destroy();
        AutoscalerContext.getInstance().removeClusterMonitor(clusterId);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberReadyToShutdownEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        log.info("String_Node_Str" + event.getClass());
        MemberReadyToShutdownEvent memberReadyToShutdownEvent=(MemberReadyToShutdownEvent)event;
        String clusterId=memberReadyToShutdownEvent.getClusterId();
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        ClusterMonitor monitor;
        monitor=asCtx.getClusterMonitor(clusterId);
        if (null == monitor) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        monitor.handleMemberReadyToShutdownEvent(memberReadyToShutdownEvent);
      }
 catch (      Exception e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberStartedEventListener(){
    @Override protected void onEvent(    Event event){
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberTerminatedEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        MemberTerminatedEvent memberTerminatedEvent=(MemberTerminatedEvent)event;
        String clusterId=memberTerminatedEvent.getClusterId();
        ClusterMonitor monitor;
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        monitor=asCtx.getClusterMonitor(clusterId);
        if (null == monitor) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        monitor.handleMemberTerminatedEvent(memberTerminatedEvent);
      }
 catch (      Exception e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberActivatedEventListener(){
    @Override protected void onEvent(    Event event){
      try {
        MemberActivatedEvent memberActivatedEvent=(MemberActivatedEvent)event;
        String clusterId=memberActivatedEvent.getClusterId();
        log.info("String_Node_Str" + memberActivatedEvent.toString());
        ClusterMonitor monitor;
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        monitor=asCtx.getClusterMonitor(clusterId);
        if (null == monitor) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        monitor.handleMemberActivatedEvent(memberActivatedEvent);
      }
 catch (      Exception e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new MemberMaintenanceListener(){
    @Override protected void onEvent(    Event event){
      try {
        MemberMaintenanceModeEvent maintenanceModeEvent=(MemberMaintenanceModeEvent)event;
        String clusterId=maintenanceModeEvent.getClusterId();
        ClusterMonitor monitor;
        AutoscalerContext asCtx=AutoscalerContext.getInstance();
        monitor=asCtx.getClusterMonitor(clusterId);
        if (null == monitor) {
          if (log.isDebugEnabled()) {
            log.debug(String.format("String_Node_Str" + "String_Node_Str",clusterId));
          }
          return;
        }
        monitor.handleMemberMaintenanceModeEvent(maintenanceModeEvent);
      }
 catch (      Exception e) {
        String msg="String_Node_Str" + e.getLocalizedMessage();
        log.error(msg,e);
      }
    }
  }
);
  topologyEventReceiver.addEventListener(new ClusterInstanceCreatedEventListener(){
    @Override protected void onEvent(    Event event){
      ClusterInstanceCreatedEvent clusterInstanceCreatedEvent=(ClusterInstanceCreatedEvent)event;
      ClusterMonitor clusterMonitor=AutoscalerContext.getInstance().getClusterMonitor(clusterInstanceCreatedEvent.getClusterId());
      ClusterInstance clusterInstance=((ClusterInstanceCreatedEvent)event).getClusterInstance();
      String instanceId=clusterInstance.getInstanceId();
      if (clusterMonitor != null) {
        TopologyManager.acquireReadLockForCluster(clusterInstanceCreatedEvent.getServiceName(),clusterInstanceCreatedEvent.getClusterId());
        try {
          Service service=TopologyManager.getTopology().getService(clusterInstanceCreatedEvent.getServiceName());
          if (service != null) {
            Cluster cluster=service.getCluster(clusterInstanceCreatedEvent.getClusterId());
            if (cluster != null) {
              try {
                ClusterContext clusterContext=(ClusterContext)clusterMonitor.getClusterContext();
                if (clusterContext == null) {
                  clusterContext=ClusterContextFactory.getVMClusterContext(instanceId,cluster,clusterMonitor.hasScalingDependents(),clusterMonitor.getDeploymentPolicyId());
                  clusterMonitor.setClusterContext(clusterContext);
                }
                log.info("String_Node_Str" + "String_Node_Str" + clusterMonitor.hasScalingDependents() + "String_Node_Str");
                clusterContext.addInstanceContext(instanceId,cluster,clusterMonitor.hasScalingDependents(),clusterMonitor.groupScalingEnabledSubtree());
                if (clusterMonitor.getInstance(instanceId) == null) {
                  ClusterInstance clusterInstance1=cluster.getInstanceContexts(instanceId);
                  clusterMonitor.addInstance(clusterInstance1);
                }
                if (clusterMonitor.hasMonitoringStarted().compareAndSet(false,true)) {
                  clusterMonitor.startScheduler();
                  log.info("String_Node_Str" + clusterInstanceCreatedEvent.getClusterId() + "String_Node_Str");
                }
 else {
                  ((ClusterMonitor)clusterMonitor).monitor();
                }
              }
 catch (              PolicyValidationException e) {
                log.error(e.getMessage(),e);
              }
catch (              PartitionValidationException e) {
                log.error(e.getMessage(),e);
              }
            }
          }
 else {
            log.error("String_Node_Str" + clusterInstanceCreatedEvent.getServiceName() + "String_Node_Str"+ clusterInstanceCreatedEvent.getClusterId());
          }
        }
  finally {
          TopologyManager.releaseReadLockForCluster(clusterInstanceCreatedEvent.getServiceName(),clusterInstanceCreatedEvent.getClusterId());
        }
      }
 else {
        log.error("String_Node_Str" + clusterInstanceCreatedEvent.getClusterId());
      }
    }
  }
);
}