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
21 import javax.management.ObjectName;
22 import javax.management.MalformedObjectNameException;
23 import java.util.List;
24 import java.util.ArrayList;
25
26 /**
27 * An MBean implementation for those attributes and operations we wish to expose on net.sf.ehcache.CacheManager.
28 * This class is not Serializable because it is an adapter around a net.sf.ehcache.CacheManager, which is itself
29 * not Serializable.
30 * @author Greg Luck
31 * @version $Id: CacheManager.java 512 2007-07-10 09:18:45Z gregluck $
32 * @since 1.3
33 */
34 public class CacheManager implements CacheManagerMBean {
35
36 private net.sf.ehcache.CacheManager cacheManager;
37 private ObjectName objectName;
38
39 /**
40 * Create a management CacheManager
41 * @param cacheManager
42 * @throws net.sf.ehcache.CacheException
43 */
44 public CacheManager(net.sf.ehcache.CacheManager cacheManager) throws CacheException {
45 this.cacheManager = cacheManager;
46 objectName = createObjectName(cacheManager);
47 }
48
49 /**
50 * Creates an object name using the scheme "net.sf.ehcache:type=CacheManager,name=<cacheManagerName>"
51 */
52 static ObjectName createObjectName(net.sf.ehcache.CacheManager cacheManager) {
53 ObjectName objectName;
54 try {
55 objectName = new ObjectName("net.sf.ehcache:type=CacheManager,name=" + cacheManager.getName());
56 } catch (MalformedObjectNameException e) {
57 throw new CacheException(e);
58 }
59 return objectName;
60 }
61
62 /**
63 * Gets the status attribute of the Ehcache
64 *
65 * @return The status value, as a String from the Status enum class
66 */
67 public String getStatus() {
68 return cacheManager.getStatus().toString();
69 }
70
71 /**
72 * Shuts down the CacheManager.
73 * <p/>
74 * If the shutdown occurs on the singleton, then the singleton is removed, so that if a singleton access method
75 * is called, a new singleton will be created.
76 */
77 public void shutdown() {
78 cacheManager.shutdown();
79 }
80
81 /**
82 * Clears the contents of all caches in the CacheManager, but without
83 * removing any caches.
84 * <p/>
85 * This method is not synchronized. It only guarantees to clear those elements in a cache
86 * at the time that the {@link net.sf.ehcache.Ehcache#removeAll()} mehod on each cache is called.
87 */
88 public void clearAll() {
89 cacheManager.clearAll();
90 }
91
92 /**
93 * Returns a JMX Cache bean
94 */
95 public Cache getCache(String name) {
96 return new Cache(cacheManager.getCache(name));
97 }
98
99 /**
100 * Gets the cache names managed by the CacheManager
101 */
102 public String[] getCacheNames() throws IllegalStateException {
103 return cacheManager.getCacheNames();
104 }
105
106 /**
107 * Gets a list of caches in this CacheManager
108 *
109 * @return a list of JMX Cache objects
110 */
111 public List getCaches() {
112 List cacheList = new ArrayList();
113 String[] caches = getCacheNames();
114 for (int i = 0; i < caches.length; i++) {
115 String cacheName = caches[i];
116 Cache cache = getCache(cacheName);
117 cacheList.add(cache);
118 }
119 return cacheList;
120 }
121
122
123 /**
124 * @return the object name for this MBean
125 */
126 ObjectName getObjectName() {
127 return objectName;
128 }
129 }