FileDocCategorySizeDatePackage
JMSDestination.javaAPI DocGlassfish v2 API22811Fri May 04 22:24:10 BST 2007com.sun.enterprise.admin.mbeans

JMSDestination

public class JMSDestination extends Object
A helper class used for performing MQ related administration operations using MQ's JMX API.
author
Sreenivas Munnangi
author
Sivakumar Thyagarajan
since
SJSAS9.0

Fields Summary
private static final boolean
USE_JMX
private static final Logger
sLogger
private static final com.sun.enterprise.util.i18n.StringManager
localStrings
Constructors Summary
public JMSDestination()



	// default constructor
	   
	
Methods Summary
public java.lang.StringJMSPing(java.lang.String tgtName)


		sLogger.log(Level.FINE, "JMSPing ...");
                MQJMXConnectorInfo mqInfo = null;
		try {
			MQJMXConnectorInfo [] cInfo =
				ConnectorRuntime.getRuntime().getMQJMXConnectorInfo(tgtName);
			if ((cInfo == null) || (cInfo.length < 1)) {
				throw new JMSAdminException(
                        localStrings.getString("admin.mbeans.rmb.error_obtaining_jms"));
			}
			int k = -1;
			for (int i=0; i<cInfo.length; i++) {
				if (tgtName.equals(cInfo[i].getASInstanceName())) {
					k = i;
					break;
				}
			}
			if (k == -1) {
				throw new JMSAdminException(
				localStrings.getString("admin.mbeans.rmb.invalid_server_instance", tgtName));
			}
                        mqInfo = cInfo[k];

			MBeanServerConnection mbsc = cInfo[k].getMQMBeanServerConnection();
                        //perform some work on the connection to check for connection health.
                        mbsc.getMBeanCount(); 

		} catch (Exception e) {
                    //log JMX Exception trace as WARNING
                    logAndHandleException(e, "admin.mbeans.rmb.error_pinging_jms");
                } finally {
                    try {
                        if(mqInfo != null) {
                            mqInfo.closeMQMBeanServerConnection();
                        }
                    } catch (Exception e) {
                      handleException(e);
                    }
                }
		return JMSAdminConstants.JMS_HOST_RUNNING;
	
private javax.management.AttributeListconvertProp2Attrs(java.util.Properties destProps)


		AttributeList destAttrs = new AttributeList();

		String propName = null;
		String propValue = null;

		for (Enumeration e = destProps.propertyNames(); e.hasMoreElements();) {
                     propName = (String) e.nextElement();
                     if (propName.equals("AutoCreateQueueMaxNumActiveConsumers")) {
                         destAttrs.add(new Attribute("AutoCreateQueueMaxNumActiveConsumers",
                                                     Integer.valueOf(destProps.getProperty("AutoCreateQueueMaxNumActiveConsumers"))));
                     } else if (propName.equals("maxNumActiveConsumers")) {
                         destAttrs.add(new Attribute("MaxNumActiveConsumers",
                                                     Integer.valueOf(destProps.getProperty("maxNumActiveConsumers"))));
                     } else if (propName.equals("MaxNumActiveConsumers")) {
                         destAttrs.add(new Attribute("MaxNumActiveConsumers",
                                                     Integer.valueOf(destProps.getProperty("MaxNumActiveConsumers"))));
                     } else if (propName.equals("AutoCreateQueueMaxNumBackupConsumers")) {
                         destAttrs.add(new Attribute("AutoCreateQueueMaxNumBackupConsumers",
                                                     Integer.valueOf(destProps.getProperty("AutoCreateQueueMaxNumBackupConsumers"))));
                     } else if (propName.equals("AutoCreateQueues")) {
                         boolean b = false;

                         propValue = destProps.getProperty("AutoCreateQueues");
                         if (propValue.equalsIgnoreCase("true")) {
                             b = true;
                         }
                         destAttrs.add(new Attribute("AutoCreateQueues",
                                                     Boolean.valueOf(b)));
                     } else if (propName.equals("AutoCreateTopics")) {
                         boolean b = false;

                         propValue = destProps.getProperty("AutoCreateTopics");
                         if (propValue.equalsIgnoreCase("true")) {
                             b = true;
                         }
                         destAttrs.add(new Attribute("AutoCreateTopics",
                                                     Boolean.valueOf(b)));
                     } else if (propName.equals("DMQTruncateBody")) {
                         boolean b = false;

                         propValue = destProps.getProperty("DMQTruncateBody");
                         if (propValue.equalsIgnoreCase("true")) {
                             b = true;
                         }
                         destAttrs.add(new Attribute("DMQTruncateBody",
                                                     Boolean.valueOf(b)));
                     } else if (propName.equals("LogDeadMsgs")) {
                         boolean b = false;

                         propValue = destProps.getProperty("LogDeadMsgs");
                         if (propValue.equalsIgnoreCase("true")) {
                             b = true;
                         }
                         destAttrs.add(new Attribute("LogDeadMsgs",
                                                     Boolean.valueOf(b)));
                     } else if (propName.equals("MaxBytesPerMsg")) {
                         destAttrs.add(new Attribute("MaxBytesPerMsg",
                                                     Long.valueOf(destProps.getProperty("MaxBytesPerMsg"))));
                     } else if (propName.equals("MaxNumMsgs")) {
                         destAttrs.add(new Attribute("MaxNumMsgs",
                                                     Long.valueOf(destProps.getProperty("MaxNumMsgs"))));
                     } else if (propName.equals("MaxTotalMsgBytes")) {
                         destAttrs.add(new Attribute("MaxTotalMsgBytes",
                                                     Long.valueOf(destProps.getProperty("MaxTotalMsgBytes"))));
                     } else if (propName.equals("NumDestinations")) {
                         destAttrs.add(new Attribute("NumDestinations",
                                                     Integer.valueOf(destProps.getProperty("NumDestinations"))));
                     }
                 }
		return destAttrs;
	
