Monday, January 11, 2010

Access WSO2 Governance as a Service From Remote Registry

WSO2 Governance as a Service is a hosted instance of WSO2 Governance Registry with multi-tenant support. WSO2 Governance as a Service provide you almost all the functionalities provided with the Governance Registry targeting the enterprise SOA governance, same time it provides all the advantages  inherent with the Software as a Service model.

Here I'm talking about how to use a popular feature available in Governance Registry, inside WSO2 Governance as a Service. i.e. Remote Registry Client. With Remote Registry Client, you can access the resources in registry programatically. It uses atom/pub protocol to communicate with the registry server.

Here is an example of using Remote Registry Client. I assumed I have an account with domain name '' with a user name 'example_user' ('example_password'). You have to change this to valid values before running this code, You can create an account in Governance as a Service freely for a limited use.
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;

class RegistryDemo {
public static void main(String[] args) throws Exception {

// calls the registry with the authentication information
 "", "example_password");

public static void callRemoteRegistry(String url, String username,
 String password) throws Exception {

Registry myRegistry = new RemoteRegistry(new URL(url), username, password);
if (!myRegistry.resourceExists("/demoResource")) {

Resource r = myRegistry.newResource();
r.setContent("demo content");
myRegistry.put("/demoResource", r);

Resource r = myRegistry.get("/demoResource");
byte[] contentBytes = (byte[])r.getContent();
String content = new String(contentBytes);
System.out.println("Content: " + content);

Sunday, January 10, 2010

Make vs Ant

Ant was developed mainly to run java programs, so it is good at building and running java programs. But you can use the good all Make program to build and even run java programs.

Say I have an ant file that will

  1. Clean the build - ant clean

  2. Compile - ant compile

  3. Make a Jar - ant jar

  4. Run - ant run or simply ant

For this, I will only compile one java file name src/test/
<project name="HelloWorld" basedir="." default="main">

<property name="src.dir" value="src"/>

<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>

<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="test.HelloWorld"/>

<target name="clean">
<delete dir="${build.dir}"/>

<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>


<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>

<jar destfile="${jar.dir}/${}.jar" basedir="${classes.dir}">
<attribute name="Main-Class" value="${main-class}"/>


<target name="run" depends="jar">

<java jar="${jar.dir}/${}.jar" fork="true"/>

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,run"/>


If you want to learn what each line of this means, just follow the excellent ant tutorial at

Here is the Makefile that will do the above tasks,

run: clean jar
java -jar $(JAR_DIR)/$(PROJECT).jar

jar: $(JAR_DIR)/$(PROJECT).jar
$(JAR_DIR)/$(PROJECT).jar: $(CLASSES_DIR)/$(PACKAGE)/*.class
echo Main-Class: $(MAIN_CLASS)> mf
test -d $(JAR_DIR) | mkdir -p $(JAR_DIR)

jar cfm $@ mf -C $(CLASSES_DIR) .
compile: $(CLASSES_DIR)/$(PACKAGE)/*.class

$(CLASSES_DIR)/$(PACKAGE)/%.class: ${SRC_DIR}/$(PACKAGE)/*.java
test -d $(CLASSES_DIR) | mkdir -p $(CLASSES_DIR)

javac -sourcepath src -d ${CLASSES_DIR} $<
rm -rf build mf

You may notice I had to provide the package name to the compile command, as it doesn't have a proper wildcards to represent the jar. Similar to ant all these make tasks will execute only if it is required. i.e. for an example if all the .class files are up to date with .java it will not try to recompile it.

Friday, January 08, 2010

WSF/PHP Code First Approach: Returning an Array of String

Here is a problem that many people have asked me how to do it. "Returning an array of string" with the code first approach. The API or WSDL generation annotation guide, contain all the things required in details. Here is an example of a service that return an array of string.

/** splitMe function
* @param string $string_to_split string to split
* (maps to the xs:string XML schema type )
* @return array of string $result split to array
*(maps to the xs:double XML schema type )

function splitMe($string_to_split)
return array("result" => split(":", $string_to_split));


$operations = array("splitMe"=>"splitMe");
$opParams = array("splitMe"=>"MIXED");

$svr = new WSService(array("operations"=>$operations,


Note that the annotation corresponding to the return value.
 * @return array of spring $result split to array

This will generate an schema with an element of maxOccurs='unbounded'. Note that you can get the wsdl from the 'serviceurl?wsdl'.
<xsd:element name="splitMeResponse">
<xsd:element name="result" maxOccurs="unbounded" type="xsd:string"/>

Now just generate a client for this service using wsdl2php and try invoke it. You will get an array of string as the response.
    $input = new splitMe();
$input->string_to_split = "a:b:c:d";

// call the operation
$response = $proxy->splitMe($input);

Thursday, January 07, 2010

Writing Business Rules in WSO2 Carbon Platform

If you want to write rules in a Java program you have lot of choices. You can use a third party library like Drools or use the JAVA built-in JSR-94 reference implementation. In WSO2 Carbon, there is a component that abstract the behaviour of different rule engine and give you a unified API. Currently it has plugged into Drools and JAVA built-in JSR-94 implementation.

The rule component in WSO2 Carbon platform mainly used by WSO2 ESB product to mediate messages according to the given business rules. But the component is written to facilitate any requirement of using business rules in WSO2 Carbon platform. I had such a requirement in past few days and manage to use the rule component easily with the help of the component author, So I thought it is worth sharing my experience in here.

Here You will be preparing the following stuff.

1. Rule configuration -

We can use this to provide the information about the rule implementation we are going to use, the rules (You can write rules inline or provide a reference to an external file) and the input and output adapter information.
<configuration xmlns="">
<executionSet uri="simpleItemRuleXML">
<source key="file:src/test/resources/rules/simple-rules.drl"/>

<!-- <source>

rule InvokeABC
// rules inbuilt to the rule conf

</source> -->
<property name="source" value="drl"/>

<session type="stateless"/>
<input name="facts" type="itemData" key="dataContext"></input>

<output name="results" type="itemData" key="dataContext"></output>

2. The Rules  -

You can write rules inline in the above configuration or put it in a file and refer it from a key which can be refered from the ResourceHelper (described below).
import java.util.Calendar;

rule YearEndDiscount
$item : org.test.pojo.SimpleItem(price > 100 )


Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.MONTH) == Calendar.JANUARY) {

$item.setPrice($item.getPrice() * 80/100);


3. Data Context -

The context object that can be used to feed and retrieve data from and to rule engine. Here is the data context for my application.
public class SimpleDataContext {

public List<NameValuePair> getInput() {

// in reality the data will be retrieve from a database or some datasource
List<NameValuePair> itemPairList = new ArrayList<NameValuePair>();
SimpleItem item1 = new SimpleItem();
itemPairList.add(new NameValuePair(item1.getName(), item1));

SimpleItem item2 = new SimpleItem();
itemPairList.add(new NameValuePair(item2.getName(), item2));

SimpleItem item3 = new SimpleItem();
itemPairList.add(new NameValuePair(item3.getName(), item3));

return itemPairList;

public void setResult(Object result) {

if (!(result instanceof SimpleItem)) {
System.out.println("it is not a SimpleItem");

SimpleItem item = (SimpleItem)result;
System.out.println("Item: " + item.getName() + ", Price: " + item.getPrice());


And the Item I'm going to manipulate using rule is a simple bean like this,
public class SimpleItem {
String name;
int price;
public String getName() {

return name;

public void setName(String name) { = name;

public int getPrice() {

return price;

public void setPrice(int price) {

this.price = price;

4. Data Adapter

You have to adapt the input and output with the rule engine. Mostly here you only have to wrap the data context. The advantage of having the data adapter is, a data adapter always associated with a input/output type. So in the rule configuration I can provide the type for the input and output. If you see my rule configuration above, you see the input/output type is marked as "ItemData". Here is my custom data adapter that is associated with the "itemData" type.
public class SimpleDataAdapter implements
ResourceAdapter, InputAdaptable, OutputAdaptable {

// the type associated with the adapter
private final static String TYPE = "itemData";
public String getType() {

return TYPE;

public Object adaptInput(ResourceDescription resourceDescription, Object tobeadapted) {

if (!(tobeadapted instanceof SimpleDataContext)) {
return null;

SimpleDataContext dataContext = (SimpleDataContext)tobeadapted;
return dataContext.getInput();

public boolean adaptOutput(ResourceDescription description,
Object value,
Object context,
ResourceHelper resourceHelper) {

if (!(context instanceof SimpleDataContext)) {
return false;

return true;

public boolean canAdapt(ResourceDescription description, Object ouptput) {
String key = description.getKey();
return key != null && !"".equals(key);


5. Resource Helper

Resource Helper will map the keys refered from the configuration to JAVA objects. This is mostly used in mediation rule configurations which can extract the message data using a key or an xpath. In this example, we don't have much keys refering from the configuration only the rule file and the data context.
public class SimpleResourceHelper extends ResourceHelper {

public ReturnValue findByKey(String key, Object source, Object defaultValue) {

if (!(source instanceof SimpleDataContext)) {
return new ReturnValue(defaultValue);

SimpleDataContext dataContext = (SimpleDataContext)source;
if (key.startsWith("file:")) {

String filename = key.substring("file:".length());
try {

BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
return new ReturnValue(in);
} catch (Exception e) {

return new ReturnValue(defaultValue);
if (key.startsWith("dataContext")) {

return new ReturnValue(dataContext);
return new ReturnValue(defaultValue);

// there are few more methods to be implemented, which can just leave not implemented for this example

That is all the accessories. Now you will be able to write the rule engine execution code.
File ruleConfigFile = new File(ruleConfigFilename);
XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(ruleConfigFile));

//create the builder
StAXOMBuilder builder = new StAXOMBuilder(parser);
//get the root element (in this case the envelope)

OMElement ruleConfig = builder.getDocumentElement();
EngineConfiguration configuration =
new EngineConfigurationFactory().create(ruleConfig, new AXIOMXPathFactory());

engineController = new EngineController(configuration, new SimpleResourceHelper());
final ResourceAdapterFactory factory = ResourceAdapterFactory.getInstance();

ResourceAdapter adapter = new SimpleDataAdapter();
String adapterType = adapter.getType();
if (!factory.containsResourceAdapter(adapterType)) {


SimpleDataContext simpleContext = new SimpleDataContext();

if (!engineController.isInitialized()) {


if (engineController.isInitialized()) {
engineController.execute(simpleContext, simpleContext);


Wednesday, January 06, 2010

Getting the size of BLOB in MySql

If you want to store binary in database, you can use BLOB as the data type of that column. In Mysql you can use TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB depending on your space requirement. Here is an example of database table using BLOB as a column type.
filename VARCHAR( 32 ) NOT NULL,
content BLOB NOT NULL,

Storing Data

$filename = "myimage.png";
$filecontent = file_get_contents($filename);
$filecontent_escaped = mysql_real_escape_string($filecontent);

$sql = "INSERT INTO BloBTest(filename, content) " +
mysql_query($sql, $link);

String filename = "myimage.png";
InputStream filecontent = new FileInputStream(filename);

String sql = "INSERT INTO BloBTest(filename, content) VALUES(?, ?)";

int size = filecontent.available();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, filename);
ps.setBinaryStream(2, filecontent, size);

Retrieving Data

$sql = "SELECT filename, content FROM BloBTest";
$result = mysql_query($sql, $link);
while ($row = mysql_fetch_assoc($result)) {

$filename = $row["filename"];
$content = $row["content"];
$new_filename = "new_" . $filename;
file_put_contents($new_filename, $content);

String sql = "SELECT filename, content FROM BloBTest";

PrepareStatement ps = conn.prepareStatement(resourceContentSQL);
ResultSet result = ps.executeQuery();

if ({
String filename = result.getString("filename");
InputStream contentStream = result.getBinaryStream("content");
String newFilename = "new_" + filename;
// storing the input stream in the file

OutputStream out=new FileOutputStream(newFilename);
byte buf[]=new byte[1024];
int len;


Retrieving the Size of the Blob

After you store your data as a blob, you can manipulate or query the data with some of the in-built String functions in mysql. For an example if you want to query the size of the blob you just stored, you can use OCTET_LENGTH function. Here is an example,  (this will give you the size in bytes.)
SELECT OCTET_LENGTH(content) FROM BloBTest WHERE filename='myimage.png'


Tuesday, January 05, 2010

Register Today for WSO2 Governance as a Service

WSO2 Governance as a Service is an online multi-tenant supported instance of WSO2 Governance Registry which is the solution for SOA Governance from the WSO2 SOA stack. You can start trying out WSO2 Governance as a Service by accessing the and creating an account for your organization (free for limited use).

In order to identify your account, you have to provide the domain name of your organization. I will demonstrate how to create an account using the "" as my domain name.

1. First go to from a web browser and click the 'Register' button. You will be asked to enter the domain name as the first step.

[caption id="attachment_1036" align="aligncenter" width="773" caption="Enter the domain"]Enter the domain[/caption]

After that, you have the option of validating the ownership of the domain right at the registration process, or you can skip the validation and continue to the next step in which case your domain will be appended '-trial' suffix. You can validate the ownership of the domain later at any stage.

Here I want to validate the domain right now, so I click 'Take me to the domain ownership confirmation page straight-away' and click the 'Submit' button.

2. This will redirect you to the domain ownership validation page. You can validate the ownership of your domain in one of two ways.

Method i). Just create a text file named 'wso2gaas.txt' in the web root of your domain and enter the given text. This is the most simplest method of two.

[caption id="attachment_1039" align="aligncenter" width="722" caption="Validate domain name using Textfile"]Validate domain name using Textfile[/caption]

Method ii). You can put a DNS entry according to the given instructions. This is a little tedious approch to validate the domain. In fact it may take a while to propagate the new DNS information, so you may have to wait hours without refreshing the page until you finally validate the domain ownership.

Click the continue button after the domain validation done. Then you will be redirected to a page requesting more information.

3. Tenant Registration Page

[caption id="attachment_1041" align="aligncenter" width="787" caption="Tenant Registration"]Tenant Registration[/caption]

4) After this step, you will be notified to check for your email which will contain a mail with a link to proceed with the registration. There you will be able to select a theme for your organization and finalize creating your account. Login to the admin account for your tenant with the credential you provided a the time of the registration.

The domain ownership validation was introduced to WSO2 Governance as a Service account registration only now. So for organizations who have already have account will have a message similar to this when they are trying to login to their account.

[caption id="attachment_1046" align="aligncenter" width="460" caption="Info box at login"]Info box at login[/caption]

So the account I have registered using the domain name '' has been renamed to ''. As the instruction of the message says you can go to the account management page after the login and validate the domain ownership.

[caption id="attachment_1043" align="aligncenter" width="808" caption="Account Management Page"]Account Management Page[/caption]