Tuesday, July 20, 2010

Sending UTF-16 messages through Synapse

When fixing SYNAPSE-662 and SYNAPSE-670, I had to send UTF-16 messages to the SimpleStockQuoteService to verify the fixes. Following are the sample configuration and client code that I used to verify the fixes.

1. Start the Synapse ESB with the following configuration.

<definitions xmlns="http://ws.apache.org/ns/synapse">
<switch source="//m0:getQuote/m0:request/m0:symbol" xmlns:m0="http://services.samples/xsd">
<case regex="IBM">
<!-- the property mediator sets a local property on the *current* message -->
<property name="symbol" value="Great stock - IBM"/>
</case>
<case regex="MSFT">
<property name="symbol" value="Are you sure? - MSFT"/>
</case>
<default>
<!-- it is possible to assign the result of an XPath expression as well -->
<property name="symbol"
expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"
xmlns:m0="http://services.samples/xsd"/>
</default>
</switch>

<log level="custom">
<!-- the get-property() XPath extension function allows the lookup of local message properties
as well as properties from the Axis2 or Transport contexts (i.e. transport headers) -->
<property name="symbol" expression="get-property('symbol')"/>
<!-- the get-property() function supports the implicit message headers To/From/Action/FaultTo/ReplyTo -->
<property name="epr" expression="get-property('To')"/>
</log>

<!-- Send the messages where they are destined to (i.e. the 'To' EPR of the message) -->
<send/>
</definitions>


2. Start the Axis2 server and deploy the SimpleStockQuoteService.

3. Use the following axis2 client (which generates a UTF-16 message) to invoke the service.

package org.wso2.esb;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;

import java.lang.*;

public class MyClient {
public static OMElement createPayload(){
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://services.samples", "ns");
OMElement method = fac.createOMElement("getQuote", omNs);
OMElement value1 = fac.createOMElement("request", omNs);
OMElement value2 = fac.createOMElement("symbol", omNs);

value2.addChild(fac.createOMText(value1, "IBM"));
value1.addChild(value2);
method.addChild(value1);
return method;
}

public static void main(String args[]){
OMElement payload = createPayload();
ServiceClient serviceclient;

ConfigurationContext configurationContext = null;
try {
configurationContext = ConfigurationContextFactory.
createConfigurationContextFromFileSystem("/home/heshan/Dev/trunk/synapse/modules/distribution/target/synapse-2.0.0-SNAPSHOT/samples/axis2Client/client_repo",
"/home/heshan/Dev/trunk/synapse/modules/distribution/target/synapse-2.0.0-SNAPSHOT/samples/axis2Client/client_repo/conf/axis2.xml");

serviceclient = new ServiceClient();
Options opt = new Options();

opt.setTo(new EndpointReference ("http://localhost:8281/services/StockQuote"));
opt.setAction("urn:getQuote");
opt.setProperty(org.apache.axis2.Constants.Configuration.CHARACTER_SET_ENCODING,"UTF-16");
opt.setSoapVersionURI(org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
serviceclient.setOptions(opt);

OMElement ome = serviceclient.sendReceive(payload);
System.out.println("The output is : " + ome);

} catch (AxisFault axisFault) {
axisFault.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

}

}
}

Wednesday, July 14, 2010

Extend the Axis2 Framework to Support JVM Based Scripting Languages

This article explains how to extend the Axis2 framework to support Java Virtual Machine (JVM) based scripting languages such as Jython, JRuby, etc. It provides a high level overview of the subject, covering some key concepts of Apache Axis2 and how it can be used to come up with an extension to a JVM based scripting language. After going through this article, a developer will be able to extend the Axis2 framework to support the JVM based scripting language of his or her choice.

If you are interested, read the full article on InfoQ.com.

I take this opportunity to thank Nandika for doing the technical review and Charles Humble for doing the editorial review.

Tuesday, June 22, 2010

Build WSO2 ESB from source

This post will discuss how to build WSO2 ESB[1] from the WSO2 Carbon[2] trunk.

1) Checkout WSO2 Carbon project's trunk.

heshan@heshan-laptop:~/Dev/trunk/$ svn co https://svn.wso2.org/repos/wso2/trunk/carbon

Following directories/files would be checked-out to your file system.

carbon
|-- components
|-- core
|-- dependencies
|-- features
|-- orbit
|-- pom.xml
|-- products

2) Build the dependencies for ESB project.

