1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| package org.jboss.cache.jmx; |
23 |
| |
24 |
| import org.jboss.cache.config.Configuration; |
25 |
| import org.jboss.cache.interceptors.Interceptor; |
26 |
| |
27 |
| import javax.management.JMException; |
28 |
| import javax.management.MBeanServer; |
29 |
| import javax.management.ObjectName; |
30 |
| import java.util.List; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public class JmxUtil |
40 |
| { |
41 |
| public static final String JBOSS_SERVER_DOMAIN = "jboss"; |
42 |
| public static final String JBOSS_CACHE_DOMAIN = "jboss.cache"; |
43 |
| public static final String SERVICE_KEY_NAME = "service"; |
44 |
| public static final String BASE_PREFIX = JBOSS_CACHE_DOMAIN + ":" + SERVICE_KEY_NAME + "=JBossCache"; |
45 |
| public static final String CLUSTER_KEY = "cluster"; |
46 |
| public static final String PREFIX = BASE_PREFIX + "," + CLUSTER_KEY + "="; |
47 |
| public static final String UNIQUE_ID_KEY = "uniqueId"; |
48 |
| public static final String NO_CLUSTER_PREFIX = BASE_PREFIX + "," + UNIQUE_ID_KEY + "="; |
49 |
| public static final String CACHE_TYPE_KEY = "cacheType"; |
50 |
| public static final String PLAIN_CACHE_TYPE = "Cache"; |
51 |
| public static final String MBEAN_CLASS_SUFFIX = "MBean"; |
52 |
| public static final String INTERCEPTOR_KEY = ",cache-interceptor="; |
53 |
| |
54 |
59
| public static void registerCacheMBean(MBeanServer server, CacheJmxWrapperMBean cache, String cacheObjectName)
|
55 |
| throws JMException |
56 |
| { |
57 |
59
| ObjectName on = new ObjectName(cacheObjectName);
|
58 |
59
| if (!server.isRegistered(on))
|
59 |
| { |
60 |
59
| server.registerMBean(cache, on);
|
61 |
| } |
62 |
| } |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
32
| public static void registerInterceptors(MBeanServer server, List<Interceptor> interceptors, String cacheObjectName)
|
72 |
| throws JMException |
73 |
| { |
74 |
32
| if (server == null || interceptors == null || cacheObjectName == null)
|
75 |
| { |
76 |
0
| return;
|
77 |
| } |
78 |
| |
79 |
32
| for (Interceptor interceptor : interceptors)
|
80 |
| { |
81 |
224
| if (!interceptor.getStatisticsEnabled())
|
82 |
12
| continue;
|
83 |
| |
84 |
212
| boolean mbeanExists = true;
|
85 |
212
| try
|
86 |
| { |
87 |
| |
88 |
212
| Class.forName(interceptor.getClass().getName() + MBEAN_CLASS_SUFFIX);
|
89 |
| } |
90 |
| catch (Throwable e) |
91 |
| { |
92 |
| |
93 |
122
| mbeanExists = false;
|
94 |
| } |
95 |
| |
96 |
| |
97 |
| |
98 |
212
| String className = interceptor.getClass().getName();
|
99 |
212
| String serviceName = cacheObjectName + INTERCEPTOR_KEY + className.substring(className.lastIndexOf('.') + 1);
|
100 |
| |
101 |
212
| ObjectName objName = new ObjectName(serviceName);
|
102 |
212
| if (!server.isRegistered(objName))
|
103 |
| { |
104 |
212
| if (mbeanExists)
|
105 |
| |
106 |
| { |
107 |
90
| server.registerMBean(interceptor, objName);
|
108 |
| } |
109 |
| |
110 |
| |
111 |
| |
112 |
| } |
113 |
| } |
114 |
| } |
115 |
| |
116 |
0
| public static String getDefaultCacheObjectName(org.jboss.cache.Cache cache)
|
117 |
| { |
118 |
| |
119 |
0
| return getDefaultCacheObjectName(cache.getConfiguration(), cache.getClass().getName());
|
120 |
| } |
121 |
| |
122 |
1
| public static String getDefaultCacheObjectName(Configuration config, String cacheImplClass)
|
123 |
| { |
124 |
| |
125 |
1
| String tmpName = null;
|
126 |
1
| if (config.getClusterName() == null)
|
127 |
| { |
128 |
0
| tmpName = NO_CLUSTER_PREFIX + getUniqueId(cacheImplClass);
|
129 |
| } |
130 |
| else |
131 |
| { |
132 |
1
| tmpName = PREFIX + config.getClusterName();
|
133 |
| } |
134 |
| |
135 |
1
| return tmpName;
|
136 |
| } |
137 |
| |
138 |
0
| public static String getUniqueId(String cacheImplClass)
|
139 |
| { |
140 |
0
| return cacheImplClass + System.currentTimeMillis();
|
141 |
| } |
142 |
| |
143 |
26
| public static void unregisterCacheMBean(MBeanServer server, String cacheObjectName)
|
144 |
| throws Exception |
145 |
| { |
146 |
26
| server.unregisterMBean(new ObjectName(cacheObjectName));
|
147 |
| } |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
32
| public static void unregisterInterceptors(MBeanServer server, List<Interceptor> interceptors, String cacheObjectName)
|
157 |
| throws Exception |
158 |
| { |
159 |
32
| if (server == null || interceptors == null || cacheObjectName == null)
|
160 |
| { |
161 |
0
| return;
|
162 |
| } |
163 |
| |
164 |
32
| for (Interceptor interceptor : interceptors)
|
165 |
| { |
166 |
| |
167 |
| |
168 |
224
| String className = interceptor.getClass().getName();
|
169 |
224
| String serviceName = cacheObjectName + INTERCEPTOR_KEY + className.substring(className.lastIndexOf('.') + 1);
|
170 |
| |
171 |
224
| ObjectName objName = new ObjectName(serviceName);
|
172 |
224
| if (server.isRegistered(objName))
|
173 |
| { |
174 |
90
| server.unregisterMBean(objName);
|
175 |
| } |
176 |
| } |
177 |
| } |
178 |
| } |