public voidcreateJMSDestination(java.lang.String destName, java.lang.String destType, java.util.Properties destProps, java.lang.String tgtName)


		sLogger.log(Level.FINE, "createJMSDestination ...");
                MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName);

		//MBeanServerConnection  mbsc = getMBeanServerConnection(tgtName);
		try {
            MBeanServerConnection mbsc = mqInfo.getMQMBeanServerConnection();
			ObjectName on = new ObjectName(
				MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME);
			String [] signature = null;
			AttributeList destAttrs = null;
			Object [] params = null;

			if (destProps != null) {
				destAttrs = convertProp2Attrs(destProps);
			}
                        
            // setAppserverDefaults(destAttrs, mqInfo);

			if (destType.equalsIgnoreCase(JMSAdminConstants.JMS_DEST_TYPE_TOPIC)) {
				destType = DestinationType.TOPIC;
			} else if (destType.equalsIgnoreCase(JMSAdminConstants.JMS_DEST_TYPE_QUEUE)) {
				destType = DestinationType.QUEUE;
			} 
		  if ((destAttrs == null) || (destAttrs.size() == 0)){
                      signature = new String [] {
                     "java.lang.String",
                     "java.lang.String"};
                       params = new Object [] {destType, destName};
                  } else {
                     signature = new String [] {
                     "java.lang.String",
                     "java.lang.String",
                     "javax.management.AttributeList"};
                     params = new Object [] {destType, destName, destAttrs};
                  }

		mbsc.invoke(on, "create", params, signature);
		} catch (Exception e) {
                    logAndHandleException(e, "admin.mbeans.rmb.error_creating_jms_dest");
		} finally {
                    try {
                        if(mqInfo != null) {
                            mqInfo.closeMQMBeanServerConnection();
                        }
                    } catch (Exception e) {
                      handleException(e);
                    }
                }
	
