ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » News/Updates » Java WMQ Exits using AOP

Post new topic  Reply to topic
 Java WMQ Exits using AOP « View previous topic :: View next topic » 
Author Message
JLRowe
PostPosted: Thu Jun 17, 2004 10:27 am    Post subject: Java WMQ Exits using AOP Reply with quote

Yatiri

Joined: 25 May 2002
Posts: 664
Location: South East London

An interesting question came up the other day, someone wanted to perform encyption of MQ messages, by encrypting them before they are placed on the queue and into persitent storage. A set of wrapper classes would do the job, but J2EE and message driven beans would ignore them. The easiest solution would be API exits written in java if such a thing existed. We could write MQPUT and MQGET exits in C and port them and support on each platform. Or, we could purchase a product, but again it would have to be supported on each different platform.

The other way is to used AOP, and specifically AspectJ which is the eclipse based AOP project for java. AOP excels at this kind of problem, we have some classes that we want to augment with additional functionality. Specifically, we want to intercept mqput and mqget calls, as they are in transit so we can get the message, encrypt it, then put it back and continue with the method call without having to change the code of the caller and callee.

This is the base java class PutTest, which simply places a message on the queue TEST1 on queue manager Development:

Code:

package test;

import com.ibm.mq.*;

public class PutTest {

   public static void main(String[] args) {
      System.out.println("Start...");
      
      try {
         MQQueueManager mqQueueManager = new MQQueueManager("Development");
         MQQueue queue = mqQueueManager.accessQueue("TEST1", MQC.MQOO_OUTPUT);
         MQMessage message = new MQMessage ();
         message.writeString("pre-scrambled content");
         System.out.println("Just before put()...");
         queue.put(message);
         System.out.println("Just after put()...");
         queue.close();
      
      } catch (Exception e) {
         e.printStackTrace();
      }
      
   }
}


If we run this code, we get the following result and our message in the queue:

Code:

Start...
Just before put()...
Just after put()...


This is our code that intercepts the mqput call:

Code:

package test;

import com.ibm.mq.*;

/**
 * Aspect MessageEncryption : An aspect to ensure our MQ messages are encrypted, by intercepting calls to mqput()
 */
public aspect MessageEncryption {

   /**
    * Static method to encrypt/decrypt a string by reversing the String
    */
   static String encryptString(String in) {
       StringBuffer encryptedText = (new StringBuffer(in)).reverse();
       return encryptedText.toString();
   }

   /**
    * This pointcut callPointCut() represents all the places in the code where
    * the method MQQueue.put is executed in the all classes
    */
   pointcut mqPutPointcut(): execution(void MQQueue.put(MQMessage));
   
   /**
    *  Our advice runs just before the called method (MQQueue.put) is executed
    */
   before(): mqPutPointcut()
   {
      System.out.println("In advice... before():mqPutPointcut");

      // get an array of objects that are being passed in the method call
      Object[] pointcutArguments = thisJoinPoint.getArgs();
      
      // there is only one object being passed, thats the first one, the MQMessage
      MQMessage message = (MQMessage)pointcutArguments[0];

      // encrypt the contents of message
      try {
         message.seek(0);
         String clearText = message.readString(message.getMessageLength());
         
         // reverse clearText to encryptedText
         String encryptedText = encryptString(clearText);
         
         // update the message text to be encrypted
          message.clearMessage();
          message.writeString(encryptedText.toString());
          message.format = MQC.MQFMT_STRING;
          
          // tell us what happened
          System.out.println("Got a reference to the MQMessage and encrypted it...");

      } catch (java.io.IOException e) {
         e.printStackTrace();
      }
      
      System.out.println("Leaving advice... before():mqPutPointcut");
   }
}





To compile the aspect, we have to do a few things. Our pointcut is right in the middle of the IBM WMQ class libraries. So, we have to pass the IBM libraries through the AspectJ compiler, allowing our advise to be weaved into the bytecode.

Our PutTest class then runs with these aspects weaved in:

Code:

Start...
Just before put()...
In advice... before():mqPutPointcut
Got a reference to the MQMessage and encrypted it...
Leaving advice... before():mqPutPointcut
Just after put()...


This time the advice has been woven into the java bytecode, and allows us to intercept the MQQueue.put(MQMessage) method, obtain a reference to the MQMessage as it is being passed from method-to-method, encrypt the message and return from the advise allowing the put method to continue with our newly encrypted message.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » News/Updates » Java WMQ Exits using AOP
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.