View Javadoc

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 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: BaseLease.java,v 1.6 2003/08/17 01:32:24 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 2/29/2000    jima    Created
47   */
48  package org.exolab.jms.lease;
49  
50  
51  import java.io.Serializable;
52  
53  
54  /***
55   * This is the implementation of a non-specific lease object. It can be used to
56   * lease any Objeect. .
57   *
58   * @version     $Revision: 1.6 $ $Date: 2003/08/17 01:32:24 $
59   * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
60   **/
61  public class BaseLease
62      implements LeaseIfc, Comparable, Serializable {
63  
64      /***
65       * Default constructor for the Serailizable/Externalizable interface
66       */
67      public BaseLease() {
68      }
69  
70      /***
71       * Construct an instance of this class with the specified objct, and duration
72       *
73       * @param       object          leased object
74       * @param       duration        duration of lease
75       * @param       listener        will get notified on lease events
76       */
77      public BaseLease(Object object, long duration, LeaseEventListenerIfc listener) {
78  
79          // test for preconditions
80          if ((object == null) ||
81              (listener == null)) {
82              throw new IllegalArgumentException("object or listener cannot be null");
83          }
84  
85          leasedObject_ = object;
86          duration_ = duration;
87          expiryTime_ = System.currentTimeMillis() + duration;
88          listener_ = listener;
89      }
90  
91      // implementation for LeaseIfc.getExpiryTime()
92      public long getExpiryTime() {
93          return expiryTime_;
94      }
95  
96      // implementation for LeaseIfc.getDuration()
97      public long getDuration() {
98          return duration_;
99      }
100 
101     // implementation for LeaseIfc.getDuration()
102     public void setDuration(long duration) {
103         duration_ = duration;
104         expiryTime_ = System.currentTimeMillis() + duration;
105     }
106 
107     // implementation for LeaseIfc.getRemainingTime()
108     public long getRemainingTime() {
109         return (System.currentTimeMillis() - expiryTime_);
110     }
111 
112     // implementation for LeaseIfc.getLeasedObject()
113     public Object getLeasedObject() {
114         return leasedObject_;
115     }
116 
117     // implementation for LeaseIfc.getLeasedObjectType()
118     public Class getLeasedObjectType() {
119         return leasedObject_.getClass();
120     }
121 
122     /***
123      * Return a reference to the listener registered with this object
124      *
125      * @return LeaseEventListenerIfc
126      */
127     public LeaseEventListenerIfc getLeaseEventListener() {
128         return listener_;
129     }
130 
131     /***
132      * Compares this object with the specified object. It returns a negative
133      * integer is this object is less than the specified object; zero if this
134      * object is equal to the specified object or a positive integer if this
135      * object is greater than the specified object
136      * <p>
137      * The comparison is based on the expiration time.
138      */
139     public int compareTo(Object object) {
140         int result = 0;
141 
142         if (object instanceof BaseLease) {
143             BaseLease lease = (BaseLease) object;
144             if (lease.getExpiryTime() != this.getExpiryTime()) {
145                 if (lease.getExpiryTime() > this.getExpiryTime()) {
146                     result = -1;
147                 } else {
148                     result = 1;
149                 }
150             }
151         }
152         return result;
153     }
154 
155     /***
156      * Notify the listeners that this lease has expird. This method has
157      * package level scope.
158      */
159     void notifyLeaseExpired() {
160         synchronized (listener_) {
161             listener_.onLeaseExpired(leasedObject_);
162         }
163     }
164 
165     /***
166      * Return a string representation of this object
167      *
168      * @return      String
169      */
170     public String toString() {
171         StringBuffer buf = new StringBuffer(leasedObject_.toString());
172         buf.append(" duration = ");
173         buf.append(duration_);
174         buf.append(" expiryTime = ");
175         buf.append(expiryTime_);
176 
177         return buf.toString();
178     }
179 
180 
181     /***
182      * This is the object that is leased
183      */
184     protected Object leasedObject_ = null;
185 
186     /***
187      * The duration of the lease in milliseconds
188      */
189     protected long duration_ = 0L;
190 
191     /***
192      * This is the time that the lease will expire
193      */
194     protected long expiryTime_ = 0L;
195 
196     /***
197      * The listener that will be notified when the lease expires
198      */
199     protected LeaseEventListenerIfc listener_ = null;
200 }
201