2006-01-31

Stomp

I have previous written about using web service as a mediator to let languages other than Java to communicate with JMS server. The approach is only for simple usage such as a testing tool. For more advanced usages, we can consider Stomp whcih is an open messaging protocol. There are various clients exist for Perl, Python and Ruby. On the server side, however, there is only ActiveMQ support.

2006-01-26

Make Perl speak JMS (3)

Finally I will implement a Perl web service client. The tool I pick is SOAP::Lite (v0.55).

SOAP::Lite comes with a handy tool similar to wsdl2java called stubmaker. Run our WSDL through the tool and it generates a Perl module MessageRoutingService.pm. This module wrapps the call to the single RouteMessage API defined in the WSDL. Following code shows how to use this module:
my @dest = ( 
SOAP::Data->name(protocol=>'jms'),
SOAP::Data->name(category=>'queue'),
SOAP::Data->name(server=>'my_jms_server'),
SOAP::Data->name(port=>'7222'),
SOAP::Data->name(target=>'my_queue'),
SOAP::Data->name(user=>'user_a'),
SOAP::Data->name(password=>'password_a') );

my @content = (SOAP::Data->name(content=>'Hello World'));
my @route_request = (SOAP::Data->name(destination=>\SOAP::Data->value(@dest)),
SOAP::Data->name(request=>\SOAP::Data->value(@content)));

my $result = MessageRoutingService->RouteMessage(@route_request);
print $result->{content};

Sounds easy? There is a small catch here. The generated stub module needs a little tweak. Somehow it misses the URI namespace for the top method wrapper element .
my %methods = (
RouteMessage => {
endpoint => 'http://localhost:8080/common_router',
soapaction => 'http://www.simple.code/router/action/RouteMessage',
uri => '',
parameters => [
SOAP::Data->new(name => 'document', type => '', attr => {}),
],
},
);

Without the URI, the method in the request looks like
 <RouteMessage xmlns="">

which is obviously wrong. After I manually add the URI in the stub to make it look like
my %methods = (
RouteMessage => {
endpoint => 'http://localhost:8080/common_router',
soapaction => 'http://www.simple.code/router/action/RouteMessage',
uri => 'http://www.simple.code/router/action/RouteMessage',
parameters => [
SOAP::Data->new(name => 'document', type => '', attr => {}),
],
},
);

Now the request becomes
<RouteMessage xmlns="http://www.simple.code/router/action/RouteMessage">

and it is happily accpted by the Java Axis server.

Make Perl speak JMS (2)

Now the WSDL is ready, I can start to implement the WS server in Java. The tool of choice is Apache Axis 1.2.1.

With the wsdl2java tool, it is straight forward to generate the codes from the WSDL file. Because we will only use the server code, so we only need to change one class HttpBindingImpl.java.
private MessageRouter mRouter = new MessageRouter();

public code.simple.www.router._2005._11._7.RouteResponseType routeMessage
(code.simple.www.router._2005._11._7.RouteRequestType document)
throws java.rmi.RemoteException
{
try
{
return mRouter.routeMessage(document);
}
catch(Exception ee)
{
throw new RemoteException("Failed to route the message", ee);
}
}

Here we just forward the request to the MessageRouter class and let it handle the details.

Axis provides several different ways to deploy the web services. The most convenient way is to use JWS file but it has certain limitations. Here I choose to use the WSDD file and embed the Axis engine in the Tomcat servlet container. As of how to integrate Axis with a servlet container, I could not find a good documentation. Eventually I figured it out and here is the code
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException
{
PrintWriter pw = null;
try
{
pw = resp.getWriter();
resp.setContentType(CONTENT_TYPE);

String reqMsg = readContent(req.getReader());
mLogger.info("Got WS request:\n" + reqMsg);
MessageContext msgContext = new MessageContext(mAxisServer);
msgContext.setRequestMessage(new Message(reqMsg));
msgContext.setTargetService("MessageRouterPort");

String respMsgStr = null;
try
{
mAxisServer.invoke(msgContext);
respMsgStr = msgContext.getResponseMessage().getSOAPPartAsString();
}
catch (AxisFault fault)
{
Message respMsg = msgContext.getResponseMessage();
if (respMsg == null)
{
respMsg = new Message(fault);
((org.apache.axis.SOAPPart)respMsg.getSOAPPart()).
getMessage().setMessageContext(msgContext);
}
respMsgStr = respMsg.getSOAPPartAsString();
}

mLogger.info("Got WS response:\n" + respMsgStr);
pw.write(respMsgStr);
}
catch (Exception me)
{
mLogger.error(me);
throw new ServletException(me);
}
finally
{
if (pw != null)
{
pw.close();
}
}
}

public void init(ServletConfig cfg) throws ServletException
{
super.init(cfg);
EngineConfigurationFactory factory =
EngineConfigurationFactoryFinder.newFactory(cfg);
EngineConfiguration config = factory.getServerEngineConfig();
mAxisServer = new AxisServer(config);
}

