1 45 package org.exolab.jms.lease; 46 47 48 55 public class BaseLease 56 implements LeaseIfc, Comparable { 57 58 61 protected final Object _leasedObject; 62 63 66 protected long _duration = 0L; 67 68 71 protected long _expiryTime = 0L; 72 73 76 protected LeaseEventListenerIfc _listener = null; 77 78 79 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 107 public long getExpiryTime() { 108 return _expiryTime; 109 } 110 111 116 public long getDuration() { 117 return _duration; 118 } 119 120 125 public void setDuration(long duration) { 126 _duration = duration; 127 _expiryTime = System.currentTimeMillis() + duration; 128 } 129 130 135 public long getRemainingTime() { 136 return (System.currentTimeMillis() - _expiryTime); 137 } 138 139 144 public Object getLeasedObject() { 145 return _leasedObject; 146 } 147 148 153 public LeaseEventListenerIfc getLeaseEventListener() { 154 return _listener; 155 } 156 157 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 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 200 protected void notifyLeaseExpired() { 201 synchronized (_listener) { 202 _listener.onLeaseExpired(_leasedObject); 203 } 204 } 205 206 } 207 | Popular Tags |