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 GetArg check(GetArg arg) throws IOException {
47  	MethodConstraints constraints = (MethodConstraints) arg.get("constraints", null);
48  	Registration reg = new Registration(arg);
49  	ConstrainableProxyUtil.verifyConsistentConstraints(
50  	    constraints, reg.server, methodMappings);
51  	return arg;
52      }
53     
54      ConstrainableRegistration(GetArg arg) throws IOException {
55  	super(check(arg));
56  	constraints = (MethodConstraints) arg.get("constraints", null);
57      }
58  
59      /**
60       * Creates new ConstrainableRegistration with given server reference,
61       * service lease and client constraints.
62       */
63      ConstrainableRegistration(  Registrar server,
64  			      ServiceLease lease,
65  				MethodConstraints constraints,
66  				boolean setConstraints)
67      {
68  	super( setConstraints ? (Registrar) ((RemoteMethodControl) server).setConstraints(
69  		  translateConstraints(constraints)): server,lease);
70  	this.constraints = constraints;
71      }
72  
73      // javadoc inherited from RemoteMethodControl.setConstraints
74      @Override
75      public RemoteMethodControl setConstraints(MethodConstraints constraints) {
76  	return new ConstrainableRegistration(server, lease, constraints, true);
77      }
78  
79      // javadoc inherited from RemoteMethodControl.getConstraints
80      @Override
81      public MethodConstraints getConstraints() {
82  	return constraints;
83      }
84  
85      /**
86       * Returns iterator used by ProxyTrustVerifier to retrieve a trust verifier
87       * for this object.
88       */
89      private ProxyTrustIterator getProxyTrustIterator() {
90  	return new SingletonProxyTrustIterator(server);
91      }
92  
93      private void writeObject(ObjectOutputStream out) throws IOException {
94  	out.defaultWriteObject();
95      }
96  
97      /**
98       * Verifies that the client constraints for this proxy are consistent with
99       * those set on the underlying server ref.
100      */
101     private void readObject(ObjectInputStream in)
102 	throws IOException, ClassNotFoundException
103     {
104 	in.defaultReadObject();
105 	ConstrainableProxyUtil.verifyConsistentConstraints(
106 	    constraints, server, methodMappings);
107     }
108 }