KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > http > HTTPClientILStorageQueue


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.il.http;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.jboss.logging.Logger;
30
31 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
32
33 /**
34  * Stores requests on behalf of clients. This could of course, be done
35  * with a JMS queue, but I decided this would be a lighter weight solution.
36  *
37  * @author Nathan Phelps (nathan@jboss.org)
38  * @version $Revision: 37459 $
39  * @created January 15, 2003
40  */

41 public class HTTPClientILStorageQueue
42 {
43     
44     private static Logger log = Logger.getLogger(HTTPClientILStorageQueue.class);
45     private static HTTPClientILStorageQueue instance = null;
46     private Map JavaDoc map = new HashMap JavaDoc();
47     private Object JavaDoc queueLock = new Object JavaDoc();
48     
49     /* The static Id variable provides a mechanism for us to identify a particular
50      * ClientIL's requests in the map. This value is retrieved by the ClientILService
51      * init method, which sends an HTTPIL request to the servlet, which in turn
52      * asks this singleton instance for the next Id. If you are wondering why
53      * we need to do this, why we just can't use the *actual* connection Id,
54      * please see my comments in ClientILService.
55      */

56     private static long id = 100; //Start at 100 so we don't get confused with the Connection Ids.
57
private static Object JavaDoc idLock = new Object JavaDoc();
58     
59     private HTTPClientILStorageQueue()
60     {
61         if (log.isTraceEnabled())
62         {
63             log.trace("created");
64         }
65     }
66     
67     public static synchronized HTTPClientILStorageQueue getInstance()
68     {
69         if (log.isTraceEnabled())
70         {
71             log.trace("getInstance()");
72         }
73         if (instance == null)
74         {
75             instance = new HTTPClientILStorageQueue();
76         }
77         return instance;
78     }
79     
80     public void put(HTTPILRequest request, String JavaDoc clientIlId) throws InterruptedException JavaDoc
81     {
82         if (log.isTraceEnabled())
83         {
84             log.trace("put(HTTPILRequest " + request.toString() + ", String " + clientIlId + ")");
85         }
86         if (clientIlId == null)
87         {
88             log.warn("A request was put in a storage queue for a null ClientIl.");
89             
90             return;
91         }
92         synchronized(this.queueLock)
93         {
94             if (this.map.containsKey(clientIlId))
95             {
96                 if (log.isDebugEnabled())
97                 {
98                     log.debug("ClientIL #" + clientIlId + " has existing storage queue, adding request to it.");
99                 }
100                 LinkedQueue queue = (LinkedQueue)this.map.get(clientIlId);
101                 queue.put(request);
102             }
103             else
104             {
105                 if (log.isDebugEnabled())
106                 {
107                     log.debug("ClientIL #" + clientIlId + " doesn't have a storage queue. Creating one and adding the request.");
108                 }
109                 LinkedQueue queue = new LinkedQueue();
110                 queue.put(request);
111                 this.map.put(clientIlId, queue);
112             }
113         }
114     }
115     
116     public HTTPILRequest[] get(String JavaDoc clientIlId, long timeout)
117     {
118         if (log.isTraceEnabled())
119         {
120             log.trace("get(String " + clientIlId + ")");
121         }
122         
123         if (clientIlId == null)
124         {
125             log.warn("A get was issued with a null clientIL Id.");
126         }
127         
128         LinkedQueue queue;
129         synchronized(queueLock)
130         {
131             queue = (LinkedQueue)this.map.get(clientIlId);
132             if (queue == null)
133             {
134                 if (log.isDebugEnabled())
135                 {
136                     log.debug("ClientIL #" + clientIlId + " doesn't have a storage queue. Creating new one.");
137                 }
138                 queue = new LinkedQueue();
139                 this.map.put(clientIlId, queue); // create a new queue for this client
140
}
141         }
142         ArrayList JavaDoc messageList = new ArrayList JavaDoc();
143         try
144         {
145             if (log.isDebugEnabled())
146             {
147                 log.debug("Polling the queue for " + String.valueOf(timeout) + " milliseconds on behalf of clientIL #" + clientIlId + ".");
148             }
149             Object JavaDoc object = queue.poll(timeout);
150             if (object != null)
151             {
152                 if (log.isDebugEnabled())
153                 {
154                     log.debug("Poll returned a HTTPILRequest, adding it to our list of requests to deliver to clientIL #" + clientIlId + ".");
155                 }
156                 messageList.add(object);
157                 while ((object = queue.poll(0)) != null)
158                 {
159                     if (log.isDebugEnabled())
160                     {
161                         log.debug("We had a request, so we're are going to see if there are any more for us, but we're not going to block this time.");
162                     }
163                     messageList.add(object);
164                     if (log.isDebugEnabled())
165                     {
166                         log.debug("Added request.");
167                     }
168                 }
169             }
170         }
171         catch (InterruptedException JavaDoc exception)
172         {
173            log.debug("An interruptedException was triggered. We'll just deliver what we have to the client and try again next time.");
174         }
175         if (log.isDebugEnabled())
176           log.debug("Returning " + String.valueOf(messageList.size()) + " requests to clientIL #" + clientIlId + ".");
177         return this.createArrayFromList(messageList); // this could be empty, and if so, its OK.
178
}
179     
180     public void purgeEntry(String JavaDoc clientIlId)
181     {
182         if (log.isTraceEnabled())
183         {
184             log.trace("purgeEntry(String " + clientIlId + ")");
185         }
186         Object JavaDoc entry;
187         synchronized(this.queueLock)
188         {
189             entry = this.map.remove(clientIlId);
190         }
191         if (entry != null && log.isDebugEnabled())
192         {
193             log.debug("Purged storage queue entry for ClientIL #" + clientIlId + ".");
194         }
195         
196     }
197     
198     public String JavaDoc getID()
199     {
200         if (log.isTraceEnabled())
201         {
202             log.trace("getID()");
203         }
204         synchronized(idLock)
205         {
206            return String.valueOf(++id);
207         }
208     }
209     
210     private HTTPILRequest[] createArrayFromList(ArrayList JavaDoc list)
211     {
212         if (log.isTraceEnabled())
213         {
214             log.trace("createArrayFromList(ArrayList length=" + String.valueOf(list.size()) + ")");
215         }
216         HTTPILRequest[] requests = new HTTPILRequest[list.size()];
217         Iterator JavaDoc itemList = list.iterator();
218         int i = 0;
219         while (itemList.hasNext())
220         {
221             requests[i] = (HTTPILRequest)itemList.next();
222             i++;
223         }
224         return requests;
225     }
226 }
227
Popular Tags