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 java.io.Serializable;
20 import javax.management.Attribute;
21 import javax.management.AttributeList;
22 import javax.management.AttributeNotFoundException;
23 import javax.management.DynamicMBean;
24 import javax.management.InvalidAttributeValueException;
25 import javax.management.MBeanException;
26 import javax.management.MBeanInfo;
27 import javax.management.MalformedObjectNameException;
28 import javax.management.NotCompliantMBeanException;
29 import javax.management.ObjectName;
30 import javax.management.ReflectionException;
31 import javax.management.StandardMBean;
32 import net.sf.ehcache.CacheException;
33 import net.sf.ehcache.Ehcache;
34 import net.sf.ehcache.hibernate.management.impl.EhcacheHibernateMbeanNames;
35
36 /***
37 * Wrapper class for store specific dynamic mbeans.
38 *
39 * @author Chris Dennis
40 */
41 public final class Store implements Serializable, DynamicMBean {
42
43 private static final long serialVersionUID = 3477287016924524437L;
44
45 private final ObjectName objectName;
46 private final DynamicMBean storeBean;
47
48 private Store(Ehcache ehcache, Object storeBean) throws NotCompliantMBeanException {
49 this.objectName = createObjectName(ehcache.getCacheManager().getName(), ehcache.getName());
50 if (storeBean instanceof DynamicMBean) {
51 this.storeBean = (DynamicMBean) storeBean;
52 } else {
53 this.storeBean = new StandardMBean(storeBean, null);
54 }
55 }
56
57 /***
58 * Get the optional store management bean for the given cache.
59 */
60 static Store getBean(Ehcache cache) throws NotCompliantMBeanException {
61 if (cache instanceof net.sf.ehcache.Cache) {
62 Object bean = ((net.sf.ehcache.Cache) cache).getStoreMBean();
63 if (bean != null) {
64 return new Store(cache, bean);
65 }
66 }
67 return null;
68 }
69
70 /***
71 * Creates an object name using the scheme "net.sf.ehcache:type=Store,CacheManager=<cacheManagerName>,name=<cacheName>"
72 */
73 static ObjectName createObjectName(String cacheManagerName, String cacheName) {
74 try {
75 return new ObjectName("net.sf.ehcache:type=Store,CacheManager=" + cacheManagerName + ",name="
76 + EhcacheHibernateMbeanNames.mbeanSafe(cacheName));
77 } catch (MalformedObjectNameException e) {
78 throw new CacheException(e);
79 }
80 }
81
82 /***
83 * @return the object name for this MBean
84 */
85 public ObjectName getObjectName() {
86 return objectName;
87 }
88
89 /***
90 * {@inheritDoc}
91 */
92 public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
93 return storeBean.getAttribute(attribute);
94 }
95
96 /***
97 * {@inheritDoc}
98 */
99 public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException,
100 MBeanException, ReflectionException {
101 storeBean.setAttribute(attribute);
102 }
103
104 /***
105 * {@inheritDoc}
106 */
107 public AttributeList getAttributes(String[] attributes) {
108 return storeBean.getAttributes(attributes);
109 }
110
111 /***
112 * {@inheritDoc}
113 */
114 public AttributeList setAttributes(AttributeList attributes) {
115 return storeBean.setAttributes(attributes);
116 }
117
118 /***
119 * {@inheritDoc}
120 */
121 public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
122 return storeBean.invoke(actionName, params, signature);
123 }
124
125 /***
126 * {@inheritDoc}
127 */
128 public MBeanInfo getMBeanInfo() {
129 return storeBean.getMBeanInfo();
130 }
131 }