KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > queue > RWLockEventQueueDecorator


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

21
22 package org.jacorb.notification.queue;
23
24 import org.jacorb.notification.interfaces.Message;
25
26 import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock;
27 import EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock;
28
29 /**
30  * @author Alphonse Bendt
31  * @version $Id: RWLockEventQueueDecorator.java,v 1.3 2005/04/27 10:43:53 alphonse.bendt Exp $
32  */

33 public class RWLockEventQueueDecorator implements MessageQueueAdapter
34 {
35     /**
36      * lock variable used to control access to the reference to the pending messages queue.
37      */

38     private final ReadWriteLock delegateLock_ = new WriterPreferenceReadWriteLock();
39
40     /**
41      * multithreaded access to this member is protected by pendingMessagesLock_
42      */

43     private MessageQueueAdapter delegate_;
44
45     /**
46      *
47      */

48     public RWLockEventQueueDecorator(MessageQueueAdapter initialDelegate)
49             throws InterruptedException JavaDoc
50     {
51         super();
52
53         delegateLock_.writeLock().acquire();
54
55         try
56         {
57             delegate_ = initialDelegate;
58         } finally
59         {
60             delegateLock_.writeLock().release();
61         }
62     }
63
64     public void replaceDelegate(MessageQueueAdapter newDelegate) throws InterruptedException JavaDoc
65     {
66         delegateLock_.writeLock().acquire();
67
68         try
69         {
70             if (delegate_.hasPendingMessages())
71             {
72                 Message[] _allMessages = delegate_.getAllMessages();
73                 for (int x = 0; x < _allMessages.length; ++x)
74                 {
75                     newDelegate.enqeue(_allMessages[x]);
76                 }
77             }
78             delegate_ = newDelegate;
79         } finally
80         {
81             delegateLock_.writeLock().release();
82         }
83     }
84
85     public void enqeue(Message message) throws InterruptedException JavaDoc
86     {
87         delegateLock_.readLock().acquire();
88
89         try
90         {
91             delegate_.enqeue(message);
92         } finally
93         {
94             delegateLock_.readLock().release();
95         }
96     }
97
98     public boolean hasPendingMessages() throws InterruptedException JavaDoc
99     {
100         delegateLock_.readLock().acquire();
101
102         try
103         {
104             return delegate_.hasPendingMessages();
105         } finally
106         {
107             delegateLock_.readLock().release();
108         }
109     }
110
111     public int getPendingMessagesCount() throws InterruptedException JavaDoc
112     {
113         delegateLock_.readLock().acquire();
114
115         try
116         {
117             return delegate_.getPendingMessagesCount();
118         } finally
119         {
120             delegateLock_.readLock().release();
121         }
122     }
123
124     public Message getMessageBlocking() throws InterruptedException JavaDoc
125     {
126         delegateLock_.readLock().acquire();
127
128         try
129         {
130             return delegate_.getMessageBlocking();
131         } finally
132         {
133             delegateLock_.readLock().release();
134         }
135     }
136
137     public Message getMessageNoBlock() throws InterruptedException JavaDoc
138     {
139         delegateLock_.readLock().acquire();
140
141         try
142         {
143             return delegate_.getMessageNoBlock();
144         } finally
145         {
146             delegateLock_.readLock().release();
147         }
148     }
149
150     public Message[] getAllMessages() throws InterruptedException JavaDoc
151     {
152         delegateLock_.readLock().acquire();
153
154         try
155         {
156             return delegate_.getAllMessages();
157         } finally
158         {
159             delegateLock_.readLock().release();
160         }
161     }
162
163     public Message[] getUpToMessages(int max) throws InterruptedException JavaDoc
164     {
165         delegateLock_.readLock().acquire();
166
167         try
168         {
169             return delegate_.getUpToMessages(max);
170         } finally
171         {
172             delegateLock_.readLock().release();
173         }
174     }
175
176     public Message[] getAtLeastMessages(int min) throws InterruptedException JavaDoc
177     {
178         delegateLock_.readLock().acquire();
179
180         try
181         {
182             return delegate_.getAtLeastMessages(min);
183         } finally
184         {
185             delegateLock_.readLock().release();
186         }
187     }
188
189     public void clear()
190     {
191         try
192         {
193             delegateLock_.writeLock().acquire();
194         } catch (InterruptedException JavaDoc e)
195         {
196             // ignore exception. force dispose.
197
}
198
199         try
200         {
201             delegate_.clear();
202         } finally
203         {
204             delegateLock_.writeLock().release();
205         }
206     }
207     
208     public String JavaDoc toString()
209     {
210         return delegate_.toString();
211     }
212 }
Popular Tags