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