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.norm; 19 20 import java.util.HashMap; 21 import java.util.Map; 22 23 import net.jini.core.lease.Lease; 24 25 /** 26 * Maps client leases to the wrapper objects that we use internally. 27 * <p> 28 * Internally we keep two tables of client leases, one mapping client 29 * leases to client lease wrappers, the other a list of client lease 30 * wrappers whose leases could not be unpacked (aka "deformed wrappers") 31 * when they were added (and thus could not be put in the map). This 32 * bifurcation is not exposed to the client. 33 * <p> 34 * Unless otherwise noted the methods of this class are not thread safe. 35 * 36 * @author Sun Microsystems, Inc. 37 */ 38 class LeaseTable { 39 /** 40 * The list we store deformed wrappers in. 41 */ 42 final private DeformedLeaseList deformedLeases = new DeformedLeaseList(); 43 44 /** 45 * The map we use to map client leases to wrappers. 46 */ 47 final private Map leaseTable = new HashMap(); 48 49 /** 50 * Find the client lease wrapper associated with the passed lease. 51 * 52 * @param clientLease the lease we need the wrapper for 53 * @return the wrapper associated with the passed lease, or 54 * <code>null</code> if we don't know about this lease 55 */ 56 ClientLeaseWrapper get(Lease clientLease) { 57 final ClientLeaseWrapper clw = 58 (ClientLeaseWrapper) leaseTable.get(clientLease); 59 if (clw == null) { 60 /* 61 * Could it be that this was a lease that could not be 62 * recovered? Check the deformed list. 63 */ 64 return deformedLeases.query(clientLease, leaseTable); 65 } else { 66 return clw; 67 } 68 } 69 70 /** 71 * Add a mapping from lease wrapper to client lease. Gets client 72 * lease from wrapper. 73 * 74 * @param clw client lease wrapper, and client lease to add to 75 * table 76 */ 77 void put(ClientLeaseWrapper clw) { 78 if (clw.isDeformed()) { 79 deformedLeases.add(clw); 80 } else { 81 leaseTable.put(clw.getClientLease(), clw); 82 } 83 } 84 85 /** 86 * Remove a lease from the table. 87 * 88 * @param clw client lease wrapper for the lease we want to 89 * remove 90 */ 91 void remove(ClientLeaseWrapper clw) { 92 if (!clw.isDeformed()) { 93 /* 94 * Not deformed, we need to remove both the client lease and 95 * the wrapper from the leaseTable. 96 */ 97 leaseTable.remove(clw.getClientLease()); 98 } else { 99 /* Deformed, we need to remove the lease from the deformed 100 * list. 101 */ 102 deformedLeases.remove(clw); 103 } 104 } 105 }