Monday, February 23, 2015

How to store and read resources with WSO2 Registry API.



In this short tutorial I will show how we can store and read data from the Registry with the WSO2 Registry API. 
Registry is a content store and a meta data repository. Registry is used to store and refer SOA artifacts like services,WSDLs and configuration files. The resources stored in the Registry can be referred by a resource path.

For this example I used Local Repository which is one of the main three Registry partitions  any WSO2 carbon based product has.

Followings are the Three main Registry partitions.

1. Local repository - Use to store configuration data and runtime data local to the server. This portion of registry is not shared with multiple servers.

2. Configuration repository - Use to store product specific configurations and this registry portion can be shared with the multiple instances of the same product.

3. Governance repository  - Use to store configurations and data which is shared across the Carbon platform. This registry partition contains services,service descriptions, endpoints and data sources.


You can find the full set of operations provided by the Registry API from following documentation.

https://docs.wso2.com/display/Governance460/Registry+API

Lets see how we can store and read data from the Registry.

Adding a Resource with API.

If you are using Maven add following repository and dependencies to your pom.xml file.

Maven repository

<repository>
<id>wso2-nexus</id>
<name>WSO2 internal Repository</name>
<url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
view raw pom.xml hosted with ❤ by GitHub
Maven dependencies

<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.registry.api</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.registry.core</artifactId>
<version>4.3.0</version>
</dependency>
view raw pom.xml hosted with ❤ by GitHub
Import following classes to access the Registry

import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource;
view raw Imports.java hosted with ❤ by GitHub
Adding a Resource
We can set content as String, byte array or InputStream for a resource.

try {
CarbonContext cCtx = CarbonContext.getThreadLocalCarbonContext();
Registry registry = cCtx.getRegistry(RegistryType.LOCAL_REPOSITORY);
Resource resource = registry.newResource();
String str = "My File Content";
resource.setContent(str.getBytes());
registry.put("/c1/c2/r1", resource);
}
catch (RegistryException e) {
e.printStackTrace();
}
Reading a Resource

try {
CarbonContext cCtx = CarbonContext.getThreadLocalCarbonContext();
Registry registry = cCtx.getRegistry(RegistryType.LOCAL_REPOSITORY);
Resource resource = registry.get("/c1/c2/r1");
Object content = resource.getContent();
String output = new String((byte[]) content);
System.out.println(output);
} catch (RegistryException e) {
e.printStackTrace();
}
Reference

https://docs.wso2.com/display/Governance460/Registry+API

No comments:

Post a Comment