KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > cache > ManageableCacheManager


1 package info.magnolia.cms.cache;
2
3 import info.magnolia.cms.beans.config.ConfigurationException;
4 import info.magnolia.cms.core.Content;
5 import info.magnolia.cms.core.Path;
6
7 import java.util.ArrayList JavaDoc;
8
9 import javax.management.MBeanServer JavaDoc;
10 import javax.management.MBeanServerFactory JavaDoc;
11 import javax.management.ObjectName JavaDoc;
12 import javax.servlet.ServletResponse JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19
20 /**
21  * A <code>CacheManager</code> that is JMX manageable.
22  * @author Andreas Brenk
23  * @author Fabrizio Giustina
24  * @since 3.0
25  */

26 public class ManageableCacheManager implements CacheManager, ManageableCacheManagerMBean {
27
28     private static final Logger log = LoggerFactory.getLogger(ManageableCacheManager.class);
29
30     private int cacheHits;
31
32     private final CacheManager cacheManager;
33
34     private int cacheMisses;
35
36     private int cachePuts;
37
38     public ManageableCacheManager(CacheManager cacheManager) {
39         this.cacheManager = cacheManager;
40     }
41
42     public boolean cacheRequest(CacheKey key, CacheableEntry entry, boolean canCompress) {
43         boolean didCache = this.cacheManager.cacheRequest(key, entry, canCompress);
44
45         if (didCache) {
46             this.cachePuts++;
47         }
48
49         return didCache;
50     }
51
52     public void flushAll() {
53         this.cacheManager.flushAll();
54     }
55
56     public int getCacheHits() {
57         return this.cacheHits;
58     }
59
60     public int getCacheMisses() {
61         return this.cacheMisses;
62     }
63
64     public int getCachePuts() {
65         return this.cachePuts;
66     }
67
68     public long getCreationTime(CacheKey request) {
69         return this.cacheManager.getCreationTime(request);
70     }
71
72     public void init(Content content) throws ConfigurationException {
73         this.cacheManager.init(content);
74         registerMBean();
75     }
76
77     public boolean isEnabled() {
78         return this.cacheManager.isEnabled();
79     }
80
81     public boolean isPaused() {
82         return this.cacheManager.isPaused();
83     }
84
85     public boolean isRunning() {
86         return this.cacheManager.isRunning();
87     }
88
89     public boolean isStarted() {
90         return this.cacheManager.isStarted();
91     }
92
93     public void pause() {
94         this.cacheManager.pause();
95     }
96
97     public void resetStatistics() {
98         this.cacheHits = 0;
99         this.cacheMisses = 0;
100         this.cachePuts = 0;
101     }
102
103     public void restart() {
104         this.cacheManager.restart();
105     }
106
107     public void resume() {
108         this.cacheManager.resume();
109     }
110
111     public void start() {
112         this.cacheManager.start();
113     }
114
115     public void stop() {
116         this.cacheManager.stop();
117     }
118
119     public boolean streamFromCache(HttpServletRequest JavaDoc request, ServletResponse JavaDoc response) {
120         return false;
121     }
122
123     /**
124      * @see info.magnolia.cms.cache.CacheManager#isCacheable(javax.servlet.http.HttpServletRequest)
125      */

126     public boolean isCacheable(HttpServletRequest JavaDoc request) {
127         return this.cacheManager.isCacheable(request);
128     }
129
130     /**
131      * @see info.magnolia.cms.cache.CacheManager#canCompress(javax.servlet.http.HttpServletRequest)
132      */

133     public boolean canCompress(HttpServletRequest JavaDoc request) {
134         return this.cacheManager.canCompress(request);
135     }
136
137     public CacheKey getCacheKey(HttpServletRequest JavaDoc request) {
138         return this.cacheManager.getCacheKey(request);
139     }
140
141     public boolean streamFromCache(CacheKey key, HttpServletResponse JavaDoc response, boolean canCompress) {
142         boolean didUseCache = this.cacheManager.streamFromCache(key, response, canCompress);
143
144         if (didUseCache) {
145             this.cacheHits++;
146         }
147         else if (isRunning()) {
148             this.cacheMisses++;
149         }
150
151         return didUseCache;
152     }
153
154     private void registerMBean() {
155         String JavaDoc appName = Path.getAppRootDir().getName();
156         // not totally failsafe, but it will work unless you run two instances in directories with the same name
157
String JavaDoc objectName = "Magnolia:type=CacheManager,domain=" + appName;
158         try {
159             ArrayList JavaDoc list = MBeanServerFactory.findMBeanServer(null);
160             MBeanServer JavaDoc mbeanServer;
161             if (list != null && list.size() > 0) {
162                 mbeanServer = (MBeanServer JavaDoc) list.get(0);
163             }
164             else {
165                 mbeanServer = MBeanServerFactory.createMBeanServer();
166             }
167             mbeanServer.registerMBean(this, new ObjectName JavaDoc(objectName));
168         }
169         catch (Exception JavaDoc e) {
170             log.error("Could not register JMX MBean '" + objectName + "'.", e);
171         }
172     }
173 }
174
Popular Tags