KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > serverless > GroupState


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.serverless;
8
9 import org.jboss.logging.Logger;
10 import org.jgroups.Address;
11 import java.util.Map JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import org.jgroups.util.Util;
16 import java.util.Iterator JavaDoc;
17
18 /**
19  *
20  * @author Ovidiu Feodorov <ovidiu@jboss.org>
21  * @version $Revision: 1.1 $ $Date: 2004/04/15 22:54:19 $
22  *
23  **/

24 class GroupState {
25
26     private static final Logger log = Logger.getLogger(GroupState.class);
27
28     private Map JavaDoc queues;
29
30     public synchronized byte[] toByteBuffer() throws Exception JavaDoc {
31         return Util.objectToByteBuffer(queues);
32     }
33
34     public synchronized void fromByteBuffer(byte[] ba) throws Exception JavaDoc {
35         Object JavaDoc o = Util.objectFromByteBuffer(ba);
36         if (o == null) {
37             queues = null;
38         }
39         else if (o instanceof Map JavaDoc) {
40             queues = (Map JavaDoc)o;
41         }
42         else {
43             throw new IllegalStateException JavaDoc("Invalid group state");
44         }
45     }
46
47
48     public synchronized void addQueueReceiver(String JavaDoc queueName, Address addr, String JavaDoc sessionID,
49                                               String JavaDoc queueReceiverID) {
50         if (queues == null) {
51             queues = new HashMap JavaDoc();
52         }
53         List JavaDoc l = (List JavaDoc)queues.get(queueName);
54         if (l == null) {
55             l = new ArrayList JavaDoc();
56             queues.put(queueName, l);
57         }
58         QueueReceiverAddress ra = new QueueReceiverAddress(addr, sessionID, queueReceiverID);
59         if (l.contains(ra)) {
60             log.warn(ra+" already in the group state");
61             return;
62         }
63         l.add(ra);
64         log.debug("New GroupState: "+toString());
65     }
66
67     /**
68      * If no such queue receiver is found, the method logs the event as a warning.
69      **/

70     public synchronized void removeQueueReceiver(String JavaDoc queueName, Address addr, String JavaDoc sessionID,
71                                                  String JavaDoc queueReceiverID) {
72
73         String JavaDoc noSuchReceiverMsg =
74             "No such queue receiver: "+queueName+"/"+addr+"/"+sessionID+"/"+queueReceiverID;
75
76         List JavaDoc l = null;
77
78         if (queues == null ||
79             ((l = (List JavaDoc)queues.get(queueName)) == null) ||
80             l.isEmpty()) {
81             log.warn(noSuchReceiverMsg);
82         }
83         if (!l.remove(new QueueReceiverAddress(addr, sessionID, queueReceiverID))) {
84             log.warn(noSuchReceiverMsg);
85         }
86         log.debug("New GroupState: "+toString());
87     }
88
89
90     /**
91      * Could return null if there is no receiver for the queue
92      **/

93     public synchronized QueueReceiverAddress selectReceiver(String JavaDoc queueName) {
94         
95         if (queues == null) {
96             return null;
97         }
98         List JavaDoc l = (List JavaDoc)queues.get(queueName);
99         if (l == null || l.size() == 0) {
100             return null;
101         }
102         QueueReceiverAddress selected = null;
103         int crtidx = 0;
104         for(Iterator JavaDoc i = l.iterator(); i.hasNext(); crtidx++) {
105             QueueReceiverAddress crt = (QueueReceiverAddress)i.next();
106             if (crt.isNextForDelivery()) {
107                 selected = crt;
108                 crt.setNextForDelivery(false);
109                 ((QueueReceiverAddress)l.get((crtidx + 1) % l.size())).setNextForDelivery(true);
110                 break;
111             }
112         }
113         if (selected == null) {
114             selected = (QueueReceiverAddress)l.get(0);
115             ((QueueReceiverAddress)l.get(1 % l.size())).setNextForDelivery(true);;
116             
117         }
118         return selected;
119     }
120
121
122     public String JavaDoc toString() {
123         return queues == null ? "null" : queues.toString();
124     }
125
126 }
127
Popular Tags