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: BaseLease.java,v 1.2 2005/03/18 03:47:30 tanderson Exp $ 44 */ 45 package org.exolab.jms.lease; 46 47 48 /*** 49 * Generic lease implementation, which may be used to lease any object. 50 * 51 * @author <a href="mailto:jima@comware.com.au">Jim Alateras</a> 52 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 53 * @version $Revision: 1.2 $ $Date: 2005/03/18 03:47:30 $ 54 */ 55 public class BaseLease 56 implements LeaseIfc, Comparable { 57 58 /*** 59 * This is the object that is leased. 60 */ 61 protected final Object _leasedObject; 62 63 /*** 64 * The duration of the lease in milliseconds. 65 */ 66 protected long _duration = 0L; 67 68 /*** 69 * This is the time that the lease will expire. 70 */ 71 protected long _expiryTime = 0L; 72 73 /*** 74 * The listener that will be notified when the lease expires. 75 */ 76 protected LeaseEventListenerIfc _listener = null; 77 78 79 /*** 80 * Construct a new <code>BaseLease</code>. 81 * 82 * @param object the leased object 83 * @param duration the duration of lease, in milliseconds 84 * @param listener the listener to notify of lease events 85 */ 86 public BaseLease(Object object, long duration, 87 LeaseEventListenerIfc listener) { 88 if (object == null) { 89 throw new IllegalArgumentException("Argument 'object' is null"); 90 } 91 92 if (listener == null) { 93 throw new IllegalArgumentException("Argument 'listener' is null"); 94 } 95 96 _leasedObject = object; 97 _duration = duration; 98 _expiryTime = System.currentTimeMillis() + duration; 99 _listener = listener; 100 } 101 102 /*** 103 * Return the absolute expiry time of this lease. 104 * 105 * @return the expiry time of the lease, in milliseconds 106 */ 107 public long getExpiryTime() { 108 return _expiryTime; 109 } 110 111 /*** 112 * Returns the duration of the lease. 113 * 114 * @return the duration of the lease, in milliseconds 115 */ 116 public long getDuration() { 117 return _duration; 118 } 119 120 /*** 121 * Change the duration of the lease. 122 * 123 * @param duration the new lease duration, in milliseconds 124 */ 125 public void setDuration(long duration) { 126 _duration = duration; 127 _expiryTime = System.currentTimeMillis() + duration; 128 } 129 130 /*** 131 * Returns the time remaining on the lease. 132 * 133 * @return the time remaining, in milliseconds 134 */ 135 public long getRemainingTime() { 136 return (System.currentTimeMillis() - _expiryTime); 137 } 138 139 /*** 140 * Returns the leased object. 141 * 142 * @return the leased object 143 */ 144 public Object getLeasedObject() { 145 return _leasedObject; 146 } 147 148 /*** 149 * Returns the listener to notify of lease events. 150 * 151 * @return the listener to notify of lease events 152 */ 153 public LeaseEventListenerIfc getLeaseEventListener() { 154 return _listener; 155 } 156 157 /*** 158 * Compares this object with the specified object. It returns a negative 159 * integer is this object is less than the specified object; zero if this 160 * object is equal to the specified object or a positive integer if this 161 * object is greater than the specified object 162 * <p/> 163 * The comparison is based on the expiration time. 164 */ 165 public int compareTo(Object object) { 166 int result = 0; 167 168 if (object instanceof BaseLease) { 169 BaseLease lease = (BaseLease) object; 170 if (lease.getExpiryTime() != this.getExpiryTime()) { 171 if (lease.getExpiryTime() > this.getExpiryTime()) { 172 result = -1; 173 } else { 174 result = 1; 175 } 176 } 177 } 178 return result; 179 } 180 181 /*** 182 * Returns a string representation of this object. 183 * 184 * @return a string representation of this 185 */ 186 public String toString() { 187 StringBuffer buf = new StringBuffer(_leasedObject.toString()); 188 buf.append(" duration = "); 189 buf.append(_duration); 190 buf.append(" expiryTime = "); 191 buf.append(_expiryTime); 192 193 return buf.toString(); 194 } 195 196 /*** 197 * Notify the listeners that this lease has expird. This method has package 198 * level scope. 199 */ 200 protected void notifyLeaseExpired() { 201 synchronized (_listener) { 202 _listener.onLeaseExpired(_leasedObject); 203 } 204 } 205 206 }