com.espertech.esperio.jms.JMSDefaultMapMessageMarshaller.java Source code

Java tutorial

Introduction

Here is the source code for com.espertech.esperio.jms.JMSDefaultMapMessageMarshaller.java

Source

/**************************************************************************************
 * Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
 * http://esper.codehaus.org                                                          *
 * http://www.espertech.com                                                           *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the GPL license       *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/
package com.espertech.esperio.jms;

import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.EventType;
import com.espertech.esper.client.EPException;
import com.espertech.esper.util.JavaClassHelper;
import com.espertech.esper.util.ExecutionPathDebugLog;

import javax.jms.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Marshals the response out of the event bean into a jms map message.
 */
public class JMSDefaultMapMessageMarshaller implements JMSMessageMarshaller {
    private final Log log = LogFactory.getLog(this.getClass());

    public Message marshal(EventBean eventBean, Session session, long timestamp) throws EPException {
        EventType eventType = eventBean.getEventType();
        MapMessage mapMessage = null;
        try {
            mapMessage = session.createMapMessage();
            String[] properties = eventType.getPropertyNames();
            for (String property : properties) {
                if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) {
                    log.debug(
                            ".Marshal EventProperty property==" + property + ", value=" + eventBean.get(property));
                }
                Class clazz = eventType.getPropertyType(property);
                if (JavaClassHelper.isNumeric(clazz)) {
                    Class boxedClazz = JavaClassHelper.getBoxedType(clazz);
                    if (boxedClazz == Double.class) {
                        mapMessage.setDouble(property, (Double) eventBean.get(property));
                    }
                    if (boxedClazz == Float.class) {
                        mapMessage.setFloat(property, (Float) eventBean.get(property));
                    }
                    if (boxedClazz == Byte.class) {
                        mapMessage.setFloat(property, (Byte) eventBean.get(property));
                    }
                    if (boxedClazz == Short.class) {
                        mapMessage.setShort(property, (Short) eventBean.get(property));
                    }
                    if (boxedClazz == Integer.class) {
                        mapMessage.setInt(property, (Integer) eventBean.get(property));
                    }
                    if (boxedClazz == Long.class) {
                        mapMessage.setLong(property, (Long) eventBean.get(property));
                    }
                } else if ((clazz == boolean.class) || (clazz == Boolean.class)) {
                    mapMessage.setBoolean(property, (Boolean) eventBean.get(property));
                } else if ((clazz == Character.class) || (clazz == char.class)) {
                    mapMessage.setChar(property, (Character) eventBean.get(property));
                } else if (clazz == String.class) {
                    mapMessage.setString(property, (String) eventBean.get(property));
                } else {
                    mapMessage.setObject(property, eventBean.get(property));
                }
            }
            mapMessage.setJMSTimestamp(timestamp);
        } catch (JMSException ex) {
            throw new EPException(ex);
        }
        return mapMessage;
    }
}