Monday, September 17, 2007

Using CHRONOS Timescale Converter Service Calls

The timescale converter web application accessible through the CHRONOS portal (Ref: http://portal.chronos.org/gridsphere/gridsphere?cid=tools_tsconvert) uses publicly available web service calls to the timescale service described by the WSDL document located at http://services.chronos.org:9090/axis/timescales.jws?wsdl. These services are available for use on any platform.

Simple clients to these end points can be created in any modern programming language. Examples using Python and Groovy, two popular dynamic languages, are given below. Examples using Ruby or C# would be quite similar.

Example using Python

#!/usr/bin/python
#

import re, string
from SOAPpy import SOAPProxy

server = SOAPProxy("http://services.chronos.org:9090/axis/timescales.jws")
time = 20.0

while time < 40.0:

result = server.convertTime("GTS 2004", "Berggren 95",time)

print time , result

time = time + 0.1



Example using Groovy

import groovy.net.soap.SoapClient

def proxy = new SoapClient("http://services.chronos.org:9090/axis/timescales.jws?wsdl")


def serviceClosure = {

time -> return proxy.convertTime("GTS 2004", "Berggren 95", time)
}

for (float f = 20;f< 40.0; f=f+0.1) {
println serviceClosure(f)
}

A slightly more advanced example of a real-world application of these services is the implementation of the service in a Java-based web application server. In this case, a user would use e.g. the Xfire (http://xfire.codehaus.org/Client+and+Server+Stub+Generation+from+WSDL) or Apache Axis (http://ws.apache.org/axis/java/client-side-axis.html) packages to generate a library from the WSDL file. The resulting library could then be easily called from various locations in the application with only a few lines of code.

The first step would involve the creation of the stub classes in a WSDL-to-Java process. We will use the Apache Axis package in this example but the process is similar for Xfire or C# style environments. The initial stub classes are generated through a call like:

java -cp axis.jar:commons-logging-1.0.4.jar:commons-discovery-0.2.jar:axis-ant.jar:log4j-1.2.8.jar:wsdl4j-1.5.1.jar:jaxrpc.jar:saaj.jar org.apache.axis.wsdl.WSDL2Java -o . -d Session -p org.chronos.ws http://services.chronos.org:9090/axis/timescales.jws?wsdl

The result of this call is a set of Java source files that would then be compiled:

javac -classpath axis.jar:commons-logging-1.0.4.jar:commons-discovery-0.2.jar:axis-ant.jar:log4j-1.2.8.jar:wsdl4j-1.5.1.jar:jaxrpc.jar:saaj.jar org/chronos/ws/*.java

and the resulting class files collected into a jar file:

jar -cvf timescale.jar org/chronos/ws/*.class

The resulting jar file can then be used to greatly simplify the creation of clients in Java or any other Java byte code compatible language like Jruby, Jpython or Groovy.

An example of Java client that gets the color scheme for the Geological Time Scale is:

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

// Make a service
org.chronos.ws.TimescalesService service = new org.chronos.ws.TimescalesServiceLocator();

// Now use the service to get a stub to the service org.chronos.ws.Timescales_PortType ts = service.gettimescales();

// Make the actual call

System.out.println("call " + ts.getColorScales());

}

}


A Groovy client that uses the jar file (here using the batch convert method for time conversion) is:
import java.text.DecimalFormat

// Make a service

def org.chronos.ws.TimescalesService service = new org.chronos.ws.TimescalesServiceLocator();

// Now use the service to get a stub to the service

def org.chronos.ws.Timescales_PortType ts = service.gettimescales();

// Make the actual call

def batchResults = (ts.convertTimeBatch("GTS 2004", "Berggren 95", 0.toDouble(), 60.toDouble(), 1.toDouble()));


DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.000" );

// Closure for 2 place formating

def decf2 = {

value -> return new Double(df2.format(value)).doubleValue();

}

for (item in batchResults) {
println decf2(item)
}


These examples illustrate the process involved in the creation of clients using only the WSDL URL. Once created, jar libraries like those at the end of the process can be dropped into application server class paths and used in frameworks like Grails (http://www.grails.org), Seam ( http://www.jboss.com/products/seam) or JSR-168 portal environments like Gridsphere (http://www.gridsphere.org). Any application or tool with network access can invoke these services in a similar manner.

All CHRONOS services a similar pattern and can be utilized in web-based or stand-alone clients that have access to the network.

Tuesday, September 04, 2007

Grails SQL date formating lib

Been working with Grails more and created a little tag lib I thought I would post up for others. The date that comes out of the domains is a rather ugly SQL date style so I create a tag lib with the following code:

 def formatSqlDate = { attrs -> 
def String startdatetime = "${attrs['targetDate']}"
def DateFormat odf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
def DateFormat df = new SimpleDateFormat( "MMM d, ''yy" );
out << df.format(odf.parse(startdatetime))
}
Then I can simple reference it with:

<g:formatsqldate targetdate="${domain.dateObject}">

A person can mode the date format string all they want and also could put in various cases or a flag to select a format style if they wished.