The deployment descriptor for the servlet is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app >
<servlet>
<servlet-name>MessageRoutingServlet</servlet-name>
<servlet-class>code.simple.MessageRoutingServlet</servlet-class>
<init-param>
<param-name>servletId</param-name>
<param-value>defaultServlet</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>MessageRoutingServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Make Perl speak JMS (1)

To add JMS capability to Plum, I need to find a way to let Perl talk "JMS". Unfortunately there is no standard wire protocol for JMS. The JMS spec. defines the Java API but not the wire protocol. This is understandable as JMS is a Java centric system. Some JMS implementations, such as JBoss 3.x and 4.x, just use the Java serialization as their wire protocol. Others may use their propriate formats. This is an effort to define a generic XML protocol that can be utilized by different JMS implementations, but it is not there yet.

For now, a relatively simple way is to create a web service in Java which will route any requests to JMS server. Then one can make any lanaguage to speak JMS as long as they can consume the web service. Of course the limitation is that only text message will be supported.

Here is my try. I created a simple web service which is defined by following WSDL:
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="MessageRouter"
targetNamespace="http://www.simple.code/router.wsdl/2005/11/7"
xmlns:wsdlns="http://www.simple.code/router.wsdl/2005/11/7"
xmlns:router="http://www.simple.code/router/2005/11/7"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:stk="http://schemas.microsoft.com/soap-toolkit/wsdl-extension"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>
<xsd:schema targetNamespace="http://www.simple.code/router/2005/11/7"
elementFormDefault="qualified">

<xsd:complexType name="DestinationType">
<xsd:sequence>
<xsd:element name="protocol" type="xsd:string"/>
<xsd:element name="category" type="xsd:string"/>
<xsd:element name="server" type="xsd:string"/>
<xsd:element name="port" type="xsd:long"/>
<xsd:element name="target" type="xsd:string"/>
<xsd:element name="user" type="xsd:string" minOccurs="0"/>
<xsd:element name="password" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="MessageHeadType">
<xsd:sequence>
<xsd:element name="header" type="xsd:string" maxOccurs="unbound"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="MessageBodyType">
<xsd:sequence>
<xsd:element name="content" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="RouteRequestType">
<xsd:sequence>
<xsd:element name="destination" type="router:DestinationType"/>
<xsd:element name="head" type="router:MessageHeadType" minOccurs="0"/>
<xsd:element name="request" type="router:MessageBodyType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="RouteResponseType">
<xsd:sequence>
<xsd:element name="response" type="router:MessageBodyType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:element name="RouteRequest" type="router:RouteRequestType"/>
<xsd:element name="RouteResponse" type="router:RouteResponseType"/>

</xsd:schema>
</types>

<message name="RouteRequest">
<part name="document" element="router:RouteRequest"/>
</message>

<message name="RouteResponse">
<part name="document" element="router:RouteResponse"/>
</message>

<portType name="MessageRouterPortType">
<operation name="RouteMessage">
<input message="wsdlns:RouteRequest" />
<output message="wsdlns:RouteResponse" />
</operation>
</portType>

<binding name="HttpBinding" type="wsdlns:MessageRouterPortType">
<stk:binding preferredEncoding="UTF-8" />
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="RouteMessage">
<soap:operation
soapAction="http://www.simple.code/router/action/RouteMessage"
style="document"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>

<service name="MessageRoutingService">
<port name="MessageRouterPort" binding="wsdlns:HttpBinding">
<soap:address
location="http://localhost:8080/common_router" />
</port>
</service>

</definitions>

2006-01-25

PLUM is ripe

My little tool PerL based Utility for Messaging is almost done. It needs to be improved in several places, but it is actually usable now. It has following functions:

* send single request to server using different protocols, currently supports HTTP(Post) and JMS(Topic and Queue)
* compare response message with expected response
* Perl RE can be used for matching XML type response
* send a batch of requests and compare responses with expected responses

The utility is a web based application. I am thinking about adding a comand line tool which is more suitable for batch processing.

Added login for the Catalyst application

I tried to add login for my testing utility which is a Catalyst based web application last night. I finished the code by carefully following the examples provided with document. Not too bad, just a couple of lines and I was done. But the application complained:
Can't locate object method "session" via package "myapp" 
I went back and reviewed the code several times and could not figure out what was wrong. After hours of struggle, changing configurations, switching to different plugin modules, I finally got the reason. In my code, I used following statement to import several Catalyst plugins:
use Catalyst qw/Static::Simple  Session  Session::State::Cookie  Session::Store::File/;
use Catalyst qw/Authentication Authentication::Store::Minimal Authentication::Credential::Password/;

It seems that the later import (authentication related) overrode the previous import(session related). Thus the session method is missing because the module Session is not imported.

To fix this, just change the code to:
use Catalyst qw/-Debug Static::Simple Session
Session::State::Cookie Session::Store::File
Authentication Authentication::Store::Minimal
Authentication::Credential::Password
/;

2006-01-23

SemanticXmlDiff failed on Unicode

