Package org.activemq.transport.openwire

Source Code of org.activemq.transport.openwire.OpenWirePacketMarshallerSupport

/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* 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 org.activemq.transport.openwire;

import org.activemq.io.impl.AbstractPacketMarshaller;
import org.activemq.message.ActiveMQDestination;
import org.activemq.message.ActiveMQQueue;
import org.activemq.message.ActiveMQTemporaryQueue;
import org.activemq.message.ActiveMQTemporaryTopic;
import org.activemq.message.ActiveMQTopic;
import org.activemq.message.Packet;
import org.activemq.message.AbstractPacket;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TemporaryQueue;
import javax.jms.TemporaryTopic;
import javax.jms.Topic;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

/**
* @version $Revision: 1.1 $
*/
public abstract class OpenWirePacketMarshallerSupport extends AbstractPacketMarshaller {

    public void writePacket(Packet packet, DataOutput dataOut) throws IOException {
        dataOut.writeShort(packet.getId());
        writeBoolean(packet.isReceiptRequired(), dataOut);
        writeString(packet.getBrokersVisitedAsString(), dataOut);
    }

    public void buildPacket(Packet p, DataInput dataIn) throws IOException {
        AbstractPacket packet = (AbstractPacket) p;
        packet.setId(dataIn.readShort());
        packet.setReceiptRequired(readBoolean(dataIn));
        packet.setBrokersVisitedAsString(readString(dataIn));
    }

    public void writeProperties(Map map, DataOutput dataOut) throws IOException {
        if (map instanceof Properties || map == null) {
            writeProperties((Properties) map, dataOut);
        }
        else {
            Properties properties = new Properties();
            properties.putAll(map);
            writeProperties(properties, dataOut);
        }
    }

    public void writeProperties(Properties properties, DataOutput dataOut) throws IOException {
        // lets turn the propertes into a string
        String text = "";
        if (properties != null) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            properties.store(out, null);
            text = new String(out.toByteArray());
        }
        dataOut.writeUTF(text);
    }

    public Properties readProperties(DataInput dataIn) throws IOException {
        Properties properties = null;
        String text = dataIn.readUTF();
        if (text != null) {
            if (text.length() > 0) {
                properties = new Properties();
                properties.load(new ByteArrayInputStream(text.getBytes()));
            }
        }
        return properties;
    }


    protected ActiveMQDestination readDestination(DataInput dataIn) throws IOException {
        int data = dataIn.readByte();
        String text = readString(dataIn);
        switch (data) {
            case ActiveMQDestination.ACTIVEMQ_QUEUE:
                return new ActiveMQQueue(text);

            case ActiveMQDestination.ACTIVEMQ_TEMPORARY_QUEUE:
                return new ActiveMQTemporaryQueue(text);

            case ActiveMQDestination.ACTIVEMQ_TEMPORARY_TOPIC:
                return new ActiveMQTemporaryTopic(text);

            case ActiveMQDestination.ACTIVEMQ_TOPIC:
                return new ActiveMQTopic(text);

            default:
                throw new IOException("Unknown destination type: " + data);
        }
    }

    protected void writeDestination(Destination destination, DataOutput dataOut) throws IOException {
        try {
            writeDestination(ActiveMQDestination.transformDestination(destination), dataOut);
        }
        catch (JMSException e) {
            throw new IOException("Failed to convert destination into ActiveMQDestination: " + e);
        }
    }

    protected void writeDestination(ActiveMQDestination destination, DataOutput dataOut) throws IOException {
        String text = null;
        int data = ActiveMQDestination.ACTIVEMQ_QUEUE;
        if (destination instanceof Topic) {
            if (destination instanceof TemporaryTopic) {
                data = ActiveMQDestination.ACTIVEMQ_TEMPORARY_TOPIC;
            }
            else {
                data = ActiveMQDestination.ACTIVEMQ_TOPIC;
            }
        }
        else if (destination instanceof TemporaryQueue) {
            data = ActiveMQDestination.ACTIVEMQ_TEMPORARY_QUEUE;
        }
        if (destination != null) {
            text = destination.getPhysicalName();
        }
        dataOut.writeByte(data);
        writeString(text, dataOut);
    }

    protected String readString(DataInput dataIn) throws IOException {
        // lets write double byte text
        int length = dataIn.readInt();
        if (length < 0) {
            return null;
        }
        byte[] bytes = new byte[length];
        dataIn.readFully(bytes);
        return new String(bytes);
    }

    protected void writeString(String text, DataOutput dataOut) throws IOException {
        if (text == null) {
            dataOut.writeInt(-1);
        }
        else {
            byte[] bytes = text.getBytes();
            dataOut.writeInt(bytes.length);
            dataOut.write(bytes);
        }
    }

    protected byte[] readBytes(DataInput dataIn) throws IOException {
        int length = dataIn.readInt();
        if (length < 0) {
            return null;
        }
        byte[] answer = new byte[length];
        dataIn.readFully(answer);
        return answer;
    }

    protected void writeBytes(byte[] data, DataOutput dataOut) throws IOException {
        if (data == null) {
            dataOut.writeInt(-1);
        }
        else {
            dataOut.writeInt(data.length);
            dataOut.write(data);
        }
    }

    protected void writeBoolean(boolean value, DataOutput dataOut) throws IOException {
        dataOut.writeByte(value ? 1 : 0);
    }

    protected boolean readBoolean(DataInput dataIn) throws IOException {
        return dataIn.readByte() > 0;
    }
}
TOP

Related Classes of org.activemq.transport.openwire.OpenWirePacketMarshallerSupport

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.