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.ObjectInput;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.io.ObjectStreamException;
26  import net.jini.core.lease.Lease;
27  import net.jini.core.lease.LeaseMap;
28  import net.jini.core.lookup.ServiceID;
29  import net.jini.id.ReferentUuid;
30  import net.jini.id.ReferentUuids;
31  import net.jini.id.Uuid;
32  import org.apache.river.api.io.AtomicSerial;
33  import org.apache.river.api.io.AtomicSerial.GetArg;
34  import org.apache.river.api.io.AtomicSerial.ReadInput;
35  import org.apache.river.api.io.AtomicSerial.ReadObject;
36  import org.apache.river.lease.AbstractLease;
37  import org.apache.river.lease.ID;
38  
39  /**
40   * The base class for lease proxies.
41   *
42   * @author Sun Microsystems, Inc.
43   *
44   */
45  @AtomicSerial
46  abstract class RegistrarLease extends AbstractLease implements ReferentUuid, ID<Uuid> {
47  
48      private static final long serialVersionUID = 2L;
49  
50      /**
51       * The registrar.
52       *
53       * @serial
54       */
55      final Registrar server;
56      /**
57       * The registrar's service ID.
58       */
59      transient ServiceID registrarID;
60      /**
61       * The internal lease id.
62       *
63       * @serial
64       */
65      final Uuid leaseID;
66  
67      /**
68       * Called reflectively by AtomicSerial serializer framework.
69       * @return 
70       */
71      @ReadInput
72      private static ReadObject getRO(){
73  	return new RO();
74      }
75      
76      private static GetArg check(GetArg arg) throws IOException{
77  	Registrar server = (Registrar) arg.get("server", null);
78  	Uuid leaseID = (Uuid) arg.get("leaseID", null);
79  	RO r = (RO) arg.getReader();
80  	if (server == null) {
81  	    throw new InvalidObjectException("null server");
82  	} else if (leaseID == null) {
83  	    throw new InvalidObjectException("null leaseID");
84  	}
85  	return arg;
86      }
87      
88      RegistrarLease(GetArg arg) throws IOException{
89  	super(check(arg));
90  	server = (Registrar) arg.get("server", null);
91  	leaseID = (Uuid) arg.get("leaseID", null);
92  	registrarID = ((RO) arg.getReader()).registrarID;
93      }
94  
95  
96  
97      /** Simple constructor. */
98      RegistrarLease(Registrar server,
99  		   ServiceID registrarID,
100 		   Uuid leaseID,
101 		   long expiration)
102     {
103 	super(expiration);
104 	this.server = server;
105 	this.registrarID = registrarID;
106 	this.leaseID = leaseID;
107 	this.expiration = expiration;
108     }
109 
110     /** Creates a lease map. */
111     @Override
112     public LeaseMap<? extends Lease,Long> createLeaseMap(long duration) {
113 	return new RegistrarLeaseMap(this, duration);
114     }
115 
116     /**
117      * Two leases can be batched if they are both RegistrarLeases and
118      * have the same server.
119      */
120     @Override
121     public boolean canBatch(Lease lease) {
122 	return (lease instanceof RegistrarLease &&
123 		registrarID.equals(((RegistrarLease) lease).registrarID));
124     }
125 
126     /** Returns the lease Uuid. */
127     @Override
128     public Uuid getReferentUuid() {
129 	return leaseID;
130     }
131 
132     /** Returns the lease Uuid's hash code. */
133     @Override
134     public int hashCode() {
135 	return leaseID.hashCode();
136     }
137 
138     /** Returns true if lease Uuids match, false otherwise. */
139     @Override
140     public boolean equals(Object obj) {
141 	return ReferentUuids.compare(this, obj);
142     }
143 
144     /**
145      * Returns a string created from the proxy class name, the registrar's
146      * service ID, the id of the lessee or event (depending on the subclass),
147      * and the result of the underlying server proxy's toString method.
148      * 
149      * @return String
150      */
151     @Override
152     public String toString() {
153 	String className = getClass().getName();
154 	return className + "[registrar=" + registrarID + " " + server
155 	    + ", lease=" + leaseID + ", " + getLeaseType() + "=" + getRegID() 
156 	    + "]";
157     }
158 
159 
160     /** Returns the registrar. */
161     Registrar getRegistrar() {
162 	return server;
163     }
164 
165     /** Returns the registrar's service ID. */
166     ServiceID getRegistrarID() {
167 	return registrarID;
168     }
169     
170     @Override
171     public Uuid identity(){
172         return leaseID;
173     }
174 
175     /** Returns the service ID, or the event ID as a Long. */
176     abstract Object getRegID();
177 
178     /** Returns the type of the lease. */
179     abstract String getLeaseType();
180 
181     /**
182      * Writes the default serializable field values for this instance, followed
183      * by the registrar's service ID encoded as specified by the
184      * ServiceID.writeBytes method.
185      */
186     private void writeObject(ObjectOutputStream out) throws IOException {
187 	out.defaultWriteObject();
188 	registrarID.writeBytes(out);
189     }
190 
191     /**
192      * Reads the default serializable field values for this instance, followed
193      * by the registrar's service ID encoded as specified by the
194      * ServiceID.writeBytes method.  Verifies that the deserialized field
195      * values are non-null.
196      */
197     private void readObject(ObjectInputStream in)
198 	throws IOException, ClassNotFoundException
199     {
200 	in.defaultReadObject();
201 	registrarID = new ServiceID(in);
202 	if (server == null) {
203 	    throw new InvalidObjectException("null server");
204 	} else if (leaseID == null) {
205 	    throw new InvalidObjectException("null leaseID");
206 	}
207     }
208 
209     private static class RO implements ReadObject{
210 	
211 	ServiceID registrarID;
212 
213 	@Override
214 	public void read(ObjectInput in) throws IOException, ClassNotFoundException {
215 	    registrarID = new ServiceID(in);
216 	}
217 	
218     }
219 
220     /**
221      * Throws InvalidObjectException, since data for this class is required.
222      */
223     private void readObjectNoData() throws ObjectStreamException {
224 	throw new InvalidObjectException("no data");
225     }
226 
227     void setExpiration(long expiration) {
228         this.expiration = expiration;
229     }
230 }