1 /**
2 * Copyright 2003-2007 Greg Luck
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
17 package net.sf.ehcache.management;
18
19 import net.sf.ehcache.CacheException;
20 import net.sf.ehcache.Ehcache;
21
22 import javax.management.ObjectName;
23 import javax.management.MalformedObjectNameException;
24
25 /**
26 * A JMX MBean implementation for Cache
27 * @author Greg Luck
28 * @version $Id: Cache.java 512 2007-07-10 09:18:45Z gregluck $
29 * @since 1.3
30 */
31 public class Cache implements CacheMBean {
32
33 /**
34 * An Ehcache backing instance
35 */
36 private Ehcache cache;
37 private ObjectName objectName;
38
39
40 /**
41 * A constructor for JCache.
42 *
43 * JCache is an adaptor for an Ehcache, and therefore requires an Ehcace in its constructor.
44 * <p/>
45 * The {@link net.sf.ehcache.config.ConfigurationFactory} and clients can create these.
46 * <p/>
47 * A client can specify their own settings here and pass the {@link Ehcache} object
48 * into {@link net.sf.ehcache.CacheManager#addCache} to specify parameters other than the defaults.
49 * <p/>
50 * Only the CacheManager can initialise them.
51 *
52 * @param cache An ehcache
53 * @throws net.sf.ehcache.CacheException
54 */
55 public Cache(Ehcache cache) throws CacheException {
56 this.cache = cache;
57 objectName = createObjectName(cache.getCacheManager().toString(), cache.getName());
58 }
59
60 /**
61 * Creates an object name using the scheme "net.sf.ehcache:type=Cache,CacheManager=<cacheManagerName>,name=<cacheName>"
62 */
63 static ObjectName createObjectName(String cacheManagerName, String cacheName) {
64 ObjectName objectName;
65 try {
66 objectName = new ObjectName("net.sf.ehcache:type=Cache,CacheManager="
67 + cacheManagerName + ",name=" + cacheName);
68 } catch (MalformedObjectNameException e) {
69 throw new CacheException(e);
70 }
71 return objectName;
72 }
73
74
75 /**
76 * Removes all cached items.
77 *
78 * @throws IllegalStateException if the cache is not {@link net.sf.ehcache.Status#STATUS_ALIVE}
79 */
80 public void removeAll() throws IllegalStateException, CacheException {
81 cache.removeAll();
82 }
83
84 /**
85 * Flushes all cache items from memory to the disk store, and from the DiskStore to disk.
86 *
87 * @throws IllegalStateException if the cache is not {@link net.sf.ehcache.Status#STATUS_ALIVE}
88 */
89 public void flush() throws IllegalStateException, CacheException {
90 cache.flush();
91 }
92
93 /**
94 * Gets the status attribute of the Cache.
95 *
96 * @return The status value as a String from the Status enum class
97 */
98 public String getStatus() {
99 return cache.getStatus().toString();
100 }
101
102 /**
103 * Gets the cache name.
104 */
105 public String getName() {
106 return cache.getName();
107 }
108
109 /**
110 * Gets the JMX read-only CacheConfiguration
111 */
112 public CacheConfiguration getCacheConfiguration() {
113 return new CacheConfiguration(cache);
114 }
115
116 /**
117 * Gets the JMX cache statistics
118 */
119 public CacheStatistics getStatistics() {
120 return new CacheStatistics(cache);
121 }
122
123
124 /**
125 * @return the object name for this MBean
126 */
127 ObjectName getObjectName() {
128 return objectName;
129 }
130 }