Last active
February 22, 2018 23:21
-
-
Save SriniBlog/35fc27d5a969878d49e2 to your computer and use it in GitHub Desktop.
This is a Sample Java Mapping Program for SAP PI Tool. Tested and Works Fine!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***** Standard Java Libraries ********/ | |
import java.io.BufferedReader; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
/***** PI Specific Libraries *********/ | |
import com.sap.aii.mapping.api.AbstractTrace; | |
import com.sap.aii.mapping.api.AbstractTransformation; | |
import com.sap.aii.mapping.api.StreamTransformationException; | |
import com.sap.aii.mapping.api.TransformationInput; | |
import com.sap.aii.mapping.api.TransformationOutput; | |
public class JM_SampleJavaMapping_PI extends AbstractTransformation { | |
BufferedReader docInput; | |
String inputLine = ""; | |
String messageID = ""; | |
TransformationInput transInput = null; | |
TransformationOutput transOutput = null; | |
StringBuffer sourcePayload = new StringBuffer(); | |
StringBuffer destPayload = new StringBuffer(); | |
public static AbstractTrace trace; | |
public boolean traceon; | |
public JM_SampleJavaMapping_PI() throws Exception | |
{ | |
docInput = null; | |
} | |
public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException | |
{ | |
trace = (AbstractTrace) getTrace(); //Capture trace object and write trace for debugging purpose. | |
messageID = arg0.getInputHeader().getMessageId(); //Message ID only available if run from PI. | |
traceon = true; //make it false if wants to run the program locally. Make it true always before compiling the code and deployment. | |
if ( traceon ){ trace.addInfo("Java Mapping Program Started!"); }else{ System.out.println("Java Mapping Program Started!"); } | |
transInput = arg0; | |
transOutput = arg1; | |
this.execute((InputStream) arg0.getInputPayload().getInputStream(), (OutputStream) arg1.getOutputPayload().getOutputStream()); | |
if ( traceon ){ trace.addInfo("Java Mapping Program Completed!"); }else{ System.out.println("Java Mapping Program Completed!"); } | |
} | |
public void execute(InputStream in, OutputStream out) throws StreamTransformationException { | |
try { | |
if ( traceon ){ trace.addInfo("Inside the Execute Method!"); }else{ System.out.println("Inside the Execute Method!"); } | |
//Open the input Stream | |
try | |
{ | |
docInput = new BufferedReader(new InputStreamReader(in)); | |
} | |
catch (Exception e) | |
{ | |
throw new StreamTransformationException("err1: Unable to parse input document - ".concat(e.getMessage())); | |
} | |
//Read the data - The Data is available in inputData | |
try | |
{ | |
while ((inputLine = docInput.readLine()) != null) { | |
sourcePayload.append(inputLine); | |
} | |
}catch (Exception e){ | |
throw new StreamTransformationException("err2: Error in reading the payload - ".concat(e.getMessage())); | |
} | |
//Write the Data | |
try | |
{ | |
out.write(new String(sourcePayload).getBytes("UTF-8")); | |
} | |
catch(Exception e) { | |
throw new StreamTransformationException("err3: Error in Writing the payload back - ".concat(e.getMessage())); | |
} | |
out.flush(); //flush the stream | |
if (traceon){ trace.addInfo("Outside the Execute Method!"); }else{ System.out.println("Outside the Execute Method!"); } | |
} catch (Exception e) { | |
if( traceon ){ trace.addWarning("[Execute Method]**Error. " + e); }else{ System.out.println("[Execute Method]**Error. " + e); } | |
} | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// This method is executed only on the local running and no effect running from PI mapping program | |
try { | |
FileInputStream fin = new FileInputStream("inputfile.txt"); | |
FileOutputStream fout = new FileOutputStream("outputfile.txt"); | |
JM_SampleJavaMapping_PI instance = new JM_SampleJavaMapping_PI(); | |
instance.traceon = false; //This will help to print the output instead of trace. | |
instance.execute(fin, fout); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (StreamTransformationException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
/************************************************************************************************************ | |
//BONUS CODE: TO BINARY READ AND WRITE - GOOD WHEN NOTHING TO MASSAGE WITH DATA - FASTEST AS WELL | |
int buffer; | |
if ( traceon ){ trace.addInfo("Inside the Execute Method!"); }else{ System.out.println("Inside the Execute Method!"); } | |
//Read and Write back the file | |
while ((buffer = in.read()) != -1) | |
{ | |
out.write(buffer); | |
} | |
out.flush(); | |
if (traceon){ trace.addInfo("Outside the Execute Method!"); }else{ System.out.println("Outside the Execute Method!"); } | |
*************************************************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment