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  
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   * A subclass of <code>AID</code> that implements the {@link
42   * RemoteMethodControl} interface by delegating to the contained activator.
43   * 
44   * <p>This class exists as a convenience for activation system daemon
45   * implementations supporting <code>RemoteMethodControl</code> on proxies,
46   * to avoid requiring all such implementations to make code available for
47   * dynamic download to clients.
48   *
49   * @author Sun Microsystems, Inc.
50   * 
51   * @since 2.0
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      /** the client constraints */
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 		// Constraints set during proxy deserialization.
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      * A <code>ProxyTrust</code> trust verifier for
123      * <code>ConstrainableAID</code> instances.
124      *
125      * @since 2.0
126      **/
127     @AtomicSerial
128     public static final class Verifier implements TrustVerifier, Serializable {
129 	private static final long serialVersionUID = 570158651966790233L;
130 
131 	/**
132 	 * The expected activator.
133 	 *
134 	 * @serial
135 	 */
136 	private final RemoteMethodControl activator;
137 
138 	/**
139 	 * Creates a verifier for the specified activator.
140 	 *
141 	 * @param activator the activator
142 	 * @throws IllegalArgumentException if the specified activator is
143 	 * not an instance of <code>RemoteMethodControl</code> or
144 	 * <code>TrustEquivalence</code>
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 	 * Verifies trust in a proxy. Returns <code>true</code> if the
175 	 * proxy passed as an argument is an instance of
176 	 * <code>ConstrainableAID</code> and that proxy's internal
177 	 * activator instance is trust equivalent to the activator of this
178 	 * verifier; returns <code>false</code> otherwise.
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      * Creates an activation identifier containing the specified
199      * remote object activator, a new unique identifier, and
200      * <code>null</code> client constraints.
201      *
202      * @param activator the activator
203      * @param uid the unique identifier
204      * @throws IllegalArgumentException if the specified activator is not
205      * an instance of <code>RemoteMethodControl</code> or
206      * <code>TrustEquivalence</code>
207      */
208     public ConstrainableAID(Activator activator, UID uid) {
209 	this(activator, uid, null);
210     }
211 
212     /**
213      * Creates an activation identifier containing the specified
214      * remote object activator and a new unique identifier.
215      *
216      * @param activator the activator
217      * @param uid the unique identifier
218      * @param constraints the client constraints, or <code>null</code>
219      * @throws IllegalArgumentException if the specified activator is not
220      * an instance of <code>RemoteMethodControl</code> or
221      * <code>TrustEquivalence</code>
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     /** Returns an iterator that yields the activator. */
244     private ProxyTrustIterator getProxyTrustIterator() {
245 	return new SingletonProxyTrustIterator(activator);
246     }
247 
248     /**
249      * Returns a new copy of this activation identifier containing the same
250      * unique identifier and a copy of the activator with the new specified
251      * constraints.
252      *
253      * @param constraints the client constraints, or <code>null</code>
254      * @return a new copy of this activation identifier containing the same
255      * unique identifier and a copy of the activator with the new specified
256      * client constraints
257      * @see #getConstraints
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      * Returns the client constraints.
270      *
271      * @return the client constraints, or <code>null</code>
272      * @see #setConstraints
273      */
274     public MethodConstraints getConstraints() {
275 	return constraints;
276     }
277 
278     /**
279      * Returns true if the object is an instance of this class with the same
280      * UID and a trust equivalent activator.
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 }