Tuesday Jul 21, 2009

Data Centric Rules in Java

In SailFin 1.0, user can provide Data Centric Rules(DCR) as xml file. User found it is difficult to define rules in xml file. Also flexibility was a major concern.

To solve above issues, in SailFinv2 user can provide DCR plugin as a java implementation. User can define rules as per their requirement in a very flexible manner.

This blog provides detail on how to write a DCR plugin in Java.


Functional Specification

For deep-dive design details on DCR plugin as jar file, user can refer to functional specification. It provides introduction to DCR Plugin, configuration and design details.


Developing DCR Plugin jar

Writing DCR plugin implementation

User implementation for DCR plugin must implement interface org.glassfish.comms.api.datacentric.DcrPlugin. A sample implementation of DCR plugin is provided below :

package mydcr;

import org.glassfish.comms.api.datacentric.DcrPlugin;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.sip.SipFactory;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipURI;
import org.glassfish.comms.api.telurl.TelUrlResolver;
import org.glassfish.comms.api.uriutils.UriTools;

public class MyDcrPluginImpl implements DcrPlugin{

    public String getKey(SipServletRequest request,
            SipFactory sipFactory, UriTools uriTools,
            TelUrlResolver telUrlResolver) {
        String key = null;
        String method = request.getMethod().toUpperCase();
        if(method.equals( "PUBLISH" ) || method.equals( "INVITE" ) || method.equals( "CANCEL" )){
           key = SipURI.class.cast(request.getTo().getURI()).getUser();
        } else if (method.equals( "REGISTER" )
                || method.equals( "SUBSCRIBE" )){
            key = SipURI.class.cast(request.getFrom().getURI()).getUser();
        }
        System.out.println("MY DCR PLUGIN - Key extracted from SIP request : " + key);
        return key;
    }

    public String getKey(HttpServletRequest request,
            SipFactory sipFactory, UriTools uriTools,
            TelUrlResolver telUrlResolver) {
        String key =  request.getParameter( "j_username" );
        System.out.println("MY DCR PLUGIN -
Key extracted from HTTP request : " + key);
        return key;
    }


}


Compiling DCR plugin

User need three jars to compile DCR plugin. These jars are ssa-api.jar, javaee.jar and comms-appserv-api.jar. They are available in SailFin installation. Below is command to compile DCR plugin :

javac -classpath <SAILFIN_INSTALL_PATH>/lib/ssa-api.jar:<SAILFIN_INSTALL_PATH>/lib/javaee.jar:<SAILFIN_INSTALL_PATH>/lib/comms-appserv-api.jar -d classes src/mydcr/MyDcrPluginImpl.java

Note: classes directory must exist. Compiled classes will reside in classes directory.

Writing Manifest file

Below is sample manifest file :

Manifest-Version: 1.0
Dcr-Plugin-Class: mydcr.MyDcrPluginImpl

Lets call this file as mymanifest

Creating DCR plugin jar

Command to create jar file using above manifest and compiled dcr plugin implementation class is provided below :

jar -cvfm mydcrplugin.jar mymanifest -C classes mydcr/MyDcrPluginImpl.class

Command to set this jar as DCR file

User can set DCR file using asadmin set-dcr-file command. One example of the command is provided below :

<SAILFIN_INSTALL_PATH>/bin/asadmin set-dcr-file --host <host-name> --port <port-number> --clbconfig  <clb-config-name> mydcrplugin.jar

User can set dcr file using admin GUI as well.


About

kshitiz

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today