Showing posts with label Call Shell Script. Show all posts
Showing posts with label Call Shell Script. Show all posts

Friday, September 11, 2020

12c SOA - How to call a shell script from BPEL using java embedding

 Implementation steps:

Step 1: Create a SOA project with empty composite.

Step 2: Create an XSD.

<?xml version="1.0" encoding="windows-1252" ?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org"

            targetNamespace="http://www.example.org" elementFormDefault="qualified">

  <xsd:element name="Input">

    <xsd:complexType>

      <xsd:sequence>

        <xsd:element name="num1" type="xsd:string"/>

        <xsd:element name="num2" type="xsd:string"/>

      </xsd:sequence>

    </xsd:complexType>

  </xsd:element>

    <xsd:element name="Output">

    <xsd:complexType>

      <xsd:sequence>

        <xsd:element name="result" type="xsd:string"/>

      </xsd:sequence>

    </xsd:complexType>

  </xsd:element>

</xsd:schema>


Step 3: Create a Synchronous BPEL service using that schema exposing as a web service.

Step 4: Create a Shell script file and saved in UNIX directory.

Test.sh:

#!/usr/bin/bash

num1=$1

num2=$2

function add(){

sum=`expr $num1 + $num2`

echo "$sum"

return $sum

}

add $num1 $num2


Step 5 : Create a Java class(Right click on project⇾ New Galary⇾ Class(Java))

Test.java:

package invokeshellscriptproject;

import java.io.*;

public class Test {

          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 = "bash /soashare/shellscript/test.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;

           }

}


Step 6: Create a string variable Var1.

Step 7:  Drag and drop a Java embedding activity in the BPEL.

Java_Embedding2:

Test pocob = new Test();               

try{         

addAuditTrailEntry("Started From This Place");        

String i = ((oracle.xml.parser.v2.XMLElement)getVariableData("inputVariable","payload","/ns1:Input/ns1:num1")).getFirstChild().getNodeValue();    

int k = Integer.parseInt(i);        

addAuditTrailEntry("k Variable " +k);        

String j = ((oracle.xml.parser.v2.XMLElement)getVariableData("inputVariable","payload","/ns1:Input/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;   

}


Step 8 : Import the java class in the BPEL. location used as packageName.ClassName

<import location="invokeshellscriptproject.Test" importType="http://schemas.oracle.com/bpel/extension/java"/>

Step 9 : Assign Var1 to result variable.

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...