Skip to Content

In our project there was a requirement in the Sender side, that, a check should be performed to detect the presence of a flag file in the application server of XI,if the flag file is present it should be deleted and the text file should be deleted as well as saved in an archive location as a zip file .

If the flag file is not present then the text file should not be deleted and raise the exception as flag file is not prenent on the server.

To create a customized Adpter Module the following are the prerequisites.

you have NWDS installed on your local machine. 

Now to create a Customized Adapter Module follow the following steps.

Step1. Open the NWDS.

step2. Go to Menu, select File and then click on New and then select Project as shown below.

image

Step3. A new window will be opened, select the option as J2EE in the left pane and the EJB Module Project in the Right pane of the window as shown below.

image

Step4. Click on Next and give the project name in my case i have given as SDNProject and click Finish as shown below. 

image

Step5. When you click on Finish a new project will be created in the J2EE window of the NWDS as shwon below.

image

Step6. Now next step is select the project and right click and select properties as shown below.

image

Step7. A new window will  be opened and select the option as Java Build Path in the left pane and select the Libraries Tab in the right pane of the window as shown below.

image

Step8. Now select the Add External Jars button and add the following jars into the project and click on OK.

aii_af_ms_spi.jar, aii_af_cci.jar, aii_af_ms_api.jar, aii_af_trace.jar, aii_af_mp.jar, aii_utilxi_misc.jar, aii_adapter_xi_svc.jar, aii_af_cpa.jar, aii_af_svc.jar, aii_util_xml.jar, ejb20.jar, exception.jar.

These all jar file you can download from the SAP Maketplace or from your XI server.

Step9. Now expand the whole project created and select the ejbmodule node and right click and select new package as shown below.

!https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN7.jpg|height=354|alt=image|width=400|src=https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN7.jpg|border=0

*Step10. Give the package name, in my case i have given as com.sap.module and click on Finish as shown below.</p><p>!https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN8.jpg|height=500|alt=image|width=565|src=https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN8.jpg|border=0! Now </p><p>Step11.* Now expand the ejbmodule node the package will be added in the project. 

Step12. Next Step, right click on the module node and select new EJB, a new window will be opened as shown below.

image

Step13. Give the EJB class name, in my case i have used SDN,select the bean type as Stateless Session Bean and click on Next, uncheck the Remote Interface CheckBox and click on Finish as shown below. 

image

image

Step14. Now expand the module node, three java classes will be generated as shown below.

!https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN12.jpg|height=230|alt=image|width=350|src=https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN12.jpg|border=0!  

Step15. Now double click on the first .java class and writh the following lines on code.In my case the logic for the adpter module will be write in the SDNBean.java class.

package com.sap.module;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import javax.ejb.CreateException;

import com.sap.aii.af.service.cpa.Channel;

/**

  • @ejbHome <{com.sap.aii.af.mp.module.ModuleHome}>

  • @ejbLocal <{com.sap.aii.af.mp.module.ModuleLocal}>

  • @ejbLocalHome <{com.sap.aii.af.mp.module.ModuleLocalHome}>

  • @ejbRemote <{com.sap.aii.af.mp.module.ModuleRemote}>

  • @stateless

  • @transactionType Container

*/

import java.io.*;

import java.util.Date;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import com.sap.aii.af.mp.module.*;

import com.sap.aii.af.ra.ms.api.Message;

import java.util.Hashtable;

import java.util.zip.*;

public class SDNBean implements SessionBean , Module {

     public void ejbRemove() {

     }

     public void ejbActivate() {

     }

     public void ejbPassivate() {

     }

     public void setSessionContext(SessionContext context) {

          myContext = context;

     }

     private SessionContext myContext;

     /**

  • Create Method.

      */

     public void ejbCreate() throws CreateException {

          // TODO : Implement

     }

