KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > cache > total > WritableCache


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.cache.total;
19
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.sape.carbon.core.component.Component;
25 import org.sape.carbon.core.component.ComponentConfiguration;
26 import org.sape.carbon.core.component.lifecycle.Configurable;
27 import org.sape.carbon.core.component.lifecycle.Initializable;
28 import org.sape.carbon.services.cache.CacheLoadException;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * <p>The Total Cache implementation of the Cache interface is intended for
35  * access to data which expires periodically as a whole or which can be changed
36  * on the fly by clients.</p>
37  *
38  * <p>
39  * This implementation uses a synchronized map to store all data. This means
40  * that all gets and puts are synchronized to ensure consistent results
41  * </p>
42  *
43  * Copyright 2002 Sapient
44  * @since carbon 1.0
45  * @author Douglas Voet, March 2002
46  * @version $Revision: 1.15 $($Author: ghinkl $ / $Date: 2003/09/30 02:43:48 $)
47  */

48 public class WritableCache
49     extends AbstractTotalCache
50     implements Configurable, Initializable {
51
52     /** The handle to Apache-commons logger */
53     private Log log = LogFactory.getLog(this.getClass());
54
55     /** Map of the cached values. */
56     private Map JavaDoc activeMap = null;
57
58     /** Holds the dataloader for this cache. */
59     private TotalCacheDataLoader dataLoader = null;
60
61     /**
62      * Refresh the contents of the cache. First, initialize a holding cache.
63      * Then load the holding cache with new data from the data loader. Finally,
64      * activate the holding cache.
65      *
66      * @throws CacheLoadException when its data loader has failed to load data
67      */

68     public void refreshAll() throws CacheLoadException {
69
70         if (log.isTraceEnabled()) {
71             log.trace("Refreshing Cache");
72         }
73
74         synchronized (this.activeMap) {
75             this.activeMap.clear();
76             this.activeMap.putAll(this.dataLoader.loadAllData());
77         }
78     }
79
80     /**
81      * @see Configurable#configure(ComponentConfiguration)
82      */

83     public void configure(ComponentConfiguration configuration) {
84
85         this.dataLoader =
86             ((TotalCacheConfiguration) configuration).getDataLoader();
87     }
88
89     /**
90      * @see Initializable#initialize(org.sape.carbon.core.component.Component)
91      */

92     public void initialize(Component thisComponent) throws Exception JavaDoc {
93
94         Map JavaDoc holdingMap;
95         // Checking for instance of appropriate dataloader. This will
96
// enable backward compatibility.
97
if (this.dataLoader instanceof ConfigurableMapTypeCacheDataLoader) {
98             holdingMap = ((ConfigurableMapTypeCacheDataLoader)this.dataLoader).
99                 getMapInstance();
100         } else {
101             holdingMap = new HashMap JavaDoc();
102         }
103         this.activeMap = Collections.synchronizedMap(holdingMap);
104     }
105
106     /**
107      * @see AbstractTotalCache#getCacheMap()
108      */

109     protected Map JavaDoc getCacheMap() {
110         return this.activeMap;
111     }
112 }
Popular Tags