KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > lease > LeaseManager


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: LeaseManager.java,v 1.2 2005/03/18 03:47:30 tanderson Exp $
44  */

45 package org.exolab.jms.lease;
46
47
48 import org.exolab.jms.service.BasicService;
49 import org.exolab.jms.service.ServiceException;
50 import org.exolab.jms.service.ServiceState;
51 import org.exolab.jms.common.util.OrderedQueue;
52
53
54 /**
55  * The LeaseManager is responsible for creating and managing the lease objects.
56  * The Leasemanager is a singleton. When a BaseLease object is created it is
57  * added to the queue according to the duration (i.e. leases with shorter
58  * durations are placed at the top of the queue.
59  * <p>
60  * When the lease expires the LeeaseManager calls the leasee's associated
61  * listener(s).
62  *
63  * @version $Revision: 1.2 $ $Date: 2005/03/18 03:47:30 $
64  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
65  */

66 public class LeaseManager extends BasicService {
67
68     /**
69      * The name of the LeaseManagerThread that scans and removes expired leases
70      */

71     private static final String JavaDoc LEASE_MANAGER_THREAD_NAME =
72         "LeaseManagerReaper";
73
74     /**
75      * An ordered list of leases.
76      */

77     private OrderedQueue _queue = null;
78
79     /**
80      * The singleton instance of the LeaseManager.
81      */

82     private static LeaseManager _instance = new LeaseManager();
83
84     /**
85      * Helper for waiting for leases to expire.
86      */

87     private final Object JavaDoc _waiter = new Object JavaDoc();
88
89
90     /**
91      * Create a new sorted tree set using the lease comparator as the
92      * sorting functor.
93      */

94     protected LeaseManager() {
95         super(LEASE_MANAGER_THREAD_NAME);
96         _queue = new OrderedQueue(new LeaseComparator());
97     }
98
99     /**
100      * Return the singleton instance of the LeaseManager
101      *
102      * @return LeaseManager
103      */

104     public static LeaseManager instance() {
105         return _instance;
106     }
107
108     /**
109      * Add a lease.
110      *
111      * @param lease the lease to add
112      */

113     public void addLease(BaseLease lease) {
114         synchronized (_queue) {
115             _queue.add(lease);
116             if (_queue.firstElement() == lease) {
117                 // inserted before the first element, so reset scan
118
synchronized (_waiter) {
119                     _waiter.notify();
120                 }
121             }
122         }
123     }
124
125     /**
126      * Remove the specified lease.
127      *
128      * @param lease lease to remove
129      * @return boolean true if successful; false otherwise
130      */

131     public boolean removeLease(BaseLease lease) {
132         boolean result = false;
133
134         synchronized (_queue) {
135             result = _queue.remove(lease);
136         }
137         if (result) {
138             synchronized (_waiter) {
139                 _waiter.notify();
140             }
141         }
142
143         return result;
144     }
145
146     /**
147      * Renew the lease on the specified object
148      *
149      * @param lease the lease to renew
150      * @param duration the new duration of the lease in ms
151      */

152     public BaseLease renewLease(BaseLease lease, long duration) {
153         BaseLease newlease = null;
154
155         if ((lease != null) && (duration > 0)) {
156             synchronized (_queue) {
157                 // check that the lease hasn't expired yet.
158
if (_queue.remove(lease)) {
159                     lease.setDuration(duration);
160                     _queue.add(lease);
161                     newlease = lease;
162                     synchronized (_waiter) {
163                         _waiter.notify();
164                     }
165                 }
166             }
167         }
168
169         return newlease;
170     }
171
172     /**
173      * Remove all the leases from the queue. Do not expire any of them
174      */

175     public void removeAll() {
176         synchronized (_queue) {
177             _queue.clear();
178         }
179     }
180
181     /**
182      * The run method will search for expired leases, remove them from the
183      * list and notify listeners
184      */

185     public void run() {
186         while (getState() != ServiceState.STOPPED) {
187             expire();
188
189             // wait until a lease is available, or the service is terminated
190
synchronized (_waiter) {
191                 try {
192                     _waiter.wait();
193                 } catch (InterruptedException JavaDoc ignore) {
194                 }
195             }
196         }
197     }
198
199     /**
200      * Stop the service.
201      *
202      * @throws ServiceException if the service fails to stop
203      */

204     public void stop() throws ServiceException {
205         super.stop();
206         synchronized (_waiter) {
207             _waiter.notifyAll();
208         }
209     }
210
211     /**
212      * Expires active leases
213      */

214     protected void expire() {
215         while (_queue.size() > 0) {
216             BaseLease lease = null;
217             boolean expired = false;
218             synchronized (_queue) {
219                 lease = (BaseLease) _queue.firstElement();
220                 if (lease == null) {
221                     continue;
222                 }
223                 
224                 if (lease.getExpiryTime() <= System.currentTimeMillis()) {
225                     // remove from the list and notify listeners
226
_queue.removeFirstElement();
227                     expired = true;
228                 }
229             }
230
231             if (expired) {
232                 lease.notifyLeaseExpired();
233             } else {
234                 // wait until the first element in the list is
235
// ready to expire
236
long time = lease.getExpiryTime() -
237                     System.currentTimeMillis();
238                 
239                 if (time > 0) {
240                     try {
241                         synchronized (_waiter) {
242                             _waiter.wait(time);
243                         }
244                     } catch (InterruptedException JavaDoc ignore) {
245                     }
246                 }
247             }
248         }
249     }
250
251 }
252
Popular Tags