| 1 4 package com.nightlabs.ipanema.jdo.cache; 5 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 10 import org.apache.log4j.Logger; 11 12 30 public class CarrierContainer 31 { 32 public static final Logger LOGGER = Logger.getLogger(CarrierContainer.class); 33 34 private Cache cache; 35 36 protected CarrierContainer(Cache cache) 37 { 38 this.cache = cache; 39 } 40 41 private long createDT = System.currentTimeMillis(); 42 45 public long getCreateDT() 46 { 47 return createDT; 48 } 49 50 54 private Map carriersByKey = new HashMap (); 55 56 59 protected void addCarrier(Carrier carrier) 60 { 61 if (closed) 62 throw new IllegalStateException ("This CarrierContainer is closed! Why the hell do you try to add a Carrier?"); 63 64 if (carrier == null) 65 throw new NullPointerException ("carrier"); 66 67 if (LOGGER.isDebugEnabled()) 68 LOGGER.debug("Adding Carrier with key " + carrier.getKey()); 69 70 synchronized (carriersByKey) { 71 carriersByKey.put(carrier.getKey(), carrier); 72 } 73 } 74 75 protected void removeCarrier(Key key) 76 { 77 if (closed) 78 return; 79 80 synchronized (carriersByKey) { 81 if (carriersByKey.remove(key) != null) { 82 if (LOGGER.isDebugEnabled()) 83 LOGGER.debug("Removed Carrier for key " + key); 84 } 85 else { 86 if (LOGGER.isDebugEnabled()) 87 LOGGER.debug("Could not remove (because did not find) Carrier for key " + key); 88 } 89 90 } 91 } 92 93 private boolean closed = false; 94 95 protected void close() 96 { 97 closed = true; 98 99 if (LOGGER.isDebugEnabled()) 100 LOGGER.debug("Closing CarrierContainer (created " + createDT + ")"); 101 102 synchronized (carriersByKey) { 103 for (Iterator it = carriersByKey.keySet().iterator(); it.hasNext(); ) { 104 Key key = (Key) it.next(); 105 106 cache.remove(key); 107 } 108 } 109 } 110 } 111 | Popular Tags |