This post will illustrate WSO2 ESB XSLT Transform Mediator in theoretical mode and also with sample codes.
When we need XSLT Mediator is need in wso2 ESB?
When we have to deal with dynamic request for ESB proxy. (If proxy request is dynamic (not static/not predefine) we will have to use XSLT mediator to mediator the message)
What is XSLT Mediator ?
The XSLT Mediator applies a specified XSLT transformation to a selected element of the current message payload. The source attribute specifies which element to have XSLT transformation. The feature element defines in to TransformerFactory.
If source element is not specified then soap body as the selected element. Parameters can be passed into the transformations through the property elements and it is optional and those can be accessed during transformation by <xsl:param name="the name of the property"/>.
What is the WSO2 ESB XSLT Mediator Syntax?

<xslt key="string" [source="xpath"]>
     <property name="string" (value="literal" | expression="xpath")/>*
     <feature name="string" value="true| false" />*
     <resource location="string" key="string"/>*
</xslt>

  • Source - Specifies which element to be selected to apply the given XSLT transformation.
  • Property - Allows optional parameters to be passed into the transformations.
  • Feature - Defines any features which should be explicitly set to the TransformerFactory.
  • Resource - Resolves XSLT imports and includes from the repository.
Here are some thing that I will be using in XSLT script
The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.[1]
omit-xml-declaration   
values : yes OR no    Optional.
"yes" specifies that the XML declaration (<?xml...?>) should be omitted in the output. "no" specifies that the XML declaration should be included in the output. The default is "no"
indent 
values:  yes OR no    Optional.
"yes" indicates that the output should be indented according to its hierarchic structure. "no" indicates that the output should not be indented according to its hierarchic structure.
This attribute is not supported by Netscape 6[2]
Let Do sample
We can try our Transport Services (BusServices)[3]
Here Is the Expecting request
<sample:getBusNo xmlns:sample="http://transport.org">
      <xs:rootId xmlns:xs="http://transport.org">2</xs:rootId>
   </sample:getBusNo>
Back end Serive expecting request
<p:getBusNo xmlns:p="http://transport.org">
<xs:rootId xmlns:xs="http://transport.org">2</xs:rootId>
</p:getBusNo>
1. Start BusServices in WSO2 AS
2. Start WSO2 ESB in offset 1
3. Go to Home > Manage > Service Bus > Local Entries in ESB
4. Then add Add In-lined XML Entry
5. Five name as "in_xslt" and Value as below (transform.xsl file contains)
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
                xmlns:xs="http://transport.org"
                xmlns:sample="http://transport.org"
                exclude-result-prefixes="sample fn">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates select="//sample:getBusNo"/>
    </xsl:template>
    <xsl:template match="sample:getBusNo">
<p:getBusNo xmlns:p="http://transport.org">
      <xs:rootId xmlns:xs="http://transport.org">
<xsl:value-of select="xs:rootId"/>
</xs:rootId>
   </p:getBusNo>
    </xsl:template>
</xsl:stylesheet>
6. Then create Proxy with XSLT
<proxy xmlns="http://ws.apache.org/ns/synapse" name="testProxyXSLT" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <xslt key="in_xslt"/>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
      <endpoint>
         <address uri="http://localhost:9763/services/BusServices"/>
      </endpoint>
   </target>
   <publishWSDL uri="http://10.100.2.150:9763/services/BusServices?wsdl"/>
   <description></description>
</proxy>                              

image
Now I need to change my respond 
from:
<ns:getBusNoResponse xmlns:ns="http://transport.org">
   <ns:return>4Colombo</ns:return>
   <ns:return>Negombo</ns:return>
   <ns:return>Galle</ns:return>
</ns:getBusNoResponse>
to:
<ns:getBusNoResponse xmlns:ns="http://transport.org">
<ns:rootName>2Colombo</ns:rootName>
<ns:rootName>Negombo</ns:rootName>
<ns:rootName>Galle</ns:rootName>
</ns:getBusNoResponse>
Here is XSLT Script for that
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
                xmlns:ns="http://transport.org"
                exclude-result-prefixes="ns fn">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates select="//ns:getBusNoResponse"/>
    </xsl:template>
    <xsl:template match="ns:getBusNoResponse">
<ns:getBusNoResponse xmlns:ns="http://transport.org">
 <xsl:for-each select="ns:return">    <ns:rootName> <xsl:value-of select="."/> </ns:rootName>
</xsl:for-each> </ns:getBusNoResponse>
    </xsl:template>
</xsl:stylesheet>
image
Add it to the out sequences in proxy
image
Here is Final testing
image
image

NOTE
If any case If I get empty String in RootID I am adding new RootName
<xsl:choose>
    <xsl:when test=".!= ''">
         <rootNmae><xsl:value-of select="."/></rootNmae>
       
    </xsl:when>
    <xsl:otherwise>
       <rootNmae>Root<xsl:text> </xsl:text>text</rootNmae>
    </xsl:otherwise>
</xsl:choose>

references
[1] http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_apply
[2] http://www.w3schools.com/xsl/el_output.asp
[3] http://madhukaudantha.blogspot.it/2013/05/wso2-esb-filter-mediator-tutorial.html
0

Add a comment

