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 org.apache.river.proxy.ConstrainableProxyUtil;
21  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import net.jini.core.constraint.MethodConstraints;
25  import net.jini.core.constraint.RemoteMethodControl;
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   * Registration subclass that supports constraints.
33   *
34   * @author Sun Microsystems, Inc.
35   *
36   */
37  @AtomicSerial
38  final class ConstrainableRegistration
39      extends Registration 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 MethodConstraints check(GetArg arg) throws IOException {
47  	MethodConstraints constraints = (MethodConstraints) arg.get("constraints", null);
48  	Registration reg = new Registration(arg);
49  	MethodConstraints proxyCon = null;
50  	if (reg.server instanceof RemoteMethodControl && 
51  	    (proxyCon = ((RemoteMethodControl)reg.server).getConstraints()) != null) {
52  	    // Constraints set during proxy deserialization.
53  	    return ConstrainableProxyUtil.reverseTranslateConstraints(
54  		    proxyCon, methodMappings);
55  	}
56  	ConstrainableProxyUtil.verifyConsistentConstraints(
57  	    constraints, reg.server, methodMappings);
58  	return constraints;
59      }
60     
61      ConstrainableRegistration(GetArg arg) throws IOException {
62  	this(arg, check(arg));
63      }
64      
65      ConstrainableRegistration(GetArg arg, MethodConstraints constraints) throws IOException{
66  	super(arg);
67  	this.constraints = constraints;
68      }
69  
70      /**
71       * Creates new ConstrainableRegistration with given server reference,
72       * service lease and client constraints.
73       */
74      ConstrainableRegistration(  Registrar server,
75  			      ServiceLease lease,
76  				MethodConstraints constraints,
77  				boolean setConstraints)
78      {
79  	super( setConstraints ? (Registrar) ((RemoteMethodControl) server).setConstraints(
80  		  translateConstraints(constraints)): server,lease);
81  	this.constraints = constraints;
82      }
83  
84      // javadoc inherited from RemoteMethodControl.setConstraints
85      @Override
86      public RemoteMethodControl setConstraints(MethodConstraints constraints) {
87  	return new ConstrainableRegistration(server, lease, constraints, true);
88      }
89  
90      // javadoc inherited from RemoteMethodControl.getConstraints
91      @Override
92      public MethodConstraints getConstraints() {
93  	return constraints;
94      }
95  
96      /**
97       * Returns iterator used by ProxyTrustVerifier to retrieve a trust verifier
98       * for this object.
99       */
100     private ProxyTrustIterator getProxyTrustIterator() {
101 	return new SingletonProxyTrustIterator(server);
102     }
103 
104     private void writeObject(ObjectOutputStream out) throws IOException {
105 	out.defaultWriteObject();
106     }
107 
108     /**
109      * Verifies that the client constraints for this proxy are consistent with
110      * those set on the underlying server ref.
111      */
112     private void readObject(ObjectInputStream in)
113 	throws IOException, ClassNotFoundException
114     {
115 	in.defaultReadObject();
116 	ConstrainableProxyUtil.verifyConsistentConstraints(
117 	    constraints, server, methodMappings);
118     }
119 }