/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.mx.metadata;

import javax.management.DynamicMBean;
import javax.management.MBeanInfo;
import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;

/**
 * This class is a bit bogus.
 *
 * @author  <a href="mailto:trevor@protocool.com">Trevor Squires</a>.
 */
public class MBeanCapability
{
   public static final int DYNAMIC_MBEAN = 0x321;
   public static final int STANDARD_MBEAN = 0x123;
   public static final int NOT_AN_MBEAN = 0xc0de;

   protected int mbeanType = NOT_AN_MBEAN;
   protected Class mbeanClass = null;
   protected MBeanInfo standardInfo = null;

   public static MBeanCapability of(Class mbeanClass) throws NotCompliantMBeanException
   {
      if (null == mbeanClass)
      {
         throw new IllegalArgumentException("Class cannot be null");
      }

      if (StandardMBean.class.isAssignableFrom(mbeanClass))
      {
         MBeanCapability result = new MBeanCapability(mbeanClass);
         result.mbeanType = STANDARD_MBEAN;
         return result;
      }
      else if (DynamicMBean.class.isAssignableFrom(mbeanClass))
      {
         return new MBeanCapability(mbeanClass);
      }
      else if (StandardMetaData.findStandardInterface(mbeanClass) != null)
      {
         return new MBeanCapability(mbeanClass, new StandardMetaData(mbeanClass).build());
      }

      throw new NotCompliantMBeanException("Class does not expose a management interface: " + mbeanClass.getName());
   }

   protected MBeanCapability(Class mbeanClass)
   {
      mbeanType = DYNAMIC_MBEAN;
   }

   protected MBeanCapability(Class mbeanClass, MBeanInfo info)
   {
      mbeanType = STANDARD_MBEAN;
      standardInfo = info;
   }

   public int getMBeanType()
   {
      return mbeanType;
   }
}
