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 java.lang.reflect.Method;
25  import java.rmi.MarshalledObject;
26  import net.jini.admin.Administrable;
27  import net.jini.core.constraint.MethodConstraints;
28  import net.jini.core.constraint.RemoteMethodControl;
29  import net.jini.core.event.RemoteEventListener;
30  import net.jini.core.lookup.ServiceID;
31  import net.jini.core.lookup.ServiceItem;
32  import net.jini.core.lookup.ServiceRegistrar;
33  import net.jini.core.lookup.ServiceTemplate;
34  import net.jini.security.proxytrust.ProxyTrustIterator;
35  import net.jini.security.proxytrust.SingletonProxyTrustIterator;
36  import org.apache.river.api.io.AtomicSerial;
37  import org.apache.river.api.io.AtomicSerial.GetArg;
38  
39  /**
40   * RegistrarProxy subclass that supports constraints.
41   *
42   * @author Sun Microsystems, Inc.
43   *
44   */
45  @AtomicSerial
46  final class ConstrainableRegistrarProxy
47      extends RegistrarProxy implements RemoteMethodControl
48  {
49      private static final long serialVersionUID = 2L;
50  
51      /** Mappings between ServiceRegistrar and Registrar methods */
52      static final Method[] methodMappings = {
53  	Util.getMethod(ServiceRegistrar.class, "getEntryClasses",
54  		       new Class[]{ ServiceTemplate.class }),
55  	Util.getMethod(Registrar.class, "getEntryClasses",
56  		       new Class[]{ Template.class }),
57  
58  	Util.getMethod(ServiceRegistrar.class, "getFieldValues",
59  		       new Class[]{ ServiceTemplate.class, int.class,
60  				    String.class }),
61  	Util.getMethod(Registrar.class, "getFieldValues",
62  		       new Class[]{ Template.class, int.class, int.class }),
63  
64  	Util.getMethod(ServiceRegistrar.class, "getGroups", new Class[0]),
65  	Util.getMethod(Registrar.class, "getMemberGroups", new Class[0]),
66  
67  	Util.getMethod(ServiceRegistrar.class, "getLocator", new Class[0]),
68  	Util.getMethod(Registrar.class, "getLocator", new Class[0]),
69  
70  	Util.getMethod(ServiceRegistrar.class, "getServiceTypes",
71  		       new Class[]{ ServiceTemplate.class, String.class }),
72  	Util.getMethod(Registrar.class, "getServiceTypes",
73  		       new Class[]{ Template.class, String.class }),
74  
75  	Util.getMethod(ServiceRegistrar.class, "lookup",
76  		       new Class[]{ ServiceTemplate.class }),
77  	Util.getMethod(Registrar.class, "lookup",
78  		       new Class[]{ Template.class }),
79  
80  	Util.getMethod(ServiceRegistrar.class, "lookup",
81  		       new Class[]{ ServiceTemplate.class, int.class }),
82  	Util.getMethod(Registrar.class, "lookup",
83  		       new Class[]{ Template.class, int.class }),
84  
85  	Util.getMethod(ServiceRegistrar.class, "notify",
86  		       new Class[]{ ServiceTemplate.class, int.class,
87  				    RemoteEventListener.class,
88  				    MarshalledObject.class, long.class }),
89  	Util.getMethod(Registrar.class, "notify",
90  		       new Class[]{ Template.class, int.class,
91  				    RemoteEventListener.class,
92  				    MarshalledObject.class, long.class }),
93  
94  	Util.getMethod(ServiceRegistrar.class, "register",
95  		       new Class[]{ ServiceItem.class, long.class }),
96  	Util.getMethod(Registrar.class, "register",
97  		       new Class[]{ Item.class, long.class }),
98  
99  	Util.getMethod(Administrable.class, "getAdmin", new Class[0]),
100 	Util.getMethod(Administrable.class, "getAdmin", new Class[0])
101     };
102 
103     /** Client constraints for this proxy, or null */
104     private final MethodConstraints constraints;
105 
106     private static GetArg check(GetArg arg) throws IOException{
107 	RegistrarProxy sup = new RegistrarProxy(arg);
108 	MethodConstraints constraints = (MethodConstraints) arg.get("constraints", null);
109 	ConstrainableProxyUtil.verifyConsistentConstraints(
110 	    constraints, sup.server, methodMappings);
111 	return arg;
112     }
113     
114     ConstrainableRegistrarProxy(GetArg arg) throws IOException{
115 	super(check(arg));
116 	constraints = (MethodConstraints) arg.get("constraints", null);
117     }
118     
119     /**
120      * Creates new ConstrainableRegistrarProxy with given server reference,
121      * service ID and client constraints.
122      */
123     ConstrainableRegistrarProxy(Registrar server,
124 				ServiceID registrarID,
125 				MethodConstraints constraints)
126     {
127 	super((Registrar) ((RemoteMethodControl) server).setConstraints(
128 		  ConstrainableProxyUtil.translateConstraints(
129 		      constraints, methodMappings)),
130 	      registrarID);
131 	this.constraints = constraints;
132     }
133 
134     // javadoc inherited from RemoteMethodControl.setConstraints
135     public RemoteMethodControl setConstraints(MethodConstraints constraints) {
136 	return new ConstrainableRegistrarProxy(
137 	    server, registrarID, constraints);
138     }
139 
140     // javadoc inherited from RemoteMethodControl.getConstraints
141     public MethodConstraints getConstraints() {
142 	return constraints;
143     }
144 
145     /**
146      * Returns iterator used by ProxyTrustVerifier to retrieve a trust verifier
147      * for this object.
148      */
149     private ProxyTrustIterator getProxyTrustIterator() {
150 	return new SingletonProxyTrustIterator(server);
151     }
152 
153     private void writeObject(ObjectOutputStream out) throws IOException {
154 	out.defaultWriteObject();
155     }
156 
157     /**
158      * Verifies that the client constraints for this proxy are consistent with
159      * those set on the underlying server ref.
160      */
161     private void readObject(ObjectInputStream in)
162 	throws IOException, ClassNotFoundException
163     {
164 	in.defaultReadObject();
165 	ConstrainableProxyUtil.verifyConsistentConstraints(
166 	    constraints, server, methodMappings);
167     }
168 }