/**
   * Add a discovered target service to the governance registry. This method will also create the
   * endpoint artifacts for the service and attach them with the related service artifacts. The
   * service artifact will be given an auto generated name.
   *
   * @param service WS-D target service to be added to the registry
   * @throws Exception if an error occurs while saving the artifacts to the registry
   */
  public static void addService(TargetService service, Map<String, String> headerMap)
      throws Exception {
    ServiceManager serviceManager = new ServiceManager(getRegistry());
    String serviceId = service.getEpr().getAddress();

    // This part is use to remove the prefix of Service ID
    if (serviceId.startsWith(DiscoveryConstants.EPR_ADDRESS_PREFIX)) {
      serviceId = serviceId.replace(DiscoveryConstants.EPR_ADDRESS_PREFIX, "");
    }

    // Delete the existing stuff and start fresh
    Service oldService = serviceManager.getService(serviceId);
    String serviceName = null;
    if (oldService != null) {
      // TODO: Change this once the necessary improvements are in the governance API
      serviceName = oldService.getQName().getLocalPart();
      serviceManager.removeService(serviceId);
    }

    // Create a new service (Use the discovery namespace)
    if (serviceName == null) {
      if (headerMap.containsKey(DiscoveryConstants.DISCOVERY_HEADER_SERVICE_NAME)) {
        serviceName =
            headerMap.get(DiscoveryConstants.DISCOVERY_HEADER_SERVICE_NAME).replace("/", "_");
      } else {
        serviceName =
            DiscoveryConstants.SERVICE_NAME_PREFIX + new GregorianCalendar().getTimeInMillis();
      }
    }

    Service newService =
        serviceManager.newService(
            new QName(DiscoveryConstants.WS_DISCOVERY_NAMESPACE, serviceName));
    newService.setId(serviceId);

    // Set the version if provided
    if (service.getMetadataVersion() != -1) {
      newService.addAttribute(
          DiscoveryConstants.ATTR_METADATA_VERSION, String.valueOf(service.getMetadataVersion()));
    }

    // Store other service metadata (scopes, types, x-addresses)
    QName[] types = service.getTypes();
    String typeList = "";
    if (types != null && types.length > 0) {
      for (int i = 0; i < types.length; i++) {
        typeList = typeList.concat(types[i].toString());
        if (i != types.length - 1) {
          typeList = typeList.concat(",");
        }
      }
    }
    newService.setAttribute(DiscoveryConstants.ATTR_TYPES, typeList);

    URI[] scopes = service.getScopes();
    String scopeList = "";
    if (scopes != null && scopes.length > 0) {
      for (int i = 0; i < scopes.length; i++) {
        scopeList = scopeList.concat(scopes[i].toString());
        if (i != scopes.length - 1) {
          scopeList = scopeList.concat(",");
        }
      }
    }
    newService.setAttribute(DiscoveryConstants.ATTR_SCOPES, scopeList);

    URI[] uris = service.getXAddresses();
    String[] endpoints = new String[uris.length];
    for (int i = 0; i < uris.length; i++) {
      endpoints[i] = ":" + uris[i].toString();
    }
    boolean activate = false;
    if (uris != null && uris.length > 0) {
      newService.setAttributes(DiscoveryConstants.ATTR_ENDPOINTS, endpoints);
      activate = true;
    }

    if (headerMap.containsKey(DiscoveryConstants.DISCOVERY_HEADER_WSDL_URI)
        && headerMap.get(DiscoveryConstants.DISCOVERY_HEADER_WSDL_URI) != "") {
      newService.setAttribute(
          "interface_wsdlURL", headerMap.get(DiscoveryConstants.DISCOVERY_HEADER_WSDL_URI));
    }

    // One hot discovered service coming thru....
    serviceManager.addService(newService);
    if (activate) {
      newService.activate();
    }
  }
 public static void remove(TargetService service) throws Exception {
   ServiceManager serviceManager = new ServiceManager(getRegistry());
   if (serviceManager.getService(service.getEpr().getAddress()) != null) {
     serviceManager.removeService(service.getEpr().getAddress());
   }
 }