@Override
  public void onTransition(String phaseName, T object) throws MuleException {
    LifecyclePhase phase = registryLifecycleManager.phases.get(phaseName);

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug(
          String.format(
              "Applying lifecycle phase: %s for registry: %s",
              phase, object.getClass().getSimpleName()));
    }

    if (phase instanceof ContainerManagedLifecyclePhase) {
      phase.applyLifecycle(object);
      return;
    }

    // overlapping interfaces can cause duplicates
    // TODO: each LifecycleManager should keep this set per executing phase
    // and clear it when the phase is fully applied
    Set<Object> duplicates = new HashSet<>();

    for (LifecycleObject lifecycleObject : phase.getOrderedLifecycleObjects()) {
      lifecycleObject.firePreNotification(registryLifecycleManager.muleContext);

      // TODO Collection -> List API refactoring
      Collection<?> targetsObj = lookupObjectsForLifecycle(lifecycleObject);
      doApplyLifecycle(phase, duplicates, lifecycleObject, targetsObj);
      lifecycleObject.firePostNotification(registryLifecycleManager.muleContext);
    }
  }
  private void doApplyLifecycle(
      LifecyclePhase phase,
      Set<Object> duplicates,
      LifecycleObject lifecycleObject,
      Collection<?> targetObjects)
      throws LifecycleException {
    if (CollectionUtils.isEmpty(targetObjects)) {
      return;
    }

    for (Object target : targetObjects) {
      if (duplicates.contains(target)) {
        continue;
      }

      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("lifecycle phase: " + phase.getName() + " for object: " + target);
      }

      phase.applyLifecycle(target);
      duplicates.add(target);
    }

    // the target object might have created and registered a new object
    // (e.g.: an endpoint which registers a connector)
    // check if there're new objects for the phase
    int originalTargetCount = targetObjects.size();
    targetObjects = lookupObjectsForLifecycle(lifecycleObject);
    if (targetObjects.size() > originalTargetCount) {
      doApplyLifecycle(phase, duplicates, lifecycleObject, targetObjects);
    }
  }
Example #3
0
  @Override
  protected void doApplyPhase(LifecyclePhase phase) throws LifecycleException {
    try {
      if (phase.getName().equals(Initialisable.PHASE_NAME)) {
        logger.debug("Initialising service: " + service.getName());
        service.doInitialise();
        fireServiceNotification(ServiceNotification.SERVICE_INITIALISED);
      } else if (phase.getName().equals(Startable.PHASE_NAME)) {
        logger.debug("Starting service: " + service.getName());
        service.doStart();
        fireServiceNotification(ServiceNotification.SERVICE_STARTED);
      } else if (phase.getName().equals(Pausable.PHASE_NAME)) {
        logger.debug("Pausing service: " + service.getName());
        service.doPause();
        fireServiceNotification(ServiceNotification.SERVICE_PAUSED);
      } else if (phase.getName().equals(Resumable.PHASE_NAME)) {
        logger.debug("Resuming service: " + service.getName());
        service.doResume();
        fireServiceNotification(ServiceNotification.SERVICE_RESUMED);
      } else if (phase.getName().equals(Stoppable.PHASE_NAME)) {
        logger.debug("Stopping service: " + service.getName());
        service.doStop();
        fireServiceNotification(ServiceNotification.SERVICE_STOPPED);
      } else if (phase.getName().equals(Disposable.PHASE_NAME)) {
        // We need to handle transitions to get to dispose since, dispose can be called from any
        // lifecycle state
        logger.debug("Disposing service: " + service.getName());

        if (getState().isPhaseComplete(Pausable.PHASE_NAME)) {
          // This is a work around to bypass the phase checking so that we can call resume even
          // though dispose was called
          setExecutingPhase(null);
          service.resume();
        }

        if (getState().isStarted()) {
          // This is a work around to bypass the phase checking so that we can call stop even though
          // dispose was called
          setExecutingPhase(null);
          service.stop();
        }
        service.doDispose();
        fireServiceNotification(ServiceNotification.SERVICE_DISPOSED);
      } else {
        throw new LifecycleException(
            CoreMessages.lifecyclePhaseNotRecognised(phase.getName()), service);
      }
    } catch (MuleException e) {
      throw new LifecycleException(e, service);
    }
  }