KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import org.apache.activemq.command.BaseCommand;
24 import org.apache.activemq.command.Message;
25 import org.apache.activemq.command.MessageAck;
26 import org.apache.activemq.command.TransactionId;
27 import org.apache.activemq.store.MessageStore;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 /**
31  * Stores a messages/acknowledgements for a transaction
32  *
33  * @version $Revision: 1.4 $
34  */

35 class KahaTransaction{
36     private static final Log log=LogFactory.getLog(KahaTransaction.class);
37     protected List JavaDoc list=new ArrayList JavaDoc();
38
39     
40      void add(KahaMessageStore store,BaseCommand command){
41         TxCommand tx=new TxCommand();
42         tx.setCommand(command);
43         tx.setMessageStoreKey(store.getId());
44         list.add(tx);
45     }
46
47     Message[] getMessages(){
48         List JavaDoc result=new ArrayList JavaDoc();
49         for(int i=0;i<list.size();i++){
50             TxCommand command=(TxCommand) list.get(i);
51             if(command.isAdd()){
52                 result.add(command.getCommand());
53             }
54         }
55         Message[] messages=new Message[result.size()];
56         return (Message[]) result.toArray(messages);
57     }
58
59     MessageAck[] getAcks(){
60         List JavaDoc result=new ArrayList JavaDoc();
61         for(int i=0;i<list.size();i++){
62             TxCommand command=(TxCommand) list.get(i);
63             if(command.isRemove()){
64                 result.add(command.getCommand());
65             }
66         }
67         MessageAck[] acks=new MessageAck[result.size()];
68         return (MessageAck[]) result.toArray(acks);
69     }
70
71     void prepare(){}
72
73     void rollback(){
74         list.clear();
75     }
76
77     /**
78      * @throws IOException
79      */

80     void commit(KahaTransactionStore transactionStore) throws IOException JavaDoc{
81         for(int i=0;i<list.size();i++){
82             TxCommand command=(TxCommand) list.get(i);
83             MessageStore ms=transactionStore.getStoreById(command.getMessageStoreKey());
84             if(command.isAdd()){
85                 ms.addMessage(null,(Message) command.getCommand());
86             }
87         }
88         for(int i=0;i<list.size();i++){
89             TxCommand command=(TxCommand) list.get(i);
90             MessageStore ms=transactionStore.getStoreById(command.getMessageStoreKey());
91             if(command.isRemove()){
92                 ms.removeMessage(null,(MessageAck) command.getCommand());
93             }
94         }
95     }
96     
97     List JavaDoc getList(){
98         return new ArrayList JavaDoc(list);
99     }
100     
101     void setList(List JavaDoc list){
102         this.list = list;
103     }
104 }
105
Popular Tags