/**
   * Initialize child mediators recursively
   *
   * @param se synapse environment
   */
  public void init(SynapseEnvironment se) {
    if (log.isDebugEnabled()) {
      log.debug("Initializing child mediators of mediator : " + getType());
    }

    for (int i = 0; i < mediators.size(); i++) {
      Mediator mediator = mediators.get(i);
      mediator.setMediatorPosition(i);

      if (mediator instanceof ManagedLifecycle) {
        ((ManagedLifecycle) mediator).init(se);
      }

      if (mediator.isContentAware()) {
        if (log.isDebugEnabled()) {
          log.debug(
              mediator.getType()
                  + " is content aware, setting sequence <"
                  + getType()
                  + "> as content aware");
        }
        contentAware = true;
      }
    }
  }
  public OMElement serializeMediator(OMElement parent, Mediator m) {

    AggregateMediator mediator = null;
    if (!(m instanceof AggregateMediator)) {
      handleException("Unsupported mediator passed in for serialization : " + m.getType());
    } else {
      mediator = (AggregateMediator) m;
    }

    OMElement aggregator = fac.createOMElement("aggregate", synNS);
    saveTracingState(aggregator, mediator);

    assert mediator != null;
    if (mediator.getCorrelateExpression() != null) {
      OMElement corelateOn = fac.createOMElement("correlateOn", synNS);
      SynapseXPathSerializer.serializeXPath(
          mediator.getCorrelateExpression(), corelateOn, "expression");
      aggregator.addChild(corelateOn);
    }

    OMElement completeCond = fac.createOMElement("completeCondition", synNS);
    if (mediator.getCompletionTimeoutMillis() != 0) {
      completeCond.addAttribute(
          "timeout", Long.toString(mediator.getCompletionTimeoutMillis() / 1000), nullNS);
    }
    OMElement messageCount = fac.createOMElement("messageCount", synNS);
    if (mediator.getMinMessagesToComplete() != 0) {
      messageCount.addAttribute(
          "min", Integer.toString(mediator.getMinMessagesToComplete()), nullNS);
    }
    if (mediator.getMaxMessagesToComplete() != 0) {
      messageCount.addAttribute(
          "max", Integer.toString(mediator.getMaxMessagesToComplete()), nullNS);
    }
    completeCond.addChild(messageCount);
    aggregator.addChild(completeCond);

    OMElement onCompleteElem = fac.createOMElement("onComplete", synNS);
    if (mediator.getAggregationExpression() != null) {
      SynapseXPathSerializer.serializeXPath(
          mediator.getAggregationExpression(), onCompleteElem, "expression");
    }
    if (mediator.getOnCompleteSequenceRef() != null) {
      onCompleteElem.addAttribute("sequence", mediator.getOnCompleteSequenceRef(), nullNS);
    } else if (mediator.getOnCompleteSequence() != null) {
      new SequenceMediatorSerializer()
          .serializeChildren(onCompleteElem, mediator.getOnCompleteSequence().getList());
    }
    aggregator.addChild(onCompleteElem);

    if (parent != null) {
      parent.addChild(aggregator);
    }

    return aggregator;
  }
  public OMElement serializeSpecificMediator(Mediator m) {

    if (!(m instanceof RelayTransformerMediator)) {
      handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }

    RelayTransformerMediator mediator = (RelayTransformerMediator) m;
    OMElement relayTransformer = fac.createOMElement("relayTransformer", synNS);

    if (mediator.getXsltKey() != null) {
      // Serialize Value using ValueSerializer
      ValueSerializer keySerializer = new ValueSerializer();
      keySerializer.serializeValue(mediator.getXsltKey(), XMLConfigConstants.KEY, relayTransformer);
    } else {
      handleException("Invalid relayTransformer mediator. XSLT registry key is required");
    }
    saveTracingState(relayTransformer, mediator);

    if (mediator.getSource() != null) {

      SynapseXPathSerializer.serializeXPath(mediator.getSource(), relayTransformer, "source");
    }
    if (mediator.getTargetPropertyName() != null) {
      relayTransformer.addAttribute(
          fac.createOMAttribute("target", nullNS, mediator.getTargetPropertyName()));
    }
    if (mediator.getInputType() != null) {
      relayTransformer.addAttribute(
          fac.createOMAttribute("input", nullNS, mediator.getInputType()));
    }
    serializeProperties(relayTransformer, mediator.getProperties());
    List<MediatorProperty> features = mediator.getFeatures();
    if (!features.isEmpty()) {
      for (MediatorProperty mp : features) {
        OMElement prop = fac.createOMElement("feature", synNS, relayTransformer);
        if (mp.getName() != null) {
          prop.addAttribute(fac.createOMAttribute("name", nullNS, mp.getName()));
        } else {
          handleException("The Feature name is missing");
        }
        if (mp.getValue() != null) {
          prop.addAttribute(fac.createOMAttribute("value", nullNS, mp.getValue()));
        } else {
          handleException("The Feature value is missing");
        }
      }
    }
    serializeMediatorProperties(relayTransformer, mediator.getAttributes(), ATTRIBUTE_Q);

    ResourceMapSerializer.serializeResourceMap(relayTransformer, mediator.getResourceMap());

    return relayTransformer;
  }
  /** {@inheritDoc} */
  public OMElement serializeSpecificMediator(Mediator mediator) {
    if (!(mediator instanceof EntitlementMediator)) {
      handleException("Unsupported mediator passed in for serialization : " + mediator.getType());
    }

    EntitlementMediator entitlement = null;
    OMElement entitlementElem = null;

    entitlement = (EntitlementMediator) mediator;
    entitlementElem = fac.createOMElement("entitlementService", synNS);
    saveTracingState(entitlementElem, entitlement);
    entitlementElem.addAttribute(
        fac.createOMAttribute("remoteServiceUrl", nullNS, entitlement.getRemoteServiceUrl()));
    entitlementElem.addAttribute(
        fac.createOMAttribute(
            "remoteServiceUserName", nullNS, entitlement.getRemoteServiceUserName()));
    entitlementElem.addAttribute(
        fac.createOMAttribute(
            "remoteServicePassword", nullNS, entitlement.getRemoteServicePassword()));

    if (entitlement.getCallbackClass() != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute("callbackClass", nullNS, entitlement.getCallbackClass()));
    }

    if (entitlement.getCacheType() != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute("cacheType", nullNS, entitlement.getCacheType()));
    }

    if (entitlement.getInvalidationInterval() != 0) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(
              "invalidationInterval",
              nullNS,
              Integer.toString(entitlement.getInvalidationInterval())));
    }

    if (entitlement.getMaxCacheEntries() != 0) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(
              "maxCacheEntries", nullNS, Integer.toString(entitlement.getMaxCacheEntries())));
    }

    if (entitlement.getClient() != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(EntitlementConstants.CLIENT, nullNS, entitlement.getClient()));
    }

    if (entitlement.getThriftHost() != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(
              EntitlementConstants.THRIFT_HOST, nullNS, entitlement.getThriftHost()));
    }

    if (entitlement.getThriftPort() != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(
              EntitlementConstants.THRIFT_PORT, nullNS, entitlement.getThriftPort()));
    }

    if (entitlement.getReuseSession() != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(
              EntitlementConstants.REUSE_SESSION, nullNS, entitlement.getReuseSession()));
    }

    String onReject = entitlement.getOnRejectSeqKey();
    if (onReject != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(XMLConfigConstants.ONREJECT, nullNS, onReject));
    } else {
      Mediator m = entitlement.getOnRejectMediator();
      SequenceMediatorSerializer serializer = new SequenceMediatorSerializer();
      if (m != null && m instanceof SequenceMediator) {
        OMElement element = serializer.serializeAnonymousSequence(null, (SequenceMediator) m);
        element.setLocalName(XMLConfigConstants.ONREJECT);
        entitlementElem.addChild(element);
      }
    }
    String onAccept = entitlement.getOnAcceptSeqKey();
    if (onAccept != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(XMLConfigConstants.ONACCEPT, nullNS, onAccept));
    } else {
      Mediator m = entitlement.getOnAcceptMediator();
      SequenceMediatorSerializer serializer = new SequenceMediatorSerializer();
      if (m != null && m instanceof SequenceMediator) {
        OMElement element = serializer.serializeAnonymousSequence(null, (SequenceMediator) m);
        element.setLocalName(XMLConfigConstants.ONACCEPT);
        entitlementElem.addChild(element);
      }
    }
    String obligation = entitlement.getObligationsSeqKey();
    if (obligation != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(EntitlementMediatorFactory.OBLIGATIONS, nullNS, obligation));
    } else {
      Mediator m = entitlement.getObligationsMediator();
      SequenceMediatorSerializer serializer = new SequenceMediatorSerializer();
      if (m != null && m instanceof SequenceMediator) {
        OMElement element = serializer.serializeAnonymousSequence(null, (SequenceMediator) m);
        element.setLocalName(EntitlementMediatorFactory.OBLIGATIONS);
        entitlementElem.addChild(element);
      }
    }
    String advice = entitlement.getAdviceSeqKey();
    if (advice != null) {
      entitlementElem.addAttribute(
          fac.createOMAttribute(EntitlementMediatorFactory.ADVICE, nullNS, advice));
    } else {
      Mediator m = entitlement.getAdviceMediator();
      SequenceMediatorSerializer serializer = new SequenceMediatorSerializer();
      if (m != null && m instanceof SequenceMediator) {
        OMElement element = serializer.serializeAnonymousSequence(null, (SequenceMediator) m);
        element.setLocalName(EntitlementMediatorFactory.ADVICE);
        entitlementElem.addChild(element);
      }
    }

    return entitlementElem;
  }