/**
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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.codehaus.activemq.service.impl;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import javax.jms.JMSException;
import org.codehaus.activemq.broker.BrokerClient;
import org.codehaus.activemq.broker.impl.BrokerClientImpl;
import org.codehaus.activemq.io.WireFormat;
import org.codehaus.activemq.io.impl.DefaultWireFormat;
import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.message.MessageAck;
import org.codehaus.activemq.message.Packet;
import org.codehaus.activemq.service.TransactionTask;
import org.codehaus.activemq.util.JMSExceptionHelper;

/**
 * @version $Revision: 1.2 $
 */
public abstract class PacketTransactionTask implements TransactionTask, Externalizable {
    private static final long serialVersionUID = -5754338187296859149L;
    private static transient final WireFormat wireFormat = new DefaultWireFormat();

    private BrokerClient brokerClient;
    private Packet packet;


    public static TransactionTask fromBytes(byte[] data) throws IOException {
        Packet packet = wireFormat.fromBytes(data);
        return createTask(packet);
    }

    public byte[] toBytes() throws JMSException, IOException {
        return wireFormat.toBytes(packet);
    }

    public static TransactionTask readTask(ObjectInput in) throws IOException {
        Packet packet = readPacket(in);
        return createTask(packet);
    }

    public static TransactionTask createTask(Packet packet) throws IOException {
        if (packet instanceof MessageAck) {
            return new MessageAckTransactionTask(null, (MessageAck) packet);
        }
        else if (packet instanceof ActiveMQMessage) {
            return new SendMessageTransactionTask(null, (ActiveMQMessage) packet);
        }
        else {
            throw new IOException("Unexpected packet type: " + packet);
        }
    }

    public static void writeTask(TransactionTask task, ObjectOutput out) throws IOException {
        if (task instanceof PacketTransactionTask) {
            PacketTransactionTask packetTask = (PacketTransactionTask) task;
            writePacket(packetTask.getPacket(), out);
        }
        else {
            out.writeObject(task);
        }
    }

    protected PacketTransactionTask(BrokerClient brokerClient, Packet packet) {
        this.brokerClient = brokerClient;
        this.packet = packet;
    }

    public Packet getPacket() {
        return packet;
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        writePacket(packet, out);
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        packet = readPacket(in);
    }


    // Implementation methods
    //-------------------------------------------------------------------------
    protected BrokerClient createBrokerClient(String consumerId) throws JMSException {
        BrokerClientImpl answer = new BrokerClientImpl();
        return answer;
    }

    protected BrokerClient getBrokerClient(String consumerId) throws JMSException {
        // TODO replace when tested recovery
        brokerClient = createBrokerClient(consumerId);
        /*
        if (brokerClient == null) {
        brokerClient = createBrokerClient(consumerId);
        }
        */
        return brokerClient;
    }

    protected static void writePacket(Packet packet, ObjectOutput out) throws IOException {
        try {
            wireFormat.writePacket(packet, out);
        }
        catch (JMSException e) {
            throw JMSExceptionHelper.newIOException(e);
        }
    }

    protected static Packet readPacket(ObjectInput in) throws IOException {
        return wireFormat.readPacket(in);
    }
}
