Meta

FraSCAty Velocity

10 August, 2011 (10:56) | Technical | By Jérémie Guillemotte

What is Velocity :

Apache Velocity Engine is a free open-source templating engine. Velocity permits you to use a simple yet powerful template language to reference objects defined in Java code. It is written in 100% pure Java. You can find more information on the official Apache site : http://velocity.apache.org/

Velocity and FraSCAti (or vice versa)

Velocity is embedded in FraSCAti. We will see in this article how to deploy a little FraSCAti Velocity application.

How it’s works :

So we have made a little test project to show how Velocity can work with FraSCAti.

The composite file =>

<pre name="code" class="xml">
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0" xmlns:frascati="http://frascati.ow2.org/xmlns/sca/1.1" name="test">
  <component name="c">
    <implementation.velocity xmlns="http://frascati.ow2.org/xmlns/web/1.0" default="index.html" location="contents"/>
    <!-- Velocity Binding -->
    <service name="Velocity">
      <binding.http xmlns="http://tuscany.apache.org/xmlns/sca/1.0" uri="/pages"/>
    </service>
    <!-- TEST Reference -->
    <reference name="xsltservice">
      <interface.java interface="com.openwide.easysoa.frascati.velocity.XSLTService"/>
      <frascati:binding.rest uri="http://api.twitter.com"/>
    </reference>
  </component>
</composite>

We declare here an sca component that use an implementation velocity tag. In this tag, it is possible to declare the default page to display and the directory where to get this page.
In addition, we declare a service where we define the endpoint where the http service will be published.
Finally, we add a reference to a well known internet service : Twitter. This reference needs a java interface and a REST Binding.

In the Maven pom file, don’t forget to include FraSCAti dependencies, especially the frascati-implementation-velocity dependency :

Maven pom file =>

<dependency>
    <groupId>org.ow2.frascati</groupId>
    <artifactId>frascati-implementation-velocity</artifactId>
    <version>1.5-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.ow2.frascati</groupId>
    <artifactId>frascati-binding-factory</artifactId>
    <version>1.5-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.ow2.frascati</groupId>
    <artifactId>frascati-implementation-fractal</artifactId>
    <version>1.5-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.ow2.frascati</groupId>
    <artifactId>frascati-binding-http</artifactId>
    <version>1.5-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.ow2.frascati</groupId>
    <artifactId>frascati-binding-rest</artifactId>
    <version>1.5-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.ow2.frascati</groupId>
    <artifactId>frascati-runtime-factory</artifactId>
    <version>1.5-SNAPSHOT</version>
</dependency>

XSLTService interface file =>

package com.openwide.easysoa.frascati.velocity;

import javax.ws.rs.*;

public interface XSLTService {
    @GET
    @Path("/1/trends.json")
    String getTrends();
}

In this interface, we declare the Twitter service to use. All the handling code will be generated by FraSCAti. The only thing to do here is to declare the method to call and some informations like the REST method to use and the path where the service can be accessed. The result of the Twitter service will be returned as a String.

Java test class =>

package com.openwide.easysoa.frascati.velocity;

import java.io.IOException;
import java.net.URL;
import org.apache.log4j.Logger;
import org.ow2.frascati.FraSCAti;
import org.ow2.frascati.assembly.factory.processor.ProcessingContextImpl;
import org.ow2.frascati.util.FrascatiException;
import org.junit.Test;
import org.junit.Before;

/**
* Unit test for simple App.
*/
public class VelocityTest {

    /**
    * Logger
    */
    private static Logger logger = Logger.getLogger(VelocityTest.class.getClass());

    /** The FraSCAti platform */
    private static FraSCAti frascati;

    // Set system properties for FraSCAti
    static {
        System.setProperty("org.ow2.frascati.bootstrap", "org.ow2.frascati.bootstrap.FraSCAti");
    }

    /**
    * Init the remote systems for the test
    * Frascati and HTTP Proxy
    * Instantiate FraSCAti and retrieve services.
    * @throws InterruptedException
    */
    @Before
    public final void setUp() throws FrascatiException, InterruptedException {
        // Start fraSCAti
        startFraSCAti();
        // Start Velocity test
        startTestComposite();
    }

