Monday, January 2, 2012

Configurable Governance Artifacts with Governance API

Previously I discussed about how to extend WSO2 Governance Registry. Now lets see how we can access those extensions using governance API.

Adding a new Artifact
To create a new Artifact you can use the following code. Here registry refers to valid org.wso2.carbon.registry.core.Registry object and projectKey refers to shortName in the registry extension file.
import org.wso2.carbon.governance.api.generic.GenericArtifactManager;
import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact;

GenericArtifactManager artifactManager = new GenericArtifactManager(registry, projectKey);
GenericArtifact artifact  = artifactManager.newGovernanceArtifact(new QName("http://www.example.com","event"));
artifactManager.addGenericArtifact(artifact);
Update a Artifact
To make further changes to a Artifact which already in the registry you can use updateGenericArtifact() method.
artifact.addAttribute("EventName","Running");
artifactManager.updateGenericArtifact(artifact);
Remove a Artifact
To remove a Artifact you should have the reference to that Artifact, which can be retrieve using artifact.getId(). Then you can use removeGenericArtifact() method to remove the Artifact.
String artifactId = artifact.getId();
artifactManager.removeGenericArtifact(artifactId);
Retrieve a Artifact
To retrieve a Artifact you can use getGenericArtifact method().
GenericArtifact artifact = artifactManager.getGenericArtifact(artifactId);
NOTE: In ordered to use getGenericArtifact method you have to have the Artifact reference ID. If you don't have it you can use the discovery method discussed below.

Discover a Artifact
Using the service discovery methods, you can retrieve Artifacts that match a given criteria. Here is an example on how to retrieve a set of Artifacts that has the attributes:
  • Key - EventName
  • Value - Running
GenericArtifact[] artifacts = artifactManager.findGenericArtifacts(new GenericArtifactFilter() {
    public boolean matches(GenericArtifact genericArtifact) throws GovernanceException {
        String attributeVal = artifact.getAttribute("EventName");
        if(attributeVal != null && attributeVal == "Running"){
            return true;
        }
        return false;
    }
});
Associate a Lifecycle to the Artifact
To associate a Lifecycle to the Artifact you can use attachLifecycle() method.
artifact.attachLifecycle("LifeCycleName");
artifactManager.updateGenericArtifact(artifact);
Retrieving Dependents And Dependencies
To get the dependents and dependancies of an Artifact you can use getDependents(), getDependencies() methods.
GovernanceArtifact[] dependents = artifact.getDependents();
GovernanceArtifact[] dependencies = artifact.getDependencies();

No comments:

Post a Comment