35.

previous chapter table of contents next chapter
  

Leasing Changes to a Service

Somtimes a service may allow changes to be made to its state by external (remote) objects. This happens all the time to service locators, which have services added and removed. A service may wish to behave in the same manner as the locators, and just grant a lease for the change. After the lease has expired , the service will remove the change. Such a situation may occur with file classification, where a new service that can handle a particular MIME type starts: it can register the filename mapping with a file classifier service. However, the file classifier service will just time out the mapping unless the new service keeps it renewed.

The example in this section follows the "Chapter 7. It gives a concrete illustration of that section, now that there is enough backround to do so.

Leased FileClassifier

A dynamically extensible version of a file classifier will have methods to add and remove MIME mappings:

 package common; import java.io.Serializable; /**  * LeaseFileClassifier.java  */ import net.jini.core.lease.Lease; public interface LeaseFileClassifier extends Serializable {     public MIMEType getMIMEType(String fileName)         throws java.rmi.RemoteException;     /*      * Add the MIME type for the given suffix.      * The suffix does not contain '.' e.g. "gif".      * @exception net.jini.core.lease.LeaseDeniedException      * a previous MIME type for that suffix exists.      * This type is removed on expiration or cancellation      * of the lease.      */     public Lease addType(String suffix, MIMEType type)         throws java.rmi.RemoteException,                net.jini.core.lease.LeaseDeniedException;     /**      * Remove the MIME type for the suffix.          */     public void removeType(String suffix)         throws java.rmi.RemoteException; } // LeaseFileClasssifier 

The addType() method returns a lease. We shall use the landlord leasing system discussed in Figure 7-3 where we considered the "foo" implementation of landlord leasing.

click to expand
Figure 13-5: Class diagram for leasing on the server

On the client side, the lease object will be a copy of the lease created on the server (normally RMI semantics), but the other objects from the service will be stubs that call into the real objects on the service. This is shown in Figure 13-6.

click to expand
Figure 13-6: Class diagram for leasing on the client

The FileClassifierLeasedResource Class

The FileClassifierLeasedResource class acts as a wrapper around the actual resource, adding cookie and time expiration fields around the resource. It adds a unique cookie mechanism, in addition to making the wrapped resource visible.

 /**  * FileClassifierLeasedResource.java  */ package lease; import common.LeaseFileClassifier; import com.sun.jini.lease.landlord.LeasedResource; public class FileClassifierLeasedResource implements LeasedResource {     static protected int cookie = 0;     protected int thisCookie;     protected LeaseFileClassifier fileClassifier;     protected long expiration = 0;     protected String suffix = null;     public FileClassifierLeasedResource(LeaseFileClassifier fileClassifier,                                         String suffix) {         this.fileClassifier = fileClassifier;         this.suffix = suffix;         thisCookie = cookie++;     }     public void setExpiration(long newExpiration) {         this.expiration = newExpiration;     }     public long getExpiration() {         return expiration;     }     public Object getCookie() {         return new Integer(thisCookie);     }     public LeaseFileClassifier getFileClassifier() {         return fileClassifier;     }     public String getSuffix() {         return suffix;     } } // FileClassifierLeasedResource 

The FileClassifierLeaseManager Class