heshan@heshan-laptop:~/Dev/trunk/carbon/dependencies$ mvn clean install -Dproduct=esb

Then the following dependencies will be built. If you encounter any build errors, try and build each project one at a time :)

dependencies
|-- abdera
|-- axiom
|-- axis2
|-- wss4j
|-- carbon-p2-plugin
|-- commons/caching
|-- commons/eventing
|-- commons/throttle
|-- commons/xkms
|-- p2
|-- rampart
|-- rhino
|-- sandesha
|-- savan
|-- synapse
|-- transports
|-- commons-vfs

Carbon Dependencies :-
Carbon platform makes use of already existing open source projects rather than trying to re-invent the wheel. We use popular and matured open source projects like apache axiom, axis2, synapse etc which fuel the very heart of the carbon platform. Dependencies contains all these open source projects that the carbon platform makes use of adding more value to them.

3) Build Carbon Orbit for ESB project.

heshan@heshan-laptop:~/Dev/trunk/carbon/orbit$ mvn clean install -Dproduct=esb
Carbon Orbit :-
WSO2 Carbon uses OSGi as its underlying modularization technology. But Many 3rd party open source libraries does not exists in osgi form (ie. in osgi bundle form). Therefore to introduce them to the carbon platform, we wrap-around them to make them proper OSGI bundles (They will be useful to any OSGI environment like felix, knoplerfish etc). Carbon-orbit project contains all these third party bundles.

4) Build Carbon Core for ESB project.

heshan@heshan-laptop:~/Dev/trunk/carbon/core$ mvn clean install -Dproduct=esb
Carbon Core :-
This is where we create the base for our platform. Everything runs on top of this base. From the simple OSGi framework to the UI framework, all the critical technologies for the platform are implemented here.

5) Build Carbon Components for ESB project.

heshan@heshan-laptop:~/Dev/trunk/carbon/components$ mvn clean install -Dproduct=esb
Carbon Components :-
Components module contains the main functionalities that are introduced by each and every product in addition to to the core level features. Among the close to 100 components each adds value to the product in a different way than the next. Some will enhance the security while some may just provide vital tools to monitor the health of the system.

6) Build Carbon Features for ESB project.

heshan@heshan-laptop:~/Dev/trunk/carbon/features$ mvn clean install -Dproduct=esb
Carbon Features :-
Equinox P2 has been integrated with WSO2 Carbon platform to enable provisioning capabilities. This enables you to extend or enhance your carbon platform with more functionalities or just strip out the irrelevant features which suits your context.

7) Build WSO2 ESB product.

heshan@heshan-laptop:~/Dev/trunk/carbon/products/esb$ mvn clean install
Carbon Product :-
Products module contains all the products ranging from ESB to BRS.

8) When the build is successful move to target folder inside esb project.

heshan@heshan-laptop:~/Dev/trunk/carbon/products/esb/modules/distribution/target$ ls
pom-transformed.xml wso2esb-3.0.0-SNAPSHOT wso2esb-3.0.0-SNAPSHOT-docs.zip wso2esb-3.0.0-SNAPSHOT.zip

Then unzip the SNAPSHOT.

heshan@heshan-laptop:~/Dev/trunk/carbon/products/esb/modules/distribution/target$ unzip wso2esb-3.0.0-SNAPSHOT.zip

Start the ESB server

heshan@heshan-laptop:~/Dev/trunk/carbon/products/esb/modules/distribution/target/wso2esb-3.0.0-SNAPSHOT/bin$ ./wso2server.sh

Now you have successfully built and started a WSO2 ESB build from Carbon trunk.

References
[1] - http://wso2.com/products/enterprise-service-bus/
[2] - http://wso2.com/products/carbon/

NOTE: If you need to build any other Carbon based product, repeat the above steps by replacing the product name to the relevant product.

mvn version used for testing this was:

heshan@heshan-laptop:~$ mvn -version
Apache Maven 2.1.0 (r755702; 2009-03-19 00:40:27+0530)
Java version: 1.6.0_03
Java home: /home/heshan/Installations/jdk1.6.0_03/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" arch: "i386" Family: "unix"


Friday, May 28, 2010

Call MS SQL Server SPs from WSO2 ESB

This post will be focusing on MS SQL Server Stored Procedures(SP) and follow up my article titled "How to call database stored procedures from WSO2 ESB" on WSO2 OxygenTank.

