View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.river.reggie.proxy;
19  
20  import java.io.IOException;
21  import java.io.InvalidObjectException;
22  import java.io.ObjectOutputStream;
23  import java.io.ObjectStreamException;
24  import java.rmi.RemoteException;
25  import net.jini.core.constraint.RemoteMethodControl;
26  import net.jini.core.lease.UnknownLeaseException;
27  import net.jini.core.lookup.ServiceID;
28  import net.jini.id.Uuid;
29  import org.apache.river.api.io.AtomicSerial;
30  import org.apache.river.api.io.AtomicSerial.GetArg;
31  
32  /**
33   * When a registrar (lookup service) grants a lease on an event registration
34   * on behalf of some object (client), a proxy is employed to allow the client
35   * to interact with the lease; this class is the implementation of that proxy.
36   * Clients only see instances of this class via the Lease interface.
37   *
38   * @author Sun Microsystems, Inc.
39   *
40   */
41  @AtomicSerial
42  public class EventLease extends RegistrarLease {
43  
44      private static final long serialVersionUID = 2L;
45      /** The type of the lease used in toString() calls. */
46      private static final String LEASE_TYPE = "event";
47  
48      /**
49       * The eventID returned in the EventRegistration.
50       *
51       * @serial
52       */
53      final long eventID;
54  
55      /**
56       * Returns EventLease or ConstrainableEventLease instance, depending on
57       * whether given server implements RemoteMethodControl.
58       */
59      public static EventLease getInstance(Registrar server,
60  				  ServiceID registrarID,
61  				  long eventID,
62  				  Uuid leaseID,
63  				  long expiration)
64      {
65  	return (server instanceof RemoteMethodControl) ?
66  	    new ConstrainableEventLease(
67  		server, registrarID, eventID, leaseID, expiration, null, true) :
68  	    new EventLease(server, registrarID, eventID, leaseID, expiration);
69      }
70  
71      private static GetArg check(GetArg arg) throws IOException{
72  	long eventID = arg.get("eventID", 0L);
73  	return arg;
74      }
75      
76      EventLease(GetArg arg) throws IOException{
77  	super(check(arg));
78  	eventID = arg.get("eventID", 0L);
79      }
80      
81      /** Constructor for use by getInstance(), ConstrainableEventLease. */
82      EventLease(Registrar server,
83  	       ServiceID registrarID,
84  	       long eventID,
85  	       Uuid leaseID,
86  	       long expiration)
87      {
88  	super(server, registrarID, leaseID, expiration);
89  	this.eventID = eventID;
90      }
91  
92      // This method's javadoc is inherited from an interface of this class
93      public void cancel() throws UnknownLeaseException, RemoteException {
94  	server.cancelEventLease(eventID, leaseID);
95      }
96  
97      /** 
98       * Renews the event lease associated with an instance of this class.
99       * Each instance of this class corresponds to a lease on an event
100      * registration for a particular client. This method renews that 
101      * lease on behalf of the client.
102      *
103      * @param duration the requested duration for the lease being renewed
104      * @return long value representing the new duration that was granted
105      *         for the renewed lease. Note that the duration returned may
106      *         be less than the duration requested.
107      * @exception UnknownLeaseException indicates the lease does not exist;
108      *            typically because the lease has expired.
109      */
110     protected long doRenew(long duration)
111 	throws UnknownLeaseException, RemoteException
112     {
113 	return server.renewEventLease(eventID, leaseID, duration);
114     }
115 
116     // This method's javadoc is inherited from a super class of this class
117     Object getRegID() {
118 	return Long.valueOf(eventID);
119     }
120     
121     // inherit javadoc
122     String getLeaseType() {
123 	return LEASE_TYPE;
124     }
125 
126     private void writeObject(ObjectOutputStream out) throws IOException {
127 	out.defaultWriteObject();
128     }
129 
130     /**
131      * Throws InvalidObjectException, since data for this class is required.
132      */
133     private void readObjectNoData() throws ObjectStreamException {
134 	throw new InvalidObjectException("no data");
135     }
136 }