FileDocCategorySizeDatePackage
MQJMXConnectorHelper.javaAPI DocGlassfish v2 API18556Fri May 04 22:34:26 BST 2007com.sun.enterprise.connectors.system

MQJMXConnectorHelper

public class MQJMXConnectorHelper extends Object
A Utility class to obtain JMXConnectorInfo for MQ broker instances
author
Sivakumar Thyagarajan sivakumar.thyagarajan@sun.com

Fields Summary
static Logger
_logger
private static final String
JMXSERVICEURLLIST
private static final String
JMXCONNECTORENV
private static com.sun.enterprise.util.i18n.StringManager
sm
Constructors Summary
Methods Summary
private static MQJMXConnectorInfo_getMQJMXConnectorInfo(java.lang.String targetName, com.sun.enterprise.config.serverbeans.JmsService jmsService, java.lang.Class mqRAClassName)
Gets the MQJMXConnector object for a standalone server instances and destinations created in DAS/PE instance. The default address list is used while setting the ConnectionURL in MQ RA.

        try {
            //If DAS, use the default address list, else obtain 
            
            String connectionURL = null;
            
            boolean isDAS = isDAS(targetName);
            
            if (isDAS) {
                logFine(" _getMQJMXConnectorInfo - in DAS");
                _logger.log(Level.FINE,"In DAS");
                connectionURL = getDefaultAddressList(jmsService).toString();
            } else {
                //Standalone server instance
                _logger.log(Level.FINE,"not in DAS");
                logFine(" _getMQJMXConnectorInfo - NOT in DAS");
                String domainurl  =
                ApplicationServer.getServerContext().getServerConfigURL();
                JmsService serverJmsService= getJmsServiceOfStandaloneServerInstance(targetName);
                MQAddressList mqadList = new MQAddressList(serverJmsService, targetName);
                mqadList.setup();
                connectionURL = mqadList.toString();                
            }
            logFine(" _getMQJMXConnectorInfo - connection URL " + connectionURL);
            
            ResourceAdapter raInstance = getConfiguredRA(mqRAClassName, connectionURL); 
            String jmxServiceURL = null, jmxServiceURLList = null;
            Map<String, ?> jmxConnectorEnv = null; 
            Method[] methds = raInstance.getClass().getMethods();
            for (int i = 0; i < methds.length; i++) {
                Method m = methds[i];
		if (m.getName().equalsIgnoreCase("get" + JMXSERVICEURLLIST)){ 
                    jmxServiceURLList = (String)m.invoke(raInstance, new Object[]{});
                } else if (m.getName().equalsIgnoreCase("get" + JMXCONNECTORENV)){
                    jmxConnectorEnv = (Map<String,?>)m.invoke(raInstance, new Object[]{});
                }
            }
            logFine(" _getMQJMXConnectorInfo - jmxServiceURLList " +  jmxServiceURLList);
            logFine(" _getMQJMXConnectorInfo - jmxConnectorEnv " + jmxConnectorEnv);
            jmxServiceURL = getFirstJMXServiceURL(jmxServiceURLList);
            
            MQJMXConnectorInfo mqInfo = new MQJMXConnectorInfo(targetName,
                            ActiveJmsResourceAdapter.getBrokerInstanceName(jmsService) ,
                            jmsService.getType(), jmxServiceURL, jmxConnectorEnv);
            return mqInfo;
        } catch (Exception e) {
            e.printStackTrace();
            ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage());
            cre.initCause(e);
            throw cre;
        } 
    
