KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > store > kahadaptor > TransactionMarshaller


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.store.kahadaptor;
19
20 import java.io.DataInput JavaDoc;
21 import java.io.DataOutput JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.activemq.command.BaseCommand;
27 import org.apache.activemq.kaha.Marshaller;
28 import org.apache.activemq.util.ByteSequence;
29 import org.apache.activemq.wireformat.WireFormat;
30
31 /**
32  * Marshall a Transaction
33  * @version $Revision: 1.10 $
34  */

35 public class TransactionMarshaller implements Marshaller{
36     
37     private WireFormat wireFormat;
38     public TransactionMarshaller(WireFormat wireFormat){
39         this.wireFormat = wireFormat;
40       
41     }
42     
43     public void writePayload(Object JavaDoc object,DataOutput JavaDoc dataOut) throws IOException JavaDoc{
44         KahaTransaction kt = (KahaTransaction) object;
45         List JavaDoc list = kt.getList();
46         dataOut.writeInt(list.size());
47         for (int i = 0; i < list.size(); i++){
48             TxCommand tx = (TxCommand) list.get(i);
49             Object JavaDoc key = tx.getMessageStoreKey();
50             ByteSequence packet = wireFormat.marshal(key);
51             dataOut.writeInt(packet.length);
52             dataOut.write(packet.data, packet.offset, packet.length);
53             Object JavaDoc command = tx.getCommand();
54             packet = wireFormat.marshal(command);
55             dataOut.writeInt(packet.length);
56             dataOut.write(packet.data, packet.offset, packet.length);
57             
58         }
59        }
60
61    
62     public Object JavaDoc readPayload(DataInput JavaDoc dataIn) throws IOException JavaDoc{
63         KahaTransaction result = new KahaTransaction();
64         List JavaDoc list = new ArrayList JavaDoc();
65         result.setList(list);
66         int number=dataIn.readInt();
67         for (int i = 0; i < number; i++){
68             TxCommand command = new TxCommand();
69             int size = dataIn.readInt();
70             byte[] data=new byte[size];
71             dataIn.readFully(data);
72             Object JavaDoc key = wireFormat.unmarshal(new ByteSequence(data));
73             command.setMessageStoreKey(key);
74             size = dataIn.readInt();
75             data=new byte[size];
76             dataIn.readFully(data);
77             BaseCommand bc = (BaseCommand) wireFormat.unmarshal(new ByteSequence(data));
78             command.setCommand(bc);
79             list.add(command);
80         }
81         return result;
82        
83     }
84 }
85
Popular Tags