This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Access Governance Registry through Javascript

The Registry Host Object is one of many APIs supported by the WSO2 Governance Registry. Read more on Supported APIs & Standards for a complete list of APIs supported by WSO2 Governance Registry.

Governance Registry can be made to access through Javascript by using the functionality extending mechanism of Host Objects. Host objects can be used to map Java logic to Javascript. Therefor we can implement our own Java class which would behave as a host object to do registry operations. 

A sample implementation of a Host Object Java class which can be used to do Registry operation would be something like below. 

public class RegistryHostObject extends ScriptableObject {


    private static final Log log = LogFactory.getLog(RegistryHostObject.class);


    private static final String hostObjectName = "MetadataStore";


    private Registry registry = null;


    public RegistryHostObject() {
        super();
    }


    public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj,
                                           boolean inNewExpr) throws ScriptException {
    }


    /**
     * Type to be used for this object inside the javascript.
     */
    public String getClassName() {
        return hostObjectName;
    }


    public static void jsFunction_remove(Context cx, Scriptable thisObj, Object[] arguments,
                                         Function funObj) throws ScriptException {
    }


    public static Scriptable jsFunction_get(Context cx, Scriptable thisObj, Object[] arguments,
                                            Function funObj) throws ScriptException {
    }


    public static String jsFunction_put(Context cx, Scriptable thisObj, Object[] arguments,
                                        Function funObj) throws ScriptException {
    }


    public static Scriptable jsFunction_newCollection(Context cx, Scriptable thisObj,
                                                      Object[] arguments,
                                                      Function funObj) throws ScriptException {
    }


    public static Scriptable jsFunction_newResource(Context cx, Scriptable thisObj,
                                                    Object[] arguments,
                                                    Function funObj) throws ScriptException {
  
    }


    public static boolean jsFunction_resourceExists(Context cx, Scriptable thisObj,
                                                    Object[] arguments,
                                                    Function funObj) throws ScriptException {
    }

}


To create an instance of the Registry it is possible to use the org.wso2.carbon.registry.core.service.RegistryService and initialize it inside the constructor visible to Javascript, jsConstructor. 

public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj,
                                           boolean inNewExpr) throws ScriptException {
        
        RegistryHostObject rho = new RegistryHostObject();
        Registry registry;
        RegistryService registryService = RegistryHostObjectContext.getRegistryService(); // A context class implementation to retrieve an instance of a RegistryService
        try {
            rho.registry = registryService.getGovernanceUserRegistry((String) args[0], (String) args[1]);
        } catch (org.wso2.carbon.registry.core.exceptions.RegistryException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        return rho;
    }

 

After initializing the RegistryService rest of the host object functions can be implemented to do registry operations as in the given example below. 

public static Scriptable jsFunction_remove(Context cx, Scriptable thisObj, Object[] arguments,
                                            Function funObj) throws ScriptException {
        RegistryHostObject rho = (RegistryHostObject) thisObj;
        try {
             rho.registry.delete((String) arguments[0]);
        } catch (RegistryException e) {
             throw new ScriptException("Registry error occurred while executing delete() operation", e);
        }
    }

In WSO2 Carbon Platform since every feature is developed as an OSGi bundle.The implemented Host Object code can also reside inside an OSGi bundle.  An OSGi bundle is also a .jar file. However, in order to act as an OSGi bundle, it needs to have several headers in itsMANIFEST.MF file. A more descriptive article about creating an OSGi bundle can be found here.