KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.activemq.command.ActiveMQDestination;
29 import org.apache.activemq.command.ConnectionInfo;
30 import org.apache.activemq.command.DestinationInfo;
31 import org.apache.activemq.command.SessionId;
32 import org.apache.activemq.command.SessionInfo;
33 import org.apache.activemq.command.TransactionId;
34
35 import java.util.concurrent.ConcurrentHashMap JavaDoc;
36 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
37
38 public class ConnectionState {
39     
40     final ConnectionInfo info;
41     private final ConcurrentHashMap JavaDoc transactions = new ConcurrentHashMap JavaDoc();
42     private final ConcurrentHashMap JavaDoc sessions = new ConcurrentHashMap JavaDoc();
43     private final List JavaDoc tempDestinations = Collections.synchronizedList(new ArrayList JavaDoc());
44     private final AtomicBoolean JavaDoc shutdown = new AtomicBoolean JavaDoc(false);
45     
46     public ConnectionState(ConnectionInfo info) {
47         this.info = info;
48         // Add the default session id.
49
addSession(new SessionInfo(info, -1));
50     }
51     
52     public String JavaDoc toString() {
53         return info.toString();
54     }
55
56     public void addTempDestination(DestinationInfo info) {
57         checkShutdown();
58         tempDestinations.add(info);
59     }
60
61     public void removeTempDestination(ActiveMQDestination destination) {
62         for (Iterator JavaDoc iter = tempDestinations.iterator(); iter.hasNext();) {
63             DestinationInfo di = (DestinationInfo) iter.next();
64             if( di.getDestination().equals(destination) ) {
65                 iter.remove();
66             }
67         }
68     }
69     
70     public void addTransactionState(TransactionId id) {
71         checkShutdown();
72         transactions.put(id, new TransactionState(id));
73     }
74     public TransactionState getTransactionState(TransactionId id) {
75         return (TransactionState)transactions.get(id);
76     }
77     public Collection JavaDoc getTransactionStates() {
78         return transactions.values();
79     }
80     public TransactionState removeTransactionState(TransactionId id) {
81         return (TransactionState) transactions.remove(id);
82     }
83
84     public void addSession(SessionInfo info) {
85         checkShutdown();
86         sessions.put(info.getSessionId(), new SessionState(info));
87     }
88     public SessionState removeSession(SessionId id) {
89         return (SessionState)sessions.remove(id);
90     }
91     public SessionState getSessionState(SessionId id) {
92         return (SessionState)sessions.get(id);
93     }
94     
95     public ConnectionInfo getInfo() {
96         return info;
97     }
98
99     public Set JavaDoc getSessionIds() {
100         return sessions.keySet();
101     }
102
103     public List JavaDoc getTempDesinations() {
104         return tempDestinations;
105     }
106
107     public Collection JavaDoc getSessionStates() {
108         return sessions.values();
109     }
110     
111     private void checkShutdown() {
112         if( shutdown.get() )
113             throw new IllegalStateException JavaDoc("Disposed");
114     }
115     
116     public void shutdown() {
117         if( shutdown.compareAndSet(false, true) ) {
118             for (Iterator JavaDoc iter = sessions.values().iterator(); iter.hasNext();) {
119                 SessionState ss = (SessionState) iter.next();
120                 ss.shutdown();
121             }
122         }
123     }
124 }
125
Popular Tags