1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.activemq.state; 19 20 import java.util.Collection; 21 import java.util.Map; 22 import java.util.Set; 23 import java.util.concurrent.ConcurrentHashMap; 24 import java.util.concurrent.atomic.AtomicBoolean; 25 26 import org.apache.activemq.command.ConsumerId; 27 import org.apache.activemq.command.ConsumerInfo; 28 import org.apache.activemq.command.ProducerId; 29 import org.apache.activemq.command.ProducerInfo; 30 import org.apache.activemq.command.SessionInfo; 31 32 public class SessionState { 33 final SessionInfo info; 34 35 private final Map<ProducerId, ProducerState> producers = new ConcurrentHashMap<ProducerId, ProducerState>(); 36 private final Map<ConsumerId, ConsumerState> consumers = new ConcurrentHashMap<ConsumerId, ConsumerState>(); 37 private final AtomicBoolean shutdown = new AtomicBoolean(false); 38 39 public SessionState(SessionInfo info) { 40 this.info = info; 41 } 42 43 public String toString() { 44 return info.toString(); 45 } 46 47 public void addProducer(ProducerInfo info) { 48 checkShutdown(); 49 producers.put(info.getProducerId(), new ProducerState(info)); 50 } 51 52 public ProducerState removeProducer(ProducerId id) { 53 ProducerState producerState = producers.remove(id); 54 if (producerState != null) { 55 if (producerState.getTransactionState() != null) { 56 // allow the transaction to recreate dependent producer on recovery 57 producerState.getTransactionState().addProducerState(producerState); 58 } 59 } 60 return producerState; 61 } 62 63 public void addConsumer(ConsumerInfo info) { 64 checkShutdown(); 65 consumers.put(info.getConsumerId(), new ConsumerState(info)); 66 } 67 68 public ConsumerState removeConsumer(ConsumerId id) { 69 return consumers.remove(id); 70 } 71 72 public SessionInfo getInfo() { 73 return info; 74 } 75 76 public Set<ConsumerId> getConsumerIds() { 77 return consumers.keySet(); 78 } 79 80 public Set<ProducerId> getProducerIds() { 81 return producers.keySet(); 82 } 83 84 public Collection<ProducerState> getProducerStates() { 85 return producers.values(); 86 } 87 88 public ProducerState getProducerState(ProducerId producerId) { 89 return producers.get(producerId); 90 } 91 92 public Collection<ConsumerState> getConsumerStates() { 93 return consumers.values(); 94 } 95 96 public ConsumerState getConsumerState(ConsumerId consumerId) { 97 return consumers.get(consumerId); 98 } 99 100 private void checkShutdown() { 101 if (shutdown.get()) { 102 throw new IllegalStateException("Disposed"); 103 } 104 } 105 106 public void shutdown() { 107 shutdown.set(false); 108 } 109 110 }