1 22 package org.jboss.mq.server.jmx; 23 24 import java.util.StringTokenizer ; 25 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 29 import javax.management.MBeanRegistration ; 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectName ; 32 import javax.management.MalformedObjectNameException ; 33 import javax.management.InvalidAttributeValueException ; 34 import javax.management.InstanceNotFoundException ; 35 import javax.management.MBeanException ; 36 import javax.management.ReflectionException ; 37 38 import org.jboss.system.ServiceMBeanSupport; 39 import org.jboss.naming.Util; 40 import org.jboss.logging.Logger; 41 42 import org.jboss.mq.MessageStatistics; 43 import org.jboss.mq.SpyDestination; 44 import org.jboss.mq.server.BasicQueueParameters; 45 import org.jboss.mq.server.JMSDestinationManager; 46 import org.jboss.mq.server.MessageCounter; 47 import org.jboss.mq.server.Receivers; 48 import org.w3c.dom.Element ; 49 50 56 abstract public class DestinationMBeanSupport extends ServiceMBeanSupport 57 implements DestinationMBean, MBeanRegistration 58 { 59 protected SpyDestination spyDest; 60 protected String destinationName; 61 protected String jndiName; 62 protected boolean jndiBound; 63 protected ObjectName jbossMQService; 64 protected Element securityConf; 66 67 protected BasicQueueParameters parameters = new BasicQueueParameters(); 68 69 ObjectName expiryDestination; 70 71 74 protected ObjectName securityManager; 75 76 81 public ObjectName getDestinationManager() 82 { 83 return jbossMQService; 84 } 85 86 91 public void setDestinationManager(ObjectName jbossMQService) 92 { 93 this.jbossMQService = jbossMQService; 94 } 95 96 protected SpyDestination getSpyDest() 97 { 98 return spyDest; 99 } 100 101 public void setSecurityConf(org.w3c.dom.Element securityConf) throws Exception 102 { 103 log.debug("Setting securityConf: " + securityConf); 104 this.securityConf = securityConf; 105 } 106 107 protected Element getSecurityConf() 108 { 109 return securityConf; 110 } 111 112 public void setSecurityManager(ObjectName securityManager) 113 { 114 this.securityManager = securityManager; 115 } 116 117 protected ObjectName getSecurityManager() 118 { 119 return securityManager; 120 } 121 122 public void createService() throws Exception 123 { 124 if (parameters.receiversImpl == null) 126 parameters.receiversImpl = (Class ) server.getAttribute(jbossMQService, "ReceiversImpl"); 127 if (parameters.recoveryRetries == 0) 128 parameters.recoveryRetries = ((Integer ) server.getAttribute(jbossMQService, "RecoveryRetries")).intValue(); 129 } 130 131 public void startService() throws Exception 132 { 133 134 setupSecurityManager(); 135 setupExpiryDestination(); 136 137 } 138 139 protected void setupSecurityManager() 140 throws InstanceNotFoundException , MBeanException , ReflectionException 141 { 142 if (securityManager != null) 143 { 144 getServer().invoke(securityManager, "addDestination", new Object []{spyDest.getName(), securityConf}, new String []{"java.lang.String", "org.w3c.dom.Element"}); 146 } 147 } 148 149 protected void setupExpiryDestination() 150 throws Exception 151 { 152 if (expiryDestination != null) 153 { 154 String jndi = (String )getServer().getAttribute(expiryDestination, "JNDIName"); 155 InitialContext ctx = new InitialContext (); 156 try 157 { 158 parameters.expiryDestination = (SpyDestination)ctx.lookup(jndi); 159 } 160 finally 161 { 162 ctx.close(); 163 } 164 } 165 } 166 167 public void stopService() throws Exception 168 { 169 if (jndiBound) 171 { 172 InitialContext ctx = getInitialContext(); 173 try 174 { 175 log.info("Unbinding JNDI name: " + jndiName); 176 Util.unbind(ctx, jndiName); 177 } 178 finally 179 { 180 ctx.close(); 181 } 182 jndiName = null; 183 jndiBound = false; 184 } 185 186 JMSDestinationManager jmsServer = (JMSDestinationManager) server.getAttribute(jbossMQService, "Interceptor"); 187 if (jmsServer != null) 188 jmsServer.closeDestination(spyDest); 189 190 teardownSecurityManager(); 192 } 193 194 protected void teardownSecurityManager() 195 throws InstanceNotFoundException , MBeanException , ReflectionException 196 { 197 if (securityManager != null) 198 { 199 getServer().invoke(securityManager, "removeDestination", new Object []{spyDest.getName()}, new String []{"java.lang.String"}); 201 } 202 } 203 204 protected ObjectName getObjectName(MBeanServer server, ObjectName name) 205 throws MalformedObjectNameException 206 { 207 destinationName = name.getKeyProperty("name"); 208 if (destinationName == null || destinationName.length() == 0) 209 { 210 throw new MalformedObjectNameException ("Property 'name' not provided"); 211 } 212 213 log = Logger.getLogger(getClass().getName() + "." + destinationName); 215 216 return name; 217 } 218 219 224 public synchronized void setJNDIName(String name) throws Exception 225 { 226 if (spyDest == null) 227 { jndiName = name; 229 return; 230 } 231 232 if (name == null) 233 { 234 throw new InvalidAttributeValueException ("Destination JNDI names can not be null"); 235 } 236 237 InitialContext ic = getInitialContext(); 238 try 239 { 240 if (jndiName != null && jndiBound) 241 { 242 Util.unbind(ic, jndiName); jndiName = null; 244 jndiBound = false; 245 } 246 247 Util.rebind(ic, name, spyDest); 248 jndiName = name; 249 jndiBound = true; 250 } 251 finally 252 { 253 ic.close(); 254 } 255 256 log.info("Bound to JNDI name: " + jndiName); 257 } 258 259 protected InitialContext getInitialContext() 260 throws NamingException 261 { 262 InitialContext ic = new InitialContext (); 263 return ic; 264 } 265 266 271 public String getJNDIName() 272 { 273 return jndiName; 274 } 275 276 281 abstract public MessageCounter[] getMessageCounter(); 282 283 289 abstract public MessageStatistics[] getMessageStatistics() throws Exception ; 290 291 296 public String listMessageCounter() 297 { 298 MessageCounter[] counter = getMessageCounter(); 299 if (counter == null) 300 return null; 301 302 String ret = "<table width=\"100%\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\">" + 303 "<tr>" + 304 "<th>Type</th>" + 305 "<th>Name</th>" + 306 "<th>Subscription</th>" + 307 "<th>Durable</th>" + 308 "<th>Count</th>" + 309 "<th>CountDelta</th>" + 310 "<th>Depth</th>" + 311 "<th>DepthDelta</th>" + 312 "<th>Last Add</th>" + 313 "</tr>"; 314 315 for( int i=0; i<counter.length; i++ ) 316 { 317 String data = counter[i].getCounterAsString(); 318 StringTokenizer token = new StringTokenizer ( data, ","); 319 String value; 320 321 ret += "<tr bgcolor=\"#" + ( (i%2)==0 ? "FFFFFF" : "F0F0F0") + "\">"; 322 323 ret += "<td>" + token.nextToken() + "</td>"; ret += "<td>" + token.nextToken() + "</td>"; ret += "<td>" + token.nextToken() + "</td>"; ret += "<td>" + token.nextToken() + "</td>"; 328 ret += "<td>" + token.nextToken() + "</td>"; 330 value = token.nextToken(); 332 if( value.equalsIgnoreCase("0") ) 333 value = "-"; 334 335 ret += "<td>" + value + "</td>"; 336 337 ret += "<td>" + token.nextToken() + "</td>"; 339 value = token.nextToken(); 341 if( value.equalsIgnoreCase("0") ) 342 value = "-"; 343 344 ret += "<td>" + value + "</td>"; 345 346 ret += "<td>" + token.nextToken() + "</td>"; 348 ret += "</tr>"; 349 } 350 351 ret += "</table>"; 352 353 return ret; 354 } 355 356 357 360 public void resetMessageCounter() 361 { 362 MessageCounter[] counter = getMessageCounter(); 363 if (counter == null) 364 return; 365 366 for( int i=0; i<counter.length; i++ ) 367 { 368 counter[i].resetCounter(); 369 } 370 } 371 372 377 public String listMessageCounterHistory() 378 { 379 MessageCounter[] counter = getMessageCounter(); 380 if (counter == null) 381 return null; 382 383 String ret = ""; 384 385 for( int i=0; i<counter.length; i++ ) 386 { 387 ret += ( counter[i].getDestinationTopic() ? "Topic '" : "Queue '" ); 389 ret += counter[i].getDestinationName() + "'"; 390 391 if( counter[i].getDestinationSubscription() != null ) 392 ret += "Subscription '" + counter[i].getDestinationSubscription() + "'"; 393 394 395 ret += "<table width=\"100%\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\">" + 397 "<tr>" + 398 "<th>Date</th>"; 399 400 for( int j = 0; j < 24; j++ ) 401 ret += "<th width=\"4%\">" + j + "</th>"; 402 403 ret += "<th>Total</th></tr>"; 404 405 StringTokenizer tokens = new StringTokenizer ( counter[i].getHistoryAsString(), ",\n"); 407 408 int days = Integer.parseInt( tokens.nextToken() ); 410 411 for( int j=0; j<days; j++ ) 412 { 413 ret += "<tr bgcolor=\"#" + ((j%2)==0 ? "FFFFFF" : "F0F0F0") + "\">"; 415 416 ret += "<td>" + tokens.nextToken() + "</td>"; 418 419 int total = 0; 421 422 for( int k=0; k<24; k++ ) 423 { 424 int value = Integer.parseInt( tokens.nextToken().trim() ); 425 426 if( value == -1 ) 427 { 428 ret += "<td></td>"; 429 } 430 else 431 { 432 ret += "<td>" + value + "</td>"; 433 434 total += value; 435 } 436 } 437 438 ret += "<td>" + total + "</td></tr>"; 439 } 440 441 ret += "</table><br><br>"; 442 } 443 444 return ret; 445 } 446 447 448 451 public void resetMessageCounterHistory() 452 { 453 MessageCounter[] counter = getMessageCounter(); 454 if (counter == null) 455 return; 456 457 for( int i=0; i<counter.length; i++ ) 458 { 459 counter[i].resetHistory(); 460 } 461 } 462 463 469 public void setMessageCounterHistoryDayLimit( int days ) 470 { 471 if( days < -1 ) 472 days = -1; 473 474 parameters.messageCounterHistoryDayLimit = days; 475 if (getState() != STARTED) 476 return; 477 478 MessageCounter[] counter = getMessageCounter(); 479 for (int i=0; i<counter.length; ++i) 480 counter[i].setHistoryLimit(days); 481 } 482 483 487 public int getMessageCounterHistoryDayLimit() 488 { 489 return parameters.messageCounterHistoryDayLimit; 490 } 491 492 public int getMaxDepth() 493 { 494 return parameters.maxDepth; 495 } 496 497 public void setMaxDepth(int depth) 498 { 499 parameters.maxDepth = depth; 500 } 501 502 public boolean getInMemory() 503 { 504 return parameters.inMemory; 505 } 506 507 public void setInMemory(boolean mode) 508 { 509 parameters.inMemory = mode; 510 } 511 512 516 public int getRedeliveryLimit() 517 { 518 return parameters.redeliveryLimit; 519 } 520 521 524 public void setRedeliveryLimit(int limit) 525 { 526 parameters.redeliveryLimit = limit; 527 } 528 529 533 public long getRedeliveryDelay() 534 { 535 return parameters.redeliveryDelay; 536 } 537 538 539 542 public void setRedeliveryDelay(long rDelay) 543 { 544 parameters.redeliveryDelay = rDelay; 545 } 546 547 public Class getReceiversImpl() 548 { 549 return parameters.receiversImpl; 550 } 551 552 public void setReceiversImpl(Class clazz) 553 { 554 if (clazz != null && Receivers.class.isAssignableFrom(clazz) == false) 555 throw new IllegalArgumentException ("Class " + clazz.getName() + " is not a Receivers implementation"); 556 parameters.receiversImpl = clazz; 557 } 558 559 public int getRecoveryRetries() 560 { 561 return parameters.recoveryRetries; 562 } 563 564 public void setRecoveryRetries(int retries) 565 { 566 parameters.recoveryRetries = retries; 567 } 568 569 public ObjectName getExpiryDestination() 570 { 571 return expiryDestination; 572 } 573 574 public void setExpiryDestination(ObjectName expiryDestination) 575 { 576 this.expiryDestination = expiryDestination; 577 } 578 579 } 580 | Popular Tags |