You may think "why is he writing a separate post addressing MS SQL Server?". The reason is that; when calling MS SQL Server SPs, you need to slightly modify the mediator configuration in order to successfully call a SP.

Now let's dig in to "how to do this".

Setting up MS SQL database server
1. Install and setup MS SQL Server.

2. Create a sample database in MS SQL Server.
Following sql statments cab be used to create the same enviornment discussed in the above article.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[company]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[company](
[compName] [varchar](10) NULL,
[id] [varchar](10) NULL,
[price] [decimal](18, 0) NULL,
[location] [varchar](10) NULL
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getCompany]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE procedure [dbo].[getCompany] @compname varchar(10) as
select * from company where compName = @compname
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updateCompany]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE procedure [dbo].[updateCompany] @compPrice decimal, @compName varchar(10) as
update company set price = @compPrice where compName = @compName
'
END
Setting up ESB server
1. Download latest version of WSO2 ESB from http://wso2.org/downloads/esb. This tutorial will be demonstrated upon the latest release of the WSO2 ESB. ie. WSO2 ESB-3.0.0.

2. Install it as a standalone server. Install location will be referred to as ESB_HOME from here onwards.

3. When using MS SQL Server, ms-sql-jdbc.jar should be added to the ESB classpath(i.e. ESB_HOME/repository/components/lib/).

4. Start WSO2ESB using the startup scripts.
eg. wso2server.bat for windows environments
wso2server.sh for linux environments

5. Open a web browser and navigate to https://localhost:9443/

6. Login to ESB. The default username/password will be admin/admin. Go through the User Interface and get yourself familiarized with WSO2 ESB environment.

ESB configuration
Following is the Synapse configuration used to call the MS SQL Stored Procedure. Pay close attention on 'how the SP query is written inside the mediator' :).
<definitions xmlns="http://ws.apache.org/ns/synapse">

<sequence name="main">
<in>
<send>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
</endpoint>
</send>
</in>

<out>
<log level="custom">
<property name="text"
value="** Reporting to the Database **"/>
</log>

<dbreport>
<connection>
<pool>
<url>jdbc:sqlserver://10.100.1.200:1433;databaseName=ESB_SP_SAMPLE</url>
<user>sa</user>
<password>test</password>
<driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver>
</pool>
</connection>
<statement>
<sql>{ call updateCompany(?,?) }</sql>
<parameter expression="//m0:return/m1:last/child::text()"
xmlns:m0="http://services.samples" xmlns:m1="http://services.samples/xsd" type="DOUBLE"/>
<parameter expression="//m0:return/m1:symbol/child::text()"
xmlns:m0="http://services.samples" xmlns:m1="http://services.samples/xsd" type="VARCHAR"/>
</statement>
</dbreport>
<log level="custom">
<property name="text"
value="** Looking up from the Database **"/>
</log>
<dblookup>
<connection>
<pool>
<url>jdbc:sqlserver://10.100.1.200:1433;databaseName=ESB_SP_SAMPLE</url>
<user>sa</user>
<password>test</password>
<driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver>
</pool>
</connection>
<statement>
<sql>{ call getCompany(?) }</sql>
<parameter expression="//m0:return/m1:symbol/child::text()"
xmlns:m0="http://services.samples" xmlns:m1="http://services.samples/xsd" type="VARCHAR"/>
<result name="stock_prize" column="price"/>
</statement>
</dblookup>
<log level="custom">
<property name="text"
expression="fn:concat('Stock Prize - ',get-property('stock_prize'))"/>
</log>
<send/>
</out>
</sequence>

</definitions>
Running the sample.
1. Start the WSO2 ESB server with the attached synapse configuration named dbreport_synapse_config.xml.

2. Start the Axis2 server and deploy the SimpleStockQuoteService if not already done.

3. Run the client.
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=WSO2

Wednesday, May 19, 2010

SOA Summer School - 2010

Summer School will focus on Service Oriented Architecture (SOA) concepts, technologies, and best practices. In addition to that, this year's Summer School sessions will be including insights into proven techniques for cloud deployments, business process management (BPM), complex event processing (CEP) , business rules processing and more.

Summer School 2010 runs for two months starting 10th June and ending on 29th July. Do you know what's the best news about Summer School is ? IT'S ABSOLUTELY FREE ! :) yes, free as in open source ;)

If you are interested, you can register here.