public voiddeleteJMSDestination(java.lang.String destName, java.lang.String destType, java.lang.String tgtName)


		sLogger.log(Level.FINE, "deleteJMSDestination ...");
                MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName);

		//MBeanServerConnection  mbsc = getMBeanServerConnection(tgtName);

		try {
			MBeanServerConnection mbsc = mqInfo.getMQMBeanServerConnection();
			ObjectName on = new ObjectName(
				MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME);
			String [] signature = null;
			Object [] params = null;

			signature = new String [] {
				"java.lang.String",
				"java.lang.String"};

			if (destType.equalsIgnoreCase("topic")) {
				destType = DestinationType.TOPIC;
			} else if (destType.equalsIgnoreCase("queue")) {
				destType = DestinationType.QUEUE;
			} 
			params = new Object [] {destType, destName};
			mbsc.invoke(on, "destroy", params, signature);
		} catch (Exception e) {
                   //log JMX Exception trace as WARNING
                   logAndHandleException(e, "admin.mbeans.rmb.error_deleting_jms_dest");
                } finally {
                    try {
                        if(mqInfo != null) {
                            mqInfo.closeMQMBeanServerConnection();
                        }
                    } catch (Exception e) {
                      handleException(e);
                    }
                }
	
private com.sun.enterprise.connectors.system.MQJMXConnectorInfogetMQJMXConnectorInfo(java.lang.String tgtName)

                sLogger.log(Level.FINE, "getMQJMXConnectorInfo for " + tgtName);
                MQJMXConnectorInfo mcInfo = null;
                                                                                                                                              
                try {
                        MQJMXConnectorInfo [] cInfo =
                                ConnectorRuntime.getRuntime().getMQJMXConnectorInfo(tgtName);
                        if ((cInfo == null) || (cInfo.length < 1)) {
                                throw new JMSAdminException(
                        localStrings.getString("admin.mbeans.rmb.error_obtaining_jms"));
                        }
                        mcInfo = cInfo[0];
                                                                                                                                              
                } catch (Exception e) {
                    handleException(e);
                }
                return mcInfo;
        
private voidhandleException(java.lang.Exception e)


        if (e instanceof JMSAdminException)  {
            throw ((JMSAdminException)e);
        }

        String msg = e.getMessage();

        JMSAdminException jae;
        if (msg == null)  {
            jae = new JMSAdminException();
        } else  {
            jae = new JMSAdminException(msg);
        }

        /*
         * Don't do this for now because the CLI does not include jms.jar
         * (at least not yet) in the classpath. Sending over a JMSException
         * will cause a class not found exception to be thrown.
         */
        //jae.setLinkedException(e);

        throw jae;
    
