Source for gnu.java.rmi.dgc.DGCImpl

   1: /* DGCImpl.java --
   2:    Copyright (c) 1996, 1997, 1998, 1999, 2002, 2005
   3:    Free Software Foundation, Inc.
   4: 
   5: This file is part of GNU Classpath.
   6: 
   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.
  11: 
  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.
  16: 
  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.
  21: 
  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.
  26: 
  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. */
  38: 
  39: package gnu.java.rmi.dgc;
  40: 
  41: import gnu.java.rmi.server.UnicastServer;
  42: import gnu.java.rmi.server.UnicastServerRef;
  43: 
  44: import java.rmi.RemoteException;
  45: import java.rmi.dgc.DGC;
  46: import java.rmi.dgc.Lease;
  47: import java.rmi.dgc.VMID;
  48: import java.rmi.server.ObjID;
  49: import java.rmi.server.RMISocketFactory;
  50: import java.util.Collection;
  51: import java.util.TimerTask;
  52: 
  53: /**
  54:  * The DGC implementation is used for the server side during the distributed
  55:  * garbage collection. This interface contains the two methods: dirty and clean.
  56:  * A dirty call is made when a remote reference is unmarshaled in a client. A
  57:  * corresponding clean call is made by client it no longer uses that remote
  58:  * reference. A reference to a remote object is also automatically released
  59:  * after so called lease period that starts after the dirty call is received. It
  60:  * is the client's responsibility to renew the leases, by making additional
  61:  * dirty calls before such leases expire.
  62:  */
  63: public class DGCImpl
  64:     extends UnicastServerRef
  65:     implements DGC
  66: {
  67:   /*
  68:    * The DGCImpl extends UnicastServerRef and not UnicastRemoteObject, because
  69:    * UnicastRemoteObject must exportObject automatically.
  70:    */
  71: 
  72:   /**
  73:    * Use the serial version UID for interoperability.
  74:    */
  75:   private static final long serialVersionUID = 1;
  76: 
  77:   /**
  78:    * Protects the array of object Id's for the scheduled period of time
  79:    * (lease). After the time expires, the protector is automatically discarded,
  80:    * making the references unprotected and hence applicable for the garbage
  81:    * collection.
  82:    */
  83:   class RefProtector extends TimerTask
  84:   {
  85:     /**
  86:      * The corresponding server references to protect. Each Id may contain
  87:      * multiple references that are stored to collection.
  88:      */
  89:     Collection[] references;
  90: 
  91:     /**
  92:      * Create the new instance of the reference protector that protects the
  93:      * given array of ids and exists for the given period of time.
  94:      *
  95:      * @param ids the ids to protect.
  96:      */
  97:     RefProtector(ObjID[] ids, long timeToLive)
  98:     {
  99:       references = new Collection[ids.length];
 100:       for (int i = 0; i < ids.length; i++)
 101:         {
 102:           references[i] = UnicastServer.getExported(ids[i]);
 103:         }
 104: 
 105:       // Schedule the existence.
 106:       LeaseRenewingTask.timer.schedule(this, timeToLive);
 107:     }
 108: 
 109:     /**
 110:      * Break all links, ensuring easy collection of the references by the gc.
 111:      */
 112:     public void run()
 113:     {
 114:       for (int i = 0; i < references.length; i++)
 115:         {
 116:           references[i].clear();
 117:           references[i] = null;
 118:         }
 119:     }
 120:   }
 121: 
 122:   /**
 123:    * This defauld lease value is used if the lease value, passed to the
 124:    * {@link #dirty} is equal to zero.
 125:    */
 126:   static final long LEASE_VALUE = 600000L;
 127: 
 128:   /**
 129:    * Create the new DGC implementation.
 130:    *
 131:    * @throws RemoteException if the super constructor throws or the
 132:    * socket factory fails.
 133:    */
 134:   public DGCImpl() throws RemoteException
 135:   {
 136:     super(new ObjID(ObjID.DGC_ID), 0, RMISocketFactory.getSocketFactory());
 137:   }
 138: 
 139:   /**
 140:    * Mark the given objects referecnes as used on the client side.
 141:    *
 142:    * @param ids the ids of the used objects.
 143:    * @param sequenceNum the number of the call (used to detect and discard late
 144:    *          calls).
 145:    * @param lease the requested lease
 146:    * @return the granted lease
 147:    */
 148:   public Lease dirty(ObjID[] ids, long sequenceNum, Lease lease)
 149:       throws RemoteException
 150:   {
 151:     // We do not fill in VMID because in this implementation it is not used.
 152:     long leaseValue = lease.getValue();
 153: 
 154:     // Grant the maximal default lease time if the passed value is zero.
 155:     if (leaseValue <= 0)
 156:       leaseValue = LEASE_VALUE;
 157: 
 158:     // Create (and shedule of the given existence) the new reference
 159:     // protector.
 160:     new RefProtector(ids, leaseValue);
 161: 
 162:     lease = new Lease(lease.getVMID(), leaseValue);
 163:     return lease;
 164:   }
 165: 
 166:   /**
 167:    * Mark the given objects as no longer used on the client side.
 168:    *
 169:    * @param ids the ids of the objects that are no longer used.
 170:    * @param sequenceNum the number of the call (used to detect and discard late
 171:    *          calls)
 172:    * @param vmid the VMID of the client.
 173:    * @param strong make the "strong" clean call.
 174:    */
 175:   public void clean(ObjID[] ids, long sequenceNum, VMID vmid, boolean strong)
 176:       throws RemoteException
 177:   {
 178:     // Not implemented
 179:     // TODO implement
 180:   }
 181: 
 182: } // End of DGCImpl