~ubuntu-branches/ubuntu/oneiric/ehcache/oneiric

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/management/CacheManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2010-06-23 10:35:31 UTC
  • mfrom: (1.1.5 upstream) (2.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100623103531-ra0qdpmotoz6ygct
Tags: 2.1.0-1
Merge changes from Thierry's PPA and upload to Debian.

Show diffs side-by-side

added added

removed removed

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