Skip to Content

In some forum entries, I found a question about debugging Java Mapping. There are a lot of Blogs about Java Mappings like this one of Thorsten Søbirk:
Using JAXP to both parse and emit XML in XI Java mapping programs

Now, how should you debug your code before a upload it to the XI? It is quite simple: Add the method “main” to your Java Mapping class.

Here is a template for a Java Mapping class with the method “main”:

 

package sample;

import java.io.*;
import com.sap.aii.mapping.api.*;


public class MyJavaMapping extends AbstractTransformation {

  DynamicConfiguration conf; // used for dynamic configuration 
                             // (adapter-specific message attributes) 
  InputParameters param;     // used for input parameters


  // the method "transform" is called by Java Mapping Runtime
  public void transform (TransformationInput arg0, TransformationOutput arg1)  
                        throws StreamTransformationException {

    conf = arg0.getDynamicConfiguration(); // provide dynamic configuration 
    param = arg0.getInputParameters();     // provide input parameters
    this.execute(arg0.getInputPayload().getInputStream(), 
                 arg1.getOutputPayload().getOutputStream());

  }

  // the method "execute" is called by "transform" and "main"
  public void execute (InputStream in, OutputStream out)  
                      throws StreamTransformationException {


    // Add your code here

  }

  // reading dynamic values
  String getDynamicValue(String namespace, String name){

    if (conf == null) // in test mode, dynamic parameters cannot be used. 
      return "";      // Use default values instead
    DynamicConfigurationKey key = 
                 DynamicConfigurationKey.create(namespace, name);
    return conf.get(key);

  }
 
  // reading input parameters
  String getInputParameter(String name){

    if (param == null){  // in test mode, input parameters cannot be used. 
      return "";         // Use default values instead
    } else {
      return (String) param.getValue(name);
    }
  }

  // the method "main" is using files for input and output
  public static void main(String[] args) {

    try {
      InputStream in = new FileInputStream(new File("in.xml"));
      OutputStream out = new FileOutputStream(new File("out.xml"));
      MyJavaMapping myMapping = new MyJavaMapping();
      myMapping.execute(in, out);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

 

All you have to do is placing an xml file “in.xml” in the working directory of your Java program, set a breakpoint and start the program.

API Documentation

https://help.sap.com/doc/javadocs_pi_sp3_xpi/7.1.3/en-US/index.html

Additional Information

See also this Weblog for Debugging Java Mappings using SAP Netweaver Developer Studio

Examples for Java Mapping (scroll down to III-Java Mapping)

To report this post you need to login first.

7 Comments

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

  1. Morten Wittrock
    Hi Stefan

    If you want to unit test your Java mappings actually running inside the WAS, you can do so using Detroubulator, a unit test tool written by myself and Mr Thorsten Søbirk. More information here:

    http://www.applicon.dk/detroubulator

    The tool is open source (GPL license) and available for download today.

    Kind regards,

    Morten Wittrock

    (0) 
  2. Ram Nagireddy
    Hi stefhan,

    i tried your code and i am getting package not found. even though i imported the packages.  can you tell me how and where i need to copy the packages so as to avoid the above specified error

    cheers
    ram

    (0) 
  3. Mayur Patel
    Hello Stefan,

    This a great way to summarize two different, old and new, way of doing java mapping. On my project I am using new PI 7.1 API. Could you show me how do i access MessageID, Namespace, Service Interface, etc.

    Thanks!

    (0) 
  4. Bojidar Dimitrov
    Hi Stefan,

    great idea with the main method, but why do you have a try/catch statement in main() ?  Any why do you print the stack-trace on stdout?  Why not this much simpler and much cleaner way:

    public static void main(final String[] args)
      throws Exception
    {
      doSomethingWhichMightThrowAnException();
    }

    (0) 
    1. Stefan Grube Post author
      As you have stated in the headline: Everyone does this. I have copied the code from elsewhere.
      The result is the same, you can use both versions, whichever you prefer.

      Regards
      Stefan

      (0) 

Leave a Reply