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.state; 018 019import java.util.ArrayList; 020import java.util.List; 021import java.util.Map; 022import java.util.concurrent.ConcurrentHashMap; 023import java.util.concurrent.atomic.AtomicBoolean; 024 025import org.apache.activemq.command.Command; 026import org.apache.activemq.command.ProducerId; 027import org.apache.activemq.command.TransactionId; 028 029public class TransactionState { 030 031 private final List<Command> commands = new ArrayList<Command>(); 032 private final TransactionId id; 033 private final AtomicBoolean shutdown = new AtomicBoolean(false); 034 private boolean prepared; 035 private int preparedResult; 036 private final Map<ProducerId, ProducerState> producers = new ConcurrentHashMap<ProducerId, ProducerState>(); 037 private final long createdAt = System.currentTimeMillis(); 038 039 public TransactionState(TransactionId id) { 040 this.id = id; 041 } 042 043 public String toString() { 044 return id.toString(); 045 } 046 047 public void addCommand(Command operation) { 048 checkShutdown(); 049 commands.add(operation); 050 } 051 052 public List<Command> getCommands() { 053 return commands; 054 } 055 056 private void checkShutdown() { 057 if (shutdown.get()) { 058 throw new IllegalStateException("Disposed"); 059 } 060 } 061 062 public void shutdown() { 063 shutdown.set(false); 064 } 065 066 public TransactionId getId() { 067 return id; 068 } 069 070 public void setPrepared(boolean prepared) { 071 this.prepared = prepared; 072 } 073 074 public boolean isPrepared() { 075 return prepared; 076 } 077 078 public void setPreparedResult(int preparedResult) { 079 this.preparedResult = preparedResult; 080 } 081 082 public int getPreparedResult() { 083 return preparedResult; 084 } 085 086 public void addProducerState(ProducerState producerState) { 087 if (producerState != null) { 088 producers.put(producerState.getInfo().getProducerId(), producerState); 089 } 090 } 091 092 public Map<ProducerId, ProducerState> getProducerStates() { 093 return producers; 094 } 095 096 public long getCreatedAt() { 097 return createdAt; 098 } 099}