KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > queue > AbstractPullOutgoingHandlerImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Vivien Quema
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.queue;
26
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.dream.AbstractComponent;
30 import org.objectweb.dream.InterruptedPullException;
31 import org.objectweb.dream.Pull;
32 import org.objectweb.dream.PullException;
33 import org.objectweb.dream.message.Message;
34 import org.objectweb.fractal.api.NoSuchInterfaceException;
35 import org.objectweb.fractal.api.control.IllegalBindingException;
36 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
37
38 /**
39  * Abstract implementation of a Pull outgoing handler. It implements overflow
40  * policies as defined in {@link PullQueueAttributeController}. Moreover, it
41  * implements the <code>pull</code> method: it checks for available message
42  * and then applies appropriate policy.
43  * <p>
44  * Pull incoming handler developers can inherit this class. They must implement
45  * two methods:
46  * <ul>
47  * <li><code>hasAvailableMessage()</code> returns a boolean indicating
48  * whether a message is available for removal.</li>
49  * <li><code>doPull()</code> removes a message from the buffer.</li>
50  * </ul>
51  */

52 public abstract class AbstractPullOutgoingHandlerImpl extends AbstractComponent
53     implements
54       Pull,
55       PullQueueAttributeController
56 {
57
58   protected boolean blockingPull = true;
59
60   // Client interfaces
61
protected Buffer bufferItf;
62   protected BufferRemoveFirstLast bufferRemoveFirstLastItf;
63
64   // ---------------------------------------------------------------------------
65
// Implementation of the Pull interface.
66
// ---------------------------------------------------------------------------
67

68   /**
69    * @see org.objectweb.dream.Pull#pull(java.util.Map)
70    */

71   public synchronized Message pull(Map JavaDoc context) throws PullException
72   {
73     if (!hasAvailableMessage() && !blockingPull)
74     {
75       return null;
76     }
77     try
78     {
79       return doPull();
80     }
81     catch (InterruptedException JavaDoc e)
82     {
83       throw new InterruptedPullException(
84           "Interrupted while waiting for an available message", e);
85     }
86   }
87
88   // ---------------------------------------------------------------------------
89
// Methods that must be implemented by concrete super classes.
90
// ---------------------------------------------------------------------------
91

92   /**
93    * Checks whether there is an available message.
94    *
95    * @return <code>true</code> if there is an available message.
96    */

97   protected abstract boolean hasAvailableMessage();
98
99   /**
100    * Pulls a message from the buffer (i.e. get or remove). This method should
101    * not check if there is a message available provided it has already been
102    * done.
103    *
104    * @return a message.
105    * @throws InterruptedException if it is interrupted while waiting an
106    * available message.
107    */

108   protected abstract Message doPull() throws InterruptedException JavaDoc;
109
110   // ---------------------------------------------------------------------------
111
// Implementation of the PullQueueAttributeController interface.
112
// ---------------------------------------------------------------------------
113

114   /**
115    * @see org.objectweb.dream.queue.PullQueueAttributeController#setBlockingPull(boolean)
116    */

117   public synchronized void setBlockingPull(boolean blockingPull)
118   {
119     this.blockingPull = blockingPull;
120   }
121
122   /**
123    * @see org.objectweb.dream.queue.PullQueueAttributeController#getBlockingPull()
124    */

125   public boolean getBlockingPull()
126   {
127     return blockingPull;
128   }
129
130   // ---------------------------------------------------------------------------
131
// Implementation of the BindingController interface.
132
// ---------------------------------------------------------------------------
133

134   /**
135    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
136    * Object)
137    */

138   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
139       throws NoSuchInterfaceException, IllegalBindingException,
140       IllegalLifeCycleException
141   {
142     super.bindFc(clientItfName, serverItf);
143     if (clientItfName.equals(Buffer.ITF_NAME))
144     {
145       bufferItf = (Buffer) serverItf;
146     }
147     else if (clientItfName.equals(BufferRemoveFirstLast.ITF_NAME))
148     {
149       bufferRemoveFirstLastItf = (BufferRemoveFirstLast) serverItf;
150     }
151   }
152
153   /**
154    * @see org.objectweb.fractal.api.control.BindingController#listFc()
155    */

156   public String JavaDoc[] listFc()
157   {
158     return new String JavaDoc[]{Buffer.ITF_NAME, BufferRemoveFirstLast.ITF_NAME};
159   }
160
161 }
Popular Tags