1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.river.phoenix.dl;
20
21 import java.io.IOException;
22 import java.io.InvalidObjectException;
23 import java.io.Serializable;
24 import java.lang.reflect.Method;
25 import java.rmi.RemoteException;
26 import java.rmi.activation.ActivationID;
27 import java.rmi.server.UID;
28 import net.jini.activation.Resolve;
29 import net.jini.core.constraint.MethodConstraints;
30 import net.jini.core.constraint.RemoteMethodControl;
31 import net.jini.export.ProxyAccessor;
32 import net.jini.security.TrustVerifier;
33 import net.jini.security.proxytrust.ProxyTrustIterator;
34 import net.jini.security.proxytrust.SingletonProxyTrustIterator;
35 import net.jini.security.proxytrust.TrustEquivalence;
36 import org.apache.river.api.io.AtomicSerial;
37 import org.apache.river.api.io.AtomicSerial.GetArg;
38 import org.apache.river.proxy.ConstrainableProxyUtil;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public final class ConstrainableAID extends AID
54 implements RemoteMethodControl, TrustEquivalence
55 {
56 private static final long serialVersionUID = 2625527831091986783L;
57 private static final Method[] methodMapping = new Method[2];
58
59 static {
60 try {
61 methodMapping[0] =
62 ActivationID.class.getMethod("activate",
63 new Class[]{boolean.class});
64 methodMapping[1] =
65 Activator.class.getMethod("activate",
66 new Class[]{ActivationID.class,
67 boolean.class});
68 } catch (NoSuchMethodException e) {
69 throw new AssertionError(e);
70 }
71 }
72
73
74 private final MethodConstraints constraints;
75
76 @AtomicSerial
77 static final class State implements Serializable, ProxyAccessor, Resolve {
78 private static final long serialVersionUID = 1673734348880788487L;
79 private final Activator activator;
80 private final UID uid;
81 private final MethodConstraints constraints;
82
83 State(Activator activator, UID uid, MethodConstraints constraints) {
84 this.activator = activator;
85 this.uid = uid;
86 this.constraints = constraints;
87 }
88
89 public State(GetArg arg) throws IOException, ClassNotFoundException{
90 this(arg.get("activator", null, Activator.class),
91 arg.get("uid", null, UID.class),
92 validate(arg));
93 }
94
95 private static MethodConstraints validate(GetArg arg) throws IOException, ClassNotFoundException{
96 Activator activator = arg.get("activator", null, Activator.class);
97 MethodConstraints constraints = arg.get("constraints", null, MethodConstraints.class);
98 MethodConstraints proxyCon = null;
99 if (activator instanceof RemoteMethodControl &&
100 (proxyCon = ((RemoteMethodControl)activator).getConstraints()) != null) {
101
102 return ConstrainableProxyUtil.reverseTranslateConstraints(
103 proxyCon, methodMapping);
104 }
105 ConstrainableProxyUtil.verifyConsistentConstraints(
106 constraints,
107 activator,
108 methodMapping);
109 return constraints;
110 }
111
112 public Object readResolve() throws InvalidObjectException {
113 return new ConstrainableAID(activator, uid, constraints);
114 }
115
116 public Object getProxy() {
117 return activator;
118 }
119 }
120
121
122
123
124
125
126
127 @AtomicSerial
128 public static final class Verifier implements TrustVerifier, Serializable {
129 private static final long serialVersionUID = 570158651966790233L;
130
131
132
133
134
135
136 private final RemoteMethodControl activator;
137
138
139
140
141
142
143
144
145
146 public Verifier(Activator activator) {
147 if (!(activator instanceof RemoteMethodControl)) {
148 throw new IllegalArgumentException(
149 "activator not a RemoteMethodControl instance");
150 } else if (!(activator instanceof TrustEquivalence)) {
151 throw new IllegalArgumentException(
152 "activator must implement TrustEquivalence");
153 }
154 this.activator = (RemoteMethodControl) activator;
155 }
156
157 private Verifier(RemoteMethodControl activator){
158 this.activator = activator;
159 }
160
161 Verifier(GetArg arg) throws IOException, ClassNotFoundException{
162 this(validate(arg.get("activator", null, RemoteMethodControl.class)));
163 }
164
165 private static RemoteMethodControl validate(RemoteMethodControl activator) throws InvalidObjectException{
166 if (!(activator instanceof TrustEquivalence)) {
167 throw new InvalidObjectException(
168 "activator must implement TrustEquivalence");
169 }
170 return activator;
171 }
172
173
174
175
176
177
178
179
180 public boolean isTrustedObject(Object obj, TrustVerifier.Context ctx)
181 throws RemoteException
182 {
183 if (obj == null || ctx == null) {
184 throw new NullPointerException();
185 } else if (!(obj instanceof ConstrainableAID)) {
186 return false;
187 }
188 RemoteMethodControl act =
189 (RemoteMethodControl) ((ConstrainableAID) obj).activator;
190 MethodConstraints mc = act.getConstraints();
191 TrustEquivalence trusted =
192 (TrustEquivalence) activator.setConstraints(mc);
193 return trusted.checkTrustEquivalence(act);
194 }
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208 public ConstrainableAID(Activator activator, UID uid) {
209 this(activator, uid, null);
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223 private ConstrainableAID(Activator activator,
224 UID uid,
225 MethodConstraints constraints)
226 {
227 super(check(activator), uid);
228 this.constraints = constraints;
229 }
230
231 private static Activator check(Activator activator)
232 {
233 if (!(activator instanceof RemoteMethodControl)) {
234 throw new IllegalArgumentException(
235 "activator not RemoteMethodControl instance");
236 } else if (!(activator instanceof TrustEquivalence)) {
237 throw new IllegalArgumentException(
238 "activator not TrustEquivalence instance");
239 }
240 return activator;
241 }
242
243
244 private ProxyTrustIterator getProxyTrustIterator() {
245 return new SingletonProxyTrustIterator(activator);
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259 public RemoteMethodControl setConstraints(MethodConstraints constraints) {
260 MethodConstraints actConstraints =
261 ConstrainableProxyUtil.translateConstraints(constraints,
262 methodMapping);
263 RemoteMethodControl act =
264 ((RemoteMethodControl) activator).setConstraints(actConstraints);
265 return new ConstrainableAID((Activator) act, uid, constraints);
266 }
267
268
269
270
271
272
273
274 public MethodConstraints getConstraints() {
275 return constraints;
276 }
277
278
279
280
281
282 public boolean checkTrustEquivalence(Object obj) {
283 if (!(obj instanceof ConstrainableAID)) {
284 return false;
285 }
286 ConstrainableAID aid = (ConstrainableAID) obj;
287 return (uid.equals(aid.uid) &&
288 ((TrustEquivalence) activator).checkTrustEquivalence(
289 aid.activator));
290 }
291
292 @Override
293 public String toString(){
294 StringBuilder sb = new StringBuilder(super.toString());
295 sb.append(", MethodConstraints ").append(constraints);
296 return sb.toString();
297 }
298
299 private Object writeReplace() {
300 return new State(activator, uid, constraints);
301 }
302
303 }