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 java.io.IOException;
21  import java.io.ObjectInputStream;
22  import java.io.ObjectOutputStream;
23  import net.jini.core.constraint.MethodConstraints;
24  import net.jini.core.constraint.RemoteMethodControl;
25  import net.jini.core.lookup.ServiceID;
26  import net.jini.security.proxytrust.ProxyTrustIterator;
27  import net.jini.security.proxytrust.SingletonProxyTrustIterator;
28  import org.apache.river.api.io.AtomicSerial;
29  import org.apache.river.api.io.AtomicSerial.GetArg;
30  
31  /**
32   * AdminProxy subclass that supports constraints.
33   *
34   * @author Sun Microsystems, Inc.
35   *
36   */
37  @AtomicSerial
38  final class ConstrainableAdminProxy
39      extends AdminProxy implements RemoteMethodControl
40  {
41      private static final long serialVersionUID = 2L;
42  
43      /** Client constraints for this proxy, or null */
44      private final MethodConstraints constraints;
45  
46      private static GetArg check(GetArg arg) throws IOException{
47  	MethodConstraints constraints = (MethodConstraints) arg.get("constraints", null);
48  	AdminProxy sup = new AdminProxy(arg);
49  	verifyConsistentConstraints(constraints, sup.server);
50  	return arg;
51      }
52      
53      ConstrainableAdminProxy(GetArg arg) throws IOException{
54  	super(check(arg));
55  	constraints = (MethodConstraints) arg.get("constraints", null);
56      }
57  
58      /**
59       * Creates new ConstrainableAdminProxy with given server reference, service
60       * ID and client constraints.
61       */
62      ConstrainableAdminProxy(Registrar server,
63  			    ServiceID registrarID,
64  			    MethodConstraints constraints)
65      {
66  	super((Registrar) ((RemoteMethodControl) server).setConstraints(
67  		  translateConstraints(constraints)),
68  	      registrarID);
69  	this.constraints = constraints;
70      }
71  
72      // javadoc inherited from RemoteMethodControl.setConstraints
73      public RemoteMethodControl setConstraints(MethodConstraints constraints) {
74  	return new ConstrainableAdminProxy(server, registrarID, constraints);
75      }
76  
77      // javadoc inherited from RemoteMethodControl.getConstraints
78      public MethodConstraints getConstraints() {
79  	return constraints;
80      }
81  
82      /**
83       * Returns iterator used by ProxyTrustVerifier to retrieve a trust verifier
84       * for this object.
85       */
86      private ProxyTrustIterator getProxyTrustIterator() {
87  	return new SingletonProxyTrustIterator(server);
88      }
89  
90      private void writeObject(ObjectOutputStream out) throws IOException {
91  	out.defaultWriteObject();
92      }
93  
94      /**
95       * Verifies that the client constraints for this proxy are consistent with
96       * those set on the underlying server ref.
97       */
98      private void readObject(ObjectInputStream in)
99  	throws IOException, ClassNotFoundException
100     {
101 	in.defaultReadObject();
102 	verifyConsistentConstraints(constraints, server);
103     }
104 }