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.ObjectOutputStream;
25 import java.lang.reflect.Method;
26 import net.jini.core.constraint.MethodConstraints;
27 import net.jini.core.constraint.RemoteMethodControl;
28 import net.jini.core.lease.Lease;
29 import net.jini.core.lease.LeaseMap;
30 import net.jini.core.lookup.ServiceID;
31 import net.jini.id.Uuid;
32 import net.jini.security.proxytrust.ProxyTrustIterator;
33 import net.jini.security.proxytrust.SingletonProxyTrustIterator;
34 import org.apache.river.api.io.AtomicSerial;
35 import org.apache.river.api.io.AtomicSerial.GetArg;
36
37
38
39
40
41
42
43 @AtomicSerial
44 final class ConstrainableEventLease
45 extends EventLease implements RemoteMethodControl
46 {
47 private static final long serialVersionUID = 2L;
48
49
50 private static final Method[] methodMappings = {
51 Util.getMethod(Lease.class, "cancel", new Class[0]),
52 Util.getMethod(Registrar.class, "cancelEventLease",
53 new Class[]{ long.class, Uuid.class }),
54
55 Util.getMethod(Lease.class, "renew", new Class[]{ long.class }),
56 Util.getMethod(Registrar.class, "renewEventLease",
57 new Class[]{ long.class, Uuid.class, long.class })
58 };
59
60
61
62
63
64 public static void verifyConsistentConstraints(
65 MethodConstraints constraints, Object proxy) throws InvalidObjectException {
66 ConstrainableProxyUtil.verifyConsistentConstraints(
67 constraints, proxy, methodMappings);
68 }
69
70
71
72 private final MethodConstraints constraints;
73
74
75 private static GetArg check(GetArg arg) throws IOException{
76 MethodConstraints constraints = (MethodConstraints) arg.get("constraints", null);
77 EventLease el = new EventLease(arg);
78 verifyConsistentConstraints(constraints, el.server);
79 return arg;
80 }
81
82 ConstrainableEventLease(GetArg arg) throws IOException{
83 super(check(arg));
84 constraints = (MethodConstraints) arg.get("constraints", null);
85 }
86
87
88
89
90
91 ConstrainableEventLease(Registrar server,
92 ServiceID registrarID,
93 long eventID,
94 Uuid leaseID,
95 long expiration,
96 MethodConstraints constraints,
97 boolean setConstraints)
98 {
99 super( setConstraints ? (Registrar) ((RemoteMethodControl) server).setConstraints(
100 ConstrainableProxyUtil.translateConstraints(
101 constraints, methodMappings)) : server,
102 registrarID,
103 eventID,
104 leaseID,
105 expiration);
106 this.constraints = constraints;
107 }
108
109
110
111
112 public LeaseMap<? extends Lease,Long> createLeaseMap(long duration) {
113 return new ConstrainableRegistrarLeaseMap(this, duration);
114 }
115
116
117
118
119
120 public boolean canBatch(Lease lease) {
121 if (!(super.canBatch(lease) && lease instanceof RemoteMethodControl)) {
122 return false;
123 }
124 return ConstrainableProxyUtil.equivalentConstraints(
125 ((RemoteMethodControl) lease).getConstraints(),
126 ConstrainableProxyUtil.translateConstraints(
127 constraints, ConstrainableRegistrarLeaseMap.methodMappings),
128 ConstrainableRegistrarLeaseMap.methodMappings);
129 }
130
131
132 public RemoteMethodControl setConstraints(MethodConstraints constraints) {
133 return new ConstrainableEventLease(
134 server, registrarID, eventID, leaseID, getExpiration(), constraints, true);
135 }
136
137
138 public MethodConstraints getConstraints() {
139 return constraints;
140 }
141
142
143
144
145
146 private ProxyTrustIterator getProxyTrustIterator() {
147 return new SingletonProxyTrustIterator(server);
148 }
149
150 private void writeObject(ObjectOutputStream out) throws IOException {
151 out.defaultWriteObject();
152 }
153
154
155
156
157
158 private void readObject(ObjectInputStream in)
159 throws IOException, ClassNotFoundException
160 {
161 in.defaultReadObject();
162 verifyConsistentConstraints(constraints, server);
163 }
164 }