KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > cache > impl > DocumentCacheImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.cache.impl;
17
18 import org.outerj.daisy.cache.DocumentCache;
19 import org.outerj.daisy.repository.commonimpl.DocumentImpl;
20 import org.outerj.daisy.repository.AvailableVariants;
21 import org.apache.commons.collections.map.LRUMap;
22 import org.apache.commons.collections.map.MultiKeyMap;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.activity.Initializable;
27 import org.apache.avalon.framework.service.Serviceable;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.ServiceException;
30
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 /**
35  * Implementation of {@link DocumentCache} that uses a LRUMap with a configurable
36  * limit. JMX manageable.
37  *
38  * @avalon.component version="1.0" name="documentcache" lifestyle="singleton"
39  * @avalon.service type="org.outerj.daisy.cache.DocumentCache"
40  */

41 public class DocumentCacheImpl implements DocumentCache, Configurable, Initializable, Serviceable, DocumentCacheImplMBean {
42     private MultiKeyMap documentCache;
43     private LRUMap documentCacheBacker;
44     private LRUMap availableVariantsCache;
45     private int documentCacheMaxSize;
46     private int availableVariantsCacheMaxSize;
47     private static final int DEFAULT_MAX_SIZE = 10000;
48     private ServiceManager serviceManager;
49
50     public void configure(Configuration configuration) throws ConfigurationException {
51         documentCacheMaxSize = configuration.getChild("documentCacheMaxSize").getValueAsInteger(DEFAULT_MAX_SIZE);
52         availableVariantsCacheMaxSize = configuration.getChild("availableVariantsCacheMaxSize").getValueAsInteger(DEFAULT_MAX_SIZE);
53     }
54
55     /**
56      * @avalon.dependency key="mbeanserver" type="javax.management.MBeanServer"
57      */

58     public void service(ServiceManager serviceManager) throws ServiceException {
59         this.serviceManager = serviceManager;
60     }
61
62     public void initialize() throws Exception JavaDoc {
63         documentCacheBacker = new LRUMap(documentCacheMaxSize);
64         documentCache = MultiKeyMap.decorate(documentCacheBacker);
65         availableVariantsCache = new LRUMap(availableVariantsCacheMaxSize);
66
67         MBeanServer JavaDoc mbeanServer = (MBeanServer JavaDoc)serviceManager.lookup("mbeanserver");
68         try {
69             mbeanServer.registerMBean(this, new ObjectName JavaDoc("Daisy:name=DocumentCache"));
70         } finally {
71             serviceManager.release(mbeanServer);
72         }
73     }
74
75     public void clear() {
76         documentCache.clear();
77         availableVariantsCache.clear();
78     }
79
80     public synchronized void put(long documentId, long branchId, long languageId, DocumentImpl document) {
81         documentCache.put(new Long JavaDoc(documentId), new Long JavaDoc(branchId), new Long JavaDoc(languageId), document);
82     }
83
84     public synchronized DocumentImpl get(long documentId, long branchId, long languageId) {
85         return (DocumentImpl)documentCache.get(new Long JavaDoc(documentId), new Long JavaDoc(branchId), new Long JavaDoc(languageId));
86     }
87
88     public synchronized void remove(long documentId) {
89         documentCache.removeAll(new Long JavaDoc(documentId));
90     }
91
92     public synchronized void remove(long documentId, long branchId, long languageId) {
93         documentCache.remove(new Long JavaDoc(documentId), new Long JavaDoc(branchId), new Long JavaDoc(languageId));
94     }
95
96     public synchronized void put(long documentId, AvailableVariants availableVariants) {
97         availableVariantsCache.put(new Long JavaDoc(documentId), availableVariants);
98     }
99
100     public synchronized AvailableVariants getAvailableVariants(long documentId) {
101         return (AvailableVariants)availableVariantsCache.get(new Long JavaDoc(documentId));
102     }
103
104     public synchronized void removeAvailableVariants(long documentId) {
105         availableVariantsCache.remove(new Long JavaDoc(documentId));
106     }
107
108     public synchronized int getDocumentCacheMaxSize() {
109         return documentCacheBacker.maxSize();
110     }
111
112     public synchronized int getDocumentCacheCurrentSize() {
113         return documentCache.size();
114     }
115
116     public int getAvailableVariantCacheMaxSize() {
117         return availableVariantsCache.maxSize();
118     }
119
120     public int getAvailableVariantCacheCurrentSize() {
121         return availableVariantsCache.size();
122     }
123
124     public synchronized void clearCache() {
125         documentCache.clear();
126     }
127 }
128
Popular Tags