public com.sun.enterprise.admin.common.JMSDestinationInfo[]listJMSDestinations(java.lang.String tgtName, java.lang.String destType)


		sLogger.log(Level.FINE, "listJMSDestination ...");
                MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName);

		//MBeanServerConnection  mbsc = getMBeanServerConnection(tgtName);
		try {
                        MBeanServerConnection mbsc = mqInfo.getMQMBeanServerConnection();
			ObjectName on = new ObjectName(
				MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME);
			String [] signature = null;
			Object [] params = null;

			ObjectName [] dests = (ObjectName [])mbsc.invoke(on, "getDestinations", params, signature);
			if ((dests != null) && (dests.length > 0)) {
                List<JMSDestinationInfo> jmsdi = new ArrayList<JMSDestinationInfo>();
				for (int i=0; i<dests.length; i++) {
					on = dests[i];

                    String jdiType = DestinationType.toStringLabel(on.getKeyProperty("desttype"));
					String jdiName = on.getKeyProperty("name");

					// check if the destination name has double quotes at the beginning 
					// and end, if yes strip them
					if ((jdiName != null) && (jdiName.length() > 1)) {
						if (jdiName.indexOf('"") == 0) {
                			jdiName = jdiName.substring(1);
            			}
            			if (jdiName.lastIndexOf('"") == (jdiName.length() - 1)) {
                			jdiName = jdiName.substring(0, jdiName.lastIndexOf('""));
            			}
					}

                    JMSDestinationInfo jdi = new JMSDestinationInfo(jdiName, jdiType);

                    if(destType == null) {
                        jmsdi.add(jdi);
                    } else if (destType.equals(JMSAdminConstants.JMS_DEST_TYPE_TOPIC) 
                            || destType.equals(JMSAdminConstants.JMS_DEST_TYPE_QUEUE)) {
                        //Physical Destination Type specific listing
                        if (jdiType.equalsIgnoreCase(destType)) {
                            jmsdi.add(jdi);
                        }
                    }
				}
				return (JMSDestinationInfo[]) jmsdi.toArray(new JMSDestinationInfo[]{});
			}
		} catch (Exception e) {
                    //log JMX Exception trace as WARNING
                    logAndHandleException(e, "admin.mbeans.rmb.error_listing_jms_dest");
                } finally {
                    try {
                        if(mqInfo != null) {
                            mqInfo.closeMQMBeanServerConnection();
                        }
                    } catch (Exception e) {
                      handleException(e);
                    }
                }

		return null;
	
private voidlogAndHandleException(java.lang.Exception e, java.lang.String errorMsg)
Logs an exception via the logger and throws a JMSAdminException. This method exists as exceptions that occur while invoke MQ JMX operations currently have "nested" exception messages and it is very difficult for a user to understand the real cause from the exception message. We now throw a generic message and log the actual exception in the server log so that users could refer to the server log for more information.

        //log JMX Exception trace as WARNING
        StringWriter s = new StringWriter();
        e.getCause().printStackTrace(new PrintWriter(s));
        sLogger.log(Level.WARNING, s.toString());
        JMSAdminException je = new JMSAdminException(localStrings.getString(errorMsg));
	/* Cause will be InvocationTargetException, cause of that
 	 * wil be  MBeanException and cause of that will be the
	 * real exception we need
	 */
	if ((e.getCause() != null) && 
	    (e.getCause().getCause() != null)) {
  		je.initCause(e.getCause().getCause().getCause());
	}
        handleException(je);
    
public voidpurgeJMSDestination(java.lang.String destName, java.lang.String destType, java.lang.String tgtName)


		sLogger.log(Level.FINE, "purgeJMSDestination ...");
                MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName);

		//MBeanServerConnection  mbsc = getMBeanServerConnection(tgtName);
		try {

                        MBeanServerConnection mbsc = mqInfo.getMQMBeanServerConnection();
			if (destType.equalsIgnoreCase("topic")) {
				destType = DestinationType.TOPIC;
			} else if (destType.equalsIgnoreCase("queue")) {
				destType = DestinationType.QUEUE;
			} 
			ObjectName on = 
				MQObjectName.createDestinationConfig(destType, destName);
			String [] signature = null;
			Object [] params = null;

			mbsc.invoke(on, "purge", params, signature);
		} catch (Exception e) {
                    //log JMX Exception trace as WARNING
                    logAndHandleException(e, "admin.mbeans.rmb.error_purging_jms_dest");
                } finally {
                    try {
                        if(mqInfo != null) {
                            mqInfo.closeMQMBeanServerConnection();
                        }
                    } catch (Exception e) {
                      handleException(e);
                    }
                }
	
private voidsetAppserverDefaults(javax.management.AttributeList destAttrs, com.sun.enterprise.connectors.system.MQJMXConnectorInfo info)


	     if (destAttrs == null) {
		destAttrs = new AttributeList();
	     }

             if (info.getBrokerType().equalsIgnoreCase(ActiveJmsResourceAdapter.LOCAL)) {
                 String localDelivery = "LocalDeliveryPreferred";
                 boolean notPresent = true;
                 for (Object obj : destAttrs) {
                     Attribute attrib = (Attribute) obj;
                     if (attrib.getName().equals(localDelivery)) {
                         notPresent  = false;
                     }
                 }
                 if (notPresent) {
                    Attribute attrib = new Attribute (localDelivery,
                                       Boolean.valueOf("true"));
                    destAttrs.add(attrib);
                 }
             }
        
public static booleanuseJMX(com.sun.enterprise.admin.target.Target target)
Determines whether MQ JMX connector needs to be used for MQ related administrative operations.

param
target Target on which a commands needs to be executed

        /*
        if(JMSDestination.USE_JMX) {
            if ((target.getType() == TargetType.DAS) || (target.getType() == TargetType.SERVER)) {
                return true; 
            }
        } 
       return false; 
       */
	   return USE_JMX;