KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > poa > RequestQueue


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

22
23 import java.util.*;
24
25 import org.apache.avalon.framework.logger.Logger;
26 import org.apache.avalon.framework.configuration.*;
27
28 import org.jacorb.orb.dsi.ServerRequest;
29 import org.jacorb.poa.except.ResourceLimitReachedException;
30 import org.jacorb.poa.util.StringPair;
31
32 /**
33  * This class manages a queue of ServerRequest objects.
34  *
35  * @author Reimo Tiedemann, FU Berlin
36  * @version $Id: RequestQueue.java,v 1.18 2004/05/06 12:40:00 nicolas Exp $
37  */

38 public class RequestQueue
39     implements Configurable
40 {
41     private RequestQueueListener queueListener;
42     private RequestController controller;
43
44     /** the configuration object for this queue */
45     private org.jacorb.config.Configuration configuration = null;
46     private Logger logger;
47     private int queueMin;
48     private int queueMax;
49     private boolean queueWait;
50
51     private boolean configured = false;
52
53     private List queue =
54         new ArrayList (POAConstants.QUEUE_CAPACITY_INI);
55                     // POAConstants.QUEUE_CAPACITY_INC);
56

57     private RequestQueue()
58     {
59     }
60
61     protected RequestQueue(RequestController controller)
62     {
63         this.controller = controller;
64     }
65
66     public void configure(Configuration myConfiguration)
67         throws ConfigurationException
68     {
69         this.configuration = (org.jacorb.config.Configuration)myConfiguration;
70         logger = configuration.getNamedLogger("jacorb.poa.queue");
71         queueMax = configuration.getAttributeAsInteger("jacorb.poa.queue_max", 100);
72         queueMin = configuration.getAttributeAsInteger("jacorb.poa.queue_min", 10);
73         queueWait = configuration.getAttributeAsBoolean("jacorb.poa.queue_wait",false);
74         configured = true;
75     }
76
77     /**
78      * Adds a request to this queue. The properties
79      * <code>jacorb.poa.queue_{min,max,wait}</code> specify what happens
80      * when the queue is full, i.e. when it already contains
81      * <code>queue_max</code> requests. If <code>queue_wait</code> is
82      * <i>off</i>, then this method does not add the request and throws a
83      * <code>ResourceLimitReachedException</code>. If <code>queue_wait</code>
84      * is <i>on</i>, then this method blocks until no more than
85      * <code>queue_min</code> requests are in the queue; it then adds the
86      * request, and returns.
87      */

88
89     protected synchronized void add(ServerRequest request)
90         throws ResourceLimitReachedException
91     {
92         if (!configured)
93             throw new Error JavaDoc("RQ.add(): not configured!");
94
95         if (queue.size() >= queueMax )
96         {
97             if (logger.isWarnEnabled())
98             {
99                 logger.warn("Request queue is full, consider increasing "
100                           + "jacorb.poa.queue_max (currently: "
101                           + queueMax + ")");
102             }
103
104             if ( queueWait )
105             {
106                 while (queue.size() > queueMin )
107                 {
108                     try
109                     {
110                         this.wait();
111                     }
112                     catch (InterruptedException JavaDoc ex)
113                     {
114                         // ignore
115
}
116                 }
117             }
118             else
119             {
120                 throw new ResourceLimitReachedException();
121             }
122         }
123         queue.add(request);
124
125         if (queue.size() == 1)
126         {
127             controller.continueToWork();
128         }
129
130         if (logger.isDebugEnabled())
131         {
132             logger.debug("rid: " + request.requestId() +
133                          " opname: " + request.operation() +
134                          " is queued (queue size: " + queue.size() + ")");
135         }
136
137         // notify a queue listener
138
if (queueListener != null)
139             queueListener.requestAddedToQueue(request, queue.size());
140     }
141
142     protected synchronized void addRequestQueueListener(RequestQueueListener listener)
143     {
144         if (!configured)
145             throw new Error JavaDoc("RQ.add(): not configured!");
146         queueListener = EventMulticaster.add(queueListener, listener);
147     }
148
149     protected synchronized StringPair[] deliverContent()
150     {
151          if (!configured)
152             throw new Error JavaDoc("RQ.add(): not configured!");
153        StringPair[] result = new StringPair[queue.size()];
154         Iterator en = queue.iterator();
155         ServerRequest sr;
156         for (int i=0; i<result.length; i++)
157         {
158             sr = (ServerRequest) en.next();
159             result[i] = new StringPair(sr.requestId()+"", new String JavaDoc( sr.objectId() ) );
160         }
161         return result;
162     }
163
164     protected synchronized ServerRequest getElementAndRemove(int rid)
165     {
166         if (!configured)
167             throw new Error JavaDoc("RQ.add(): not configured!");
168         if (!queue.isEmpty())
169         {
170             Iterator en = queue.iterator();
171             ServerRequest result;
172             while (en.hasNext())
173             {
174                 result = (ServerRequest) en.next();
175                 if (result.requestId() == rid)
176                 {
177                     en.remove();
178                     this.notifyAll();
179                     // notify a queue listener
180
if (queueListener != null)
181                         queueListener.requestRemovedFromQueue(result, queue.size());
182                     return result;
183                 }
184             }
185         }
186         return null;
187     }
188
189     protected synchronized ServerRequest getFirst()
190     {
191          if (!configured)
192             throw new Error JavaDoc("RQ.add(): not configured!");
193        if (!queue.isEmpty())
194         {
195             return (ServerRequest) queue.get(0);
196         }
197         return null;
198     }
199
200     protected boolean isEmpty()
201     {
202         if (!configured)
203             throw new Error JavaDoc("RQ.add(): not configured!");
204         return queue.isEmpty();
205     }
206
207     protected synchronized ServerRequest removeFirst()
208     {
209         if (!configured)
210             throw new Error JavaDoc("RQ.add(): not configured!");
211         if (!queue.isEmpty())
212         {
213             ServerRequest result = (ServerRequest) queue.get(0);
214             queue.remove(0);
215             this.notifyAll();
216             // notify a queue listener
217

218             if (queueListener != null)
219                 queueListener.requestRemovedFromQueue(result, queue.size());
220             return result;
221         }
222         return null;
223     }
224
225     protected synchronized ServerRequest removeLast()
226     {
227          if (!configured)
228             throw new Error JavaDoc("RQ.add(): not configured!");
229        if (!queue.isEmpty())
230         {
231             int index = queue.size() - 1;
232             ServerRequest result = (ServerRequest) queue.get(index);
233             queue.remove(index);
234             this.notifyAll();
235             // notify a queue listener
236
if (queueListener != null)
237                 queueListener.requestRemovedFromQueue(result, queue.size());
238             return result;
239         }
240         return null;
241     }
242
243     protected synchronized void removeRequestQueueListener(RequestQueueListener listener)
244     {
245         if (!configured)
246             throw new Error JavaDoc("RQ.add(): not configured!");
247         queueListener = EventMulticaster.remove(queueListener, listener);
248     }
249
250     protected int size()
251     {
252         return queue.size();
253     }
254 }
255
Popular Tags