public static void deactivateService(TargetService service) throws Exception {
    TargetService targetService = getService(service.getEpr());

    if (targetService == null) {
      throw new DiscoveryException(
          "Error while updating discovery metadata. No service "
              + "exists with the ID: "
              + service.getEpr().getAddress());
    }

    removeService(service, Collections.<String, String>emptyMap());
  }
  public static void removeService(TargetService service, Map<String, String> headerMap)
      throws Exception {
    ServiceManager serviceManager = new ServiceManager(getRegistry());
    String serviceId = service.getEpr().getAddress();

    Service oldService = serviceManager.getService(serviceId);
    oldService.deactivate();
  }
  private static TargetService getTargetService(Service service) throws Exception {

    EndpointReference epr;
    String eprAttr = service.getAttribute(DiscoveryConstants.ATTR_EPR);
    if (eprAttr != null) {
      epr = EndpointReferenceHelper.fromString(eprAttr);
    } else {
      epr = new EndpointReference(service.getId());
    }

    TargetService targetService = new TargetService(epr);
    String types = service.getAttribute(DiscoveryConstants.ATTR_TYPES);
    if (types != null) {
      targetService.setTypes(Util.toQNameArray(types.split(",")));
    }

    String scopes = service.getAttribute(DiscoveryConstants.ATTR_SCOPES);
    if (scopes != null) {
      targetService.setScopes(Util.toURIArray(scopes.split(",")));
    }

    String[] endpoints = service.getAttributes(DiscoveryConstants.ATTR_ENDPOINTS);
    URI[] uris = new URI[endpoints.length];
    for (int i = 0; i < endpoints.length; i++) {
      uris[i] = URI.create(endpoints[i].substring(1));
    }
    if (endpoints != null) {
      targetService.setXAddresses(uris);
    }

    String mdv = service.getAttribute(DiscoveryConstants.ATTR_METADATA_VERSION);
    if (mdv != null) {
      targetService.setMetadataVersion(Integer.valueOf(mdv));
    } else {
      // Set a default metadata version
      targetService.setMetadataVersion(1);
    }

    return targetService;
  }
  /**
   * 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());
   }
 }
  public static void removeServiceEndpoints(TargetService service) throws Exception {
    TargetService oldService = getService(service.getEpr());

    if (oldService == null) {
      throw new DiscoveryException(
          "Error while updating discovery metadata. No service "
              + "exists with the ID: "
              + service.getEpr().getAddress());
    }

    // When marking an existing service as inactive try to hold on to
    // the old metadata of the service
    if (service.getScopes() == null) {
      service.setScopes(oldService.getScopes());
    }
    if (service.getTypes() == null) {
      service.setTypes(oldService.getTypes());
    }
    if (service.getMetadataVersion() == -1) {
      service.setMetadataVersion(oldService.getMetadataVersion());
    }

    if (service.getXAddresses() != null && oldService.getXAddresses() != null) {
      // According to the spec this is the set of addresses on
      // which the service is NO LONGER AVAILABLE.
      // We should remove them from the registry.
      List<URI> existingAddresses = new ArrayList<URI>();
      for (URI xAddr : oldService.getXAddresses()) {
        existingAddresses.add(xAddr);
      }

      for (URI xAddr : service.getXAddresses()) {
        existingAddresses.remove(xAddr);
      }

      if (existingAddresses.size() > 0) {
        service.setXAddresses(existingAddresses.toArray(new URI[existingAddresses.size()]));
      } else {
        service.setXAddresses(null);
      }
    }

    addService(service, Collections.<String, String>emptyMap());
  }