KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > jdo > cache > CarrierContainer


1 /*
2  * Created on Jul 28, 2005
3  */

4 package com.nightlabs.ipanema.jdo.cache;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.apache.log4j.Logger;
11
12 /**
13  * Whenever a new <tt>Carrier</tt> is created, it will be put into a <tt>CarrierContainer</tt>.
14  * The <tt>Cache</tt> manages a <tt>LinkedList</tt> of such containers with one being
15  * the active container. All newly created <tt>Carrier</tt>s are put into this active one.
16  * After a certain time, the <tt>Cache</tt> rolls its <tt>CarrierContainer</tt>s. This
17  * means, a new container is created, added as first container and the oldest containers
18  * are dropped (so only a certain number of containers is permanently existing).
19  * <p>
20  * This mechanism prevents iterating all <tt>Carrier</tt>s and checking the creation time of them.
21  * This way, simply all <tt>Carrier</tt>s of an old <tt>CarrierContainer</tt> are dropped.
22  * <p>
23  * This works similar to the server side management of <tt>CacheSession</tt>s in
24  * <tt>CacheSessionContainer</tt>s, but with the following difference: A <tt>CacheSession</tt>
25  * changes its container when it is refreshed by simply using it while a <tt>Carrier</tt>
26  * in the local cache stays in its container and therefore "dies" always after a limited time.
27  *
28  * @author Marco Schulze - marco at nightlabs dot de
29  */

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     /**
43      * @return Returns the createDT.
44      */

45     public long getCreateDT()
46     {
47         return createDT;
48     }
49
50     /**
51      * key: {@link Key} key<br/>
52      * value: {@link Carrier} carrier
53      */

54     private Map JavaDoc carriersByKey = new HashMap JavaDoc();
55
56     /**
57      * @param carrier The <tt>Carrier</tt> to add - never <tt>null</tt>.
58      */

59     protected void addCarrier(Carrier carrier)
60     {
61         if (closed)
62             throw new IllegalStateException JavaDoc("This CarrierContainer is closed! Why the hell do you try to add a Carrier?");
63
64         if (carrier == null)
65             throw new NullPointerException JavaDoc("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 JavaDoc it = carriersByKey.keySet().iterator(); it.hasNext(); ) {
104                 Key key = (Key) it.next();
105
106                 cache.remove(key);
107             }
108         }
109     }
110 }
111
Popular Tags