We used have  Singleton Design Pattern in our applications whenever it is needed. As we know that in singleton design pattern we can create only one instance and can access in the whole application. But in some cases, it will break the singleton behavior.

There are mainly 3 concepts which can break singleton property of a singleton class in java. In this post, we will discuss how it can break and how to prevent those.

Here is sample Singleton class and SingletonTest class.

Singleton.Java

package demo1;

public final class Singleton {

    private static volatile Singleton instance = null;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

SingletonTest.java


package demo1;

public class SingletonTest {
    public static void main(String[] args) {
        Singleton object1 = Singleton.getInstance();
        Singleton object2 = Singleton.getInstance();
        System.out.println("Hashcode of Object 1 - " + object1.hashCode());
        System.out.println("Hashcode of Object 2 - " + object2.hashCode());
    }
}

Here is output, you can see it the same hashcode for objectOne and objectTwo

Hashcode of Object 1 - 1836019240
Hashcode of Object 2 - 1836019240

Now we will break this pattern. First, we will use java reflection.

Reflection

Java  Reflection is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime. Using Reflection API we can create multiple objects in singleton class. Consider the following example.

ReflectionSingleton.java

package demo1;

import java.lang.reflect.Constructor;

public class ReflectionSingleton {
    public static void main(String[] args)  {

        Singleton objOne = Singleton.getInstance();
        Singleton objTwo = null;
        try {
            Constructor constructor = Singleton.class.getDeclaredConstructor();
            constructor.setAccessible(true);
            objTwo = (Singleton) constructor.newInstance();
        } catch (Exception ex) {
            System.out.println(ex);
        }

        System.out.println("Hashcode of Object 1 - "+objOne.hashCode());
        System.out.println("Hashcode of Object 2 - "+objTwo.hashCode());

    }
}

Example to show how reflection can break the singleton pattern with Java reflect. You will get two hash code as below. It has a break on the singleton pattern.

Hashcode of Object 1 - 1836019240
Hashcode of Object 2 - 325040804

Prevent Singleton pattern from Reflection

There are many ways to prevent Singleton pattern from Reflection API, but one of the best solutions is to throw run time exception in the constructor if the instance already exists. In this, we can not able to create a second instance.

    private Singleton() {
        if( instance != null ) {
           throw new InstantiationError( "Creating of this object is not allowed." );
        }
    }

Deserialization

In serialization, we can save the object of a byte stream into a file or send over a network. Suppose if you serialize the Singleton class and then again de-serialize that object will create a new instance, hence deserialization will break the Singleton pattern.

Below code is to illustrate how the Singleton pattern breaks with deserialization.

Implements Serializable interface for Singleton Class.

DeserializationSingleton.Java

package demo1;

import java.io.*;

public class DeserializationSingleton {

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

        Singleton instanceOne = Singleton.getInstance();
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream("file.text"));
        out.writeObject(instanceOne);
        out.close();

        ObjectInput in = new ObjectInputStream(new FileInputStream("file.text"));
        Singleton instanceTwo = (Singleton) in.readObject();
        in.close();

        System.out.println("hashCode of instance 1 is - " + instanceOne.hashCode());
        System.out.println("hashCode of instance 2 is - " + instanceTwo.hashCode());
    }

}
The output is below and you can see two hashcodes.

hashCode of instance 1 is - 2125039532
hashCode of instance 2 is - 381259350

Prevent Singleton Pattern from Deserialization

To overcome this issue, we need to override readResolve() method in Singleton class and return same Singleton instance. Update Singleton.java, with below method.

   protected Object readResolve() { 
           return instance; 
     }  

Now run above DeserializationDemo class and see the output.

hashCode of instance 1 is - 2125039532
hashCode of instance 2 is - 2125039532

Cloning

Using the "clone" method we can create a copy of original object, samething if we applied clone in singleton pattern, it will create two instances one original and another one cloned object. In this case will break Singleton principle as shown in below code.

Implement the "Cloneable" interface and override the clone method in the above Singleton class.

Singleton.java


    @Override
    protected Object clone() throws CloneNotSupportedException  {
        return super.clone();
    }

Then Test with cloning for breaking the singleton
CloningSingleton.java


public class CloningSingleton {
    public static void main(String[] args) throws CloneNotSupportedException, Exception {
        Singleton instanceOne = Singleton.getInstance();
        Singleton instanceTwo = (Singleton) instanceOne.clone();
        System.out.println("hashCode of instance 1 - " + instanceOne.hashCode());
        System.out.println("hashCode of instance 2 - " + instanceTwo.hashCode());
    }

}

Here is the output

hashCode of instance 1 - 1836019240
hashCode of instance 2 - 325040804

If we see the above output, two instances have different hashcodes means these instances are not the same.


Prevent Singleton Pattern from Cloning

In the above code, breaks the Singleton principle i. e created two instances. To overcome the above issue we need to implement/override clone() method and throw an exception CloneNotSupportedException from clone method. If anyone try to create clone object of Singleton, it will throw an exception as see below code.

    @Override
    protected Object clone() throws CloneNotSupportedException  {
        throw new CloneNotSupportedException();
    }

Now we can run the CloningSingleton class, it will throw CloneNotSupportedException while creating a clone object of Singleton object.


13

View comments

I am
I am
Archives
Total Pageviews
Total Pageviews
2 0 5 6 1 8 8
Categories
Categories
Loading