/**
   * Create a Axis2 Based Server Environment
   *
   * @param serverConfigurationInformation ServerConfigurationInformation instance
   */
  private void createNewInstance(ServerConfigurationInformation serverConfigurationInformation) {

    try {
      configurationContext =
          ConfigurationContextFactory.createConfigurationContextFromFileSystem(
              serverConfigurationInformation.getAxis2RepoLocation(),
              serverConfigurationInformation.getAxis2Xml());

      configurationContext.setProperty(AddressingConstants.ADDR_VALIDATE_ACTION, Boolean.FALSE);

      startJmxAdapter();

      listenerManager = configurationContext.getListenerManager();
      if (listenerManager == null) {

        // create and initialize the listener manager but do not start
        listenerManager = new ListenerManager();
        listenerManager.init(configurationContext);
      }

      // do not use the listener manager shutdown hook, because it clashes with the
      // SynapseServer shutdown hook.
      listenerManager.setShutdownHookRequired(false);

    } catch (Throwable t) {
      handleFatal("Failed to create a new Axis2 instance...", t);
    }
  }
  /**
   * Adds the synapse handlers to the inflow Dispatch phase and starts the listener manager if the
   * axis2 instance is created by the Synapse
   */
  public void start() {

    // add the Synapse handlers
    if (configurationContext != null) {
      List<Phase> inflowPhases = configurationContext.getAxisConfiguration().getInFlowPhases();
      for (Phase inPhase : inflowPhases) {
        // we are interested about the Dispatch phase in the inflow
        if (PhaseMetadata.PHASE_DISPATCH.equals(inPhase.getPhaseName())) {
          try {
            inPhase.addHandler(prepareSynapseDispatcher());
            inPhase.addHandler(prepareMustUnderstandHandler());
          } catch (PhaseException e) {
            handleFatal("Couldn't start Synapse, " + "Cannot add the required Synapse handlers", e);
          }
        }
      }
    } else {
      handleFatal("Couldn't start Synapse, ConfigurationContext not found");
    }

    // if the axis2 instance is created by us, then start the listener manager
    if (serverConfigurationInformation.isCreateNewInstance()) {
      if (listenerManager != null) {
        listenerManager.start();
      } else {
        handleFatal("Couldn't start Synapse, ListenerManager not found");
      }
      /* if JMX Adapter has been configured and started, output usage information rather
      at the end of the startup process to make it more obvious */
      if (jmxAdapter != null && jmxAdapter.isRunning()) {
        log.info(
            "Management using JMX available via: " + jmxAdapter.getJmxInformation().getJmxUrl());
      }
    }
  }
  /** {@inheritDoc} */
  public void destroy() {

    try {
      // only if we have created the server
      if (serverConfigurationInformation.isCreateNewInstance()) {

        // destroy listener manager
        if (listenerManager != null) {
          listenerManager.destroy();
        }

        stopJmxAdapter();
        RMIRegistryController.getInstance().shutDown();

        // we need to call this method to clean the temp files we created.
        if (configurationContext != null) {
          configurationContext.terminate();
        }
      }
      initialized = false;
    } catch (Exception e) {
      log.error("Error stopping the Axis2 Based Server Environment", e);
    }
  }
  /** Cleanup the axis2 environment and stop the synapse environment. */
  public void stop() {
    try {
      // stop tasks
      SynapseTaskManager synapseTaskManager = synapseEnvironment.getTaskManager();
      if (synapseTaskManager.isInitialized()) {
        synapseTaskManager.cleanup();
      }

      EnterpriseBeanstalkManager manager =
          (EnterpriseBeanstalkManager)
              serverContextInformation.getProperty(
                  EnterpriseBeanstalkConstants.BEANSTALK_MANAGER_PROP_NAME);
      if (manager != null) {
        manager.destroy();
      }

      // stop the listener manager
      if (listenerManager != null) {
        listenerManager.stop();
      }

      // detach the synapse handlers
      if (configurationContext != null) {
        List<Phase> inflowPhases = configurationContext.getAxisConfiguration().getInFlowPhases();
        for (Phase inPhase : inflowPhases) {
          // we are interested about the Dispatch phase in the inflow
          if (PhaseMetadata.PHASE_DISPATCH.equals(inPhase.getPhaseName())) {
            List<HandlerDescription> synapseHandlers = new ArrayList<HandlerDescription>();
            for (Handler handler : inPhase.getHandlers()) {
              if (SynapseDispatcher.NAME.equals(handler.getName())
                  || SynapseMustUnderstandHandler.NAME.equals(handler.getName())) {
                synapseHandlers.add(handler.getHandlerDesc());
              }
            }

            for (HandlerDescription handlerMD : synapseHandlers) {
              inPhase.removeHandler(handlerMD);
            }
          }
        }
      } else {
        handleException(
            "Couldn't detach the Synapse handlers, " + "ConfigurationContext not found.");
      }

      // continue stopping the axis2 environment if we created it
      if (serverConfigurationInformation.isCreateNewInstance()
          && configurationContext != null
          && configurationContext.getAxisConfiguration() != null) {
        Map<String, AxisService> serviceMap =
            configurationContext.getAxisConfiguration().getServices();
        for (AxisService svc : serviceMap.values()) {
          svc.setActive(false);
        }

        // stop all modules
        Map<String, AxisModule> moduleMap =
            configurationContext.getAxisConfiguration().getModules();
        for (AxisModule mod : moduleMap.values()) {
          if (mod.getModule() != null && !"synapse".equals(mod.getName())) {
            mod.getModule().shutdown(configurationContext);
          }
        }
      }
    } catch (AxisFault e) {
      log.error("Error stopping the Axis2 Environment");
    }
  }