Services with Governance API
Note
Please follow the documentation on how to create a client-side connection to use the Governance API before following these examples.
Adding a new Service
Assuming that the governanceRegistry
variable refers to a valid governanceRegistry
object, you can create a new service and and save it using the following code.
import org.wso2.carbon.governance.api.services.ServiceManager; import org.wso2.carbon.governance.api.services.dataobjects.Service; ServiceManager serviceManager = new ServiceManager(governanceRegistry); Service service = serviceManager.newService(new QName("http://my.service.ns1", "MyService")); // you can do the changes to the service instance (like setting/unsettings attributes) in here. serviceManager.addService(service);
Updating a Service
After you have made additional changes to the service (after adding attributes), call updateService
to update the service.
service.addAttribute("Owner", "Financial Department"); serviceManager.updateService(service);
Removing a Service
In order to remove a service, you should have the reference to the service ID, which can be retrieved from service.getId();
String serviceId = service.getId(); serviceManager.removeService(serviceId);
Retrieve a Service
You can retrieve a service from the service ID using the following code.
Tip
This method can only be used if you have the reference to the service ID. If you do not have any reference to the service ID, you can use the #discovery methods to retrieve services.
Service service = serviceManager.getService(serviceId);
Discovering Services
Using the service discovery methods, you can retrieve services that match a given criteria. Here is an example on how to retrieve set of services that has the attributes:
- key -
Owner
. - value -
Financial Department
.
Service[] services = serviceManager.findServices(new ServiceFilter() { public boolean matches(Service service) throws GovernanceException { String attributeVal = service.getAttribute("Owner"); return (attributeVal != null && attributeVal.equals("Financial Department")); } });
Attaching a WSDL to a Service
You can attach WSDLs, schemas, policies, Endpoints or any other governance artifact to the service.
Tip
In order to use the attach method, you have to add the service to the registry (using addService
), or you can use an existing service (which is already added).
To attach any Governance artifact, use the attach
method which the Service
class inherits from the GovernanceArtifact
class.
service.attach(governanceArtifact);
Or use the Service
specific method to attach WSDLs, schemas, policies, and Endpoints.
service.attachWsdl(wsdl); service.attachSchema(schema); service.attachPolicy(poliyc); service.attachWndpoint(endpoint);
Retrieving Dependents and Dependencies
The GovernanceArtifact
class has the getDependents()
and getDependencies()
methods that are inherited by Service
, Policy
, Schema
and Wsdl
.
GovernanceArtifact[] dependents = service.getDependents(); GovernanceArtifact[] dependencies = service.getDependencies();