Wednesday, February 13, 2013

How to invoke custom artifacts using Governance Registry API.

Configurable Governance Artifacts(RXT) is one of the powerful extension point is supported by WSO2 Governance Registry. You can build your own custom artifacts using the functionality of RXT. And also G-Reg is providing a Governance API to manage those artifacts.

How to run the sample
================

1. Add new sample_application.rxt to Registry.

   Go to the Extensions > Configure > Artifact Types
   Click on "Add new Artifact".
   Copy the content of the sample_application.rxt and save.

2. Sign out and sign in, now you should be able to view the new artifact type called "Enterprise Application".

3. Go to the wso2greg-4.5.3/bin and run the ant command.
   (Now all the required jars, which is used to invoke the client will be placed in wso2greg-4.5.3/repository/lib)

4. Create a java project using the SampleWSRegistryClient.java.

5. Point the class path to wso2greg-4.5.3/repository/lib.

6. Run the client code. (You have to change the some values of the parameters (CARBON_HOME,serverURL,username,password)
   in code according to your environment).

import java.io.File;  
  
import org.apache.axis2.context.ConfigurationContext;  
import org.apache.axis2.context.ConfigurationContextFactory;  
import org.wso2.carbon.base.ServerConfiguration;  
import org.wso2.carbon.governance.api.exception.GovernanceException;  
import org.wso2.carbon.governance.api.generic.GenericArtifactFilter;  
import org.wso2.carbon.governance.api.generic.GenericArtifactManager;  
import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact;  
import org.wso2.carbon.governance.api.util.GovernanceUtils;  
import org.wso2.carbon.registry.core.Registry;  
import org.wso2.carbon.registry.core.session.UserRegistry;  
import org.wso2.carbon.registry.ws.client.registry.WSRegistryServiceClient;  
  
import javax.xml.namespace.QName;  
  
public class SampleWSRegistryClient {  
  
    private static ConfigurationContext configContext = null;  
  
    private static final String CARBON_HOME = "/home/ajith/products/wso2greg-4.5.3";  
    private static final String axis2Repo = CARBON_HOME + File.separator + "repository" +  
            File.separator + "deployment" + File.separator + "client";  
    private static final String axis2Conf =  
            ServerConfiguration.getInstance().getFirstProperty("Axis2Config.clientAxis2XmlLocation");  
    private static final String username = "admin";  
    private static final String password = "admin";  
    private static final String serverURL = "https://localhost:9443/services/";  
  
    private static WSRegistryServiceClient initialize() throws Exception {  
  
        System.setProperty("javax.net.ssl.trustStore", CARBON_HOME + File.separator + "repository" +  
                File.separator + "resources" + File.separator + "security" + File.separator +  
                "wso2carbon.jks");  
        System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");  
        System.setProperty("javax.net.ssl.trustStoreType", "JKS");  
        System.setProperty("carbon.repo.write.mode", "true");  
        configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(  
                axis2Repo, axis2Conf);  
        return new WSRegistryServiceClient(serverURL, username, password, configContext);  
  
    }  
  
    public static void main(String[] args) throws Exception {  
  
        Registry registry = initialize();  
        //We should use the UserRegisty to initialize the GenericArtifactManager  
        registry = GovernanceUtils.getGovernanceUserRegistry(registry, "admin");  
        //You should load governance Artifacts  
        GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);  
        //"applications" is the short name of the RXT  
        GenericArtifactManager artifactManager = new GenericArtifactManager(registry, "applications");  
  
        // add new artifact  
        GenericArtifact artifact = artifactManager.newGovernanceArtifact(new QName("http://www.example.com","app2"));  
        artifact.setAttribute("overview_version","3.7.8");  
        artifactManager.addGenericArtifact(artifact);  
  
        //Get all "application" artifacts and it's attributes  
        GenericArtifact[] applicationArtifacts = artifactManager.getAllGenericArtifacts();  
        for(GenericArtifact artifact1 : applicationArtifacts){  
           String[] attributes = artifact1.getAttributeKeys();  
            for(String attribute : attributes){  
                System.out.println("Artifact attributes --- > " +attribute);  
  
                System.out.println(attribute + " : " + artifact1.getAttribute(attribute));  
  
            }  
        }  
  
        //Search for Application using attributes.  
        GenericArtifact[] filteredArtifacts = artifactManager.findGenericArtifacts(  
                new GenericArtifactFilter() {  
                    public boolean matches(GenericArtifact artifact) throws GovernanceException {  
                        String attributeVal = artifact.getAttribute("overview_name");  
                        return (attributeVal != null && attributeVal.equals("app2"));  
                    }  
                });  
        //Print some search values  
        for(GenericArtifact filter : filteredArtifacts){  
            System.out.println( "Name : " +  
                    filter.getAttribute("overview_name") +", Version " +filter.getAttribute("overview_version")+  
                    " Id :" +filter.getId());  
        }  
    }  
}
The attribute name of the custom artifact is produce like this.

tableName_fieldName