1 |
| package org.jboss.cache.jmx; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.Cache; |
5 |
| import org.jboss.cache.DefaultCacheFactory; |
6 |
| import org.jboss.cache.config.Configuration; |
7 |
| |
8 |
| import javax.management.MBeanServer; |
9 |
| import javax.management.MBeanServerFactory; |
10 |
| import javax.management.MBeanServerInvocationHandler; |
11 |
| import javax.management.MalformedObjectNameException; |
12 |
| import javax.management.ObjectName; |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| public class CacheJmxWrapperTestBase extends TestCase |
21 |
| { |
22 |
| public static final String CLUSTER_NAME = "CacheMBeanTest"; |
23 |
| |
24 |
| protected Cache cache; |
25 |
| protected CacheJmxWrapperMBean jmxWrapper; |
26 |
| protected MBeanServer mBeanServer; |
27 |
| protected ObjectName mBeanName; |
28 |
| protected String mBeanNameStr; |
29 |
| |
30 |
33
| protected void setUp() throws Exception
|
31 |
| { |
32 |
33
| mBeanServer = MBeanServerFactory.createMBeanServer("CacheMBeanTest");
|
33 |
| |
34 |
33
| mBeanNameStr = JmxUtil.PREFIX + CLUSTER_NAME;
|
35 |
33
| mBeanName = new ObjectName(mBeanNameStr);
|
36 |
| } |
37 |
| |
38 |
33
| protected void tearDown() throws Exception
|
39 |
| { |
40 |
33
| try
|
41 |
| { |
42 |
33
| cleanup();
|
43 |
| } |
44 |
| finally |
45 |
| { |
46 |
33
| if (mBeanServer != null)
|
47 |
| { |
48 |
33
| MBeanServerFactory.releaseMBeanServer(mBeanServer);
|
49 |
33
| mBeanServer = null;
|
50 |
| } |
51 |
| } |
52 |
| } |
53 |
| |
54 |
10
| protected CacheJmxWrapperMBean registerWrapper() throws Exception
|
55 |
| { |
56 |
10
| if (cache == null)
|
57 |
10
| cache = createCache(createConfiguration());
|
58 |
10
| return registerWrapper(cache);
|
59 |
| } |
60 |
| |
61 |
13
| protected CacheJmxWrapperMBean registerWrapper(Cache toWrap) throws Exception
|
62 |
| { |
63 |
13
| CacheJmxWrapper wrapper = new CacheJmxWrapper(toWrap);
|
64 |
13
| return registerWrapper(wrapper);
|
65 |
| } |
66 |
| |
67 |
7
| protected CacheJmxWrapperMBean registerWrapper(Configuration config) throws Exception
|
68 |
| { |
69 |
7
| CacheJmxWrapper wrapper = new CacheJmxWrapper();
|
70 |
7
| wrapper.setConfiguration(config);
|
71 |
7
| return registerWrapper(wrapper);
|
72 |
| } |
73 |
| |
74 |
33
| protected CacheJmxWrapperMBean registerWrapper(CacheJmxWrapperMBean wrapper) throws Exception
|
75 |
| { |
76 |
33
| JmxUtil.registerCacheMBean(mBeanServer, wrapper, mBeanNameStr);
|
77 |
33
| jmxWrapper = (CacheJmxWrapperMBean) MBeanServerInvocationHandler.newProxyInstance(mBeanServer, mBeanName, CacheJmxWrapperMBean.class, false);
|
78 |
33
| return jmxWrapper;
|
79 |
| } |
80 |
| |
81 |
12
| protected void unregisterWrapper() throws Exception
|
82 |
| { |
83 |
12
| mBeanServer.unregisterMBean(mBeanName);
|
84 |
| } |
85 |
| |
86 |
8
| protected CacheJmxWrapper createWrapper(Configuration config)
|
87 |
| { |
88 |
8
| CacheJmxWrapper wrapper = new CacheJmxWrapper();
|
89 |
8
| wrapper.setConfiguration(config);
|
90 |
8
| return wrapper;
|
91 |
| } |
92 |
| |
93 |
17
| protected Cache createCache(Configuration config)
|
94 |
| { |
95 |
17
| cache = DefaultCacheFactory.getInstance().createCache(config, false);
|
96 |
17
| return cache;
|
97 |
| } |
98 |
| |
99 |
31
| protected Configuration createConfiguration()
|
100 |
| { |
101 |
31
| Configuration c = new Configuration();
|
102 |
31
| c.setClusterName(CLUSTER_NAME);
|
103 |
31
| c.setExposeManagementStatistics(true);
|
104 |
31
| c.setCacheMode(Configuration.CacheMode.LOCAL);
|
105 |
31
| return c;
|
106 |
| } |
107 |
| |
108 |
33
| private void cleanup() throws Exception
|
109 |
| { |
110 |
33
| if (cache != null)
|
111 |
| { |
112 |
17
| try
|
113 |
| { |
114 |
17
| cache.stop();
|
115 |
| } |
116 |
| catch (Exception ignored) |
117 |
| { |
118 |
| } |
119 |
| |
120 |
17
| cache = null;
|
121 |
| } |
122 |
33
| if (jmxWrapper != null)
|
123 |
| { |
124 |
33
| try
|
125 |
| { |
126 |
33
| jmxWrapper.stop();
|
127 |
20
| jmxWrapper.destroy();
|
128 |
| } |
129 |
| catch (Exception ignored) |
130 |
| { |
131 |
| } |
132 |
| |
133 |
33
| jmxWrapper = null;
|
134 |
| } |
135 |
| |
136 |
33
| if (mBeanServer != null && mBeanName != null && mBeanServer.isRegistered(mBeanName))
|
137 |
20
| mBeanServer.unregisterMBean(mBeanName);
|
138 |
| } |
139 |
| |
140 |
34
| protected void interceptorRegistrationTest(boolean expectMbeans) throws MalformedObjectNameException, NullPointerException
|
141 |
| { |
142 |
34
| interceptorRegistrationTest(mBeanNameStr, expectMbeans);
|
143 |
| } |
144 |
| |
145 |
35
| protected void interceptorRegistrationTest(String baseName, boolean expectMbeans) throws MalformedObjectNameException, NullPointerException
|
146 |
| { |
147 |
| |
148 |
35
| ObjectName[] interceptorMBeanNames = {
|
149 |
| new ObjectName(baseName + JmxUtil.INTERCEPTOR_KEY + "TxInterceptor"), |
150 |
| new ObjectName(baseName + JmxUtil.INTERCEPTOR_KEY + "CacheMgmtInterceptor"), |
151 |
| new ObjectName(baseName + JmxUtil.INTERCEPTOR_KEY + "InvocationContextInterceptor") |
152 |
| }; |
153 |
| |
154 |
35
| for (ObjectName n : interceptorMBeanNames)
|
155 |
| { |
156 |
105
| if (expectMbeans)
|
157 |
27
| assertTrue(n + " should be registered", mBeanServer.isRegistered(n));
|
158 |
| else |
159 |
78
| assertFalse(n + " should not be registered", mBeanServer.isRegistered(n));
|
160 |
| } |
161 |
| } |
162 |
| } |