com/scalagent/kjoram/QueueBrowser.java

00001 /*
00002  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
00003  * Copyright (C) 2001 - ScalAgent Distributed Technologies
00004  * Copyright (C) 1996 - Dyade
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA.
00020  *
00021  * Initial developer(s): Frederic Maistre (INRIA)
00022  * Contributor(s): Nicolas Tachker (ScalAgent)
00023  */
00024 package com.scalagent.kjoram;
00025 
00026 import com.scalagent.kjoram.jms.*;
00027 
00028 import java.util.*;
00029 
00030 import com.scalagent.kjoram.excepts.IllegalStateException;
00031 import com.scalagent.kjoram.excepts.*;
00032 
00033 
00034 public class QueueBrowser
00035 {
00037   private Session sess;
00039   private Queue queue;
00041   private String selector;
00043   private boolean closed = false;
00044 
00056   QueueBrowser(Session sess, Queue queue, String selector) throws JMSException
00057   {
00058     if (queue == null)
00059       throw new InvalidDestinationException("Invalid queue: " + queue);
00060 
00061     this.sess = sess;
00062     this.queue = queue;
00063     this.selector = selector;
00064 
00065     if (sess.browsers == null)
00066       sess.browsers = new Vector();
00067     sess.browsers.addElement(this);
00068 
00069     if (JoramTracing.dbgClient)
00070       JoramTracing.log(JoramTracing.DEBUG, this + ": created.");
00071   }
00072 
00074   public String toString()
00075   {
00076     return "QueueBrowser:" + sess.ident;
00077   }
00078 
00084   public Queue getQueue() throws JMSException
00085   {
00086     if (closed)
00087       throw new IllegalStateException("Forbidden call on a closed browser.");
00088 
00089     return queue;
00090   }
00091 
00097   public String getMessageSelector() throws JMSException
00098   {
00099     if (closed)
00100       throw new IllegalStateException("Forbidden call on a closed browser.");
00101 
00102     return selector;
00103   }
00104 
00114   public Enumeration getEnumeration() throws JMSException
00115   {
00116     if (JoramTracing.dbgClient)
00117       JoramTracing.log(JoramTracing.DEBUG, this
00118                        + ": requests an enumeration.");
00119     if (closed)
00120       throw new IllegalStateException("Forbidden call on a closed browser.");
00121 
00122     // Sending a "browse" request:
00123     QBrowseRequest browReq = new QBrowseRequest(queue.getName(), selector);
00124     // Expecting an answer:
00125     QBrowseReply reply = (QBrowseReply) sess.cnx.syncRequest(browReq);
00126 
00127     if (JoramTracing.dbgClient)
00128       JoramTracing.log(JoramTracing.DEBUG, this
00129                        + ": received an enumeration.");
00130 
00131     // Processing the received messages:
00132     Vector momMessages = reply.getMessages();
00133     Vector messages = null;
00134     if (momMessages != null) {
00135       messages = new Vector();
00136       com.scalagent.kjoram.messages.Message momMsg;
00137       for (int i = 0; i < momMessages.size(); i++) {
00138         momMsg = (com.scalagent.kjoram.messages.Message) momMessages.elementAt(i);
00139         messages.addElement(Message.wrapMomMessage(null, momMsg));
00140       }
00141     }
00142                           
00143     // Return an enumeration:
00144     return new QueueEnumeration(messages);
00145   }
00146 
00152   public void close() throws JMSException
00153   {
00154     // Ignoring the call if the browser is already closed:
00155     if (closed)
00156       return;
00157 
00158     sess.browsers.removeElement(this);
00159     closed = true;
00160 
00161     if (JoramTracing.dbgClient)
00162       JoramTracing.log(JoramTracing.DEBUG, this + " closed.");
00163   }
00164 
00169   private class QueueEnumeration implements java.util.Enumeration
00170   {
00172     private Vector messages;
00173 
00179     private QueueEnumeration(Vector messages)
00180     {
00181       this.messages = messages;
00182     }
00183 
00185     public boolean hasMoreElements()
00186     {
00187       if (messages == null)
00188         return false;
00189       return (! messages.isEmpty());
00190     }
00191 
00193     public Object nextElement()
00194     {
00195       if (messages == null || messages.isEmpty())
00196         throw new NoSuchElementException();
00197 
00198       Object ret = messages.elementAt(0);
00199       messages.removeElementAt(0);
00200       return ret;
00201     }
00202   }
00203 }

Generated on Tue Sep 16 16:14:24 2008 for joram by  doxygen 1.5.0