/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.activeblaze.wire;

import org.apache.activeblaze.BlazeException;
import org.apache.activeblaze.BlazeRuntimeException;

import javax.jms.JMSException;
import javax.jms.TextMessage;
import java.io.IOException;

/**
 * @version $Revision$
 */
public class BlazeJmsTextMessage extends BlazeJmsMessage implements TextMessage {
    protected String text;

    public int getPacketType() {
        return PacketType.JMS_TEXT.getNumber();
    }

    public BlazeJmsTextMessage clone() {
        BlazeJmsTextMessage copy = new BlazeJmsTextMessage();
        try {
            copy(copy);
        } catch (Exception e) {
            throw new BlazeRuntimeException(e);
        }
        return copy;
    }

    /**
     * @param copy
     * @throws BlazeException
     */
    protected void copy(BlazeJmsTextMessage copy) throws IOException {
        storeContent();
        super.copy(copy);
        copy.text = this.text;
    }

    /**
     * @return the type
     * @see org.apache.activeblaze.BlazeMessage#getType()
     */
    public int getType() {
        return JmsMessageType.TEXT.ordinal();
    }

    public void setText(String text) {
        this.text = text;
        setContent(null);
    }

    protected void storeContent() throws IOException {
        super.storeContent();
        try {
            BufferOutputStream os = new BufferOutputStream(this.text != null ? this.text.length() : 10);
            MarshallingSupport.writeUTF8(os, this.text);
            this.payload = os.toBuffer();
        } catch (IOException e) {
            throw new BlazeRuntimeException(e);
        }
    }

    public String getText() throws JMSException {
        if (this.text == null) {
            try {
                this.text = MarshallingSupport.readUTF8(new BufferInputStream(this.payload));
            } catch (IOException e) {
                JMSException ex = new JMSException(e.getMessage());
                ex.initCause(e);
                ex.setLinkedException(e);
                throw ex;
            }
        }
        return this.text;
    }

    /**
     * Clears out the message body. Clearing a message's body does not clear its
     * header values or property entries.
     * <p/>
     * <p/>
     * If this message body was read-only, calling this method leaves the
     * message body in the same state as an empty body in a newly created
     * message.
     *
     * @throws JMSException if the JMS provider fails to clear the message body due to
     *                      some internal error.
     */
    public void clearBody() throws JMSException {
        super.clearBody();
        this.text = null;
    }
}