private static MQJMXConnectorInfo_getMQJMXConnectorInfoForCluster(java.lang.String target, com.sun.enterprise.config.serverbeans.JmsService jmsService, java.lang.Class mqRAClassName)
Gets the MQJMXConnector object for a cluster. Since this code is executed in DAS, an admin API is used to resolve hostnames and ports of cluster instances for LOCAL type brokers while creating the connectionURL.

        // Create a new RA instance.
        ResourceAdapter raInstance = null;
        // Set the ConnectionURL.
        try {
            MQAddressList list = null;

            if (jmsService.getType().equalsIgnoreCase(ActiveJmsResourceAdapter.REMOTE)) {
                list = getDefaultAddressList(jmsService);
            } else {
                String domainurl  =
                    ApplicationServer.getServerContext().getServerConfigURL();
                AppserverClusterViewFromCacheRepository rep
                    = new AppserverClusterViewFromCacheRepository(domainurl);

                java.util.Map<String,JmsHost> hostMap =
                    rep.getResolvedLocalJmsHostsInCluster(target);

                if ( hostMap.size() == 0 ) {
                    String msg = sm.getString("mqjmx.no_jms_hosts");
                    throw new ConnectorRuntimeException(msg);
                }
                
                list = new MQAddressList();
                for (JmsHost host : hostMap.values()) {
                    list.addMQUrl(host);
                }
            }

            String connectionUrl = list.toString();
            raInstance = getConfiguredRA(mqRAClassName, connectionUrl);
        } catch (Exception e) {
            e.printStackTrace();
            ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage());
            cre.initCause(e);
            throw cre;
        } 
        
        try {
            String jmxServiceURL = null, jmxServiceURLList = null;
            Map<String, ?> jmxConnectorEnv = null; 
            Method[] methds = raInstance.getClass().getMethods();
            for (int i = 0; i < methds.length; i++) {
                Method m = methds[i];
                if (m.getName().equalsIgnoreCase("get" + JMXSERVICEURLLIST)){ 
                    jmxServiceURLList = (String)m.invoke(raInstance, new Object[]{});
                    if (jmxServiceURLList != null && !jmxServiceURLList.trim().equals("")){
                        jmxServiceURL = getFirstJMXServiceURL(jmxServiceURLList);
                    }
                } else if (m.getName().equalsIgnoreCase("get" + JMXCONNECTORENV)){
                    jmxConnectorEnv = (Map<String,?>)m.invoke(raInstance, new Object[]{});
                }
            }
            MQJMXConnectorInfo mqInfo = new MQJMXConnectorInfo(target,
                            ActiveJmsResourceAdapter.getBrokerInstanceName(jmsService) ,
                            jmsService.getType(), jmxServiceURL, jmxConnectorEnv);
            return mqInfo;
        } catch (Exception e) {
            e.printStackTrace();
            ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage());
            cre.initCause(e);
            throw cre;
        } 
    
private static javax.resource.spi.ResourceAdaptergetConfiguredRA(java.lang.Class mqRAclassname, java.lang.String connectionURL)

        ResourceAdapter raInstance = (ResourceAdapter) mqRAclassname.newInstance();
        Method setConnectionURL = mqRAclassname.getMethod(
                       "set" + ActiveJmsResourceAdapter.CONNECTION_URL,
                        new Class[] { String.class});
        setConnectionURL.invoke(raInstance, new Object[] {connectionURL});
        logFine(" getConfiguredRA - set connectionURL as " + connectionURL);
        return raInstance;
    
private static MQAddressListgetDefaultAddressList(com.sun.enterprise.config.serverbeans.JmsService jmsService)

        MQAddressList list = new MQAddressList(jmsService);
        list.setup();
        return list;
    
private static java.lang.StringgetFirstJMXServiceURL(java.lang.String jmxServiceURLList)

        //If type is REMOTE, MQ RA returns a null jmxServiceURL and a non-null
        //jmxServiceURLList for PE also.
        if ((jmxServiceURLList == null) || ("".equals(jmxServiceURLList))) {
            return jmxServiceURLList;
        } else {
            StringTokenizer tokenizer = new StringTokenizer(jmxServiceURLList, " ");
            return  tokenizer.nextToken();
        }
    
