Saturday 22 September 2012

How to call Shell Script in BPEL process using Java Embeed

Step 1:- Write Shell Script For addition of two number.
Step 2:- Create XSD which will contain two numbers.













<?xml version= '1.0' encoding= 'UTF-8' ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xmlns.oracle.com/ns/Numeric"
     xmlns:num="http://xmlns.oracle.com/ns/Numeric" attributeFormDefault="qualified" elementFormDefault="qualified">
    <element name="Numeric" type="num:NumericType"/>
    <complexType name="NumericType">
        <sequence>
            <element name="Num1" type="int"/>
            <element name="Num2" type="int"/>
                    </sequence>
    </complexType>
</schema>

Step 3:-Write a Java code to call shell script along with passing variable to script. Below code is written to open the script created in cygwin.

package com.test;

import java.io.*;
public class MyTest1 {
       
          public String ShellScript(int i, int j) throws IOException
          {

               String Var=null;
          try{
                Process P;
                System.out.println(i);
                System.out.println(j);
              String strParam = "D:\\cygwin\\bin\\bash -c '/cygdrive/d/testShell/SumOfTwoNumber.sh "
                                                     + i +" + "+ j+" '";
                                     P = Runtime.getRuntime().exec(strParam);

                          System.out.println("here " + strParam);
             BufferedReader br = new BufferedReader(new InputStreamReader(P.getInputStream()));
            String line=null;
            
              while ( (line = br.readLine()) != null){
                        System.out.println("Output" + ">" + line);
                  Var=line;
                        }
                System.out.println(line);
                System.out.println("Output of Var:-"  +Var);
             
            int wait = P.waitFor();
            System.out.println("exit code: "+wait);
               
            }
            catch (InterruptedException ie)
            {
              System.out.println("exception caught");
            }
            catch (Throwable t)
                  {
                    t.printStackTrace();
                  }
               return Var;
              
           }
For Script available in Bash or Corn Shell then need to modify line.
 String strParam = "bin\bash -c \SumOfTwoNumber.sh + i +" + "+ j+" ";
 String strParam ="bin\ksh -c \SumOfTwoNumber.sh + i +" + "+ j+" ";


Step4:- Create BPEL process to contain java embeed.

Modify BPEL source code to containg following below class
<bpelx:exec import="YourFullyQualifiedClassName(example com.oracle.shellscript)"/>

Java Embeed will contain following below code
MyTest1 pocob = new MyTest1();               
try{         
addAuditTrailEntry("Started From This Place");        
String i = ((oracle.xml.parser.v2.XMLElement)getVariableData("inputVariable","payload","/ns1:Numeric/ns1:Num1")).getFirstChild().getNodeValue();   
int k = Integer.parseInt(i);        
addAuditTrailEntry("k Variable " +k);        
String j = ((oracle.xml.parser.v2.XMLElement)getVariableData("inputVariable","payload","/ns1:Numeric/ns1:Num2")).getFirstChild().getNodeValue();           
int n = Integer.parseInt(j);        
addAuditTrailEntry("n Variable " +n);        
addAuditTrailEntry("Before Variable");        
String greetings = pocob.ShellScript(k,n);       
addAuditTrailEntry("After Variable");        
addAuditTrailEntry("Value:- " +greetings);        
setVariableData("Var1",greetings);     //Var1 is a variable created in BPEL process to contain result     
addAuditTrailEntry("After Assignment");        
}        catch(Exception io){                                                                         
      addAuditTrailEntry("Exception occured:"+io.getMessage());                                                          
      javax.xml.namespace.QName qName=new javax.xml.namespace.QName("http://schemas.oracle.com/bpel/extension","remoteFault");                                                                               
      com.oracle.bpel.client.BPELFault bpelFault=new com.oracle.bpel.client.BPELFault(qName);               
      throw bpelFault;                                
    }       
finally{        
pocob=null;   
  
}

Add following libraries in classpath.

Assign Var1 Variable to BPEL output variable.


Enjoy the testing.