private void addServerIPAndHostEntries() {
    String hostName = serverConfigurationInformation.getHostName();
    String ipAddress = serverConfigurationInformation.getIpAddress();
    if (hostName != null && !"".equals(hostName)) {
      Entry entry = new Entry(SynapseConstants.SERVER_HOST);
      entry.setValue(hostName);
      synapseConfiguration.addEntry(SynapseConstants.SERVER_HOST, entry);
    }

    if (ipAddress != null && !"".equals(ipAddress)) {
      Entry entry = new Entry(SynapseConstants.SERVER_IP);
      entry.setValue(ipAddress);
      if (synapseConfiguration.getAxisConfiguration().getTransportsIn() != null) {
        Map<String, TransportInDescription> transportInConfigMap =
            synapseConfiguration.getAxisConfiguration().getTransportsIn();
        if (transportInConfigMap != null) {
          TransportInDescription transportInDescription = transportInConfigMap.get("http");
          if (transportInDescription != null) {
            Parameter bindAddressParam = transportInDescription.getParameter("bind-address");
            if (bindAddressParam != null) {
              entry.setValue(bindAddressParam.getValue());
            }
          }
        }
      }
      synapseConfiguration.addEntry(SynapseConstants.SERVER_IP, entry);
    }
  }
  /** {@inheritDoc} */
  public SynapseConfiguration createSynapseConfiguration() {

    String synapseXMLLocation = serverConfigurationInformation.getSynapseXMLLocation();
    Properties properties = SynapsePropertiesLoader.loadSynapseProperties();
    if (serverConfigurationInformation.getResolveRoot() != null) {
      properties.put(
          SynapseConstants.RESOLVE_ROOT, serverConfigurationInformation.getResolveRoot());
    }

    if (serverConfigurationInformation.getSynapseHome() != null) {
      properties.put(
          SynapseConstants.SYNAPSE_HOME, serverConfigurationInformation.getSynapseHome());
    }

    if (synapseXMLLocation != null) {
      synapseConfiguration =
          SynapseConfigurationBuilder.getConfiguration(synapseXMLLocation, properties);
    } else {
      log.warn(
          "System property or init-parameter '"
              + SynapseConstants.SYNAPSE_XML
              + "' is not specified. Using default configuration..");
      synapseConfiguration = SynapseConfigurationBuilder.getDefaultConfiguration();
    }

    Enumeration keys = properties.keys();
    while (keys.hasMoreElements()) {
      String key = (String) keys.nextElement();
      synapseConfiguration.setProperty(key, properties.getProperty(key));
    }

    // Set the Axis2 ConfigurationContext to the SynapseConfiguration
    synapseConfiguration.setAxisConfiguration(configurationContext.getAxisConfiguration());
    MessageContextCreatorForAxis2.setSynConfig(synapseConfiguration);

    // set the Synapse configuration into the Axis2 configuration
    Parameter synapseConfigurationParameter =
        new Parameter(SynapseConstants.SYNAPSE_CONFIG, synapseConfiguration);
    try {
      configurationContext.getAxisConfiguration().addParameter(synapseConfigurationParameter);
    } catch (AxisFault e) {
      handleFatal(
          "Could not set parameters '"
              + SynapseConstants.SYNAPSE_CONFIG
              + "' to the Axis2 configuration : "
              + e.getMessage(),
          e);
    }

    addServerIPAndHostEntries();

    return synapseConfiguration;
  }
  /**
   * 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");
        }
      }
    }
  }
  /**
   * The mediation library deployer will handling the process of deploying the libararyArtifacts,
   * this is required since the library specific artifacts has to be initialized priorly for the
   * cases like connectors
   */
  private void deployMediationLibraryArtifacts() {
    if (configurationContext == null || synapseConfiguration == null) {
      return;
    }
    DeploymentEngine deploymentEngine =
        (DeploymentEngine) configurationContext.getAxisConfiguration().getConfigurator();
    String carbonRepoPath = configurationContext.getAxisConfiguration().getRepository().getPath();
    SynapseArtifactDeploymentStore deploymentStore =
        synapseConfiguration.getArtifactDeploymentStore();

    String synapseImportDir =
        synapseConfiguration.getPathToConfigFile()
            + File.separator
            + MultiXMLConfigurationBuilder.SYNAPSE_IMPORTS_DIR;

    /*Registering Import Deployer is not required here.*/
    // deploymentEngine.addDeployer(new ImportDeployer(), synapseImportDir, "xml");

    String libsPath = carbonRepoPath + File.separator + "synapse-libs";
    deploymentEngine.addDeployer(new LibraryArtifactDeployer(), libsPath, "zip");
  }
  /**
   * Setup synapse in axis2 environment and return the created instance.
   *
   * @return SynapseEnvironment instance
   */
  public SynapseEnvironment createSynapseEnvironment() {

    try {
      deployMediationLibraryArtifacts();
      deployMediatorExtensions();
      deploySynapseService();
      deployProxyServices();
      deployEventSources();
      // deployMediatorExtensions();
    } catch (AxisFault axisFault) {
      log.fatal("Synapse startup failed...", axisFault);
      throw new SynapseException("Synapse startup failed", axisFault);
    }

    synapseEnvironment =
        new Axis2SynapseEnvironment(
            configurationContext, synapseConfiguration, serverContextInformation);
    MessageContextCreatorForAxis2.setSynEnv(synapseEnvironment);

    Parameter synapseEnvironmentParameter =
        new Parameter(SynapseConstants.SYNAPSE_ENV, synapseEnvironment);
    try {
      configurationContext.getAxisConfiguration().addParameter(synapseEnvironmentParameter);
    } catch (AxisFault e) {
      handleFatal(
          "Could not set parameter '"
              + SynapseConstants.SYNAPSE_ENV
              + "' to the Axis2 configuration : "
              + e.getMessage(),
          e);
    }

    synapseEnvironment.getTaskManager().init(taskDescriptionRepository, taskScheduler);
    synapseConfiguration.init(synapseEnvironment);
    synapseEnvironment.setInitialized(true);

    return synapseEnvironment;
  }
 /**
  * Undeploys all event sources.
  *
  * @throws AxisFault if an error occurs undeploying the event sources.
  */
 private void undeployEventSources() throws AxisFault {
   log.info("Undeploying EventSources...");
   for (SynapseEventSource eventSource : synapseConfiguration.getEventSources()) {
     configurationContext.getAxisConfiguration().removeService(eventSource.getName());
   }
 }
 /**
  * Deploys all event sources.
  *
  * @throws AxisFault if an error occurs deploying the event sources.
  */
 private void deployEventSources() throws AxisFault {
   log.info("Deploying EventSources...");
   for (SynapseEventSource eventSource : synapseConfiguration.getEventSources()) {
     eventSource.buildService(configurationContext.getAxisConfiguration());
   }
 }
 /** {@inheritDoc} */
 public void destroySynapseConfiguration() {
   if (synapseConfiguration != null) {
     synapseConfiguration.destroy();
     synapseConfiguration = null;
   }
 }