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
018 package org.apache.activemq.state;
019
020 import java.util.Collection;
021 import java.util.Map;
022 import java.util.Set;
023 import java.util.concurrent.ConcurrentHashMap;
024 import java.util.concurrent.atomic.AtomicBoolean;
025
026 import org.apache.activemq.command.ConsumerId;
027 import org.apache.activemq.command.ConsumerInfo;
028 import org.apache.activemq.command.ProducerId;
029 import org.apache.activemq.command.ProducerInfo;
030 import org.apache.activemq.command.SessionInfo;
031
032 public class SessionState {
033 final SessionInfo info;
034
035 private final Map<ProducerId, ProducerState> producers = new ConcurrentHashMap<ProducerId, ProducerState>();
036 private final Map<ConsumerId, ConsumerState> consumers = new ConcurrentHashMap<ConsumerId, ConsumerState>();
037 private final AtomicBoolean shutdown = new AtomicBoolean(false);
038
039 public SessionState(SessionInfo info) {
040 this.info = info;
041 }
042
043 public String toString() {
044 return info.toString();
045 }
046
047 public void addProducer(ProducerInfo info) {
048 checkShutdown();
049 producers.put(info.getProducerId(), new ProducerState(info));
050 }
051
052 public ProducerState removeProducer(ProducerId id) {
053 ProducerState producerState = producers.remove(id);
054 if (producerState != null) {
055 if (producerState.getTransactionState() != null) {
056 // allow the transaction to recreate dependent producer on recovery
057 producerState.getTransactionState().addProducerState(producerState);
058 }
059 }
060 return producerState;
061 }
062
063 public void addConsumer(ConsumerInfo info) {
064 checkShutdown();
065 consumers.put(info.getConsumerId(), new ConsumerState(info));
066 }
067
068 public ConsumerState removeConsumer(ConsumerId id) {
069 return consumers.remove(id);
070 }
071
072 public SessionInfo getInfo() {
073 return info;
074 }
075
076 public Set<ConsumerId> getConsumerIds() {
077 return consumers.keySet();
078 }
079
080 public Set<ProducerId> getProducerIds() {
081 return producers.keySet();
082 }
083
084 public Collection<ProducerState> getProducerStates() {
085 return producers.values();
086 }
087
088 public ProducerState getProducerState(ProducerId producerId) {
089 return producers.get(producerId);
090 }
091
092 public Collection<ConsumerState> getConsumerStates() {
093 return consumers.values();
094 }
095
096 public ConsumerState getConsumerState(ConsumerId consumerId) {
097 return consumers.get(consumerId);
098 }
099
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 }