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,16 +1330,18 @@
 Class);%0A
+//
 %09%09%09if (m
@@ -1357,24 +1357,26 @@
  != null) %7B%0A
+//
 %09%09%09%09try %7B%0A%09%09
@@ -1373,16 +1373,18 @@
 %09%09try %7B%0A
+//
 %09%09%09%09%09log
@@ -1413,24 +1413,26 @@
 perties:%22);%0A
+//
 %09%09%09%09%09while (
@@ -1474,32 +1474,34 @@
 reElements()) %7B%0A
+//
 %09%09%09%09%09%09String nam
@@ -1550,32 +1550,34 @@
 .nextElement();%0A
+//
 %09%09%09%09%09%09logger.err
@@ -1627,32 +1627,38 @@
 rty(name));%0A
+//
 %09%09%09%09%09%7D%0A
-%0A
+//%0A//
 %09%09%09%09%09logger.
@@ -1688,16 +1688,18 @@
 cts:%22);%0A
+//
 %09%09%09%09%09whi
@@ -1744,24 +1744,26 @@
 ements()) %7B%0A
+//
 %09%09%09%09%09%09String
@@ -1819,16 +1819,18 @@
 ment();%0A
+//
 %09%09%09%09%09%09lo
@@ -1888,16 +1888,22 @@
 ));%0A
+//
 %09%09%09%09%09%7D%0A
-%0A
+//%0A//
 %09%09%09%09
@@ -1960,24 +1960,26 @@
 JMSType());%0A
+//
 %09%09%09%09%09logger.
@@ -2038,24 +2038,26 @@
 ssageID());%0A
+//
 %09%09%09%09%09logger.
@@ -2124,26 +2124,32 @@
 ion());%0A
+//
 %09%09%09%09%0A
+//
 %09%09%09%09%0A
+//
 %09%09%09%09%7D ca
@@ -2172,16 +2172,18 @@
 n e1) %7B%0A
+//
 %09%09%09%09%09log
@@ -2227,29 +2227,18 @@
 ));%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

Disable mystery message debug logging