     public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)

        throws ModuleException  {

          try

               {

               Object obj = null;                    

               Message msg = null;

               String fileName = null;

               String dirName = null;

               obj = inputModuleData.getPrincipalData();

               msg = (Message) obj;

               Hashtable mp = (Hashtable)

               inputModuleData.getSupplementalData(“module.parameters”);

               if (mp != null) fileName = (String)mp.get(“FileName”);

               Channel channel = new Channel(moduleContext.getChannelID());

               String filename = channel.getValueAsString(“file.sourceFileName”);

               Date d1 = new Date();

               DateFormat dateFormat = new SimpleDateFormat(“yyyyMMdd”);

               DateFormat dateFormat1 = new SimpleDateFormat(“hhmmss”);

               String date = dateFormat.format(d1);

               String time = dateFormat1.format(d1);

               String FileNameAvail = msg.getMessageProperty(“http://sap.com/xi/XI/System/File“,”FileName”);

               int a = FileNameAvail.lastIndexOf(“.”);

               int b = FileNameAvail.length();

               String str0 = FileNameAvail.substring(0,3);

               String str1 = FileNameAvail.substring(3,a-8);

               String FlagName = str0 + “FLAG” + str1 + date + “.flg”;

               String NewFileName = filename.substring(0,(a-8)) + date + FileNameAvail.substring(a,b);

               dirName = msg.getMessageProperty(“http://sap.com/xi/XI/System/File“,”Directory”);

               String Checkdir = CheckdirName(dirName);

               if (!Checkdir.equals(“true”))

               {

                 throw new Exception(FileNameAvail + “Directory is not available”);

               }

               msg.setMessageProperty(“http://sap.com/xi/XI/System/File“,”FileName”, NewFileName);

               String CheckArchivedir = CheckdirName(moduleContext.getContextData(“archivedir”));

               if (!CheckArchivedir.equals(“true”))

               {

               throw new IOException(“Archive Directory is not available”);

               }

               if(FileNameAvail.equals(NewFileName))

               {

                String check = CheckFlagFile(FlagName, dirName);

                 if (!check.equals(“true”))

                  {

                    throw new Exception(“Flag file is not available”);

                  }

                }

               else

               {

                 DeleteFile(FileNameAvail, dirName);

                 throw new Exception(fileName + ” has been already processed”);

               }

            String destination = moduleContext.getContextData(“archivedir”)+ “/” + FileNameAvail.substring(0,(a-8)) + date + “_”+ time + FileNameAvail.substring(a,b) ;

            String Source = dirName +”

” + FileNameAvail;

            String ZipNameDest = moduleContext.getContextData(“archivedir”) + “/” + FileNameAvail.substring(0,(a-8)) + date + “_”+ time + “.zip”;

           ZipArchiveFile(Source, ZipNameDest);

          }

          catch(StringIndexOutOfBoundsException e)

          {

           try

            {

               Object obj = null;                    

               Message msg = null;

               obj = inputModuleData.getPrincipalData();

               msg = (Message) obj;

               String FileNameAvail = msg.getMessageProperty(“http://sap.com/xi/XI/System/File“,”FileName”);

               String dirName = msg.getMessageProperty(“http://sap.com/xi/XI/System/File“,”Directory”);

               DeleteFile(FileNameAvail, dirName);

          }

          catch(Exception e1){e1.printStackTrace();}

     }                      

          catch(Exception ex)

          {

           ModuleException me = new ModuleException(ex);

           throw me;     

          }

          return inputModuleData;     

          }

public static void DeleteFile(String FileNameAvail, String dirName) throws Exception

                         {

                              try{

                                               String FlagfileLocation = dirName + “

” + FileNameAvail;

                                               File f1 = new File(FlagfileLocation);

                                               boolean success = f1.exists();

                                               if( success ){

                                                    f1.delete();

                                                               }

                                                else

                                                       {

                                                       throw new Exception(“File Deleted”);

                                                       }

                              }

                              catch (Exception e)

                                             {

                              ModuleException me = new ModuleException(e);

                              throw me;

                                             }                      

                         }

            public static String CheckdirName(String dirName)  //checking for the data file directory name

            {                                                                 // if not present the exception will be raised

                              File f1 = new File(dirName);

               boolean success = f1.exists();

               if( success ){

                    return “true”;

               }

               else return “false”;     

            }     

          public static String CheckFlagFile(String FlagName, String dirName)//checking for the flag file

               {                                                                             //if present delete the file

                 String FlagfileLocation = dirName + “

” + FlagName;            // if not present raise the exception.

                 File f1 = new File(FlagfileLocation);

                 boolean success = f1.exists();

                 if( success ){

                      f1.delete();

                      return “true”;

                 }

                      else return “false”;     

               }     

     public static void ZipArchiveFile(String Source, String destination)//function to archive the

     {                                                                                     //data file as a zip file.

          try {

                String inFilename  = Source;

                String outFilename = destination;

                FileInputStream in = new FileInputStream(

                  inFilename);

                ZipOutputStream out = new ZipOutputStream(

                  new FileOutputStream(outFilename));

                // Add ZIP entry to output stream.

                out.putNextEntry(new ZipEntry(inFilename));

                byte[] buf = new byte[1024];

                int len;

                  while ((len = in.read(buf)) > 0) {

                       out.write(buf, 0, len);

                  }

                out.closeEntry();

                out.close();

                in.close();

           } catch (IOException e) {

           }

     }

}

The code is also checking for the existence of the flag file, if the flag file will be present on the XI server the flag file will be deleted and the data file will also be deleted and archive the data file in the specified directory and if the flag file will not be present on the XI server the exception will be raised the flag file is not present on the server and no data file will be deleted as well no archive file will be generated.* </p><p>The code is also checking for the existence of the directory(channel as well as archive), if the directory will not be present the exception will be raised directory path not found.</p><p>Step16.* After writing the code in the SDNBean.java class save the file.

Step17. Now open the  ejb-j2ee-engine.xml file and add the following line of code after the     <jndi-name>SDNReceiver</jndi-name> and save the file.

image

Step18. Now open the ejb-jar.xml file remove the selected line of code in image as shown below.

image

and add the following line of code as shown below.

<home>com.sap.aii.af.mp.module.ModuleHome</home></p><p>*    <remote>com.sap.aii.af.mp.module.ModuleRemote</remote></p><p>   <local-home>com.sap.aii.af.mp.module.ModuleLocalHome</local-home></p><p>    </p><p>!https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN16.jpg|height=154|alt=image|width=350|src=https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN16.jpg|border=0! </p><p>Step19.* Save all the files and right click on the project and select BuildEJBArchive to create the .jar file as shown below.

image

The Created Jar file is shown below.

!https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN18.jpg|height=282|alt=image|width=350|src=https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN18.jpg|border=0

Step20. Next step is to create the Enterprise Application Project, to create the Enterprise Application Project select New>Project>EnterpriseAPplicationProject.

Step21. Give the name of the project and click on Next add the jar file project in the Enterprise Application project and click on Finish as shown below.

image

Step22. Open the application-j2ee-engine.xml file and add the references to the file and save the file.

Step23. Next step is to create the EAR file. To create the EAR file right click on the project and and select the option as Build Application Archive as shown below.

image

Step24. Deploy the created EAR file on the server.

Step25. Now to test the Adapter Module create a simple file to file scenario and add the following entries in the sender communication channel.

Step26. To use the Customized Sender Adapter module in the sender communication channel add the following in the module tab of the adapter as shown below.

In Processing Sequence:

image

In Module Configuration:

!https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN23.jpg|height=65|alt=image|width=400|src=https://weblogs.sdn.sap.com/weblogs/images/251827065/SDN23.jpg|border=0!</body>

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

    1. Chris Chris
      it’s not couple of lines and also to avoid confusion, he did good job, appreciate him.

      what is your problem in having like that?

      (0) 
      1. Alvaro Tejada Galindo
        The blogs are very similar…It’s like if I wrote two blog on READ_TEXT and SAVE_TEXT FM…Why use two blog when you can make just one showing both approaches?…

        Greetings,

        Blag.

        (0) 
  1. PRAVEEN KURNI
    Hi Rinku,
    very good effort in explaining the procedure, to create new modules for the adapters. Actually I’m trying to create similar kind of module that Zips all payloads in to single Zipfile. Because  standard SAP module payLoadZipBean doesnt serve the purpose.
    keep it up, good work.

    Best Regards
    Praveen K Kurni

    (0) 
  2. Karsten Geiseler
    Besides the fact, that this is grubby java coding, using the java.io package within EJBs violates the specification. This file access might be a bottleneck in terms of scalability and performance. File access is not transactional, there is no pooling or caching and concurrent access is restricted!
    (0) 

Leave a Reply