1 22 package org.jboss.cache.jmx; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.jboss.cache.Cache; 27 import org.jboss.cache.CacheImpl; 28 import org.jboss.cache.config.Configuration; 29 import org.jboss.cache.config.ConfigurationException; 30 import org.jboss.cache.factories.DefaultCacheFactory; 31 import org.jboss.cache.interceptors.CacheMgmtInterceptor; 32 import org.jboss.cache.interceptors.Interceptor; 33 34 import javax.management.ListenerNotFoundException ; 35 import javax.management.MBeanNotificationInfo ; 36 import javax.management.MBeanRegistration ; 37 import javax.management.MBeanServer ; 38 import javax.management.MalformedObjectNameException ; 39 import javax.management.NotificationFilter ; 40 import javax.management.NotificationListener ; 41 import javax.management.ObjectName ; 42 43 public class CacheJmxWrapper implements CacheJmxWrapperMBean, MBeanRegistration 44 { 45 private Log log = LogFactory.getLog(getClass().getName()); 46 47 private MBeanServer server; 48 private String cacheObjectName; 49 private boolean registeredInterceptorsInCreate; 50 private boolean interceptorsRegistered; 51 private CacheImpl cache; 52 private boolean selfConstructed = false; 53 private Configuration config; 54 private boolean created; 55 private boolean registerInterceptors = true; 56 57 public CacheJmxWrapper() 58 { 59 } 60 61 public CacheJmxWrapper(Cache cache) 62 { 63 setCache(cache); 64 } 65 66 public org.jboss.cache.Cache getCache() 67 { 68 return cache; 69 } 70 71 public Configuration getConfiguration() 72 { 73 return cache == null ? null : cache.getConfiguration(); 74 } 75 76 public String getConfigurationAsString() 77 { 78 return cache == null ? "Cache is null" : cache.getConfiguration().toString(); 79 } 80 81 public String getConfigurationAsHtmlString() 82 { 83 return cache == null ? "Cache is null" : formatHtml(cache.getConfiguration().toString()); 84 } 85 86 public String getCacheDetails() 87 { 88 return cache == null ? "Cache is null" : cache.printDetails(); 89 } 90 91 public String getCacheDetailsAsHtml() 92 { 93 return cache == null ? "Cache is null" : formatHtml(cache.printDetails()); 94 } 95 96 public int getNumberOfNodes() 97 { 98 return cache == null ? -1 : cache.getNumberOfNodes(); 99 } 100 101 public int getNumberOfAttributes() 102 { 103 return cache == null ? -1 : cache.getNumberOfAttributes(); 104 } 105 106 public String getLockInfo() 107 { 108 return cache == null ? "Cache is null" : cache.printLockInfo(); 109 } 110 111 public String getLockInfoAsHtml() 112 { 113 return cache == null ? "Cache is null" : formatHtml(cache.printLockInfo()); 114 } 115 116 public void addNotificationListener(NotificationListener notificationListener, NotificationFilter notificationFilter, Object object) throws IllegalArgumentException 117 { 118 getCacheMgmtInterceptor().addNotificationListener(notificationListener, notificationFilter, object); 119 } 120 121 public void removeNotificationListener(NotificationListener notificationListener) throws ListenerNotFoundException 122 { 123 getCacheMgmtInterceptor().removeNotificationListener(notificationListener); 124 } 125 126 public MBeanNotificationInfo [] getNotificationInfo() 127 { 128 return getCacheMgmtInterceptor().getNotificationInfo(); 129 } 130 131 private CacheMgmtInterceptor getCacheMgmtInterceptor() 132 { 133 for (Interceptor i : cache.getInterceptors()) 134 { 135 if (i instanceof CacheMgmtInterceptor) return (CacheMgmtInterceptor) i; 136 } 137 throw new RuntimeException ("Cache management interceptor not found"); 138 } 139 140 146 protected String formatHtml(String s) 147 { 148 s = s.replaceAll("\r\n", "<br />"); 149 s = s.replaceAll("\r", "<br />"); 150 s = s.replaceAll("\n", "<br />"); 151 s = s.replaceAll("\t", " "); 152 s = s.replaceAll(" ", " "); 153 return s; 154 } 155 156 public void create() throws Exception 157 { 158 if (cache == null) 159 { 160 if (config == null) 161 { 162 throw new ConfigurationException("Must call setConfiguration() or setCache() before call to create()"); 163 } 164 165 constructCache(); 166 } 167 168 if (selfConstructed) 169 { 170 cache.create(); 171 } 172 173 registeredInterceptorsInCreate = registerInterceptors(); 174 175 created = true; 176 } 177 178 public void start() throws Exception 179 { 180 if (selfConstructed) 181 { 182 cache.start(); 183 } 184 } 185 186 public void stop() 187 { 188 if (selfConstructed) 189 { 190 cache.stop(); 191 } 192 } 193 194 public void destroy() 195 { 196 if (selfConstructed) 197 { 198 cache.destroy(); 199 200 if (registeredInterceptorsInCreate) 201 { 202 unregisterInterceptors(); 203 } 204 } 205 206 created = false; 207 } 208 209 public boolean getRegisterInterceptors() 210 { 211 return registerInterceptors; 212 } 213 214 public void setRegisterInterceptors(boolean register) 215 { 216 this.registerInterceptors = register; 217 } 218 219 protected void constructCache() throws Exception 220 { 221 log.debug("Constructing Cache"); 222 setCache(DefaultCacheFactory.getInstance().createCache(config, false)); 223 selfConstructed = true; 224 } 225 226 protected boolean registerInterceptors() throws Exception 227 { 228 if (registerInterceptors && !interceptorsRegistered && server != null) 229 { 230 log.debug("Registering interceptors"); 231 JmxUtil.registerInterceptors(server, cache.getInterceptorChain(), cacheObjectName); 232 interceptorsRegistered = true; 233 return true; 234 } 235 return false; 236 } 237 238 protected void unregisterInterceptors() 239 { 240 if (registerInterceptors && interceptorsRegistered && server != null) 241 { 242 try 243 { 244 log.debug("Unreqistering interceptors"); 245 JmxUtil.unregisterInterceptors(server, cache.getInterceptorChain(), getCacheObjectName()); 246 interceptorsRegistered = false; 247 } 248 catch (Exception e) 249 { 250 log.error("Exception unregistering interceptors from JMX", e); 251 } 252 } 253 } 254 255 256 258 261 public ObjectName preRegister(MBeanServer server, ObjectName objName) 262 throws Exception 263 { 264 this.server = server; 265 266 if (cacheObjectName == null) 267 { 268 if (objName == null) 269 { 270 getCacheObjectName(); 272 } 273 else 274 { 275 cacheObjectName = objName.getCanonicalName(); 276 } 277 } 278 279 return new ObjectName (cacheObjectName); 280 } 281 282 286 public void postRegister(Boolean registrationDone) 287 { 288 if (Boolean.TRUE.equals(registrationDone)) 289 { 290 log.debug("Registered in JMX under " + cacheObjectName); 291 292 if (cache != null) 293 { 294 try 295 { 296 registerInterceptors(); 297 } 298 catch (Exception e) 299 { 300 log.error("Caught exception registering cache interceptors with JMX", e); 301 } 302 } 303 } 304 } 305 306 309 public void preDeregister() throws Exception 310 { 311 } 312 313 317 public void postDeregister() 318 { 319 unregisterInterceptors(); 320 321 server = null; 322 } 323 324 326 332 public void setConfiguration(Configuration config) 333 { 334 this.config = config; 335 } 336 337 342 public void setCache(Cache cache) 343 { 344 if (created) 345 { 346 throw new IllegalStateException ("Cannot set underlying cache after call to create()"); 347 } 348 349 this.cache = (CacheImpl) cache; 351 this.config = (cache == null ? null : cache.getConfiguration()); 352 } 353 354 public String getCacheObjectName() 355 { 356 if (cacheObjectName == null) 357 { 358 cacheObjectName = JmxUtil.getCacheObjectName(config, CacheImpl.class.getName()); 359 } 360 return cacheObjectName; 361 } 362 363 public void setCacheObjectName(String name) throws MalformedObjectNameException 364 { 365 if (name != null) 366 { 367 new ObjectName (name); 369 } 370 this.cacheObjectName = name; 371 } 372 373 375 public MBeanServer getMBeanServer() 376 { 377 return server; 378 } 379 380 382 } 383 | Popular Tags |