*
* @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) throws Exception {
ServiceManager serviceManager = new ServiceManager(getRegistry());
String serviceId = service.getEpr().getAddress();
// 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) {
serviceName = DiscoveryConstants.SERVICE_NAME_PREFIX +
new GregorianCalendar().getTimeInMillis();
}
Service newService = serviceManager.newService(new QName(
DiscoveryConstants.WS_DISCOVERY_NAMESPACE, serviceName));
newService.setId(serviceId);
// Save the endpoint reference element as an attribute
OMElement epr = EndpointReferenceHelper.toOM(OMAbstractFactory.getSOAP11Factory(),
service.getEpr(),
AddressingConstants.Final.WSA_ENDPOINT_REFERENCE,
AddressingConstants.Final.WSA_NAMESPACE);
newService.addAttribute(DiscoveryConstants.ATTR_EPR, epr.toString());
// 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();
if (types != null && types.length > 0) {
newService.setAttributes(DiscoveryConstants.ATTR_TYPES, Util.toStringArray(types));
}
URI[] scopes = service.getScopes();
if (scopes != null && scopes.length > 0) {
newService.setAttributes(DiscoveryConstants.ATTR_SCOPES, Util.toStringArray(scopes));
}
URI[] xAddresses = service.getXAddresses();
boolean activate = false;
if (xAddresses != null && xAddresses.length > 0) {
newService.setAttributes(DiscoveryConstants.ATTR_XADDRESSES,
Util.toStringArray(xAddresses));
activate = true;
}
// One hot discovered service coming thru....
serviceManager.addService(newService);
if (activate) {
newService.activate();
}
}