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());
  }
  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;
  }