The FileClassifierLeaseManager class is very similar to the code given for the FooLeaseManager in Chapter 7:

 /**  * FileClassifierLeaseManager.java  */ package lease; import java.util.*; import common.LeaseFileClassifier; import net.jini.core.lease.Lease; import com.sun.jini.lease.landlord.LeaseManager; import com.sun.jini.lease.landlord.LeasedResource; import com.sun.jini.lease.landlord.LeaseDurationPolicy; import com.sun.jini.lease.landlord.Landlord; import com.sun.jini.lease.landlord.LandlordLease; import com.sun.jini.lease.landlord.LeasePolicy; public class FileClassifierLeaseManager implements LeaseManager {     protected static long DEFAULT_TIME = 30*1000L;     protected Vector fileClassifierResources = new Vector();     protected LeaseDurationPolicy policy;     public FileClassifierLeaseManager(Landlord landlord) {         policy = new LeaseDurationPolicy(Lease.FOREVER,                                          DEFAULT_TIME,                                          landlord,                                          this,                                          new LandlordLease.Factory());         new LeaseReaper().start();     }     public void register(LeasedResource r, long duration) {         fileClassifierResources.add(r);     }     public void renewed(LeasedResource r, long duration, long olddur) {         // no smarts in the scheduling, so do nothing     }     public void cancelAll(Object[] cookies) {         for (int n = cookies.length; --n >= 0;) {             cancel(cookies[n]);         }     }     public void cancel(Object cookie) {         for (int n = fileClassifierResources.size(); --n >= 0;) {             FileClassifierLeasedResource r = (FileClassifierLeasedResource)                                              fileClassifierResources.elementAt(n);             if (!policy.ensureCurrent(r)) {                 System.out.println("Lease expired for cookie = " +                                    r.getCookie());                 try {                     r.getFileClassifier().removeType(r.getSuffix());                 } catch(java.rmi.RemoteException e) {                     e.printStackTrace();                 }                 fileClassifierResources.removeElementAt(n);             }         }     }        public LeasePolicy getPolicy() {         return policy;     }     public LeasedResource getResource(Object cookie) {         for (int n = fileClassifierResources.size(); --n >= 0;) {             FileClassifierLeasedResource r = (FileClassifierLeasedResource)                                              fileClassifierResources.elementAt(n);             if (r.getCookie().equals(cookie)) {                 return r;             }         }             return null;       }     class LeaseReaper extends Thread {         public void run() {             while (true) {                 try {                     Thread.sleep(DEFAULT_TIME) ;                 }                 catch (InterruptedException e) {                 }                 for (int n = fileClassifierResources.size()1; n >= 0; n--) {                    FileClassifierLeasedResource r = (FileClassifierLeasedResource)                                            fileClassifierResources.elementAt(n)                    if (!policy.ensureCurrent(r)) {                        System.out.println("Lease expired for cookie = " +                                           r.getCookie()) ;                        try {                            r.getFileClassifier().removeType(r.getSuffix());                        } catch(java.rmi.RemoteException e) {                            e.printStackTrace();                        }                        fileClassifierResources.removeElementAt(n);                    }                 }             }         }     } } // FileClassifierLeaseManager 

The FileClassifierLandlord Class

The FileClassifierLandlord class is very similar to the FooLandlord in Chapter 7:

 /**  * FileClassifierLandlord.java  */ package lease; import common.LeaseFileClassifier; import com.sun.jini.lease.landlord.*; import net.jini.core.lease.LeaseDeniedException; import net.jini.core.lease.Lease; import java.rmi.server.UnicastRemoteObject; import java.rmi.Remote; public class FileClassifierLandlord extends UnicastRemoteObject implements Land- lord, Remote {     FileClassifierLeaseManager manager = null;     public FileClassifierLandlord() throws java.rmi.RemoteException {         manager = new FileClassifierLeaseManager(this);     }     public void cancel(Object cookie) {         manager.cancel(cookie);     }        public void cancelAll(Object[] cookies) {         manager.cancelAll(cookies);     }     public long renew(java.lang.Object cookie,                                  long extension)         throws net.jini.core.lease.LeaseDeniedException,                net.jini.core.lease.UnknownLeaseException {         LeasedResource resource = manager.getResource(cookie);         if (resource != null) {             return manager.getPolicy().renew(resource, extension);         }         return 1;     }     public Lease newFileClassifierLease(LeaseFileClassifier fileClassifier,                                         String suffixKey, long duration)         throws LeaseDeniedException {         FileClassifierLeasedResource r = new         FileClassifierLeasedResource(fileClassifier,                                                                        suffixKey);         return manager.getPolicy().leaseFor(r, duration);     }     public Landlord.RenewResults renewAll(java.lang.Object[] cookie,                                               long[] extension) {         return null;     } } // FileClassifierLandlord 
  


A Programmer[ap]s Guide to Jini Technology
A Programmer[ap]s Guide to Jini Technology
ISBN: 1893115801
EAN: N/A
Year: 2000
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net