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 */ 017 018package org.apache.activemq.store.amq; 019 020import java.util.ArrayList; 021import java.util.Iterator; 022import java.util.List; 023 024import org.apache.activemq.command.JournalTopicAck; 025import org.apache.activemq.command.Message; 026import org.apache.activemq.command.MessageAck; 027import org.apache.activemq.kaha.impl.async.Location; 028 029/** 030 */ 031/** 032 * Operations 033 * 034 * 035 */ 036public class AMQTx { 037 038 private final Location location; 039 private List<AMQTxOperation> operations = new ArrayList<AMQTxOperation>(); 040 041 public AMQTx(Location location) { 042 this.location = location; 043 } 044 045 public void add(AMQMessageStore store, Message msg, Location location) { 046 operations.add(new AMQTxOperation(AMQTxOperation.ADD_OPERATION_TYPE, store.getDestination(), msg, 047 location)); 048 } 049 050 public void add(AMQMessageStore store, MessageAck ack) { 051 operations.add(new AMQTxOperation(AMQTxOperation.REMOVE_OPERATION_TYPE, store.getDestination(), ack, 052 null)); 053 } 054 055 public void add(AMQTopicMessageStore store, JournalTopicAck ack) { 056 operations.add(new AMQTxOperation(AMQTxOperation.ACK_OPERATION_TYPE, store.getDestination(), ack, 057 null)); 058 } 059 060 public Message[] getMessages() { 061 List<Object> list = new ArrayList<Object>(); 062 for (Iterator<AMQTxOperation> iter = operations.iterator(); iter.hasNext();) { 063 AMQTxOperation op = iter.next(); 064 if (op.getOperationType() == AMQTxOperation.ADD_OPERATION_TYPE) { 065 list.add(op.getData()); 066 } 067 } 068 Message rc[] = new Message[list.size()]; 069 list.toArray(rc); 070 return rc; 071 } 072 073 public MessageAck[] getAcks() { 074 List<Object> list = new ArrayList<Object>(); 075 for (Iterator<AMQTxOperation> iter = operations.iterator(); iter.hasNext();) { 076 AMQTxOperation op = iter.next(); 077 if (op.getOperationType() == AMQTxOperation.REMOVE_OPERATION_TYPE) { 078 list.add(op.getData()); 079 } 080 } 081 MessageAck rc[] = new MessageAck[list.size()]; 082 list.toArray(rc); 083 return rc; 084 } 085 086 /** 087 * @return the location 088 */ 089 public Location getLocation() { 090 return this.location; 091 } 092 093 public List<AMQTxOperation> getOperations() { 094 return operations; 095 } 096 097 public void setOperations(List<AMQTxOperation> operations) { 098 this.operations = operations; 099 } 100}