private static com.sun.enterprise.config.serverbeans.JmsServicegetJmsServiceOfStandaloneServerInstance(java.lang.String targetName)

        logFine("getJMSServiceOfSI LL " + targetName);
        ConfigContext con = com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext();
        
        Config cfg = null;
        if (isAConfig(targetName)) {
            cfg = ServerHelper.getConfigByName(con, targetName);
        } else {
            cfg = ServerHelper.getConfigForServer(con, targetName);
        }
        
        logFine("cfg " + cfg);
        JmsService jmsService     = cfg.getJmsService();
        logFine("jmsservice " + jmsService);
        return jmsService;
    
private static ActiveJmsResourceAdaptergetMQAdapter()

        //Start ActiveJMSResourceAdapter.
        final String mqraModuleName = ConnectorRuntime.DEFAULT_JMS_ADAPTER;

        ActiveJmsResourceAdapter air = (ActiveJmsResourceAdapter)
        java.security.AccessController.doPrivileged
            (new java.security.PrivilegedExceptionAction() {
                 public java.lang.Object run() throws Exception {
                     ManagerFactory.getSAConnectorModulesManager().
                     loadOneSystemApp(mqraModuleName, true);
                     return (ActiveJmsResourceAdapter) ConnectorRegistry.getInstance().
                           getActiveResourceAdapter(mqraModuleName);
             }
        });
        return air;
    
public static MQJMXConnectorInfo[]getMQJMXConnectorInfo(java.lang.String target)

    

          
                                  
        try {
            final Target tgt = getTarget(target);
            final Config config = tgt.getConfigs()[0];
            final JmsService jmsService = config.getJmsService();
            
            ActiveJmsResourceAdapter air = getMQAdapter();
            final Class mqRAClassName = air.getResourceAdapter().getClass();
            
            MQJMXConnectorInfo mqjmxForServer = (MQJMXConnectorInfo)
            java.security.AccessController.doPrivileged
                (new java.security.PrivilegedExceptionAction() {
                 public java.lang.Object run() throws Exception {
                     if(!isClustered(tgt)) {
                         _logger.log(Level.FINE, "Getting JMX connector for" +
                                               " standalone target " + target);
                         return _getMQJMXConnectorInfo(tgt.getName(),
                                                      jmsService, mqRAClassName);
                     } else {
                         _logger.log(Level.FINE, "Getting JMX connector for"  +
                                                    " cluster target " + target);
                         return _getMQJMXConnectorInfoForCluster(tgt.getName(), 
                                                      jmsService, mqRAClassName);
                     }
                 }
            });
          
            return new MQJMXConnectorInfo[]{mqjmxForServer};
        } catch (Exception e) {
            //e.printStackTrace();
            ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage());
            cre.initCause(e);
            throw cre;
        }
    
private static com.sun.enterprise.admin.target.TargetgetTarget(java.lang.String target)

        final TargetType[] vaildTargetTypes = new TargetType[] {
                        TargetType.CONFIG,
                        TargetType.SERVER,
                        TargetType.DOMAIN,
                        TargetType.CLUSTER,
                        TargetType.STANDALONE_SERVER,
                        TargetType.UNCLUSTERED_SERVER,
                        TargetType.STANDALONE_CLUSTER,
                        TargetType.DAS };
        final Target tgt = TargetBuilder.INSTANCE.createTarget(
                        vaildTargetTypes, target, 
                        com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext());
        assert tgt != null;
        return tgt;
    
private static booleanisAConfig(java.lang.String targetName)

        ConfigContext con = com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext();
        return ServerHelper.isAConfig(con, targetName);
    
private static booleanisClustered(com.sun.enterprise.admin.target.Target tgt)

        return tgt.getType() == TargetType.CLUSTER;
    
private static booleanisDAS(java.lang.String targetName)

        ConfigContext con = com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext();
        if (isAConfig(targetName)) {
            return false;   
        }
        return ServerHelper.isDAS(con, targetName);
    
private static voidlogFine(java.lang.String s)

        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "MQJMXConnectorHelper :: " + s);
        }