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 org.apache.river.proxy.ConstrainableProxyUtil;
21  import java.io.IOException;
22  import java.io.InvalidObjectException;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectStreamException;
25  import java.io.Serializable;
26  import java.lang.reflect.Method;
27  import java.rmi.RemoteException;
28  import net.jini.core.constraint.MethodConstraints;
29  import net.jini.core.constraint.RemoteMethodControl;
30  import net.jini.core.entry.Entry;
31  import net.jini.core.lease.Lease;
32  import net.jini.core.lease.UnknownLeaseException;
33  import net.jini.core.lookup.ServiceID;
34  import net.jini.core.lookup.ServiceRegistration;
35  import net.jini.id.ReferentUuid;
36  import net.jini.id.ReferentUuids;
37  import net.jini.id.Uuid;
38  import org.apache.river.api.io.AtomicSerial;
39  import org.apache.river.api.io.AtomicSerial.GetArg;
40  
41  /**
42   * Implementation class for the ServiceRegistration interface.
43   *
44   * @author Sun Microsystems, Inc.
45   *
46   */
47  @AtomicSerial
48  public class Registration implements ServiceRegistration, ReferentUuid, Serializable 
49  {
50  
51      private static final long serialVersionUID = 2L;
52  
53      /** Mappings between ServiceRegistration and Registrar methods */
54      static final Method[] methodMappings = {
55  	Util.getMethod(ServiceRegistration.class, "addAttributes",
56  		       new Class[]{ Entry[].class }),
57  	Util.getMethod(Registrar.class, "addAttributes",
58  		       new Class[]{ ServiceID.class, Uuid.class,
59  				    EntryRep[].class }),
60  
61  	Util.getMethod(ServiceRegistration.class, "modifyAttributes",
62  		       new Class[]{ Entry[].class, Entry[].class }),
63  	Util.getMethod(Registrar.class, "modifyAttributes",
64  		       new Class[]{ ServiceID.class, Uuid.class,
65  				    EntryRep[].class, EntryRep[].class }),
66  
67  	Util.getMethod(ServiceRegistration.class, "setAttributes",
68  		       new Class[]{ Entry[].class }),
69  	Util.getMethod(Registrar.class, "setAttributes",
70  		       new Class[]{ ServiceID.class, Uuid.class,
71  				    EntryRep[].class }),
72      };
73  
74      /**
75       * The registrar
76       *
77       * @serial
78       */
79      final Registrar server;
80      /**
81       * The service lease
82       *
83       * @serial
84       */
85      final ServiceLease lease;
86  
87      
88  //    @Override
89  //    public void write(PutArg arg) {
90  //	arg.put("server", server);
91  //	arg.put("lease", lease);
92  //    }
93      
94      private static boolean check(GetArg arg) throws IOException {
95  	Registrar server = (Registrar) arg.get("server", null);
96  	if (server == null) throw new InvalidObjectException("null server");
97  	ServiceLease lease = (ServiceLease) arg.get("lease", null);
98  	if (lease == null) throw new InvalidObjectException("null lease");
99  	return true;
100     }
101     
102     Registration(GetArg arg) throws IOException {
103 	this(arg, check(arg));
104     }
105     
106     private Registration(GetArg arg, boolean check) throws IOException {
107 	server = (Registrar) arg.get("server", null);
108 	lease = (ServiceLease) arg.get("lease", null);
109     }
110 
111     /**
112      * Returns Registration or ConstrainableRegistration instance, depending on
113      * whether given server implements RemoteMethodControl.
114      */
115     public static Registration getInstance(Registrar server, ServiceLease lease) {
116 	return (server instanceof RemoteMethodControl) ?
117 	    new ConstrainableRegistration(server, lease, null, true) :
118 	    new Registration(server, lease);
119     }
120 
121     /**
122      * Portable factory
123      * Returns Registration or ConstrainableRegistration instance, depending on
124      * whether given server implements RemoteMethodControl.
125      */
126     public static Object getInstance(Object server, ServiceLease lease,
127 	    MethodConstraints constraints) throws InvalidObjectException {
128 	if (server instanceof Registrar){
129 	    if (server instanceof RemoteMethodControl) {
130 		ConstrainableProxyUtil.verifyConsistentConstraints(
131 		constraints, server, methodMappings);
132 		return new ConstrainableRegistration((Registrar)server, lease, constraints, false) ;
133 	    }else{
134 		return new Registration((Registrar)server, lease);
135 	    }
136 	}
137 	throw new ClassCastException("server must be an instance of Registrar");
138     }
139     
140     static MethodConstraints translateConstraints(MethodConstraints constraints){
141 	return ConstrainableProxyUtil.translateConstraints(
142 		      constraints, methodMappings);
143     }
144 
145     /** Constructor for use by getInstance(), ConstrainableRegistration. */
146     Registration(Registrar server, ServiceLease lease) {
147 	this.server = server;
148 	this.lease = lease;
149     }
150 
151     // This method's javadoc is inherited from an interface of this class
152     @Override
153     public ServiceID getServiceID() {
154 	return lease.getServiceID();
155     }
156 
157     // This method's javadoc is inherited from an interface of this class
158     @Override
159     public Lease getLease() {
160 	return lease;
161     }
162 
163     // This method's javadoc is inherited from an interface of this class
164     @Override
165     public void addAttributes(Entry[] attrSets)
166 	throws UnknownLeaseException, RemoteException
167     {
168 	server.addAttributes(lease.getServiceID(),
169 			     lease.getReferentUuid(),
170 			     EntryRep.toEntryRep(attrSets, true));
171     }
172 
173     // This method's javadoc is inherited from an interface of this class
174     @Override
175     public void modifyAttributes(Entry[] attrSetTmpls, Entry[] attrSets)
176 	throws UnknownLeaseException, RemoteException
177     {
178 	server.modifyAttributes(lease.getServiceID(),
179 				lease.getReferentUuid(),
180 				EntryRep.toEntryRep(attrSetTmpls, false),
181 				EntryRep.toEntryRep(attrSets, false));
182     }
183 
184     // This method's javadoc is inherited from an interface of this class
185     @Override
186     public void setAttributes(Entry[] attrSets)
187 	throws UnknownLeaseException, RemoteException
188     {
189 	server.setAttributes(lease.getServiceID(),
190 			     lease.getReferentUuid(),
191 			     EntryRep.toEntryRep(attrSets, true));
192     }
193 
194     // This method's javadoc is inherited from an interface of this class
195     @Override
196     public Uuid getReferentUuid() {
197 	return lease.getReferentUuid();
198     }
199 
200     /** Returns the registration Uuid's hash code. */
201     @Override
202     public int hashCode() {
203 	return lease.getReferentUuid().hashCode();
204     }
205 
206     /** Returns true if registration Uuids match, false otherwise. */
207     @Override
208     public boolean equals(Object obj) {
209 	return ReferentUuids.compare(this, obj);
210     }
211 
212     /**
213      * Returns a string created from the proxy class name and the result
214      * of calling toString on the contained lease.
215      * 
216      * @return String
217      */
218     @Override
219     public String toString() {
220 	return getClass().getName() + "[" + lease + "]";
221     }
222 
223 
224     /** Verifies that member fields are non-null. */
225     private void readObject(ObjectInputStream in)
226 	throws IOException, ClassNotFoundException
227     {
228 	in.defaultReadObject();
229 	if (server == null) {
230 	    throw new InvalidObjectException("null server");
231 	} else if (lease == null) {
232 	    throw new InvalidObjectException("null lease");
233 	}
234     }
235 
236     /**
237      * Throws InvalidObjectException, since data for this class is required.
238      */
239     private void readObjectNoData() throws ObjectStreamException {
240 	throw new InvalidObjectException("no data");
241     }
242 }