/**
   * Removes all Synapse proxy services from the Axis2 configuration.
   *
   * @throws AxisFault if an error occurs undeploying proxy services
   */
  private void undeployProxyServices() throws AxisFault {

    log.info("Undeploying Proxy services...");

    for (ProxyService proxy : synapseConfiguration.getProxyServices()) {
      configurationContext.getAxisConfiguration().removeService(proxy.getName());
    }
  }
  /** Adds all Synapse proxy services to the Axis2 configuration. */
  private void deployProxyServices() {

    boolean failSafeProxyEnabled =
        SynapseConfigUtils.isFailSafeEnabled(SynapseConstants.FAIL_SAFE_MODE_PROXY_SERVICES);

    log.info("Deploying Proxy services...");
    String thisServerName = serverConfigurationInformation.getServerName();
    if (thisServerName == null || "".equals(thisServerName)) {
      thisServerName = serverConfigurationInformation.getHostName();
      if (thisServerName == null || "".equals(thisServerName)) {
        thisServerName = "localhost";
      }
    }

    for (ProxyService proxy : synapseConfiguration.getProxyServices()) {

      // start proxy service if either, pinned server name list is empty
      // or pinned server list has this server name
      List pinnedServers = proxy.getPinnedServers();
      if (pinnedServers != null && !pinnedServers.isEmpty()) {
        if (!pinnedServers.contains(thisServerName)) {
          log.info(
              "Server name not in pinned servers list."
                  + " Not deploying Proxy service : "
                  + proxy.getName());
          continue;
        }
      }

      try {
        AxisService proxyService =
            proxy.buildAxisService(
                synapseConfiguration, configurationContext.getAxisConfiguration());
        if (proxyService != null) {
          log.info("Deployed Proxy service : " + proxy.getName());
          if (!proxy.isStartOnLoad()) {
            proxy.stop(synapseConfiguration);
          }
        } else {
          log.warn("The proxy service " + proxy.getName() + " will NOT be available");
        }
      } catch (SynapseException e) {
        if (failSafeProxyEnabled) {
          log.warn(
              "The proxy service "
                  + proxy.getName()
                  + " cannot be deployed - "
                  + "Continue in Proxy Service fail-safe mode.");
        } else {
          handleException("The proxy service " + proxy.getName() + " : Deployment Error");
        }
      }
    }
  }
 protected OMElement saveToFile(ProxyService proxy, SynapseConfiguration synapseConfiguration) {
   try {
     return serializer.serializeProxy(proxy, synapseConfiguration, null);
   } catch (Exception e) {
     handleException(
         "Error while saving the proxy service: " + proxy.getName() + " to " + "the file system",
         e);
   }
   return null;
 }
  /**
   * Retrieve the sequence from Continuation state which message should be injected to.
   *
   * @param seqContState SeqContinuationState which contain the sequence information
   * @param synCtx message context
   * @return sequence which message should be injected to
   */
  public static SequenceMediator retrieveSequence(
      MessageContext synCtx, SeqContinuationState seqContState) {
    SequenceMediator sequence = null;

    switch (seqContState.getSeqType()) {
      case NAMED:
        {
          sequence = (SequenceMediator) synCtx.getSequence(seqContState.getSeqName());
          if (sequence == null) {
            // This can happen only if someone delete the sequence while running
            handleException("Sequence : " + seqContState.getSeqName() + " not found");
          }
          break;
        }
      case PROXY_INSEQ:
        {
          String proxyName = (String) synCtx.getProperty(SynapseConstants.PROXY_SERVICE);
          ProxyService proxyService = synCtx.getConfiguration().getProxyService(proxyName);
          if (proxyService != null) {
            sequence = proxyService.getTargetInLineInSequence();
          } else {
            handleException("Proxy Service :" + proxyName + " not found");
          }
          break;
        }
      case API_INSEQ:
        {
          String apiName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API);
          String resourceName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_RESOURCE);
          API api = synCtx.getEnvironment().getSynapseConfiguration().getAPI(apiName);
          if (api != null) {
            Resource resource = api.getResource(resourceName);
            if (resource != null) {
              sequence = resource.getInSequence();
            } else {
              handleException("Resource : " + resourceName + " not found");
            }
          } else {
            handleException("REST API : " + apiName + " not found");
          }
          break;
        }
      case PROXY_OUTSEQ:
        {
          String proxyName = (String) synCtx.getProperty(SynapseConstants.PROXY_SERVICE);
          ProxyService proxyService = synCtx.getConfiguration().getProxyService(proxyName);
          if (proxyService != null) {
            sequence = proxyService.getTargetInLineOutSequence();
          } else {
            handleException("Proxy Service :" + proxyName + " not found");
          }
          break;
        }
      case API_OUTSEQ:
        {
          String apiName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API);
          String resourceName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_RESOURCE);
          API api = synCtx.getEnvironment().getSynapseConfiguration().getAPI(apiName);
          if (api != null) {
            Resource resource = api.getResource(resourceName);
            if (resource != null) {
              sequence = resource.getOutSequence();
            } else {
              handleException("Resource : " + resourceName + " not found");
            }
          } else {
            handleException("REST API : " + apiName + " not found");
          }
          break;
        }
      case PROXY_FAULTSEQ:
        {
          String proxyName = (String) synCtx.getProperty(SynapseConstants.PROXY_SERVICE);
          ProxyService proxyService = synCtx.getConfiguration().getProxyService(proxyName);
          if (proxyService != null) {
            sequence = proxyService.getTargetInLineFaultSequence();
          } else {
            handleException("Proxy Service :" + proxyName + " not found");
          }
          break;
        }
      case API_FAULTSEQ:
        {
          String apiName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API);
          String resourceName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_RESOURCE);
          API api = synCtx.getEnvironment().getSynapseConfiguration().getAPI(apiName);
          if (api != null) {
            Resource resource = api.getResource(resourceName);
            if (resource != null) {
              sequence = resource.getFaultSequence();
            } else {
              handleException("Resource : " + resourceName + " not found");
            }
          } else {
            handleException("REST API : " + apiName + " not found");
          }
          break;
        }
    }

    return sequence;
  }
 protected String getFileName(ProxyService proxy) {
   return proxy.getFileName();
 }