KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > state > SessionState


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.state;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.activemq.command.ConsumerId;
25 import org.apache.activemq.command.ConsumerInfo;
26 import org.apache.activemq.command.ProducerId;
27 import org.apache.activemq.command.ProducerInfo;
28 import org.apache.activemq.command.SessionInfo;
29
30 import java.util.concurrent.ConcurrentHashMap JavaDoc;
31 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
32
33 public class SessionState {
34     final SessionInfo info;
35     
36     public final ConcurrentHashMap JavaDoc producers = new ConcurrentHashMap JavaDoc();
37     public final ConcurrentHashMap JavaDoc consumers = new ConcurrentHashMap JavaDoc();
38     private final AtomicBoolean JavaDoc shutdown = new AtomicBoolean JavaDoc(false);
39     
40     public SessionState(SessionInfo info) {
41         this.info = info;
42     }
43     public String JavaDoc 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     public ProducerState removeProducer(ProducerId id) {
52         return (ProducerState) producers.remove(id);
53     }
54     
55     public void addConsumer(ConsumerInfo info) {
56         checkShutdown();
57         consumers.put(info.getConsumerId(), new ConsumerState(info));
58     }
59     public ConsumerState removeConsumer(ConsumerId id) {
60         return (ConsumerState) consumers.remove(id);
61     }
62     
63     public SessionInfo getInfo() {
64         return info;
65     }
66     
67     public Set JavaDoc getConsumerIds() {
68         return consumers.keySet();
69     }
70     public Set JavaDoc getProducerIds() {
71         return producers.keySet();
72     }
73     public Collection JavaDoc getProducerStates() {
74         return producers.values();
75     }
76     public ProducerState getProducerState(ProducerId producerId) {
77         return (ProducerState) producers.get(producerId);
78     }
79     
80     public Collection JavaDoc getConsumerStates() {
81         return consumers.values();
82     }
83     
84     public ConsumerState getConsumerState(ConsumerId consumerId) {
85         return (ConsumerState)consumers.get(consumerId);
86     }
87     
88     private void checkShutdown() {
89         if( shutdown.get() )
90             throw new IllegalStateException JavaDoc("Disposed");
91     }
92     
93     public void shutdown() {
94         shutdown.set(false);
95     }
96
97 }
98
Popular Tags