package fi.csc.microarray.messaging;

import java.util.Collections;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.apache.log4j.Logger;

import fi.csc.microarray.messaging.message.ChipsterMessage;
import fi.csc.microarray.util.Exceptions;

/**
 * For converting JMS-type messages into Nami type messages.
 * @author akallio
 *
 */
public class MessageListenerWrapper implements MessageListener {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(MessageListenerWrapper.class);

	private MessagingListener actualListener;
	
	public MessageListenerWrapper(MessagingListener listener) {
		actualListener = listener;
	}

	public void onMessage(Message msg) {
		
		String msgClass = "";
		MapMessage mapMessage = null;
		try {
			msgClass = msg.getStringProperty(ChipsterMessage.KEY_CLASS);
			logger.debug("message received, class is " + msgClass);
			mapMessage = (MapMessage)msg;
			ChipsterMessage chipsterMessage = (ChipsterMessage)Class.forName(msgClass).newInstance();
			chipsterMessage.unmarshal(mapMessage);
			actualListener.onChipsterMessage(chipsterMessage);
			
		} catch (Exception e) {
			logger.error("Exception when handling a message.", e);
			logger.error("message class was: " + msgClass);
//			if (mapMessage != null) {
//				try {
//					logger.error("message properties:");
//					while (mapMessage.getPropertyNames().hasMoreElements()) {
//						String name = (String) mapMessage.getPropertyNames().nextElement();
//						logger.error(name + " : " + mapMessage.getStringProperty(name));
//					}
//
//					logger.error("message map objects:");
//					while (mapMessage.getMapNames().hasMoreElements()) {
//						String name = (String) mapMessage.getMapNames().nextElement();
//						logger.error(name + " : " + mapMessage.getString(name));
//					}
//
//					logger.error("message JMSType: " + mapMessage.getJMSType());
//					logger.error("message JMSMessageID: " + mapMessage.getJMSMessageID());
//					logger.error("message JMSDestination: " + mapMessage.getJMSDestination());
//				
//				
//				} catch (JMSException e1) {
//					logger.error(Exceptions.getStackTrace(e1));
//				}
//			}
		} 
	}

}

@@ -1330,18 +1330,16 @@
 Class);%0A
-//
 %09%09%09if (m
@@ -1355,26 +1355,24 @@
  != null) %7B%0A
-//
 %09%09%09%09try %7B%0A//
@@ -1369,18 +1369,16 @@
 %09%09try %7B%0A
-//
 %09%09%09%09%09log
@@ -1400,35 +1400,34 @@
 age 
+first 
 propert
-ies
+y
 :%22);%0A
-//
 %09%09%09%09%09
-while
+if
  (ma
@@ -1467,34 +1467,32 @@
 reElements()) %7B%0A
-//
 %09%09%09%09%09%09String nam
@@ -1541,34 +1541,32 @@
 .nextElement();%0A
-//
 %09%09%09%09%09%09logger.err
@@ -1616,38 +1616,32 @@
 rty(name));%0A
-//
 %09%09%09%09%09%7D%0A
-//%0A//
+%0A
 %09%09%09%09%09logger.
@@ -1655,16 +1655,22 @@
 message 
+first 
 map obje
@@ -1675,26 +1675,20 @@
 ject
-s
 :%22);%0A
-//
 %09%09%09%09%09
-while
+if
  (ma
@@ -1727,26 +1727,24 @@
 ements()) %7B%0A
-//
 %09%09%09%09%09%09String
@@ -1800,18 +1800,16 @@
 ment();%0A
-//
 %09%09%09%09%09%09lo
@@ -1867,22 +1867,16 @@
 ));%0A
-//
 %09%09%09%09%09%7D%0A
-//%0A//
+%0A
 %09%09%09%09
@@ -1933,26 +1933,24 @@
 JMSType());%0A
-//
 %09%09%09%09%09logger.
@@ -2009,26 +2009,24 @@
 ssageID());%0A
-//
 %09%09%09%09%09logger.
@@ -2097,24 +2097,18 @@
 ));%0A
-//
 %09%09%09%09%0A
-//
 %09%09%09%09%0A
-//
 %09%09%09%09
@@ -2135,18 +2135,16 @@
 n e1) %7B%0A
-//
 %09%09%09%09%09log
@@ -2188,18 +2188,14 @@
 ));%0A
-//
 %09%09%09%09%7D%0A
-//
 %09%09%09%7D

Debug manager mystic message