I am using CPAN module SemanticXmlDiff to compare two set of XML files. It complains about not being able to handle "wide character". After looking at the source code, it turns out the problem is the MD5 function used inside the module. The MD5 hash is used to spead up the comparson of text node in the XML file. However, MD5 only supports computing hash of ASCII characters, thus it will fail on wide characters which has multiple bytes.

The solution? According to the Digest::Digest module, the text should be encoded before it is passed to MD5 function. To apply this, I change the following line from
$doc->{"$test_context"}->{TextChecksum} = md5_base64("$text");

to
 $doc->{"$test_context"}->{TextChecksum} = md5_base64(encode_utf8("$text"));

Now it works!

2006-01-19

Configure Catalyst 5.61 with Apache2 on Windows

I reconfigured my Catalyst app using mod_perl today. It was configured using CGI before as I was making a lot of changes and would like to page to always reflect the last change. The original configuration is:
<VirtualHost *:80>
ServerAdmin my@server.com
DocumentRoot c:/site/myapp/root
ServerName myserver2
ErrorLog c:/site/myapp/myapp-error.log
CustomLog c:/site/myapp/myapp-access.log common
ScriptAliasMatch ^/(.+) c:/site/myapp/script/myapp_cgi.pl/$1
Alias /static c:/site/myapp/root/static
<Location "/static">
SetHandler none
</Location>
</VirtualHost>

It works but the performance is horrorable. The new configuration is:
<VirtualHost *:80>
ServerAdmin my@server.com
DocumentRoot c:/site/myapp/root
ServerName myserver2
ErrorLog c:/site/myapp/myapp-error.log
CustomLog c:/site/myapp/myapp-access.log common
PerlOptions +Parent
PerlSwitches -IC:/site/myapp/lib
<Location />
SetHandler modperl
PerlHandler myapp
</Location>
<LocationMatch "/static">
SetHandler none
</LocationMatch>
</VirtualHost>

2006-01-18

Use DBD: :Oracle 1.16 to access Oracle 9.2 database

I try to connect to an Oracle 9.2 database using following code:
 my $dbh = DBI->connect($config->{dburl}, $config->{dbuser}, $config->{dbpass});

It failed with error:
DBI connect('host=host;sid=dbsid','user',...) failed: ORA-12705: invalid or unknown NLS parameter value specified (DBD ERROR: OCISessionBegin) at ..

I have to explicitly set the ENV variable NLS_LANG to make it work. The new code looks like:
$ENV{NLS_LANG}="AMERICAN_AMERICA.UTF8";
my $dbh = DBI->connect($config->{dburl}, $config->{dbuser}, $config->{dbpass});

and it works.

The initial thought is that the default NLS_LANG from DBD::Oracle has to match the server configuration which is "AMERICAN_AMERICA.UTF8". But the follwoing code works as well:
$ENV{NLS_LANG}="AMERICAN_AMERICA.US7ASCII";
my $dbh = DBI->connect($config->{dburl}, $config->{dbuser}, $config->{dbpass});

So maybe the DBD:Oracle is sending some invalid NLS_LANG string though I havn't figured out what it is.

The SimpleDateFormat class is NOT threadsafe

In researching a defect in my project, I find SimpleDateFormat class is not thread safe. The "bug" is reported back in JDK 1.2 and JavaDoc is added in JDK 1.5. I just don't see it. :(

2006-01-14

Perl object is just a blessed hash

To work on a small testing utility, I have to pick up perl again. Fortunately my memory still served me well until I got into this problem. How to trun a hash into object. After a little reading of my Learning Perl book. I figured out how it works. Once you understand perl object is just a blessed hash, then the solution is clear.

============================
package MyTest;

use strict;

my %hash = (color=>"red", shape=>"round");
my $hashref = \%hash;

sub not_typical_new {
my ($proto, $arg) = @_;
my $class = ref($proto) || $proto;
my $self = $arg;
bless ($self, $class);
return $self;
}

my $obj = mytest->not_typical_new($hashref);
print $obj->{color}, "\n";

Added a nice JSCalendar to the blogger

I added a nice JSCalendar to my blog. The instructions from ecmanaut helped. The first part is relatively easy. But the seconf part is difficult to follow. I ended up have to dig into the source code to find out how it works. After a hour or so, I finally figured out the basic functions. The challenge is because I used a customized template so that many CSS tags used by the scripts are not included. I have to change the template to include al l those CSS tags just to make the calendar work. I still cannot get the title hint to work as it requires more template change which I am not willing to spend more time. So I stopped here. But it is good enough for me now.

The del.icio.us tag script does not get along with Google's tools bar

I added the del.icio.us greasemonkey script so that I can create the bookmark after the post. However, it stopped working today. After a search on google, I learned that the script, for some reason, does not work with Google's toolbar extension. After remove the Google's toolbar, it works just fine. :(

2006-01-13

Added the freshtag to the blog

I added the freshtag to my blog today. It allows me to use del.icio.us tag to categorize my blog.