package fi.csc.microarray.messaging;

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

import org.apache.log4j.Logger;

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

/**
 * 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 = "";
		try {
			msgClass = msg.getStringProperty(ChipsterMessage.KEY_CLASS);
			logger.debug("message received, class is " + msgClass);
			MapMessage 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);

		} 
	}

}

@@ -31,16 +31,78 @@
 aging;%0A%0A
+import java.util.Collections;%0A%0Aimport javax.jms.JMSException;%0A
 import j
@@ -114,32 +114,32 @@
 jms.MapMessage;%0A
-
 import javax.jms
@@ -275,16 +275,58 @@
 Message;
+%0Aimport fi.csc.microarray.util.Exceptions;
 %0A%0A/**%0A *
@@ -806,16 +806,48 @@
 s = %22%22;%0A
+%09%09MapMessage mapMessage = null;%0A
 %09%09try %7B%0A
@@ -972,27 +972,16 @@
 ss);%0A%09%09%09
-MapMessage 
 mapMessa
@@ -1330,16 +1330,892 @@
 Class);%0A
+%09%09%09if (mapMessage != null) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09logger.error(%22message properties:%22);%0A%09%09%09%09%09while (mapMessage.getPropertyNames().hasMoreElements()) %7B%0A%09%09%09%09%09%09String name = (String) mapMessage.getPropertyNames().nextElement();%0A%09%09%09%09%09%09logger.error(name + %22 : %22 + mapMessage.getStringProperty(name));%0A%09%09%09%09%09%7D%0A%0A%09%09%09%09%09logger.error(%22message map objects:%22);%0A%09%09%09%09%09while (mapMessage.getMapNames().hasMoreElements()) %7B%0A%09%09%09%09%09%09String name = (String) mapMessage.getMapNames().nextElement();%0A%09%09%09%09%09%09logger.error(name + %22 : %22 + mapMessage.getString(name));%0A%09%09%09%09%09%7D%0A%0A%09%09%09%09%09logger.error(%22message JMSType: %22 + mapMessage.getJMSType());%0A%09%09%09%09%09logger.error(%22message JMSMessageID: %22 + mapMessage.getJMSMessageID());%0A%09%09%09%09%09logger.error(%22message JMSDestination: %22 + mapMessage.getJMSDestination());%0A%09%09%09%09%0A%09%09%09%09%0A%09%09%09%09%7D catch (JMSException e1) %7B%0A%09%09%09%09%09logger.error(Exceptions.getStackTrace(e1));%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%09%0A%09%09%09%09%0A%09%09%09%7D
 %0A%09%09%7D %0A%09%7D

Add more debug for MapMessages without class name string