public void destroy() {
    if (metricsMBean != null) {
      metricsMBean.destroy();
    }

    if (enableMBeanStats) {
      MBeanRegistrar.getInstance().unRegisterMBean("Endpoint", endpointName);
    }
    metricsMBean = null;

    this.initialized = false;
  }
  public void setName(String endpointName) {
    this.endpointName = endpointName;
    if (enableMBeanStats) {
      /*if (endpointName != null && !"".equals(endpointName.trim())){
          //we skip stat collection for endpoints with no defined name
          log.warn("Endpoint Name not found. Skipped JMX statistics collection for this endpoint");
          return;
      }*/
      metricsMBean = new EndpointView(endpointName, this);
      //            if(metricsMBean != null) {
      //                metricsMBean.destroy();
      //            }

      MBeanRegistrar.getInstance().registerMBean(metricsMBean, "Endpoint", endpointName);
    }
  }
 static {
   JMXSecretsMBean = new JMXSecretsProvider();
   MBeanRegistrar.getInstance()
       .registerMBean(JMXSecretsMBean, "SecretsProvider", "SecretsProvider");
 }
  @Override
  public String updateSynapseArtifact(
      OMElement artifactConfig,
      String fileName,
      String existingArtifactName,
      Properties properties) {

    Endpoint ep = EndpointFactory.getEndpointFromElement(artifactConfig, false, properties);

    CustomLogSetter.getInstance().setLogAppender((ep != null) ? ep.getArtifactContainerName() : "");

    if (log.isDebugEnabled()) {
      log.debug("Endpoint update from file : " + fileName + " has started");
    }

    try {
      if (ep == null) {
        handleSynapseArtifactDeploymentError(
            "Endpoint update failed. The artifact "
                + "defined in the file: "
                + fileName
                + " is not a valid endpoint.");
        return null;
      }
      ep.setFileName(new File(fileName).getName());

      if (log.isDebugEnabled()) {
        log.debug("Endpoint: " + ep.getName() + " has been built from the file: " + fileName);
      }

      ep.init(getSynapseEnvironment());
      Endpoint existingEp =
          getSynapseConfiguration().getDefinedEndpoints().get(existingArtifactName);
      if (existingArtifactName.equals(ep.getName())) {
        getSynapseConfiguration().updateEndpoint(existingArtifactName, ep);
      } else {
        // The user has changed the name of the endpoint
        // We should add the updated endpoint as a new endpoint and remove the old one
        getSynapseConfiguration().addEndpoint(ep.getName(), ep);
        getSynapseConfiguration().removeEndpoint(existingArtifactName);
        log.info("Endpoint: " + existingArtifactName + " has been undeployed");
      }

      log.info("Endpoint: " + ep.getName() + " has been updated from the file: " + fileName);

      waitForCompletion();
      existingEp.destroy();
      if (existingArtifactName.equals(ep.getName())) {
        // If the endpoint name was same as the old one, above method call (destroy)
        // will unregister the endpoint MBean - So we should register it again.
        MBeanRegistrar.getInstance().registerMBean(ep.getMetricsMBean(), "Endpoint", ep.getName());
      }
      return ep.getName();

    } catch (DeploymentException e) {
      handleSynapseArtifactDeploymentError(
          "Error while updating the endpoint from the " + "file: " + fileName);
    }

    return null;
  }