Package com.betfair.cougar.transport.jms

Source Code of com.betfair.cougar.transport.jms.JMSEventUnMarshaller

/*
* Copyright 2013, The Sporting Exchange Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.betfair.cougar.transport.jms;

import com.betfair.cougar.core.api.events.Event;
import com.betfair.cougar.core.api.exception.CougarException;
import com.betfair.cougar.core.api.exception.CougarFrameworkException;
import com.betfair.cougar.logging.CougarLogger;
import com.betfair.cougar.logging.CougarLoggingUtils;
import com.betfair.cougar.marshalling.api.databinding.DataBindingFactory;
import com.betfair.cougar.transport.api.protocol.events.AbstractEvent;
import com.betfair.cougar.transport.api.protocol.events.EventUnMarshaller;
import org.springframework.beans.factory.annotation.Required;

import javax.jms.JMSException;
import javax.jms.TextMessage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.logging.Level;

/**
* This JMS specific implementation of the event resolver interface will take a
* JSON based TextMessage, and marshall it to the IDL defined event object.
*/
public class JMSEventUnMarshaller implements EventUnMarshaller<TextMessage> {
    private final static CougarLogger logger = CougarLoggingUtils.getLogger(JMSEventUnMarshaller.class);

    public static final String DEFAULT_ENCODING_TYPE = "utf-8";

    private DataBindingFactory dataBindingFactory;

    //String encoding type, defaulted to UTF-8, but you can override this in spring config
    private String encodingType = DEFAULT_ENCODING_TYPE;

    @Override
    public Event unmarshallEvent(List<Class<? extends Event>> eventBodyClasses, Class<? extends Event> defaultBodyClass, TextMessage transportEvent) throws CougarException {
        InputStream is = null;
        try {
            String messageText = ((TextMessage)transportEvent).getText();
            logger.log(Level.FINE, "Received message with body [" + messageText + "]");
            is = new ByteArrayInputStream(messageText.getBytes(encodingType));

            String eventNameFromMessage = transportEvent.getStringProperty(JMSPropertyConstants.EVENT_NAME_FIELD_NAME);
            Class<? extends Event> correctClass = defaultBodyClass;
            for (Class c : eventBodyClasses) {
                if (c.getSimpleName().equals(eventNameFromMessage)) {
                    correctClass = c;
                    break;
                }
            }
            if (correctClass == null) {
                throw new CougarFrameworkException("Can't find event class for event named '"+eventNameFromMessage+"'");
            }

            AbstractEvent event = (AbstractEvent) dataBindingFactory.getUnMarshaller().unmarshall(is, correctClass, encodingType, true);

            event.setMessageId(transportEvent.getStringProperty(JMSPropertyConstants.MESSAGE_ID_FIELD_NAME));
            event.setCougarMessageRouteString(transportEvent.getStringProperty(JMSPropertyConstants.MESSAGE_ROUTING_FIELD_NAME));

            //When other types of JMS field types (eg, not stored in the message body) become necessary
            //This is where they'll be added

            return event;
        } catch (UnsupportedEncodingException ex) {
            //This is never going to happen in a month of Sundays
            throw new CougarFrameworkException("Unsupported encoding exception for JMS Event", ex);
        } catch (JMSException jmsex) {
            throw new CougarFrameworkException("Unsupported encoding exception for JMS Event", jmsex);
        }
    }


    public DataBindingFactory getDataBindingFactory() {
        return dataBindingFactory;
    }

    @Required
    public void setDataBindingFactory(DataBindingFactory dataBindingFactory) {
        this.dataBindingFactory = dataBindingFactory;
    }

    public String getEncodingType() {
        return encodingType;
    }

    public void setEncodingType(String encodingType) {
        this.encodingType = encodingType;
    }


}
TOP

Related Classes of com.betfair.cougar.transport.jms.JMSEventUnMarshaller

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.