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