    /**
    * Wait for an user action to stop the test
    * @throws ClientException
    * @throws SOAPException
    * @throws IOException
    */
    @Test
    public final void testWaitUntilRead() throws Exception{
        logger.info("Velocity test started, wait for user action to stop !");
        // Just push a key in the console window to stop the test
        System.in.read();
        logger.info("Velocity test stopped !");
    }

    /**
    * Start FraSCAti
    * @throws FrascatiException
    */
    private static void startFraSCAti() throws FrascatiException{
        frascati = FraSCAti.newFraSCAti();
    }

    /**
    * Start Velocity Test
    * @throws FrascatiException
    */
    private static void startTestComposite() throws FrascatiException{
        URL compositeUrl = ClassLoader.getSystemResource("frascati-velocity-test.composite") ;
        frascati.processComposite(compositeUrl.toString(), new ProcessingContextImpl());
    }

}

In this class, the job is to create and launch a FraSCAti engine, load the composite file to start the velocity test application and wait for an user action to stop the test.

And to finish, the index.html file =>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Frascati Velocity Test</title>
  </head>
  <body>
    <p>Working !</p>
    <p>#set( $email = "moc.tsetnull@oof" ) Email : $email</p>
    <p>Test Parameter : $testParam</p>
    <p>
      Requesting Twitter service : http://api.twitter.com/1/trends.json
      <br/>
      Twitter test response : <br /> $xsltservice.getTrends()
    </p>
  </body>
</html>

So in this html file, we can see three things :

  • First, it is possible to set a ‘$email’ velocity variable and display it.
  • Second, it is very simple to get a ‘testParam’ URL parameter.
  • Third, we have a direct access to the referenced object ‘xsltservice’ which has been declared in the sca composite file. FraSCAti does the job for us.

How to run :

Open a console and type the following Maven command : mvn install
This command will build the project and launch the test.

How to test :

Open your favorite Web Browser and go to the address : http://localhost:8090/pages/index.html?testParam=test

If all works well, you can see that the index.html page is displayed :

Working !

Email : moc.tsetnull@oof

Test Parameter : test

Requesting Twitter service : http://api.twitter.com/1/trends.json
Twitter test response :
{“trends”:[{"name":"#replacebandnameswithlesbian","url":"http:\/\/twitter.com\/search\/%23replacebandnameswithlesbian"},{"name":"#WhyAreYou","url":"http:\/\/twitter.com\/search\/%23WhyAreYou"},{"name":"#londonriots","url":"http:\/\/twitter.com\/search\/%23londonriots"},{"name":"JUSTIN WON","url":"http:\/\/twitter.com\/search\/%22JUSTIN%20WON%22"},{"name":"My Lesbian Romance","url":"http:\/\/twitter.com\/search\/%22My%20Lesbian%20Romance%22"},{"name":"Who Gon Stop Me","url":"http:\/\/twitter.com\/search\/%22Who%20Gon%20Stop%20Me%22"},{"name":"No Church In the Wild","url":"http:\/\/twitter.com\/search\/%22No%20Church%20In%20the%20Wild%22"},{"name":"Gotta Have It","url":"http:\/\/twitter.com\/search\/%22Gotta%20Have%20It%22"},{"name":"Lift Off","url":"http:\/\/twitter.com\/search\/%22Lift%20Off%22"},{"name":"TCA","url":"http:\/\/twitter.com\/search\/TCA"}],”as_of”:”Mon, 08 Aug 2011 09:51:34 +0000″}

Some informations are displayed, you can see the test parameter included in the test url and the result of the request made on the Twitter service.

Conclusion :

It is quite easy to use Velocity in FraSCAti. The complete sources of this little test project can be downloaded at : http://github/easysoa/samples/frascati-velocity-sample.

You can find more informations about FraSCATi at http://wiki.ow2.org/frascati/Wiki.jsp?page=FraSCAti

Next step with FraSCAti and Velocity in EasySOA will be to make a generic UI Scaffolder proxy.

Write a comment





Couldn't connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)