Skip to Content

                    

image

                    

In the above message mapping, we can see javac, jre, args,
java as tags. The javac contains the path of the compiler, javac.exe. The jre
contains the path of the java.exe. The args contains param as subelements,
which are the arguments to the java code. Finally, the java tag contains the
source code of the mapping program.

Java Functions

Below is the java function getParameter and Dyna_SAP_Compiler,
accepting the input.

image

                    

getParameter()

The all the param tags are made into a single context by a
custom java function getParameter(), which gives the output as arguments
separated by a space.

image

                    

Dyna_SAP_Compiler

image

                    

Java Code: Dyna_SAP_Compiler

   //write your code here

String output=””;

InputStream in=null;

     try {

String className = JavaCode.substring(JavaCode.indexOf(“class”) + 5, JavaCode.indexOf(“{“)).trim();

FileOutputStream f = new FileOutputStream(new File(“c:
“+ className + “.java”));

f.write(JavaCode.getBytes());

f.close();

String javac=javac_exe” -classpath c:
. c:
className + “.java -d c:
“;

String java=jre_exe” -classpath c:
. “
className” “arguments;

Runtime rt = Runtime.getRuntime();

Process p = rt.exec(javac);

in = p.getInputStream();

InputStreamReader isr = new InputStreamReader(in);

BufferedReader br = new BufferedReader(isr);

String line=””;

while ( (line = br.readLine()) != null){}

p.waitFor();

p = rt.exec(java);

in = p.getInputStream();

isr = new InputStreamReader(in);

br = new BufferedReader(isr);

output=””;

line=””;

while ( (line = br.readLine()) != null)

{

output=line“\n”;

}

p.waitFor();

     } catch (Exception e) {

     output = e.getMessage();

}

return output.substring(689);

image

                    

This shows the unbelievable flexibility of XI to have design time work (mapping)
at runtime (using Java code).

Note:


Using CDATA for java tag gave me some
error (unable to load map, but compiled successfully). Java code can be URL
encoded! After compiling, the result is always pre-occupied by 689 bytes (may
differ) and then comes the actual output result. The above program can be
modified in a way to stop repeated compilation if already compiled for
optimized results.

To report this post you need to login first.

17 Comments

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

    1. Bernd,
      Execution scripts are not possible if they where sent as string in messages without compilation and some code to execute because, even jsp(s) are compiled to java servlets finally and gets executed. Hence, compiler calls are really necessary.
      May be I might have misunderstood your idea. Please correct me if I am wrong.
      Regards,
      Felix
      (0) 
      1. Pran Bhas
        From a Java context: Not necessarily

        (1)Execution scripts can be ObjectStreams which can be deserialized at the target to an Object   (ObjectInput/OutputStream , Serializable APIs)

        (2) In addition to this instead of transmitting source code and then causing compilation and then invocation, the actual compiled class can itself be transmitted ,the Refection APIs can be used for runtime loading of class and method invocation ,provided the classes are neatly placed into the classpath and is available to the class loader.

        (0) 
        1. Pran Bhas,
          If you use external jars, you will be in serious trouble deploying or importing it as archieve, if you send it as serialized objects. In this case there is no need for you to send as serialized objects since its jar files are hard-coded (imported)! It has its own limitations!
          regards,
          Felix
          (0) 
            1. No! What I ment was, if you use external Jars in your program and got converted to serialized objects and not about accessing serialized objects. Assume if I use an external jar dom4j.jar, I can’t access it from serialized object unless I import or deploy it.
              (0) 
              1. Pran Bhas
                Similar disadvantages exist when executing Java at a process level , you only have access the output stream and error stream and you are put at a disadvantage of not able to manipulate at an object level utilise methods etc…

                There are tradeoffs in both techniques….

                (0) 
                1. Hi,
                  No! you still haven’t got it.

                  >>you only have access the output stream and error stream and you are put at a disadvantage of not able to manipulate at an object level utilise methods etc…

                  Since java source is on the message and it is independent of the java function. There is no need for me to access the compiled java code from the java function. If at all I need to access it, I can use a Class.for() to load the dynamically compiled class and use it! Hence, I believe there is no tradeoff in this technique. 🙂
                  regards,
                  Felix

                  (0) 
                    1. This is just an idea passing java code in payload where I see the flexibility of XI. I think I can improve this idea using the techniques suggested 🙂
                      (0) 
                          1. Sravya Talanki
                            It is definetly some thing new to learn.But I really fail to understand the need for such complicating mapping.We can develop java mappings in SAPNW studio and use them instead of doing like this.In the real time when we want to support/enhance the interface once the deployment is over.This approach will complicate the matter.I think experts like you only can mantain/enhance the code in this case..:)
                            Rather than all this they should integrate SAP NW studio with XI for using java mapping.That would be more useful in real time than this.
                            (0) 
                            1. Hi Sravya,

                              >>the need for such complicating mapping

                              It is not a complicating mapping. The whole idea is to send the java code for mapping itself in the message. For all others like adapters, modules, java functions, etc.. you predefine the mapping.

                              Assume you have different types of messages having different functionalities!? It is not possible to map one-by-one all of it. To avoid this, we go for other kinds of mapping say, java mapping/ abap mapping and xslt mapping. This is a step furthur. In this method you give the logic not on adapters/modules/java functions, rather, you give it in the message itself! This gives a total advantage of having completely a different logic for each message which can never be achieved in traditional methods!

                              regards,
                              Felix

                              (0) 
        2. Pran Bhas,
          The idea here is to have total independency from XI server and enable the end user to write his mapping code directly into the message and change it as frequently as he wants. But converting it to serialized objects, then to Base64 and creating the message is a huge pain!
          regards,
          Felix
          (0) 
      2. Bernd Eckenfels
        there are a few scripting solutions out there who run interpreted or can build byte code, they dont need a java compiler. with beanshell you can evaluate a string.

        Gruss
        Bernd

        (0) 
        1. Bernd,
          Yes, you are right!
          I think I can use beanshell to avoid java compilation in message mapping function. Thank you for your valuable suggestion. 🙂
          Regards
          Felix
          (0) 

Leave a Reply