Tuesday, July 20, 2010

ora:readFile() to read file in middle of a process

We always use file adapter to poll a file from a location but file adapter should be used only in case when the process has to initiate i.e. when the first activity is a receive activity from file adapter,but if you are using file adapter in the middle of the process then it is not a good design process.We should rather use the embedded xpath function ora:readFile().

This function take an argument which is the file name of the file to be read.We have to specify the full path till the file in this field.We will create a simple bpel process and see how does this function work in BPEL process.

Pre-requirement

SOA Suite 10g
Jdeveloper

Design
=========
Create a new Bpel process,Make this process asynchronous one.




Create a new variable of Message type same as inputprocessRequest as shown below



Drag and drop an assign activity in between and create a copy operation.




now i am using here ora:readFile() fucntion and in the filename i am providing the path location and the name of the file which
has to be processed.And i am assigning it to the temp variable which i have created in the previous step.


ora:readFile('file:///D:/input.txt')


Here there is one important point that forward slash is three times after file:


so if you will use only two slash or one slash you will get error something like this

XPath expression failed to execute.
Error while processing xpath expression, the expression is "ora:readFile('file://D:/input.txt')", the reason is message can't be null.
Please verify the xpath query.

So be careful in this ,Now once you complete this , deploy this project.





Since this is asynchronous so we need to invoke it .

Now in the input.txt file i have written arpit .

Now once you will invoke this process you will get the output.


Now if you will check the output in the console you will get something like this


<temp>
- <part xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="payload">
- <MidProcessProcessRequest xmlns="http://xmlns.oracle.com/MidProcess">

<input>

YXJwaXQ=

</input>

</MidProcessProcessRequest>

</part>
</temp>


We have provided arpit as text in input but got some encrypted value as result.

This happens because the string is in fact a base64 encoded version of the file.

so we need to change it to normal text.

Fot his purpose we will drag and drop a java activity after assign activity so now my process will look something like this.



Now go inside you java embed activity and give the following code



try{
Element node = (Element)getVariableData("temp","payload","/client:MidProcessProcessRequest/client:input");


String normal=node.getTextContent();
addAuditTrailEntry("Value Entered is : " + normal);


String decoded = Base64Decoder.decode(normal);




addAuditTrailEntry("Value Entered is : " + (String)(decoded));
setVariableData("outputVariable", "payload","/client:MidProcessProcessResponse/client:result",decoded);

}
catch(Exception exp){
System.out.println("Failed to decode rejected message using base64 " +
" encoding due to : " + exp);
}





Here we are decoding the value.

Once you have made the changes save this project and deploy this project again.


Now if you will check the output of embed activity you will find







addAuditTrailEntry("Value Entered is : " + normal); is fetching us the base64 encoded version of the file


and addAuditTrailEntry("Value Entered is : " + (String)(decoded)); is giving us the original value in the text file.

No comments: