1 18 19 package org.apache.activemq.state; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Set ; 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 ; 36 import java.util.concurrent.atomic.AtomicBoolean ; 37 38 public class ConnectionState { 39 40 final ConnectionInfo info; 41 private final ConcurrentHashMap transactions = new ConcurrentHashMap (); 42 private final ConcurrentHashMap sessions = new ConcurrentHashMap (); 43 private final List tempDestinations = Collections.synchronizedList(new ArrayList ()); 44 private final AtomicBoolean shutdown = new AtomicBoolean (false); 45 46 public ConnectionState(ConnectionInfo info) { 47 this.info = info; 48 addSession(new SessionInfo(info, -1)); 50 } 51 52 public String 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 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 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 getSessionIds() { 100 return sessions.keySet(); 101 } 102 103 public List getTempDesinations() { 104 return tempDestinations; 105 } 106 107 public Collection getSessionStates() { 108 return sessions.values(); 109 } 110 111 private void checkShutdown() { 112 if( shutdown.get() ) 113 throw new IllegalStateException ("Disposed"); 114 } 115 116 public void shutdown() { 117 if( shutdown.compareAndSet(false, true) ) { 118 for (Iterator iter = sessions.values().iterator(); iter.hasNext();) { 119 SessionState ss = (SessionState) iter.next(); 120 ss.shutdown(); 121 } 122 } 123 } 124 } 125 | Popular Tags |