Professional Documents
Culture Documents
This document may discuss sample coding or other information that does not include SAP
official interfaces and therefore is not supported by SAP. Changes made based on this
information are not supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information,
code or methods suggested in this document, and anyone using these methods does so at
his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect
to the content of this technical article or code sample, including any liability resulting from
incompatibility between the content within this document and the materials and services
offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable
with respect to the content of this document.
Step by step guide to develop a module
for reading file name in a sender file
adapter
Applies To:
SAP NW 04 XI 3.0
Article Summary
This is a step by step guide for developing, deploying and configuring a module exit to read
the file name in a sender file adapter. This module will get the file name and add it as an
element to the XML Payload
Table of Content
Step by step guide to develop a module for reading file name in a sender file adapter ........... 2
Applies To: ................................................................................................................................ 2
Article Summary........................................................................................................................ 2
Table of Content........................................................................................................................ 2
Introduction ............................................................................................................................... 3
Prerequisites ............................................................................................................................. 3
Step by Step procedure ............................................................................................................ 3
Copy the required JAR files from XI Server .............................................................................. 3
Set up class path variables in NW Developer Studio ............................................................... 4
Create an EJB module project .................................................................................................. 4
Create an EJB module bean..................................................................................................... 5
Implement the bean .................................................................................................................. 7
Edit the bean deployment descriptors ejb-jar.xml.................................................................. 9
Create bean archive................................................................................................................ 10
Create Enterprise application project...................................................................................... 10
Create references in the Enterprise Application Project ......................................................... 11
Build Application archive and deploy ...................................................................................... 12
Appendix ................................................................................................................................. 13
Disclaimer & Liability Notice.................................................................................................... 17
Introduction
An integration scenario involving a sender file adapter, it may be necessary to know the
name of the file which was processed. This is particularly true when the files are picked up
using * and the file name is to be used in the condition editor for receiver determination.
This can be accomplished by developing a module processor exit bean. Please read SAP
Help documentation for more information on Module Processor and extending the module
chain.
Prerequisites
The following JAR files are required to develop the module. The jar files need to be externally
referenced in the application. Copy these jars files from the provided location in the XI server,
to you local system.
<serverDir> -> If XI has been installed on Windows under drive D, then server directory will
look like : d:\usr\sap\<SID>\<INSTANCE>\j2ee\cluster\server0\.
<SID> -> System ID, <INSTANCE> -> J2EE Instance.
Example: d:\usr\sap\XI3\DVEBMGS00\j2ee\cluster\server0\
While saving the jar files in the local system (for e.g. c:\jarfiles), suggestion is to follow the
below pattern
C:\jarfiles\com.sap.aii.af.lib\aii_af_ms_spi.jar
C:\jarfiles\com.sap.xi.util.misc\aii_utilxi_misc.jar
Set up class path variables in NW Developer Studio
We need to set up a class path variable for referencing these .jar files in our application.
Open up NW Dev Studio and follow the below steps
The module we are going to develop is an EJB. Hence, in the NW Dev Studio, we need to
create an EJB module project first.
Choose File->New->Project. In the popup screen, choose J2EE. From the right frame,
choose, EJB Module Project.
Click .
Provide a project name. In our example, we will say SampleFileAdatperModule. Leave the
Check box Use Default checked. This will place all your developments in the default
workspace.
Click to confirm your entries.
This will create the project as shown below
Now we have to set up reference to the jar files in our project. To do this, choose the J2EE
Explorer view in your left frame if not already chosen and then select the project, right click
and choose Properties
In the popup window, choose Java build path. On the right frame, choose tab libraries.
Click on button
In the next window, choose SAP_USER_ADD_LIBS and click . Please note that
this is the variable we created in the previous steps.
Select the required jar files and click confirm all by clicking OK. This will create the build
paths. Make sure that all the jar files are included this way.
We now create the module bean under the EJB module project created above. Follow the
below steps for the same
Choose the folder ejbModule in the J2EE Explorer view and from the context menu (right
click), choose New->EJB.
In the next screen, provide the following information
EJB Name -> A name for your bean. For example: GetFileName
EJB Project -> SampleFileAdatperModule
Bean Type -> Stateless Session Bean
Default EJB Package -> Some arbitrary package name. For e.g. com.kk.FileModule.
Remove the check box generate default interfaces. We do this, because we are going to
have only a local interface for this bean.
Click Next
In the next screen, remove the check box on Remote Interfaces and click Next.
Below is provided, the code snippet for implementing the bean. The full code is provided in
the appendix.
Add a business method process(). This method will be called by the Module Processor.
public ModuleData process(ModuleContext moduleContext, ModuleData
inputModuleData) throws ModuleException {
Read the XML payload and message for getting the needed data.
Now, we have read the file name, we now have to get the XML payload and add this file
name at the appropriate place in the XML. For parsing and adding new element, we use
DOM.
try{
// Get the XML Payload
XMLPayload xmlpayload = msg.getDocument();
DocumentBuilderFactory factory;
factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// populate the Filename element with the value of the filename obtained from supplement
data
childElement.appendChild
(document.createTextNode(fileName));
Now, we have to transform our XML document in DOM stream to Byte stream so that it can
be assigned back to the XML payload.
// Set the content for xmlpayload and the principal data of ModuleData to be returned.
xmlpayload.setContent(docContent);
inputModuleData.setPrincipalData(msg);
}
You can add additional tracing information for error handling, which out of scope of this
document.
From the J2EE Explorer tab, double click on node ejb-jar.xml. On the right side pane, choose
tab source and replace the home, remote, local-home and local nodes as below
<enterprise-beans>
<session>
<ejb-name>GetFileNameBean</ejb-name>
<home>com.sap.aii.af.mp.module.ModuleHome</home>
<remote>com.sap.aii.af.mp.module.ModuleRemote</remote>
<local-home>com.sap.aii.af.mp.module.ModuleLocalHome</local-home>
<local>com.sap.aii.af.mp.module.ModuleLocal</local>
<ejb-class>com.kk.FileModule.GetFileNameBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
Now, we have to create an archive (.jar) for our bean. In the J2EE explorer, right click on the
EJB project (KKSampleAdapterModule) and choose option Build Archive
To deploy our bean, we need to create an enterprise application project. Follow the below
steps
Menu File -> New -> Project -> J2EE -> Enterprise Application Project
In the next screen, provide an arbitrary project name. In our example, we will give it a name
com.kk.FileModule.
The deployed application need to reference the libraries and services in the deployed J2EE
engine.
In the created enterprise application project, double click on the node application-j2ee-
engine.xml.
Repeat the above steps with the below values for other references
Now we have to build an .ear file and deploy the same to the J2EE engine.
Once the .ear file has been generated, right click on it and choose Deploy to J2EE Engine.
You may need the SDM password for deploying.
Please note that the application will be deployed to the engine provided under preferences in
your NW Developer Studio. To verify the same, follow the menu path Window -> Preferences
-> SAP J2EE Engine. On the right hand pane, check the entries. You can either provide a
local J2EE engine or a remote host.
Once successfully deployed, you should be able to use this bean in your communication
channel. In the communication channel, the module is referred as localejbs/JNDI Name. In
our case, it will be /sap.com/com.kk.FileModule/GetFileNameBean.
This module should be placed before the module localeejbs/CallSAPAdapter in the sender
file communication channel.
Appendix
package com.kk.FileModule;
// Classes for EJB
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**
* @ejbLocal <{com.kk.FileModule.GetFileNameLocal}>
* @ejbLocalHome <{com.kk.FileModule.GetFileNameLocalHome}>
* @stateless
* @transactionType Container
*/
public class GetFileNameBean implements SessionBean, Module {
} catch (Exception e) {
ModuleException me = new ModuleException(e);
throw me;
}
try{
XMLPayload xmlpayload = msg.getDocument();
DocumentBuilderFactory factory;
factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder =
factory.newDocumentBuilder();
Document document = builder.parse((InputStream)
xmlpayload.getInputStream());
Element rootNode =
document.getDocumentElement();
if(rootNode != null){
Element childElement =
document.createElement("FileName");
childElement.appendChild(document.createTextNode(fileName));
rootNode.appendChild(childElement);
if(docContent != null){
xmlpayload.setContent(docContent);
inputModuleData.setPrincipalData(msg);
} catch (Exception e) {
ModuleException me = new ModuleException(e);
throw me;
}
return inputModuleData;
}
}
Disclaimer & Liability Notice
This document may discuss sample coding, which does not include official interfaces and
therefore is not supported. Changes made based on this information are not supported and
can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing of the code and
methods suggested here, and anyone using these methods, is doing it under his/her own
responsibility.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect
to the content of the technical article, including any liability resulting from incompatibility
between the content of the technical article and the materials and services offered by SAP.
You agree that you will not hold SAP responsible or liable with respect to the content of the
Technical Article or seek to do so.
Copyright 2004 SAP AG, Inc. All Rights Reserved. SAP, mySAP, mySAP.com, xApps,
xApp, and other SAP products and services mentioned herein as well as their
respective logos are trademarks or registered trademarks of SAP AG in Germany and
in several other countries all over the world. All other product, service names,
trademarks and registered trademarks mentioned are the trademarks of their
respective owners.