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.InvalidObjectException;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.lang.reflect.Method;
26  import net.jini.core.constraint.MethodConstraints;
27  import net.jini.core.constraint.RemoteMethodControl;
28  import net.jini.core.lease.Lease;
29  import net.jini.core.lease.LeaseMap;
30  import net.jini.core.lookup.ServiceID;
31  import net.jini.id.Uuid;
32  import net.jini.security.proxytrust.ProxyTrustIterator;
33  import net.jini.security.proxytrust.SingletonProxyTrustIterator;
34  import org.apache.river.api.io.AtomicSerial;
35  import org.apache.river.api.io.AtomicSerial.GetArg;
36  
37  /**
38   * EventLease subclass that supports constraints.
39   *
40   * @author Sun Microsystems, Inc.
41   *
42   */
43  @AtomicSerial
44  final class ConstrainableEventLease
45      extends EventLease implements RemoteMethodControl
46  {
47      private static final long serialVersionUID = 2L;
48  
49      /** Mappings between Lease and Registrar methods */
50      private static final Method[] methodMappings = {
51  	Util.getMethod(Lease.class, "cancel", new Class[0]),
52  	Util.getMethod(Registrar.class, "cancelEventLease",
53  		       new Class[]{ long.class, Uuid.class }),
54  
55  	Util.getMethod(Lease.class, "renew", new Class[]{ long.class }),
56  	Util.getMethod(Registrar.class, "renewEventLease",
57  		       new Class[]{ long.class, Uuid.class, long.class })
58      };
59  
60      /**
61       * Verifies that the client constraints for this proxy are consistent with
62       * those set on the underlying server ref.
63       */
64      public static void verifyConsistentConstraints(
65  	MethodConstraints constraints, Object proxy) throws InvalidObjectException {
66  	ConstrainableProxyUtil.verifyConsistentConstraints(
67  	    constraints, proxy, methodMappings);
68      }
69  
70  
71      /** Client constraints for this proxy, or null */
72      private final MethodConstraints constraints;
73  
74      
75      private static GetArg check(GetArg arg) throws IOException{
76  	MethodConstraints constraints = (MethodConstraints) arg.get("constraints", null);
77  	EventLease el = new EventLease(arg);
78  	verifyConsistentConstraints(constraints, el.server);
79  	return arg;
80      }
81      
82      ConstrainableEventLease(GetArg arg) throws IOException{
83  	super(check(arg));
84  	constraints = (MethodConstraints) arg.get("constraints", null);
85      }
86  
87      /**
88       * Creates new ConstrainableEventLease with given server reference, event
89       * and lease IDs, expiration time and client constraints.
90       */
91      ConstrainableEventLease(Registrar server,
92  			    ServiceID registrarID,
93  			    long eventID,
94  			    Uuid leaseID,
95  			    long expiration,
96  			    MethodConstraints constraints,
97  			    boolean setConstraints)
98      {
99  	super( setConstraints ? (Registrar) ((RemoteMethodControl) server).setConstraints(
100 		  ConstrainableProxyUtil.translateConstraints(
101 		      constraints, methodMappings)) : server,
102 	      registrarID,
103 	      eventID,
104 	      leaseID,
105 	      expiration);
106 	this.constraints = constraints;
107     }
108 
109     /**
110      * Creates a constraint-aware lease map.
111      */
112     public LeaseMap<? extends Lease,Long> createLeaseMap(long duration) {
113 	return new ConstrainableRegistrarLeaseMap(this, duration);
114     }
115 
116     /**
117      * Two leases can be batched if they are both RegistrarLeases, share the
118      * same server, and have compatible constraints.
119      */
120     public boolean canBatch(Lease lease) {
121 	if (!(super.canBatch(lease) && lease instanceof RemoteMethodControl)) {
122 	    return false;
123 	}
124 	return ConstrainableProxyUtil.equivalentConstraints(
125 	    ((RemoteMethodControl) lease).getConstraints(),
126 	    ConstrainableProxyUtil.translateConstraints(
127 		constraints, ConstrainableRegistrarLeaseMap.methodMappings),
128 	    ConstrainableRegistrarLeaseMap.methodMappings);
129     }
130 
131     // javadoc inherited from RemoteMethodControl.setConstraints
132     public RemoteMethodControl setConstraints(MethodConstraints constraints) {
133 	return new ConstrainableEventLease(
134 	    server, registrarID, eventID, leaseID, getExpiration(), constraints, true);
135     }
136 
137     // javadoc inherited from RemoteMethodControl.getConstraints
138     public MethodConstraints getConstraints() {
139 	return constraints;
140     }
141 
142     /**
143      * Returns iterator used by ProxyTrustVerifier to retrieve a trust verifier
144      * for this object.
145      */
146     private ProxyTrustIterator getProxyTrustIterator() {
147 	return new SingletonProxyTrustIterator(server);
148     }
149 
150     private void writeObject(ObjectOutputStream out) throws IOException {
151 	out.defaultWriteObject();
152     }
153 
154     /**
155      * Verifies that the client constraints for this proxy are consistent with
156      * those set on the underlying server ref.
157      */
158     private void readObject(ObjectInputStream in)
159 	throws IOException, ClassNotFoundException
160     {
161 	in.defaultReadObject();
162 	verifyConsistentConstraints(constraints, server);
163     }
164 }