KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > queue > BasicMessageQueueAdapter


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21
22 package org.jacorb.notification.queue;
23
24 import org.jacorb.notification.interfaces.Message;
25
26 /**
27  * @author Alphonse Bendt
28  * @version $Id: BasicMessageQueueAdapter.java,v 1.4 2005/04/27 10:40:22 alphonse.bendt Exp $
29  */

30 public class BasicMessageQueueAdapter implements MessageQueueAdapter
31 {
32     private final MessageQueue queue_;
33
34     private static final Message[] EMPTY = new Message[0];
35
36     /**
37      *
38      */

39     public BasicMessageQueueAdapter(MessageQueue queue)
40     {
41         super();
42
43         queue_ = queue;
44     }
45
46     /*
47      * @see org.jacorb.notification.queue.EventQueueDelegate#enqeue(org.jacorb.notification.interfaces.Message)
48      */

49     public void enqeue(Message message)
50     {
51         // enqueue a copy of the Message to ensure this queue owns the Message
52
queue_.put(message);
53     }
54
55     /*
56      * (non-Javadoc)
57      *
58      * @see org.jacorb.notification.queue.EventQueueDelegate#hasPendingData()
59      */

60     public boolean hasPendingMessages()
61     {
62         return !queue_.isEmpty();
63     }
64
65     /*
66      * (non-Javadoc)
67      *
68      * @see org.jacorb.notification.queue.EventQueueDelegate#getPendingMessagesCount()
69      */

70     public int getPendingMessagesCount()
71     {
72         return queue_.getSize();
73     }
74
75     /*
76      * caller gets ownership over the returned message
77      *
78      * @see org.jacorb.notification.queue.EventQueueDelegate#getMessageBlocking()
79      */

80     public Message getMessageBlocking() throws InterruptedException JavaDoc
81     {
82         return queue_.getMessage(true);
83     }
84
85     /*
86      * caller gets ownership over the returned message
87      *
88      * @see org.jacorb.notification.queue.EventQueueDelegate#getMessageNoBlock()
89      */

90     public Message getMessageNoBlock() throws InterruptedException JavaDoc
91     {
92         return queue_.getMessage(false);
93     }
94
95     /*
96      * caller gets ownership over the returned message
97      *
98      * @see org.jacorb.notification.queue.EventQueueDelegate#getAllMessages()
99      */

100     public Message[] getAllMessages() throws InterruptedException JavaDoc
101     {
102         return queue_.getAllMessages(false);
103     }
104
105     /*
106      * caller gets ownership over the returned message
107      *
108      * @see org.jacorb.notification.queue.EventQueueDelegate#getUpToMessages(int)
109      */

110     public Message[] getUpToMessages(int max) throws InterruptedException JavaDoc
111     {
112         return queue_.getMessages(max, false);
113     }
114
115     /*
116      * caller gets ownership over the returned message
117      *
118      * @see org.jacorb.notification.queue.EventQueueDelegate#getAtLeastMessages(int)
119      */

120     public Message[] getAtLeastMessages(int min) throws InterruptedException JavaDoc
121     {
122         if (queue_.getSize() >= min)
123         {
124             return queue_.getAllMessages(true);
125         }
126
127         return EMPTY;
128     }
129
130     public void clear()
131     {
132         try
133         {
134             Message[] allMessages = queue_.getAllMessages(false);
135
136             for (int i = 0; i < allMessages.length; i++)
137             {
138                 Message message = allMessages[i];
139                 message.dispose();
140             }
141         } catch (InterruptedException JavaDoc e)
142         {
143             // should not happen as above call does not wait.
144
}
145     }
146     
147     public String JavaDoc toString()
148     {
149         return queue_.toString();
150     }
151 }
Popular Tags