ClusterUtil.java :  » JMS » Open-Message-Queue » com » sun » messaging » jmq » jmsserver » management » util » Java Open Source

Java Open Source » JMS » Open Message Queue 
Open Message Queue » com » sun » messaging » jmq » jmsserver » management » util » ClusterUtil.java
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 *
 * You can obtain a copy of the license at
 * https://glassfish.dev.java.net/public/CDDLv1.0.html.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * https://glassfish.dev.java.net/public/CDDLv1.0.html.  
 *
 * If applicable add the following below this CDDL HEADER,
 * with the fields enclosed by brackets "[]" replaced with 
 * your own identifying information: Portions Copyright 
 * [year] [name of copyright owner]
 */

/*
 * @(#)ClusterUtil.java  1.5 05/02/06
 *
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
 */ 

package com.sun.messaging.jmq.jmsserver.management.util;

import java.util.Hashtable;

import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.OpenDataException;

import com.sun.messaging.jms.management.server.BrokerClusterInfo;

import com.sun.messaging.jmq.jmsserver.Globals;
import com.sun.messaging.jmq.jmsserver.cluster.*;
import com.sun.messaging.jmq.io.MQAddress;
import com.sun.messaging.jmq.jmsserver.core.BrokerMQAddress;
import com.sun.messaging.jmq.jmsserver.data.handlers.admin.GetClusterHandler;
import com.sun.messaging.jmq.jmsserver.resources.BrokerResources;
import com.sun.messaging.jmq.util.log.Logger;

public class ClusterUtil  {
    /*
     * Broker Cluster Info item names for Config MBeans
     */
    private static final String[] configBrokerInfoItemNames = {
                            BrokerClusterInfo.ADDRESS,
                            BrokerClusterInfo.ID
                    };

    /*
     * Broker Cluster Info item descriptions for Config MBeans
     * TBD: use real descriptions
     */
    private static final String[] configBrokerInfoItemDesc = configBrokerInfoItemNames;

    /*
     * Broker Cluster Info item types for Config MBeans
     */
    private static final OpenType[] configItemTypes = {
          SimpleType.STRING,    // address
          SimpleType.STRING    // id
                    };

    /*
     * Broker Cluster Info composite type for Config MBeans
     */
    private static CompositeType configCompType = null;

    public static String getBrokerAddress(String brokerID)  {
        ClusterManager cmgr;
        ClusteredBroker bkr;
  MQAddress  addr;

  if (brokerID == null)  {
      return (null);
  }

        cmgr = Globals.getClusterManager();
  if (cmgr == null)  {
      return (null);
  }

        bkr = cmgr.getBroker(brokerID);
  if (bkr == null)  {
      return (null);
  }

        addr = bkr.getBrokerURL();
  if (addr == null) {
      return (null);
  }

        return (addr.toString());
    }

    public static String getShortBrokerAddress(String brokerID)  {
  BrokerMQAddress ba = null;
  String longAddr = getBrokerAddress(brokerID);
  Logger logger = Globals.getLogger();

  if (longAddr == null)  {
      return (null);
  }

  try  {
      ba = BrokerMQAddress.createAddress(longAddr);
  } catch (Exception e)  {
            BrokerResources  rb = Globals.getBrokerResources();

            logger.log(Logger.WARNING, 
    rb.getString(rb.W_JMX_FAILED_TO_OBTAIN_BKR_ADDRESS_FROM_ID, brokerID),
    e);

      return (null);
  }

  if (ba == null)  {
      return (null);
  }

        return (ba.getHost().getHostName() + ":" + ba.getPort());
    }

    public static boolean isMasterBroker(String brokerAddress)  {
  ClusterManager cm = Globals.getClusterManager();
  ClusteredBroker cb = null, master;
  String id = null;
  boolean isMaster = false;

  if (cm == null)  {
      return (false);
  }

  try  {
            id = cm.lookupBrokerID(BrokerMQAddress.createAddress(brokerAddress));
        } catch (Exception e)  {
      return (false);
        }

  if ((id == null) || (id.equals("")))  {
            return (false);
        }

  try  {
      cb = cm.getBroker(id);

      if (cb == null)  {
                return (false);
      }
  } catch(Exception e)  {
            return (false);
  }

  master = cm.getMasterBroker();

  if (master == null)  {
      return (false);
  }

  return (master.equals(cb));
    }

    public static CompositeData getConfigCompositeData(ClusteredBroker cb) 
          throws OpenDataException  {
  Logger logger = Globals.getLogger();
  CompositeData cds = null;

  Hashtable bkrInfo = GetClusterHandler.getBrokerClusterInfo(cb, logger);

  String id = null;

  if (Globals.getHAEnabled())  {
      id = (String)bkrInfo.get(BrokerClusterInfo.ID);
  }

  Object[] brokerInfoItemValues = {
          bkrInfo.get(BrokerClusterInfo.ADDRESS),
          id
      };

        if (configCompType == null)  {
            configCompType = new CompositeType("BrokerClusterInfoConfig", "BrokerClusterInfoConfig", 
                        configBrokerInfoItemNames, configBrokerInfoItemDesc, configItemTypes);
        }

  cds = new CompositeDataSupport(configCompType, 
      configBrokerInfoItemNames, brokerInfoItemValues);
  
  return (cds);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.