Skip to main content
summaryrefslogtreecommitdiffstats
blob: e3aa029fb70163d19f5d720d1ca0b24f361e2a1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jini.lease;

import java.lang.ref.WeakReference;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import net.jini.core.lease.Lease;
import net.jini.core.lease.LeaseDeniedException;
import net.jini.core.lease.UnknownLeaseException;
import org.eclipse.osee.framework.jini.util.OseeJini;

/**
 * This class manages tasks associated with being a lease grantor. It generates leases, handles renewal & cancelation of
 * the leases, and checks the existing leases to determine if any have expired. Provides a callback by way of the
 * ILeasee interface to the leasing service to notify it when a lease is canceled or expired.
 * 
 * @author David Diepenbrock
 */
public class OseeLeaseGrantor implements ILeaseGrantor {

   public static final long maxDuration = 10 * 60 * 1000; /* 10 minutes */
   public static final long minDuration = 2 * 60 * 1000; /* 2 minutes */
   private ILeaseGrantor thisRemoteReference;
   private final Map<Object, LeaseData> leaseStore;
   private final WeakReference<ILeasee> leasee;
   private final LeaseChecker leaseChecker;
   private final Timer myTimer;
   private boolean cancelTimer;

   /**
    * Use of this constructor will generate a new timer thread for lease checks
    * 
    * @param leasee The "parent" object to be notified when a lease expires or is canceled.
    */
   public OseeLeaseGrantor(ILeasee leasee) {
      this(leasee, new Timer());
      // debug = new Debug(false, true, this.getClass().getName());
      cancelTimer = true; // We will need to cancel the timer on shutdown.
   }

   /**
    * @param leasee The "parent" object to be notified when a lease expires or is canceled
    * @param timer An existing Timer thread to schedule lease checks
    */
   public OseeLeaseGrantor(ILeasee leasee, Timer timer) {
      this.leasee = new WeakReference<ILeasee>(leasee);
      myTimer = timer;
      cancelTimer = false; // If a timer was provided we don't need to cancel it on shutdown
      leaseStore = Collections.synchronizedMap(new HashMap<Object, LeaseData>());

      try {
         thisRemoteReference = (ILeaseGrantor) OseeJini.getRemoteReference(this);
      } catch (ExportException ex) {
         ex.printStackTrace();
      }

      long leaseCheckFrequency = minDuration / 2;
      leaseChecker = new LeaseChecker();
      timer.schedule(leaseChecker, leaseCheckFrequency, leaseCheckFrequency);
   }

   /**
    * Call this to notify the grantor that we are shutting down. We need to clean up the task for checking the leases.
    */
   public void shutdown() {
      leaseChecker.cancel();
      if (cancelTimer) {
         myTimer.cancel();
      }
   }

   /**
    * Returns a new lease
    * 
    * @return Return lease reference.
    */
   public OseeLease newLease(Object consumer, long duration) throws LeaseDeniedException, ExportException {
      // debug.report("New Lease:" + consumer + " @" + new Date());
      long actualDuration = checkDuration(duration);
      OseeLease lease = new OseeLease(thisRemoteReference, consumer, actualDuration);
      leaseStore.put(consumer, new LeaseData(actualDuration));
      return lease;
   }

   /**
    * Note that the consumer must reset their start timer on the lease. The Grantor cannot set the time since the system
    * clocks may differ.
    * 
    * @see org.eclipse.osee.framework.jini.lease.ILeaseGrantor#renewRequest
    */
   @Override
   public void renewRequest(Lease lease, Object consumer, long duration) throws LeaseDeniedException, UnknownLeaseException, RemoteException {

      // debug.report("Lease renewRequest: " + consumer + " @" + new Date());

      synchronized (leaseStore) {
         LeaseData leaseData = leaseStore.get(consumer);
         if (leaseData == null) {
            throw new LeaseDeniedException("Consumer does not currently hold a lease");
         }

         if (lease instanceof OseeLease) {
            long actualDuration = checkDuration(duration);
            leaseData.setDuration(actualDuration);
            leaseData.setStartTime();
            ((OseeLease) lease).setDuration(actualDuration);
         } else {
            throw new UnknownLeaseException("Unknown Lease Type");
         }
      }
   }

   @Override
   public void cancelRequest(Lease lease, Object consumer) throws UnknownLeaseException, RemoteException {
      // debug.report("Lease cancelRequest: " + consumer + " @" + new Date());
      if (leaseStore.remove(consumer) != null) {
         leasee.get().onLeaseCompleted(consumer);
      }
   }

   public boolean isLeaseExpired(Object consumer) {
      LeaseData leaseData = leaseStore.get(consumer);
      if (leaseData != null) {
         return leaseData.isExpired();
      }

      return true;
   }

   private long checkDuration(long duration) throws LeaseDeniedException {
      long actualDuration;
      if (duration > maxDuration) {
         actualDuration = maxDuration;
      } else if (duration >= minDuration) {
         actualDuration = duration;
      } else if (duration == Lease.ANY) {
         actualDuration = maxDuration;
      } else {
         throw new LeaseDeniedException("Duration too short - must be at least " + minDuration + " milliseconds.");
      }

      return actualDuration;
   }

   private class LeaseChecker extends TimerTask {

      @Override
      public void run() {
         synchronized (leaseStore) {
            Set<Entry<Object, LeaseData>> set = leaseStore.entrySet();
            Iterator<Entry<Object, LeaseData>> iter = set.iterator();
            while (iter.hasNext()) {
               Entry<Object, LeaseData> entry = iter.next();
               if (entry.getValue().isExpired()) {
                  leasee.get().onLeaseCompleted(entry.getKey());
                  iter.remove();
               }
            }
         }
      }

   }

}

Back to the top