1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
43
44
45
46
47 @AtomicSerial
48 public class Registration implements ServiceRegistration, ReferentUuid, Serializable
49 {
50
51 private static final long serialVersionUID = 2L;
52
53
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
76
77
78
79 final Registrar server;
80
81
82
83
84
85 final ServiceLease lease;
86
87
88
89
90
91
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
113
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
123
124
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
146 Registration(Registrar server, ServiceLease lease) {
147 this.server = server;
148 this.lease = lease;
149 }
150
151
152 @Override
153 public ServiceID getServiceID() {
154 return lease.getServiceID();
155 }
156
157
158 @Override
159 public Lease getLease() {
160 return lease;
161 }
162
163
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
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
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
195 @Override
196 public Uuid getReferentUuid() {
197 return lease.getReferentUuid();
198 }
199
200
201 @Override
202 public int hashCode() {
203 return lease.getReferentUuid().hashCode();
204 }
205
206
207 @Override
208 public boolean equals(Object obj) {
209 return ReferentUuids.compare(this, obj);
210 }
211
212
213
214
215
216
217
218 @Override
219 public String toString() {
220 return getClass().getName() + "[" + lease + "]";
221 }
222
223
224
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
238
239 private void readObjectNoData() throws ObjectStreamException {
240 throw new InvalidObjectException("no data");
241 }
242 }