Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / rmi / dgc / DGCImpl.java
bloba7bc0940ed0841900d0d076446a48fb8aa0a3e78
1 /* DGCImpl.java --
2 Copyright (c) 1996, 1997, 1998, 1999, 2002, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
39 package gnu.java.rmi.dgc;
41 import gnu.java.rmi.server.UnicastServerRef;
43 import java.rmi.RemoteException;
44 import java.rmi.dgc.DGC;
45 import java.rmi.dgc.Lease;
46 import java.rmi.dgc.VMID;
47 import java.rmi.server.ObjID;
48 import java.rmi.server.RMISocketFactory;
49 import java.util.Hashtable;
51 /**
52 * The DGC implementation is used for the server side during the distributed
53 * garbage collection. This interface contains the two methods: dirty and clean.
54 * A dirty call is made when a remote reference is unmarshaled in a client. A
55 * corresponding clean call is made by client it no longer uses that remote
56 * reference. A reference to a remote object is also automatically released
57 * after so called lease period that starts after the dirty call is received. It
58 * is the client's responsibility to renew the leases, by making additional
59 * dirty calls before such leases expire.
61 public class DGCImpl
62 extends UnicastServerRef
63 implements DGC
66 * The DGCImpl extends UnicastServerRef and not UnicastRemoteObject, because
67 * UnicastRemoteObject must exportObject automatically.
70 /**
71 * This defauld lease value is used if the lease value, passed to the
72 * {@link #dirty} is equal to zero.
74 static final long LEASE_VALUE = 600000L;
76 // leaseCache caches a LeaseRecord associated with a vmid
77 Hashtable leaseCache = new Hashtable();
79 public DGCImpl() throws RemoteException
81 super(new ObjID(ObjID.DGC_ID), 0, RMISocketFactory.getSocketFactory());
84 /**
85 * Mark the given objects referecnes as used on the client side.
87 * @param ids the ids of the used objects.
88 * @param sequenceNum the number of the call (used to detect and discard late
89 * calls).
90 * @param lease the requested lease
91 * @return the granted lease
93 public Lease dirty(ObjID[] ids, long sequenceNum, Lease lease)
94 throws RemoteException
96 VMID vmid = lease.getVMID();
97 if (vmid == null)
98 vmid = new VMID();
100 long leaseValue = lease.getValue();
101 if (leaseValue <= 0)
102 leaseValue = LEASE_VALUE;
104 lease = new Lease(vmid, leaseValue);
105 LeaseRecord lr = (LeaseRecord) leaseCache.get(vmid);
106 if (lr != null)
107 lr.reset(leaseValue);
108 else
110 lr = new LeaseRecord(vmid, leaseValue, ids);
111 leaseCache.put(vmid, lr);
114 return (lease);
118 * Mark the given objects as no longer used on the client side.
120 * @param ids the ids of the objects that are no longer used.
121 * @param sequenceNum the number of the call (used to detect and discard late
122 * calls)
123 * @param vmid the VMID of the client.
124 * @param strong make the "strong" clean call.
126 public void clean(ObjID[] ids, long sequenceNum, VMID vmid, boolean strong)
127 throws RemoteException
129 // Not implemented
130 // TODO implement
134 * LeaseRecord associates a vmid to expireTime.
136 static class LeaseRecord
139 * The lease id.
141 final VMID vmid;
144 * The lease expiration time.
146 long expireTime;
149 * The array of ObjeID's that must be protected from being garbage
150 * collected.
152 final ObjID [] objects;
155 * Create the new lease record.
157 * @param vmid lease id.
158 * @param leaseValue lease value
160 LeaseRecord(VMID vmid, long leaseValue, ObjID [] an_objects)
162 this.vmid = vmid;
163 reset(leaseValue);
164 objects = an_objects;
168 * Prolong the expiration time till current time + passed value
170 * @param leaseValue the value after that (since the current moment)
171 * the lease should expire in the future.
173 void reset(long leaseValue)
175 long l = System.currentTimeMillis();
176 expireTime = l + leaseValue;
180 * Check if the lease has been expired.
182 * @return true if the lease has been expired, false if it is still valid.
184 boolean isExpired()
186 long l = System.currentTimeMillis();
187 if (l > expireTime)
188 return true;
189 return false;
192 } // End of LeaseRecord
194 } // End of DGCImpl