Return-Path: Delivered-To: apmail-incubator-river-commits-archive@locus.apache.org Received: (qmail 15504 invoked from network); 10 Dec 2008 05:25:31 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 10 Dec 2008 05:25:31 -0000 Received: (qmail 23362 invoked by uid 500); 10 Dec 2008 05:25:43 -0000 Delivered-To: apmail-incubator-river-commits-archive@incubator.apache.org Received: (qmail 23340 invoked by uid 500); 10 Dec 2008 05:25:43 -0000 Mailing-List: contact river-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: river-dev@incubator.apache.org Delivered-To: mailing list river-commits@incubator.apache.org Received: (qmail 23259 invoked by uid 99); 10 Dec 2008 05:25:43 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Dec 2008 21:25:43 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 10 Dec 2008 05:24:17 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id F1D4723888A2; Tue, 9 Dec 2008 21:25:06 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r724995 [2/6] - in /incubator/river/jtsk/skunk/niclas1/shared: ./ action/ action/src/ action/src/main/ action/src/main/java/ action/src/main/java/com/ action/src/main/java/com/sun/ action/src/main/java/com/sun/jini/ action/src/main/java/com... Date: Wed, 10 Dec 2008 05:25:04 -0000 To: river-commits@incubator.apache.org From: niclas@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081210052506.F1D4723888A2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/ConstrainableLandlordLease.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/ConstrainableLandlordLease.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/ConstrainableLandlordLease.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/ConstrainableLandlordLease.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,310 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import com.sun.jini.proxy.ConstrainableProxyUtil; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.lang.reflect.Method; +import net.jini.core.constraint.MethodConstraints; +import net.jini.core.constraint.RemoteMethodControl; +import net.jini.core.lease.Lease; +import net.jini.core.lease.LeaseMap; +import net.jini.export.proxytrust.ProxyTrustIterator; +import net.jini.export.proxytrust.SingletonProxyTrustIterator; +import net.jini.id.ReferentUuid; +import net.jini.id.Uuid; + +/** + * Constrainable sub-class of LandlordLease. + * Instances of this class can be verified using the + * LandlordProxyVerifier class. + * + * @author Sun Microsystems, Inc. + * @see LandlordProxyVerifier + * @since 2.0 + */ +final public class ConstrainableLandlordLease extends LandlordLease + implements RemoteMethodControl +{ + static final long serialVersionUID = 1L; + + /** + * Returns the public method for the specified Class type, + * method name, and array of parameter types. + *

+ * This method is typically used in place of {@link Class#getMethod + * Class.getMethod} to get a method that should definitely be defined; + * thus, this method throws an error instead of an exception if the + * given method is missing. + *

+ * This method is convenient for the initialization of a static + * variable for use as the mappings argument to + * {@link com.sun.jini.proxy.ConstrainableProxyUtil#translateConstraints + * ConstrainableProxyUtil.translateConstraints}. + * + * @param type the Class type that defines the + * method of interest + * @param name String containing the name of the + * method of interest + * @param parameterTypes the Class types of the parameters + * to the method of interest + * @return a Method object that provides information about, + * and access to, the method of interest + * @throws NoSuchMethodError if the method of interest + * cannot be found + * @throws NullPointerException + * if type or + * name is null + */ + private static Method getMethod( Class type, String name, + Class[] parameterTypes ) + { + try + { + return type.getMethod( name, parameterTypes ); + } + catch( NoSuchMethodException e ) + { + throw (Error) + ( new NoSuchMethodError( e.getMessage() ).initCause( e ) ); + } + } + + /** + * Convenience field containing the renew method + * defined in the Lease interface. This field is used in the + * method mapping arrays and when retrieving method constraints + * for comparison in canBatch. + */ + private static final Method renewMethod = + getMethod( Lease.class, "renew", new Class[]{ long.class } ); + + /** + * Convenience field containing the cancel method + * defined in the Lease interface. This field is used in the + * method mapping arrays and when retrieving method constraints + * for comparison in canBatch. + */ + private static final Method cancelMethod = + getMethod( Lease.class, "cancel", new Class[]{ } ); + + /** + * Convenience field containing the renewAll method + * defined in the Landlord interface. This field is + * used in the method mapping arrays. + */ + private static final Method renewAllMethod = + getMethod( Landlord.class, "renewAll", + new Class[]{ Uuid[].class, long[].class } ); + + /** + * Convenience field containing the cancelAll method + * defined in the Landlord interface. This field is + * used in the method mapping arrays. + */ + private static final Method cancelAllMethod = + getMethod( Landlord.class, "cancelAll", new Class[]{ Uuid[].class } ); + + /** + * Array containing element pairs in which each pair of elements + * represents a mapping between two methods having the following + * characteristics: + *

    + *
  • the first element in the pair is one of the public, remote + * methods that may be invoked by the client through + * Lease. + *
  • the second element in the pair is the method, implemented + * in the backend server class, that is ultimately executed in + * the server's backend when the client invokes the corresponding + * method in this proxy. + *
+ */ + private static final Method[] methodMapArray = { + renewMethod, + getMethod( Landlord.class, "renew", + new Class[]{ Uuid.class, long.class } ), + + cancelMethod, + getMethod( Landlord.class, "cancel", new Class[]{ Uuid.class } ) + }; + + /** + * When creating a LeaseMap we generate an implicit + * set of constraints based on the constraints found on the + * the initial lease, where Lease.renew maps to + * Landlord.renewAll and Lease.cancel maps to + * Landlord.cancelAll. This array holds this mapping. + * Mapping also used by ConstrainableLandlordLeaseMap in + * canContainKey. + */ + static final Method[] leaseMapMethodMapArray = { + renewMethod, renewAllMethod, + cancelMethod, cancelAllMethod + }; + + /** + * canBatch needs to check if this lease and + * the passed in lease have compatible constraints, this + * is the set of methods to compare. Structured so we can + * use {@link ConstrainableProxyUtil.equivalentConstraints}. + */ + private static final Method[] comparableMethodsMapArray = { + renewMethod, renewMethod, + cancelMethod, cancelMethod + }; + + /** + * Client constraints placed on this proxy (may be null) + * + * @serial + */ + private final MethodConstraints methodConstraints; + + /** + * Create a new ConstrainableLandlordLease. + * + * @param cookie a Uuid that universally and uniquely + * identifies the lease this object is to be a proxy for + * @param landlord Landlord object that will be used to + * communicate renew and cancel requests to the granter + * of the lease. + * @param landlordUuid a universally unique id that has been + * assigned to the server granting of the lease. + * Ideally the Uuid {@link + * ReferentUuid#getReferentUuid landlord.getUuid} would + * return if landlord implemented + * {@link ReferentUuid}. Used to determine when + * two leases can be batched together. + * @param expiration The initial expiration time of the lease in + * milliseconds since the beginning of the epoch. + * @param methodConstraints the client method constraints to place on + * this proxy (may be null). + * @throws NullPointerException if landlord, + * landlordUuid, or cookie + * is null. + * @throws ClassCastException if landlord + * does not implement RemoteMethodControl. + */ + public ConstrainableLandlordLease( Uuid cookie, Landlord landlord, + Uuid landlordUuid, long expiration, + MethodConstraints methodConstraints ) + { + super( cookie, constrainServer( landlord, methodConstraints, + methodMapArray ), + landlordUuid, expiration ); + this.methodConstraints = methodConstraints; + } + + /** + * Returns a copy of the given {@link Landlord} proxy having the + * client method constraints that result after a specified mapping + * is applied to the given method constraints. For details on the + * mapping see {@link ConstrainableProxyUtil#translateConstraints + * ConstrainableProxyUtil.translateConstraints}. + * + * @param server the proxy to attach constraints to + * @param constraints the source method constraints + * @param mapping mapping of methods to methods + * @throws NullPointerException if server is + * null. + * @throws ClassCastException if server + * does not implement RemoteMethodControl. + */ + private static Landlord constrainServer( Landlord server, + MethodConstraints constraints, Method[] mapping ) + { + final MethodConstraints serverRefConstraints + = ConstrainableProxyUtil.translateConstraints( constraints, + mapping ); + final RemoteMethodControl constrainedServer = + ( (RemoteMethodControl) server ). + setConstraints( serverRefConstraints ); + + return (Landlord) constrainedServer; + } + + // doc inherited from super + public RemoteMethodControl setConstraints( MethodConstraints constraints ) + { + return new ConstrainableLandlordLease( cookie(), landlord(), + landlordUuid(), expiration, constraints ); + } + + // doc inherited from super + public MethodConstraints getConstraints() + { + return methodConstraints; + } + + /** + * Returns a proxy trust iterator that is used in + * ProxyTrustVerifier to retrieve this object's + * trust verifier. + */ + private ProxyTrustIterator getProxyTrustIterator() + { + return new SingletonProxyTrustIterator( landlord() ); + } + + private void readObject( ObjectInputStream s ) + throws IOException, ClassNotFoundException + { + s.defaultReadObject(); + + /* basic validation of landlord and cookie were performed by + * LandlordLease.readObject(), we just need to verify than + * landlord implements RemoteMethodControl and that it has + * appropriate constraints. + */ + ConstrainableProxyUtil.verifyConsistentConstraints( + methodConstraints, landlord(), methodMapArray ); + } + + // doc inherited from super + public boolean canBatch( Lease lease ) + { + if( !super.canBatch( lease ) ) + { + return false; + } + + // Same landlord, check to see if we have comparable constraints. + if( !( lease instanceof ConstrainableLandlordLease ) ) + { + return false; + } + + final MethodConstraints lmc = + ( (ConstrainableLandlordLease) lease ).methodConstraints; + + return + ConstrainableProxyUtil.equivalentConstraints( + methodConstraints, lmc, comparableMethodsMapArray ); + } + + // doc inherited from super + public LeaseMap createLeaseMap( long duration ) + { + return new ConstrainableLandlordLeaseMap( + constrainServer( landlord(), methodConstraints, + leaseMapMethodMapArray ), + landlordUuid(), this, duration ); + } +} + Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/ConstrainableLandlordLeaseMap.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/ConstrainableLandlordLeaseMap.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/ConstrainableLandlordLeaseMap.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/ConstrainableLandlordLeaseMap.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import com.sun.jini.proxy.ConstrainableProxyUtil; +import net.jini.core.constraint.MethodConstraints; +import net.jini.core.constraint.RemoteMethodControl; +import net.jini.core.lease.Lease; +import net.jini.id.ReferentUuid; +import net.jini.id.Uuid; + +/** + * Constrainable sub-class of LandlordLeaseMap. + * + * @author Sun Microsystems, Inc. + * @since 2.0 + */ +final public class ConstrainableLandlordLeaseMap extends LandlordLeaseMap +{ + /** + * Create a new ConstrainableLandlordLeaseMap. + * + * @param landlord Reference to the entity that created + * lease. Assumes that any + * necessary method constraints have been + * attached. + * @param landlordUuid a universally unique id that has been + * assigned to the server granting of the lease. + * Ideally the Uuid {@link + * ReferentUuid.getUuid landlord.getUuid} would + * return if landlord implemented + * {@link ReferentUuid}. Used to determine when + * leases can be added to this map. + * @param lease First lease to be placed in the map. It is + * assumed that canContainKey(lease) + * would be true. Must work with the + * landlord protocol. + * @param duration The duration the lease should be renewed for if + * renewAll is called. + * @throws ClassCastException if landlord + * does not implement RemoteMethodControl. + * @throws NullPointerException if landlord is null. + */ + ConstrainableLandlordLeaseMap( Landlord landlord, Uuid landlordUuid, + Lease lease, long duration ) + { + super( landlord, landlordUuid, lease, duration ); + if( !( landlord instanceof RemoteMethodControl ) ) + { + throw new ClassCastException( "landlord must implement " + + "RemoteMethodControl" ); + } + } + + // doc inherited from super + public boolean canContainKey( Object key ) + { + if( !super.canContainKey( key ) ) + { + return false; + } + + // Same landlord, check to see if we have comparable constraints. + if( !( key instanceof ConstrainableLandlordLease ) ) + { + return false; + } + + // The key's constraints + final MethodConstraints lmc = + ( (ConstrainableLandlordLease) key ).getConstraints(); + + // Our constraints + final MethodConstraints omc = + ( (RemoteMethodControl) landlord() ).getConstraints(); + + // Are they equivalent (after applying the map)? + return ConstrainableProxyUtil.equivalentConstraints( + lmc, omc, ConstrainableLandlordLease.leaseMapMethodMapArray ); + + } +} Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/FixedLeasePeriodPolicy.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/FixedLeasePeriodPolicy.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/FixedLeasePeriodPolicy.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/FixedLeasePeriodPolicy.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import net.jini.core.lease.Lease; +import net.jini.core.lease.LeaseDeniedException; + +/** + * Simple implementation of LeasePeriodPolicy that grants + * lease times based on a fixed default and maximum lease. Will grant + * renewals longer than the maximum if the current lease and the + * request are both longer than the maximum. + * + * @author Sun Microsystems, Inc. + * @since 2.0 + */ +public class FixedLeasePeriodPolicy implements LeasePeriodPolicy +{ + /** + * Under normal circumstances, the maximum lease or renewal to grant + */ + final private long maximum; + + /** + * The default lease length + */ + final private long defaultLength; + + /** + * Create a new FixedLeasePeriodPolicy with + * the specified values for the maxium and default lease + * lengths. + * + * @param maximum the length in milliseconds of the longest lease + * this object should normally grant + * @param defaultLength the length in milliseconds of the + * default lease grants + * @throws IllegalArgumentException if either argument + * is not positive. + */ + public FixedLeasePeriodPolicy( long maximum, long defaultLength ) + { + if( maximum <= 0 ) + { + throw new IllegalArgumentException( "FixedLeasePeriodPolicy:" + + "maximum lease time must be larger than 0, " + + "passed:" + maximum ); + } + + if( defaultLength <= 0 ) + { + throw new IllegalArgumentException( "FixedLeasePeriodPolicy:" + + "default lease time must be larger than 0, " + + "passed:" + defaultLength ); + } + + this.maximum = maximum; + this.defaultLength = defaultLength; + } + + + /** + * Returns the duration this policy is willing to grant for the + * passed resource at this time. The duration actually granted + * will be shorter if the duration extends pass the end of the + * epoch. Must return a positive number. + *

+ * Note the duration returned by this method will be shorter than + * the final duration granted if the requested duration extends + * past the current expiration, and duration return by this + * method is before the current expiration. + * + * @param resource the resource having a lease granted + * or renewed + * @param requestedDuration the duration the client wants + * @throws IllegalArgumentException thrown if requestedDuration + * is less than 0 and not equal to + * Lease.ANYLENGTH or + * Lease.FOREVER. + */ + protected long calculateDuration( LeasedResource resource, + long requestedDuration ) + { + if( requestedDuration == Lease.FOREVER ) + { + requestedDuration = Long.MAX_VALUE; + } + else if( requestedDuration == Lease.ANY ) + { + requestedDuration = defaultLength; + } + else if( requestedDuration < 0 ) + { + throw new + IllegalArgumentException( "Negative lease duration " + + "requested" ); + } + + return Math.min( requestedDuration, maximum ); + } + + /** + * Method that provides some notion of the current time in milliseconds + * since the beginning of the epoch. Default implementation + * calls System.currentTimeMillis() + */ + protected long currentTime() + { + return System.currentTimeMillis(); + } + + /** + * Calculates an expiration based on the passed time and a requested + * duration. Will clip the expiration to the end of the epoch + * + * @param preferredDuration the duration the policy wants to grant + * @param now the current time in milliseconds since + * the beginning of the epoch + * @return the new expiration time for the lease in milliseconds since + * the beginning of the epoch + */ + private long calcExpiration( long preferredDuration, long now ) + { + long expiration = now + preferredDuration; + // Any addition of two positive longs is guaranteed to be + // negative if it overflowed + if( expiration < 0 ) + { + // Not enough time for the duration we want, set to end of + // epoch + return Long.MAX_VALUE; + } + + return expiration; + } + + + public Result grant( LeasedResource resource, long requestedDuration ) + throws LeaseDeniedException + { + final long now = currentTime(); + final long expiration = + calcExpiration( calculateDuration( resource, requestedDuration ), + now ); + final long duration = expiration - now; + return new Result( expiration, duration ); + } + + public Result renew( LeasedResource resource, long requestedDuration ) + throws LeaseDeniedException + { + final long now = currentTime(); + + // The new expiration we would grant independent of the + // current expiration time + long newExpiration = + calcExpiration( calculateDuration( resource, requestedDuration ), + now ); + + // Do not change the expiration time to be sooner that it is + // now unless the client explicitly asked for it. If they did + // ask for a sooner expiration give them exactly what they + // asked for. + + // The current expiration of the lease + final long oldExpiration = resource.getExpiration(); + + if( oldExpiration > newExpiration ) + { + // need to make sure they asked for it + + if( requestedDuration == Lease.ANY ) + { + // they did not explicitly ask for a sooner + // expiration, give them at least what they had + newExpiration = oldExpiration; + } + else + { + // The new expiration time they are requesting + // (possibly clipped to the end of the epoch) this is + // not affected by what we are willing to grant. + // [Note we know at this point that requestedDuration + // is >= 0] + final long requestedExpiration = calcExpiration( + requestedDuration, now ); + + if( requestedExpiration > oldExpiration ) + { + // they made a request for something longer, give + // them at least what they had + newExpiration = oldExpiration; + } + else + { + // They made a request for something shorter, + // give them what they at least what they want + newExpiration = requestedExpiration; + } + } + } + + // Now that we finally have decided on a new expiration, + // get the real duration and return the result. + return new Result( newExpiration, newExpiration - now ); + } +} Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/Landlord.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/Landlord.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/Landlord.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/Landlord.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import net.jini.core.lease.Lease; +import net.jini.core.lease.LeaseDeniedException; +import net.jini.core.lease.LeaseMap; +import net.jini.core.lease.UnknownLeaseException; +import net.jini.id.Uuid; + +/** + * Interface that granters of leases must implement in order to work + * with the LandlordLease implementation of the + * Lease interface. + * + * @author Sun Microsystems, Inc. + * @see net.jini.core.lease.Lease + * @see LandlordLease + * @since 2.0 + */ +public interface Landlord extends Remote +{ + /** + * Called by the lease when its {@link Lease#renew renew} method is called. + * Renews the lease that is associated with the given cookie. + * + * @param cookie the Uuid associated with the lease who's + * renew method was called + * @param duration argument passed to the renew call + * @return The new duration the lease should have + * @throws LeaseDeniedException if the landlord is unwilling to + * renew the lease + * @throws UnknownLeaseException if landlord does not know about + * a lease with the specified cookie + * @throws RemoteException if a communications failure occurs + */ + public long renew( Uuid cookie, long duration ) + throws LeaseDeniedException, UnknownLeaseException, RemoteException; + + /** + * Called by the lease when its {@link Lease#cancel cancel} method is + * called. Cancels the lease that is associated with the given + * cookie. + * + * @param cookie the Uuid associated with the lease who's + * renew method was called + * @throws UnknownLeaseException if landlord does not know about + * a lease with the specified cookie + * @throws RemoteException if a communications failure occurs + */ + public void cancel( Uuid cookie ) + throws UnknownLeaseException, RemoteException; + + /** + * Called by the lease map when its {@link LeaseMap#renewAll + * renewAll} method is called. Should renew the lease that is + * associated with each element of cookies + * + * @param cookies an array of Uuids, each universally and + * uniquely identifying a lease granted by this + * Landlord + * @param durations an array of longs, each representing an a + * duration in milliseconds that the client + * wants the lease associated with the Uuid + * from the corresponding element of + * cookies renewed for + * @return A RenewResults object that contains the new + * duration of each lease that was successfully renewed or + * the exception encountered for each lease that could not + * be renewed + * @throws RemoteException if a communications failure occurs + */ + public RenewResults renewAll( Uuid[] cookies, long[] durations ) + throws RemoteException; + + /** + * Called by the lease map when its {@link LeaseMap#cancelAll + * cancelAll} method is called. Should cancel the lease that is + * associated with each element of cookies + * + * @param cookies an array of Uuids, each universally and + * uniquely identifying a lease granted by this + * Landlord + * @return If all the leases specified in the cookies + * could be cancelled return null. Otherwise, + * return a Map that for each failed cancel + * attempt maps the corresponding cookie object to an + * exception describing the failure. + * @throws RemoteException if a communications failure occurs + */ + public java.util.Map cancelAll( Uuid[] cookies ) throws RemoteException; + + /** + * Simple class that holds return values of + * the {@link Landlord#renewAll Landlord.renewAll} method. + */ + public class RenewResults implements java.io.Serializable + { + static final long serialVersionUID = 2L; + + /** + * For each cookie passed to {@link Landlord#renewAll renewAll}, + * granted[i] is the granted lease time, or -1 if the + * renewal for that lease generated an exception. If there was + * an exception, the exception is held in denied. + * + * @serial + * @see #denied + */ + public long[] granted; + + /** + * The ith -1 in granted + * was denied because of denied[i]. If nothing was + * denied, this field is null. + * + * @serial + */ + public Exception[] denied; + + /** + * Create a RenewResults object setting the field + * granted to the passed value, and denied + * to null. + * + * @param granted The value for the field granted + */ + public RenewResults( long[] granted ) + { + this( granted, null ); + } + + /** + * Create a RenewResults object setting the field + * granted and denied fields to the + * passed values. + * + * @param granted the value for the field granted + * @param denied the value for the field denied + */ + public RenewResults( long[] granted, Exception[] denied) { + this.granted = granted; + this.denied = denied; + } + } +} Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordLease.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordLease.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordLease.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordLease.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,252 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import com.sun.jini.lease.AbstractLease; +import java.io.IOException; +import java.io.InvalidObjectException; +import java.io.ObjectInputStream; +import java.rmi.RemoteException; +import net.jini.core.lease.Lease; +import net.jini.core.lease.LeaseDeniedException; +import net.jini.core.lease.LeaseMap; +import net.jini.core.lease.UnknownLeaseException; +import net.jini.id.ReferentUuid; +import net.jini.id.ReferentUuids; +import net.jini.id.Uuid; + +/** + * Basic implementation of net.jini.core.lease.Lease that works + * with the the Landlord protocol. + * + * @author Sun Microsystems, Inc. + * @see Landlord + * @since 2.0 + */ +public class LandlordLease extends AbstractLease implements ReferentUuid +{ + static final long serialVersionUID = 2L; + + /** + * A universally unique ID that identifies the lease this object + * represents. + * + * @serial + */ + final private Uuid cookie; + + /** + * Owner of the resource associated with this lease. + * + * @serial + */ + final private Landlord landlord; + + /** + * The Uuid of the landlord. Used + * to determine if two leases can be batched together. + * + * @serial + */ + final private Uuid landlordUuid; + + /** + * Create a new LandlordLease. + * + * @param cookie a Uuid that universally and uniquely + * identifies the lease this object is to be a proxy for + * @param landlord Landlord object that will be used to + * communicate renew and cancel requests to the granter + * of the lease + * @param landlordUuid a universally unique id that has been + * assigned to the server granting of the lease. + * Ideally the Uuid {@link + * ReferentUuid#getReferentUuid landlord.getUuid} would + * return if landlord implemented + * {@link ReferentUuid}. Used to determine when + * two leases can be batched together. + * @param expiration the initial expiration time of the lease in + * milliseconds since the beginning of the epoch + * @throws NullPointerException if landlord, + * landlordUuid or cookie + * is null + */ + public LandlordLease( Uuid cookie, Landlord landlord, Uuid landlordUuid, + long expiration ) + { + super( expiration ); + + if( cookie == null ) + { + throw new NullPointerException( "Can't create a LandlordLease " + + "with a null cookie" ); + } + + if( landlord == null ) + { + throw new NullPointerException( "Can't create a LandlordLease " + + "with a null landlord" ); + } + + if( landlordUuid == null ) + { + throw new NullPointerException( "Can't create a LandlordLease " + + "with a null landlordUuid" ); + } + + this.cookie = cookie; + this.landlord = landlord; + this.landlordUuid = landlordUuid; + } + + // Implementation of the Lease interface + + // purposefully inherit doc comment from supertype + + public void cancel() throws UnknownLeaseException, RemoteException + { + landlord.cancel( cookie ); + } + + // purposefully inherit doc comment from supertype + protected long doRenew( long renewDuration ) + throws LeaseDeniedException, UnknownLeaseException, RemoteException + { + if( renewDuration < 0 && !( renewDuration == Lease.FOREVER || + renewDuration == Lease.ANY ) ) + { + throw new IllegalArgumentException( "Lease renewal: " + + "Asked for a negative duration" ); + } + return landlord.renew( cookie, renewDuration ); + } + + // purposefully inherit doc comment from supertype + public Uuid getReferentUuid() + { + return cookie; + } + + // purposefully inherit doc comment from supertype + public boolean equals( Object other ) + { + return ReferentUuids.compare( this, other ); + + // Note, we do not include the expiration in the equality test. + // If the lease is copied and ether the copy or the original + // is renewed they are conceptually the same because they + // still represent the same claim on the same resource + // --however their expiration will be different + } + + // inherit doc comment + public boolean canBatch( Lease lease ) + { + if( lease instanceof LandlordLease ) + { + return landlordUuid.equals( ( (LandlordLease) lease ).landlordUuid ); + } + + return false; + } + + /** + * Return the landlord. + */ + Landlord landlord() + { + return landlord; + } + + /** + * Return the landlord's Uuid. + */ + Uuid landlordUuid() + { + return landlordUuid; + } + + /** + * Return the cookie. + */ + Uuid cookie() + { + return cookie; + } + + /** + * Set the expiration. + */ + void setExpiration( long expiration ) + { + this.expiration = expiration; + } + + // inherit doc comment + public LeaseMap createLeaseMap( long duration ) + { + return new LandlordLeaseMap( landlord, landlordUuid, this, duration ); + } + + // purposefully inherit doc comment from supertype + public int hashCode() + { + return cookie.hashCode(); + } + + // purposefully inherit doc comment from supertype + public String toString() + { + return "LandlordLease:" + cookie + " landlord:" + landlord + + " landlordUuid:" + landlordUuid + " " + super.toString(); + } + + /** + * Read this object back validating state. + */ + private void readObject( ObjectInputStream in ) + throws IOException, ClassNotFoundException + { + in.defaultReadObject(); + + if( cookie == null ) + { + throw new InvalidObjectException( "null cookie reference" ); + } + + if( landlord == null ) + { + throw new InvalidObjectException( "null landlord reference" ); + } + + if( landlordUuid == null ) + { + throw new InvalidObjectException( "null landlordUuid reference" ); + } + } + + /** + * We should always have data in the stream, if this method + * gets called there is something wrong. + */ + private void readObjectNoData() throws InvalidObjectException + { + throw new + InvalidObjectException( "LandlordLease should always have data" ); + } +} Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordLeaseMap.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordLeaseMap.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordLeaseMap.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordLeaseMap.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import com.sun.jini.lease.AbstractLeaseMap; +import java.rmi.RemoteException; +import java.util.ConcurrentModificationException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import net.jini.core.lease.Lease; +import net.jini.core.lease.LeaseMapException; +import net.jini.id.ReferentUuid; +import net.jini.id.Uuid; + +/** + * Implementation of LeaseMap for LandlordLease. + * + * @author Sun Microsystems, Inc. + * @see LandlordLease + * @see net.jini.core.lease.LeaseMap + * @since 2.0 + */ +public class LandlordLeaseMap extends AbstractLeaseMap +{ + /** + * The landlord which this map will talk to. + */ + private final Landlord landlord; + + /** + * The Uuid of the {@link #landlord}. Used + * to determine if a lease can be placed in this map. + */ + private final Uuid landlordUuid; + + /** + * Create a new LandlordLeaseMap. + * + * @param landlord Owner of the resource lease is for. + * @param landlordUuid a universally unique id that has been + * assigned to the server granting of the lease. + * Ideally the Uuid {@link + * ReferentUuid.getUuid landlord.getUuid} would + * return if landlord implemented + * {@link ReferentUuid}. Used to determine when + * two leases can be batched together. + * @param lease first lease to be placed in the map. It is + * assumed that canContainKey(lease) + * would be true. Must work with the + * landlord protocol. + * @param duration the duration the lease should be renewed for if + * renewAll is called + * @throws NullPointerException if landlord or + * landlordUuid is null. + */ + LandlordLeaseMap( Landlord landlord, Uuid landlordUuid, Lease lease, + long duration ) + { + super( lease, duration ); + if( landlord == null ) + { + throw new NullPointerException( "Landlord must be non-null" ); + } + + if( landlordUuid == null ) + { + throw new NullPointerException( "landlordUuid must be non-null" ); + } + + this.landlord = landlord; + this.landlordUuid = landlordUuid; + } + + // inherit doc comment + public boolean canContainKey( Object key ) + { + if( key instanceof LandlordLease ) + { + return landlordUuid.equals( ( (LandlordLease) key ).landlordUuid() ); + } + + return false; + } + + // inherit doc comment + public void cancelAll() throws LeaseMapException, RemoteException + { + Uuid[] cookies = new Uuid[size()]; + LandlordLease[] leases = new LandlordLease[cookies.length]; + Iterator it = keySet().iterator(); + for( int i = 0; it.hasNext(); i++ ) + { + LandlordLease lease = (LandlordLease) it.next(); + leases[ i ] = lease; + cookies[ i ] = lease.cookie(); + } + + final Map rslt = landlord.cancelAll( cookies ); + + if( rslt == null ) + { + // Everything worked out, normal return + return; + } + else + { + // Some the leases could not be canceled, generate a + // LeaseMapException + + // translate the cookie->exception map into a + // lease->exception map + + int origSize = rslt.size(); + for( int i = 0; i < cookies.length; i++ ) + { + Object exception = rslt.remove( cookies[ i ] ); + // remove harmless if not in map + + if( exception != null ) + { // if it was in map + rslt.put( leases[ i ], exception ); // put back as lease + remove( leases[ i ] ); // remove from this map + } + } + + if( origSize != rslt.size() ) // some cookie wasn't found + { + throw new ConcurrentModificationException(); + } + + throw new LeaseMapException( + "Failure canceling one or more leases", rslt ); + } + } + + + // inherit doc comment + public void renewAll() throws LeaseMapException, RemoteException + { + Uuid[] cookies = new Uuid[size()]; + long[] extensions = new long[cookies.length]; + LandlordLease[] leases = new LandlordLease[cookies.length]; + Iterator it = keySet().iterator(); + for( int i = 0; it.hasNext(); i++ ) + { + LandlordLease lease = (LandlordLease) it.next(); + leases[ i ] = lease; + cookies[ i ] = lease.cookie(); + extensions[ i ] = ( (Long) get( lease ) ).longValue(); + } + + long now = System.currentTimeMillis(); + Landlord.RenewResults results = + landlord.renewAll( cookies, extensions ); + + Map bad = null; + int d = 0; + for( int i = 0; i < cookies.length; i++ ) + { + if( results.granted[ i ] != -1 ) + { + long newExp = now + results.granted[ i ]; + if( newExp < 0 ) // Overflow, set to Long.MAX_VALUE + { + newExp = Long.MAX_VALUE; + } + + leases[ i ].setExpiration( newExp ); + } + else + { + if( bad == null ) + { + bad = new HashMap( results.denied.length + + results.denied.length / 2 ); + } + Object badTime = remove( leases[ i ] ); // remove from this map + if( badTime == null ) // better be in there + { + throw new ConcurrentModificationException(); + } + bad.put( leases[ i ], results.denied[ d++ ] );// add to "bad" map + } + } + + if( bad != null ) + { + throw new LeaseMapException( "renewing", bad ); + } + } + + /** + * Return the landlord. + */ + Landlord landlord() + { + return landlord; + } +} + Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordProxyVerifier.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordProxyVerifier.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordProxyVerifier.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordProxyVerifier.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,212 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import java.io.IOException; +import java.io.InvalidObjectException; +import java.io.ObjectInputStream; +import java.io.Serializable; +import java.rmi.RemoteException; +import net.jini.core.constraint.MethodConstraints; +import net.jini.core.constraint.RemoteMethodControl; +import net.jini.export.proxytrust.TrustEquivalence; +import net.jini.id.ReferentUuid; +import net.jini.id.Uuid; +import net.jini.security.TrustVerifier; + +/** + * This class defines a trust verifier for the proxies defined + * in the landlord package. + * + * @author Sun Microsystems, Inc. + * @see net.jini.security.TrustVerifier + * @since 2.0 + */ +final public class LandlordProxyVerifier + implements Serializable, TrustVerifier +{ + private static final long serialVersionUID = 1L; + + /** + * The canonical instance of the server reference. This + * instance will be used by the isTrusted method + * as the known trusted object used to determine whether or not a + * given proxy is equivalent in trust, content, and function. + * + * @serial + */ + private final RemoteMethodControl landlord; + + /** + * The Uuid associated landlord. + * + * @serial + */ + private final Uuid landlordUuid; + + /** + * Returns a verifier for the proxies defined in the landlord + * package with the specified server reference and server + * Uuid. + * + * @param landlord the reference to the Landlord + * being used by the leases for communication + * back to the server. + * @param landlordUuid a universally unique id that has been + * assigned to the server granting of the lease. + * Ideally the Uuid {@link + * ReferentUuid#getReferentUuid landlord.getUuid} would + * return if landlord implemented + * {@link ReferentUuid}. Used to determine when + * two leases can be batched together. + * @throws UnsupportedOperationException if landlord does + * not implement both {@link RemoteMethodControl} and {@link + * TrustEquivalence} + * @throws NullPointerException if either argument is + * null. + */ + public LandlordProxyVerifier( Landlord landlord, Uuid landlordUuid ) + { + if( landlord == null ) + { + throw new NullPointerException( "landlord must not be null" ); + } + + if( landlordUuid == null ) + { + throw new NullPointerException( "landlordUuid must not be null" ); + } + + if( !( landlord instanceof RemoteMethodControl ) ) + { + throw new UnsupportedOperationException + ( "cannot construct verifier - server reference does not " + + "implement RemoteMethodControl" ); + } + + if( !( landlord instanceof TrustEquivalence ) ) + { + throw new UnsupportedOperationException + ( "cannot construct verifier - server reference does not " + + "implement TrustEquivalence" ); + } + + this.landlord = (RemoteMethodControl) landlord; + this.landlordUuid = landlordUuid; + } + + /** + * Returns true if the specified proxy object (that is + * not yet known to be trusted) is equivalent in trust, content, and + * function to the canonical server reference this object was + * constructed with; otherwise returns false. + * + * @param obj proxy object that will be compared to this class's stored + * canonical proxy to determine whether or not the given + * proxy object is equivalent in trust, content, and function. + * @return true if the specified object (that is not yet + * known to be trusted) is equivalent in trust, + * content, and function to the canonical inner + * proxy object referenced in this class; + * otherwise returns false. + * @throws NullPointerException if any argument is null + */ + public boolean isTrustedObject( Object obj, TrustVerifier.Context ctx ) + throws RemoteException + { + /* Validate the arguments */ + if( obj == null || ctx == null ) + { + throw new NullPointerException( "arguments must not be null" ); + } + + /* Prepare the input proxy object for trust verification. The types + * of proxies, specific to the service, that this method will + * handle are: + * - ConstrainableLandlordLease + */ + + // Server reference from input proxy + final RemoteMethodControl inputProxyServer; + if( obj instanceof ConstrainableLandlordLease ) + { + final ConstrainableLandlordLease cll = + (ConstrainableLandlordLease) obj; + + // Check the landlordUuid of obj + if( !landlordUuid.equals( cll.landlordUuid() ) ) + { + return false; + } + + // Extract the landlord ref + inputProxyServer = (RemoteMethodControl) cll.landlord(); + } + else + { + // A proxy type we don't know about + return false; + } + + /* Get the client constraints currently set on the server reference + * of contained in the input proxy + */ + final MethodConstraints mConstraints = + inputProxyServer.getConstraints(); + + /* Create a copy of the canonical server reference with its method + * constraints replaced with the method constraints on + * server reference of the input proxy. + */ + final TrustEquivalence constrainedServer = + (TrustEquivalence) landlord.setConstraints( mConstraints ); + + /* With respect to trust, content, and function, test whether + * the server reference from the input proxy is equivalent to + * the canonical server reference we have (once method + * constraints have been normalized.) + */ + return constrainedServer.checkTrustEquivalence( inputProxyServer ); + } + + /** + * Verifies that the server reference implements + * TrustEquivalence. + */ + private void readObject( ObjectInputStream in ) + throws IOException, ClassNotFoundException + { + in.defaultReadObject(); + + if( landlord == null ) + { + throw new InvalidObjectException( "null landlord reference" ); + } + + if( landlordUuid == null ) + { + throw new InvalidObjectException( "null landlordUuid reference" ); + } + + if( !( landlord instanceof TrustEquivalence ) ) + { + throw new InvalidObjectException( + "server does not implement TrustEquivalence" ); + } + } +} Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordUtil.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordUtil.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordUtil.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LandlordUtil.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import com.sun.jini.landlord.Landlord.RenewResults; +import java.util.ArrayList; +import java.util.Map; +import net.jini.core.lease.LeaseException; +import net.jini.core.lease.UnknownLeaseException; +import net.jini.id.Uuid; + + +/** + * Static methods useful for implementing landlords. + * + * @author Sun Microsystems, Inc. + * @since 2.0 + */ +public class LandlordUtil +{ + /** + * Call landlord.renew() for each object in + * cookie[], passing cookie[i] and + * durations[i]. Gather the results and + * exceptions into a RenewResults object. + * + * @param landlord A grantor of leases. + * @param cookies an array of Uuids, each uniquely and + * universally identifying a lease + * @param durations an array of longs, each representing a + * duration in milliseconds that the client + * wants the lease associated with the object + * from the corresponding element of + * cookies renewed for. + * @return A RenewResults object that contains the new + * duration of each lease that was successfully renewed or + * the exception encountered for each lease that could not + * be renewed. + */ + public static RenewResults renewAll( LocalLandlord landlord, + Uuid[] cookies, long[] durations ) + { + final int count = cookies.length; + final long[] granted = new long[count]; + final ArrayList exceptions = new ArrayList( count ); + + for( int i = 0; i < count; i++ ) + { + try + { + granted[ i ] = landlord.renew( cookies[ i ], durations[ i ] ); + } + catch( LeaseException le ) + { + // Set flag for client-side handling + granted[ i ] = -1; + exceptions.add( le ); + } + } + + if( exceptions.size() == 0 ) + { + return new Landlord.RenewResults( granted ); + } + else + { + // Note: Can't just cast exceptions.toArray() to Exception[] + final Exception[] es = new Exception[exceptions.size()]; + return new RenewResults( granted, + (Exception[]) exceptions.toArray( es ) ); + } + } + + /** + * Call landlord.cancel() for each object in + * cookies[], passing cookies[i]. + * + * @param landlord a grantor of leases + * @param cookies an array of Uuids, each uniquely and + * universally identifying a lease + * @return If no exceptions are encountered, return + * null, otherwise, return a Map that + * for each failed cancel() call maps + * the passed cookie object to the exception that + * was generated + */ + public static Map cancelAll( LocalLandlord landlord, Uuid[] cookies ) + { + final int count = cookies.length; + + Map map = null; + for( int i = 0; i < count; i++ ) + { + try + { + landlord.cancel( cookies[ i ] ); + } + catch( UnknownLeaseException e ) + { + if( map == null ) + { + map = new java.util.HashMap(); + } + map.put( cookies[ i ], e ); + } + } + + return map; + } +} Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeaseFactory.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeaseFactory.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeaseFactory.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeaseFactory.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import net.jini.core.constraint.RemoteMethodControl; +import net.jini.export.proxytrust.TrustEquivalence; +import net.jini.id.ReferentUuid; +import net.jini.id.Uuid; +import net.jini.security.TrustVerifier; + +/** + * Factory for {@link LandlordLease} instances. + * + * @author Sun Microsystems, Inc. + * @since 2.0 + */ +public class LeaseFactory +{ + /** + * The inner proxy for the leases we create + */ + final private Landlord landlord; + + /** + * The Uuid of the server granting the leases + */ + final private Uuid landlordUuid; + + /** + * Create a new LeaseFactory that will create + * LandlordLeases with the specified + * Landlord and landlord Uuid. + * + * @param landlord the inner proxy that the leases will + * use to communicate back to the server. + * @param landlordUuid a universally unique id that has + * been assigned to the server granting of the lease. + * Ideally the Uuid {@link + * ReferentUuid#getReferentUuid landlord.getUuid} + * would return if landlord implemented + * {@link ReferentUuid}. Used + * to determine when two leases can be batched together. + * @throws NullPointerException if either argument is + * null + */ + public LeaseFactory( Landlord landlord, Uuid landlordUuid ) + { + if( landlord == null ) + { + throw new NullPointerException( "landlord must be non-null" ); + } + + if( landlordUuid == null ) + { + throw new NullPointerException( "landlordUuid must be non-null" ); + } + + this.landlord = landlord; + this.landlordUuid = landlordUuid; + } + + /** + * Return a new LandlordLease with the specified + * initial expiration and cookie using the inner proxy and + * Uuid the factory was created with. Will return + * a {@link ConstrainableLandlordLease} if inner proxy this + * factory was created with implements {@link RemoteMethodControl}. + * + * @param cookie a Uuid that will universally and uniquely + * identify the lease + * @param expiration the initial expiration time of the lease. + * @return a new LandlordLease. + * @throws NullPointerException if the cookie argument is + * null. + */ + public LandlordLease newLease( Uuid cookie, long expiration ) + { + if( landlord instanceof RemoteMethodControl ) + { + return new ConstrainableLandlordLease( cookie, landlord, + landlordUuid, expiration, null ); + } + return new LandlordLease( cookie, landlord, landlordUuid, expiration ); + } + + /** + * Return a TrustVerifier that will verify the + * proxies produced by this factory. Currently the verifier + * returned will be an instance of {@link LandlordProxyVerifier}. + * + * @return a new TrustVerifier. + * @throws UnsupportedOperationException if the {@link Landlord} this + * factory was created with does not implement both + * {@link RemoteMethodControl} and {@link TrustEquivalence}. + */ + public TrustVerifier getVerifier() + { + return new LandlordProxyVerifier( landlord, landlordUuid ); + } +} Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeasePeriodPolicy.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeasePeriodPolicy.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeasePeriodPolicy.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeasePeriodPolicy.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import net.jini.core.lease.LeaseDeniedException; + +/** + * Interface for objects that define what policy to use + * when calculating lease grants and renewals. + * + * @author Sun Microsystems, Inc. + * @since 2.0 + */ +public interface LeasePeriodPolicy +{ + /** + * Simple class that holds a pair of times, the + * new expiration and duration for a lease. + */ + public class Result + { + /** + * The expiration time. + */ + final public long expiration; + + /** + * The duration. + */ + final public long duration; + + /** + * Simple constructor to create a new Result + * object. + * + * @param expiration the value for the expiration field. + * @param duration the value for the duration field. + */ + public Result( long expiration, long duration ) + { + this.expiration = expiration; + this.duration = duration; + } + } + + /** + * Calculate the initial expiration and duration for + * a new lease. + * + * @param resource the server side representation of the new lease that + * needs an initial expiration and duration + * @param requestedDuration the initial duration the + * requester of the resources would like. May be + * Lease.ANY + * @return A Result that holds + * both the expiration and duration. + * @throws LeaseDeniedException if the grant request + * is denied. + * @throws IllegalArgumentException if the + * requested duration is not Lease.ANY + * and is negative. + */ + public Result grant( LeasedResource resource, long requestedDuration ) + throws LeaseDeniedException; + + /** + * Calculate the expiration and duration for an + * existing lease that is being renewed. + * + * @param resource the server side representation of the lease that is + * being renewed + * @param requestedDuration the duration the client is requesting. May be + * Lease.ANY. + * @return A Result that holds + * both the expiration and duration. + * @throws LeaseDeniedException if the renewal request + * is denied. + * @throws IllegalArgumentException if the + * requested duration is not Lease.ANY + * and is negative. + */ + public Result renew( LeasedResource resource, long requestedDuration ) + throws LeaseDeniedException; +} Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeasedResource.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeasedResource.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeasedResource.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LeasedResource.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import net.jini.id.ReferentUuid; +import net.jini.id.Uuid; + +/** + * Server side representation of a lease + * + * @author Sun Microsystems, Inc. + * @see LeasePeriodPolicy + * @since 2.0 + */ +public interface LeasedResource +{ + /** + * Changes the expiration time of the lease. + * + * @param newExpiration The new expiration time in milliseconds + * since the beginning of the epoch + */ + public void setExpiration( long newExpiration ); + + /** + * Returns the expiration time of the lease. + * + * @return The expiration time in milliseconds since the beginning + * of the epoch + */ + public long getExpiration(); + + /** + * Returns the universally unique identifier associated with this + * lease. Any proxies for this lease that implement {@link ReferentUuid} + * should return this object from their + * {@link ReferentUuid#getReferentUuid getReferentUuid} + * method and should base their implementation of equals on + * this object. + */ + public Uuid getCookie(); +} + Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LocalLandlord.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LocalLandlord.java?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LocalLandlord.java (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/LocalLandlord.java Tue Dec 9 21:25:00 2008 @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sun.jini.landlord; + +import net.jini.core.lease.LeaseDeniedException; +import net.jini.core.lease.UnknownLeaseException; +import net.jini.id.Uuid; + +/** + * Interface that defines the basic landlord primitives (renew and + * cancel) as local methods. Classes already implementing + * Landlord should be able to implement this interface + * simply by adding it the the implements clause of their + * class declaration. + *

+ * Some users may want to implement this interface using an adaptor + * class. This could be useful in situations where the landlord's + * cancel and renew methods don't do quite the right thing for batch + * lease operations + *

+ * This interface gives local landlord utilities access to the cancel + * and renew methods. + * + * @author Sun Microsystems, Inc. + * @see com.sun.jini.landlord.Landlord + * @since 2.0 + */ +public interface LocalLandlord +{ + /** + * Renew the lease that is associated with the given cookie. + * + * @param cookie an object that universally and uniquely identifies a + * lease granted by this LocalLandlord + * @param duration the duration in milliseconds the client + * wants the lease renewed for + * @return The new duration the lease should have + * @throws LeaseDeniedException if the landlord is unwilling to + * renew the lease + * @throws UnknownLeaseException if landlord does not know about + * a lease with the specified cookie + */ + public long renew( Uuid cookie, long duration ) + throws LeaseDeniedException, UnknownLeaseException; + + /** + * Cancel the lease that is associated with the given cookie. + * + * @param cookie an object that universally and uniquely identifies a + * lease granted by this LocalLandlord + * @throws UnknownLeaseException if landlord does not know about + * a lease with the specified cookie + */ + public void cancel( Uuid cookie ) throws UnknownLeaseException; +} + Added: incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/package.html URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/package.html?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/package.html (added) +++ incubator/river/jtsk/skunk/niclas1/shared/landlord/src/main/java/com/sun/jini/landlord/package.html Tue Dec 9 21:25:00 2008 @@ -0,0 +1,58 @@ + + + + + +

+ This is a utility for services implementing leases. The {@link + com.sun.jini.landlord.Landlord} is the basic interface that defines the + utility. A service that exports leases implements the + Landlord interface, which is used for callbacks by the utility + classes when a lease's status changes. Each leased resource is identified + by a landlord-generated {@link net.jini.id.Uuid} called a cookie. The + {@link net.jini.core.lease.Lease} object handed to the client contains the + cookie, and that cookie is used when communicating with the landlord about + the lease. + +

+ The utility classes define a lease object type {@link + com.sun.jini.landlord.LandlordLease}. The Lease objects + handed to clients will be of this type. LandlordLease objects + talk to the Landlord about the lease. A factory class, {@link + com.sun.jini.landlord.LeaseFactory}, can be used to generate new + leases. The package also contains a constrainable implementation of + Lease, {@link + com.sun.jini.landlord.ConstrainableLandlordLease}, that implements + {@link net.jini.core.constraint.RemoteMethodControl} and supports proxy + trust verification (when used with {@link + com.sun.jini.landlord.LandlordProxyVerifier}) and suitable implementations + of {@link net.jini.core.lease.LeaseMap}. It also contains utilities and + interfaces to assist you in implementing the Landlord + interfaces: {@link com.sun.jini.landlord.LandlordUtil}, {@link + com.sun.jini.landlord.LeasePeriodPolicy}, and {@link + com.sun.jini.landlord.FixedLeasePeriodPolicy}. + +

+ Using the classes in this package, you can write services that export + leases and handle primarily those factors that are unique to your service, + letting the utility classes do generic lease-related work. + + + + Added: incubator/river/jtsk/skunk/niclas1/shared/logging/pom.xml URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/niclas1/shared/logging/pom.xml?rev=724995&view=auto ============================================================================== --- incubator/river/jtsk/skunk/niclas1/shared/logging/pom.xml (added) +++ incubator/river/jtsk/skunk/niclas1/shared/logging/pom.xml Tue Dec 9 21:25:00 2008 @@ -0,0 +1,16 @@ + + 4.0.0 + + org.apache.river.service + master-services + 10.0-SNAPSHOT + + org.apache.river.shared + river-logging + Apache River - Shared - Logging + jar + 10.0-SNAPSHOT + +