001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021:
022: package org.jacorb.notification.queue;
023:
024: import org.jacorb.notification.interfaces.Message;
025: import org.jacorb.notification.queue.MessageQueue.DiscardListener;
026:
027: /**
028: * @author Alphonse Bendt
029: * @version $Id: DefaultMessageQueueAdapter.java,v 1.1 2006/02/25 15:28:40 alphonse.bendt Exp $
030: */
031: public class DefaultMessageQueueAdapter implements MessageQueueAdapter {
032: private final MessageQueue queue_;
033:
034: private static final Message[] EMPTY = new Message[0];
035:
036: public DefaultMessageQueueAdapter(MessageQueue queue) {
037: super ();
038:
039: queue_ = queue;
040: }
041:
042: /*
043: * @see org.jacorb.notification.queue.EventQueueDelegate#enqeue(org.jacorb.notification.interfaces.Message)
044: */
045: public void enqeue(Message message) {
046: queue_.put(message);
047: }
048:
049: /*
050: * (non-Javadoc)
051: *
052: * @see org.jacorb.notification.queue.EventQueueDelegate#hasPendingData()
053: */
054: public boolean hasPendingMessages() {
055: return !queue_.isEmpty();
056: }
057:
058: /*
059: * (non-Javadoc)
060: *
061: * @see org.jacorb.notification.queue.EventQueueDelegate#getPendingMessagesCount()
062: */
063: public int getPendingMessagesCount() {
064: return queue_.getSize();
065: }
066:
067: /*
068: * caller gets ownership over the returned message
069: *
070: * @see org.jacorb.notification.queue.EventQueueDelegate#getMessageBlocking()
071: */
072: public Message getMessageBlocking() throws InterruptedException {
073: return queue_.getMessage(true);
074: }
075:
076: /*
077: * caller gets ownership over the returned message
078: *
079: * @see org.jacorb.notification.queue.EventQueueDelegate#getMessageNoBlock()
080: */
081: public Message getMessageNoBlock() throws InterruptedException {
082: return queue_.getMessage(false);
083: }
084:
085: /*
086: * caller gets ownership over the returned message
087: *
088: * @see org.jacorb.notification.queue.EventQueueDelegate#getAllMessages()
089: */
090: public Message[] getAllMessages() throws InterruptedException {
091: return queue_.getAllMessages(false);
092: }
093:
094: /*
095: * caller gets ownership over the returned message
096: *
097: * @see org.jacorb.notification.queue.EventQueueDelegate#getUpToMessages(int)
098: */
099: public Message[] getUpToMessages(int max)
100: throws InterruptedException {
101: return queue_.getMessages(max, false);
102: }
103:
104: /*
105: * caller gets ownership over the returned message
106: *
107: * @see org.jacorb.notification.queue.EventQueueDelegate#getAtLeastMessages(int)
108: */
109: public Message[] getAtLeastMessages(int min)
110: throws InterruptedException {
111: if (queue_.getSize() >= min) {
112: return queue_.getAllMessages(true);
113: }
114:
115: return EMPTY;
116: }
117:
118: public void clear() {
119: try {
120: Message[] allMessages = queue_.getAllMessages(false);
121:
122: for (int i = 0; i < allMessages.length; i++) {
123: Message message = allMessages[i];
124: message.dispose();
125: }
126: } catch (InterruptedException e) {
127: // should not happen as above call does not wait.
128: }
129: }
130:
131: public String toString() {
132: return queue_.toString();
133: }
134:
135: public String getDiscardPolicyName() {
136: return queue_.getDiscardPolicyName();
137: }
138:
139: public String getOrderPolicyName() {
140: return queue_.getOrderPolicyName();
141: }
142:
143: public void addDiscardListener(DiscardListener listener) {
144: queue_.addDiscardListener(listener);
145: }
146:
147: public void removeDiscardListener(DiscardListener listener) {
148: queue_.removeDiscardListener(listener);
149: }
150: }
|