File adapter is one of the most commonly used adapters in SAP PI. There are often real business scenarios where SAP PI is used to integrate different legacy systems which can support only file as an integration medium. The files which are meant to be sent to the 3rd party legacy system are generated by the standard built in file adapter.
However in such scenarios as explained above, there is often a requirement to archive these files which are generated by SAP PI. This could be because of various reasons e.g. SAP systems need to retain a copy of data (files) which are being sent to other systems for audit reasons etc.
The challenges in doing so are the following,
One of the solutions to solve this problem would be to create generic solution which can work across multiple platforms. The salient features of the solution are as follows,
The solution works as follows,
The Archiving utility is used by the receiver file adapter. This would make sure that a copy of the file is created in the Archive Folder.
In order to use the archiving utility, the following are the steps you need to follow.
As highlighted we would be dealing with two folder or directories.
Target Directory: This is the directory where the file is created by the standard file receiver adapter.
Archive Directory: This is the directory location where the file would be copied by the “Archiving Utility”.
Example:
Target Directory: /usr/sap/PX6/Interfaces/outbound/OBGL/Target
Archive Directory: /usr/sap/PX6/Interfaces/outbound/OBGL/Archive
Go to the Receiver File Communication Channel for the scenario where you want to use the Archiving Utility.
Fill in the following details,
In our example:
Target Directory: /usr/sap/PX6/Interfaces/outbound/OBGL/Target
File Name Scheme: OutputFile.csv
Fill in the following details,
Command Line:
/usr/sap/PX6/DVEBMGS30/exe/sapjvm_5/bin/java
-classpath /usr/sap/PX6/Interfaces/JavaArchiveUtitlity
-jar /usr/sap/PX6/Interfaces/JavaArchiveUtitlity/StandAloneArchiving.jar
%F %ArchiveFileLocation
Timeout (secs): 100
The above configuration is to run the “Archiving Utility” from the command line. The command to trigger any java jar application is as follows,
$> java - classpath [ class path ] – jar [ location of the java executable jar] [argument 1] [argument 2]
So using this understanding let’s analyze the above argument,
Location of the Java = /usr/sap/PX6/DVEBMGS30/exe/sapjvm_5/bin/java
[ class path ] = /usr/sap/PX6/Interfaces/JavaArchiveUtitlity
[ location of the java executable jar] =
/usr/sap/PX6/Interfaces/JavaArchiveUtitlity/StandAloneArchiving.jar
[argument 1] = %F
‘%F’ refers to the folder location where the file is being written by the file adapter
( Reference : See the Run Operating System Command Before/After Message Processing section)
[argument 2] = %ArchiveFileLocation
Variable substitution parameter refering to folder where the file needs to be archived. This can be filled dynamically.
Also enable the following parameters in the “Advanced” section,
In order to see the log created by the Archiving Utility, you have to go to the path:
Directory: /usr/sap/PX6/Interfaces/JavaArchiveUtitlity
Name: ArchivingTraceFile.trc
The same file can also be seen through AL11 when mapped. The file contain the details about
How the archiving tool logic works? It’s simple.
The source code to archive the files is as follows,
package com.sap.sudip.archiving;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ArchivingTool
{
static FileWriter fstream;
static BufferedWriter out;
static
{
try
{
fstream = new FileWriter("ArchivingTraceFile.trc", true);
out = new BufferedWriter(fstream);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
String strSourceDirectory = args[0];
String strAchiveDirectory = args[1];
File dir = new File(strSourceDirectory);
String[] children = dir.list();
try {
out.write("Source Directory" + children.length);
}
catch (IOException e1) {
e1.printStackTrace();
}
if (children != null)
{
for (int i = 0; i < children.length; i++)
{
String strFilename = children[i];
String[] filenameparts = strFilename.split("\\.");
String strArchiveFileName = null;
String strDateTime = getDateTime();
String strTimeStamp = getTimeStampFromDate(strDateTime);
if (filenameparts.length > 1)
strArchiveFileName = filenameparts[0] + strTimeStamp +
"." + filenameparts[1];
else {
strArchiveFileName = strFilename + strTimeStamp;
}
if (CopyFile.copyfile(strSourceDirectory + "//" + strFilename,
strReadyDirectory + "//" + strFilename, false)) {
try
{
out.write(strDateTime + " : SUCCESS : File copied :" +
strReadyDirectory + "//" +
strFilename + " from " +
strSourceDirectory + "//" + strFilename +
"\n");
}
catch (IOException e) {
e.printStackTrace();
}
}
if (!CopyFile.copyfile(strSourceDirectory + "//" + strFilename,
strAchiveDirectory + "//" + strArchiveFileName, true))
continue;
try {
out.write(strDateTime + " : SUCCESS : File copied :" +
strAchiveDirectory + "//" +
strArchiveFileName + " from " +
strSourceDirectory + "//" + strFilename +
"\n");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
try
{
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getDateTime()
{
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
private static String getTimeStampFromDate(String strDate) {
strDate = strDate.replace(":", "");
strDate = strDate.replace("/", "");
strDate = strDate.replace(" ", "");
return strDate;
}
}