1 23 24 28 29 package com.sun.enterprise.admin.mbeans; 30 31 import java.io.PrintWriter ; 32 import java.io.StringReader ; 33 import java.io.StringWriter ; 34 import java.util.ArrayList ; 35 import java.util.Enumeration ; 36 import java.util.List ; 37 import java.util.Properties ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 41 import javax.management.Attribute ; 42 import javax.management.AttributeList ; 43 import javax.management.MBeanServerConnection ; 44 import javax.management.ObjectName ; 45 46 import com.sun.enterprise.admin.common.JMSDestinationInfo; 47 import com.sun.enterprise.admin.common.constant.AdminConstants; 48 import com.sun.enterprise.admin.common.constant.JMSAdminConstants; 49 import com.sun.enterprise.admin.common.exception.JMSAdminException; 50 import com.sun.enterprise.admin.target.Target; 51 import com.sun.enterprise.admin.target.TargetType; 52 import com.sun.enterprise.connectors.ConnectorRuntime; 53 import com.sun.enterprise.connectors.system.MQJMXConnectorInfo; 54 import com.sun.enterprise.connectors.system.ActiveJmsResourceAdapter; 55 import com.sun.enterprise.util.i18n.StringManager; 56 import com.sun.messaging.jms.management.server.DestinationType; 57 import com.sun.messaging.jms.management.server.MQObjectName; 58 59 67 public class JMSDestination { 68 69 private static final boolean USE_JMX = 73 !(Boolean.getBoolean("DONT_USE_MQ_JMX")); 74 75 private static final Logger sLogger = 76 Logger.getLogger(AdminConstants.kLoggerName); 77 private static final StringManager localStrings = 78 StringManager.getManager( JMSDestination.class); 79 80 81 public JMSDestination () { 83 } 84 85 public void createJMSDestination(String destName, String destType, 87 Properties destProps, String tgtName) throws JMSAdminException { 88 89 sLogger.log(Level.FINE, "createJMSDestination ..."); 90 MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName); 91 92 try { 94 MBeanServerConnection mbsc = mqInfo.getMQMBeanServerConnection(); 95 ObjectName on = new ObjectName ( 96 MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME); 97 String [] signature = null; 98 AttributeList destAttrs = null; 99 Object [] params = null; 100 101 if (destProps != null) { 102 destAttrs = convertProp2Attrs(destProps); 103 } 104 105 setAppserverDefaults(destAttrs, mqInfo); 106 107 if (destType.equalsIgnoreCase(JMSAdminConstants.JMS_DEST_TYPE_TOPIC)) { 108 destType = DestinationType.TOPIC; 109 } else if (destType.equalsIgnoreCase(JMSAdminConstants.JMS_DEST_TYPE_QUEUE)) { 110 destType = DestinationType.QUEUE; 111 } 112 113 signature = new String [] { 114 "java.lang.String", 115 "java.lang.String", 116 "javax.management.AttributeList"}; 117 params = new Object [] {destType, destName, destAttrs}; 118 119 mbsc.invoke(on, "create", params, signature); 120 } catch (Exception e) { 121 logAndHandleException(e, "admin.mbeans.rmb.error_creating_jms_dest"); 122 } finally { 123 try { 124 if(mqInfo != null) { 125 mqInfo.closeMQMBeanServerConnection(); 126 } 127 } catch (Exception e) { 128 handleException(e); 129 } 130 } 131 } 132 133 public void deleteJMSDestination(String destName, String destType, 135 String tgtName) 136 throws JMSAdminException { 137 138 sLogger.log(Level.FINE, "deleteJMSDestination ..."); 139 MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName); 140 141 143 try { 144 MBeanServerConnection mbsc = mqInfo.getMQMBeanServerConnection(); 145 ObjectName on = new ObjectName ( 146 MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME); 147 String [] signature = null; 148 Object [] params = null; 149 150 signature = new String [] { 151 "java.lang.String", 152 "java.lang.String"}; 153 154 if (destType.equalsIgnoreCase("topic")) { 155 destType = DestinationType.TOPIC; 156 } else if (destType.equalsIgnoreCase("queue")) { 157 destType = DestinationType.QUEUE; 158 } 159 params = new Object [] {destType, destName}; 160 mbsc.invoke(on, "destroy", params, signature); 161 } catch (Exception e) { 162 logAndHandleException(e, "admin.mbeans.rmb.error_deleting_jms_dest"); 164 } finally { 165 try { 166 if(mqInfo != null) { 167 mqInfo.closeMQMBeanServerConnection(); 168 } 169 } catch (Exception e) { 170 handleException(e); 171 } 172 } 173 } 174 175 public JMSDestinationInfo [] listJMSDestinations(String tgtName, String destType) 177 throws JMSAdminException { 178 179 sLogger.log(Level.FINE, "listJMSDestination ..."); 180 MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName); 181 182 try { 184 MBeanServerConnection mbsc = mqInfo.getMQMBeanServerConnection(); 185 ObjectName on = new ObjectName ( 186 MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME); 187 String [] signature = null; 188 Object [] params = null; 189 190 ObjectName [] dests = (ObjectName [])mbsc.invoke(on, "getDestinations", params, signature); 191 if ((dests != null) && (dests.length > 0)) { 192 List <JMSDestinationInfo> jmsdi = new ArrayList <JMSDestinationInfo>(); 193 for (int i=0; i<dests.length; i++) { 194 on = dests[i]; 195 196 String jdiType = DestinationType.toStringLabel(on.getKeyProperty("desttype")); 197 String jdiName = on.getKeyProperty("name"); 198 199 if ((jdiName != null) && (jdiName.length() > 1)) { 202 if (jdiName.indexOf('"') == 0) { 203 jdiName = jdiName.substring(1); 204 } 205 if (jdiName.lastIndexOf('"') == (jdiName.length() - 1)) { 206 jdiName = jdiName.substring(0, jdiName.lastIndexOf('"')); 207 } 208 } 209 210 JMSDestinationInfo jdi = new JMSDestinationInfo(jdiName, jdiType); 211 212 if(destType == null) { 213 jmsdi.add(jdi); 214 } else if (destType.equals(JMSAdminConstants.JMS_DEST_TYPE_TOPIC) 215 || destType.equals(JMSAdminConstants.JMS_DEST_TYPE_QUEUE)) { 216 if (jdiType.equalsIgnoreCase(destType)) { 218 jmsdi.add(jdi); 219 } 220 } 221 } 222 return (JMSDestinationInfo[]) jmsdi.toArray(new JMSDestinationInfo[]{}); 223 } 224 } catch (Exception e) { 225 logAndHandleException(e, "admin.mbeans.rmb.error_listing_jms_dest"); 227 } finally { 228 try { 229 if(mqInfo != null) { 230 mqInfo.closeMQMBeanServerConnection(); 231 } 232 } catch (Exception e) { 233 handleException(e); 234 } 235 } 236 237 return null; 238 } 239 240 241 public String JMSPing(String tgtName) 244 throws JMSAdminException { 245 246 sLogger.log(Level.FINE, "JMSPing ..."); 247 MQJMXConnectorInfo mqInfo = null; 248 try { 249 MQJMXConnectorInfo [] cInfo = 250 ConnectorRuntime.getRuntime().getMQJMXConnectorInfo(tgtName); 251 if ((cInfo == null) || (cInfo.length < 1)) { 252 throw new JMSAdminException( 253 localStrings.getString("admin.mbeans.rmb.error_obtaining_jms")); 254 } 255 int k = -1; 256 for (int i=0; i<cInfo.length; i++) { 257 if (tgtName.equals(cInfo[i].getASInstanceName())) { 258 k = i; 259 break; 260 } 261 } 262 if (k == -1) { 263 throw new JMSAdminException( 264 localStrings.getString("admin.mbeans.rmb.invalid_server_instance", tgtName)); 265 } 266 mqInfo = cInfo[k]; 267 268 MBeanServerConnection mbsc = cInfo[k].getMQMBeanServerConnection(); 269 mbsc.getMBeanCount(); 271 272 } catch (Exception e) { 273 logAndHandleException(e, "admin.mbeans.rmb.error_pinging_jms"); 275 } finally { 276 try { 277 if(mqInfo != null) { 278 mqInfo.closeMQMBeanServerConnection(); 279 } 280 } catch (Exception e) { 281 handleException(e); 282 } 283 } 284 return JMSAdminConstants.JMS_HOST_RUNNING; 285 } 286 287 288 public void purgeJMSDestination(String destName, String destType, String tgtName) 290 throws JMSAdminException { 291 292 sLogger.log(Level.FINE, "purgeJMSDestination ..."); 293 MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName); 294 295 try { 297 298 MBeanServerConnection mbsc = mqInfo.getMQMBeanServerConnection(); 299 if (destType.equalsIgnoreCase("topic")) { 300 destType = DestinationType.TOPIC; 301 } else if (destType.equalsIgnoreCase("queue")) { 302 destType = DestinationType.QUEUE; 303 } 304 ObjectName on = 305 MQObjectName.createDestinationConfig(destType, destName); 306 String [] signature = null; 307 Object [] params = null; 308 309 mbsc.invoke(on, "purge", params, signature); 310 } catch (Exception e) { 311 logAndHandleException(e, "admin.mbeans.rmb.error_purging_jms_dest"); 313 } finally { 314 try { 315 if(mqInfo != null) { 316 mqInfo.closeMQMBeanServerConnection(); 317 } 318 } catch (Exception e) { 319 handleException(e); 320 } 321 } 322 } 323 324 325 private MQJMXConnectorInfo getMQJMXConnectorInfo(String tgtName) 326 throws JMSAdminException { 327 sLogger.log(Level.FINE, "getMQJMXConnectorInfo for " + tgtName); 328 MQJMXConnectorInfo mcInfo = null; 329 330 try { 331 MQJMXConnectorInfo [] cInfo = 332 ConnectorRuntime.getRuntime().getMQJMXConnectorInfo(tgtName); 333 if ((cInfo == null) || (cInfo.length < 1)) { 334 throw new JMSAdminException( 335 localStrings.getString("admin.mbeans.rmb.error_obtaining_jms")); 336 } 337 mcInfo = cInfo[0]; 338 339 } catch (Exception e) { 340 handleException(e); 341 } 342 return mcInfo; 343 } 344 345 369 370 private void setAppserverDefaults(AttributeList destAttrs, 371 MQJMXConnectorInfo info) { 372 373 if (destAttrs == null) { 374 destAttrs = new AttributeList (); 375 } 376 377 if (info.getBrokerType().equalsIgnoreCase(ActiveJmsResourceAdapter.LOCAL)) { 378 String localDelivery = "LocalDeliveryPreferred"; 379 boolean notPresent = true; 380 for (Object obj : destAttrs) { 381 Attribute attrib = (Attribute ) obj; 382 if (attrib.getName().equals(localDelivery)) { 383 notPresent = false; 384 } 385 } 386 if (notPresent) { 387 Attribute attrib = new Attribute (localDelivery, 388 new Boolean ("true")); 389 destAttrs.add(attrib); 390 } 391 } 392 } 393 394 private AttributeList convertProp2Attrs(Properties destProps) { 396 397 AttributeList destAttrs = new AttributeList (); 398 399 String propName = null; 400 String propValue = null; 401 402 for (Enumeration e = destProps.propertyNames() ; e.hasMoreElements() ;) { 403 propName = (String ) e.nextElement(); 404 405 if (propName.equals("AutoCreateQueueMaxNumActiveConsumers")) { 406 destAttrs.add(new Attribute ("AutoCreateQueueMaxNumActiveConsumers", 407 new Integer (destProps.getProperty("AutoCreateQueueMaxNumActiveConsumers")))); 408 } else if (propName.equals("maxNumActiveConsumers")) { 409 destAttrs.add(new Attribute ("MaxNumActiveConsumers", 410 new Integer (destProps.getProperty("maxNumActiveConsumers")))); 411 } else if (propName.equals("MaxNumActiveConsumers")) { 412 destAttrs.add(new Attribute ("MaxNumActiveConsumers", 413 new Integer (destProps.getProperty("MaxNumActiveConsumers")))); 414 } else if (propName.equals("AutoCreateQueueMaxNumBackupConsumers")) { 415 destAttrs.add(new Attribute ("AutoCreateQueueMaxNumBackupConsumers", 416 new Integer (destProps.getProperty("AutoCreateQueueMaxNumBackupConsumers")))); 417 } else if (propName.equals("AutoCreateQueues")) { 418 boolean b = false; 419 propValue = destProps.getProperty("AutoCreateQueues"); 420 if (propValue.equalsIgnoreCase("true")) { 421 b = true; 422 } 423 destAttrs.add(new Attribute ("AutoCreateQueues", new Boolean (b))); 424 } else if (propName.equals("AutoCreateTopics")) { 425 boolean b = false; 426 propValue = destProps.getProperty("AutoCreateTopics"); 427 if (propValue.equalsIgnoreCase("true")) { 428 b = true; 429 } 430 destAttrs.add(new Attribute ("AutoCreateTopics", new Boolean (b))); 431 } else if (propName.equals("DMQTruncateBody")) { 432 boolean b = false; 433 propValue = destProps.getProperty("DMQTruncateBody"); 434 if (propValue.equalsIgnoreCase("true")) { 435 b = true; 436 } 437 destAttrs.add(new Attribute ("DMQTruncateBody", new Boolean (b))); 438 } else if (propName.equals("LogDeadMsgs")) { 439 boolean b = false; 440 propValue = destProps.getProperty("LogDeadMsgs"); 441 if (propValue.equalsIgnoreCase("true")) { 442 b = true; 443 } 444 destAttrs.add(new Attribute ("LogDeadMsgs", new Boolean (b))); 445 } else if (propName.equals("MaxBytesPerMsg")) { 446 destAttrs.add(new Attribute ("MaxBytesPerMsg", 447 new Long (destProps.getProperty("MaxBytesPerMsg")))); 448 } else if (propName.equals("MaxNumMsgs")) { 449 destAttrs.add(new Attribute ("MaxNumMsgs", 450 new Long (destProps.getProperty("MaxNumMsgs")))); 451 } else if (propName.equals("MaxTotalMsgBytes")) { 452 destAttrs.add(new Attribute ("MaxTotalMsgBytes", 453 new Long (destProps.getProperty("MaxTotalMsgBytes")))); 454 } else if (propName.equals("NumDestinations")) { 455 destAttrs.add(new Attribute ("NumDestinations", 456 new Integer (destProps.getProperty("NumDestinations")))); 457 } 458 } 459 return destAttrs; 460 } 461 462 472 private void logAndHandleException(Exception e, String errorMsg) 473 throws JMSAdminException { 474 StringWriter s = new StringWriter (); 476 e.getCause().printStackTrace(new PrintWriter (s)); 477 sLogger.log(Level.WARNING, s.toString()); 478 479 JMSAdminException je = new JMSAdminException(localStrings.getString(errorMsg)); 480 handleException(je); 481 } 482 483 484 private void handleException(Exception e) 485 throws JMSAdminException { 486 487 if (e instanceof JMSAdminException) { 488 throw ((JMSAdminException)e); 489 } 490 491 String msg = e.getMessage(); 492 493 JMSAdminException jae; 494 if (msg == null) { 495 jae = new JMSAdminException(); 496 } else { 497 jae = new JMSAdminException(msg); 498 } 499 500 505 507 throw jae; 508 } 509 510 516 public static boolean useJMX(Target target) { 517 525 return USE_JMX; 526 } 527 528 } 529 | Popular Tags |