001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.store.kahadaptor;
018
019import java.io.DataInput;
020import java.io.DataOutput;
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024import org.apache.activemq.kaha.Marshaller;
025import org.apache.activemq.kaha.impl.async.Location;
026import org.apache.activemq.store.amq.AMQTx;
027import org.apache.activemq.store.amq.AMQTxOperation;
028import org.apache.activemq.wireformat.WireFormat;
029
030/**
031 * Marshall an AMQTx
032 * 
033 * 
034 */
035public class AMQTxMarshaller implements Marshaller<AMQTx> {
036
037    private WireFormat wireFormat;
038
039    public AMQTxMarshaller(WireFormat wireFormat) {
040        this.wireFormat = wireFormat;
041    }
042
043    public AMQTx readPayload(DataInput dataIn) throws IOException {
044        Location location = new Location();
045        location.readExternal(dataIn);
046        AMQTx result = new AMQTx(location);
047        int size = dataIn.readInt();
048        for (int i = 0; i < size; i++) {
049            AMQTxOperation op = new AMQTxOperation();
050            op.readExternal(wireFormat, dataIn);
051            result.getOperations().add(op);
052        }
053        return result;
054    }
055
056    public void writePayload(AMQTx amqtx, DataOutput dataOut) throws IOException {
057        amqtx.getLocation().writeExternal(dataOut);
058        List<AMQTxOperation> list = amqtx.getOperations();
059        List<AMQTxOperation> ops = new ArrayList<AMQTxOperation>();
060        
061        for (AMQTxOperation op : list) {
062            if (op.getOperationType() == op.ADD_OPERATION_TYPE) {
063                ops.add(op);
064            }
065        }
066        dataOut.writeInt(ops.size());
067        for (AMQTxOperation op : ops) {
068            op.writeExternal(wireFormat, dataOut);
069        }
070    }
071}