/*
 * 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.net.axis.server;

import org.jboss.axis.EngineConfiguration;
import org.jboss.axis.EngineConfigurationFactory;
import org.jboss.axis.server.AxisServer;
import org.jboss.mx.util.MBeanServerLocator;

import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

/**  test
 * <p> A configuration factory that accesses axis server engines 
 * via JMX attribute access. </p>
 * @author jung
 * @version $Revision: 1.2.2.5 $
 * @since 9.9.2002
 */

public class JMXEngineConfigurationFactory
        implements EngineConfigurationFactory
{

   //
   // Attributes
   //

   protected ObjectName objectName;
   protected MBeanServer server;

   //
   // Constructors
   //

   /** construct a new factory tied to a particular engine provider mbean */

   protected JMXEngineConfigurationFactory(String name)
           throws MalformedObjectNameException
   {
      server = MBeanServerLocator.locateJBoss();
      this.objectName = new ObjectName(name);
   }

   //
   // Protected Helpers
   //

   /**
    * find attribute through JMX server and mbean
    */

   protected Object getAttribute(String attributeName)
   {
      try
      {
         return server.getAttribute(objectName, attributeName);
      }
      catch (JMException e)
      {
         return null;
      }
   }

   //
   // Public API
   //

   /** return axis server associated with mbean */
   public AxisServer getAxisServer()
   {
      return (AxisServer)getAttribute("AxisServer");
   }

   /* (non-Javadoc)
      * @see org.jboss.axis.EngineConfigurationFactory#getClientEngineConfig()
      */
   public EngineConfiguration getClientEngineConfig()
   {
      return (EngineConfiguration)getAttribute("ClientEngineConfiguration");
   }

   /* (non-Javadoc)
      * @see org.jboss.axis.EngineConfigurationFactory#getServerEngineConfig()
      */
   public EngineConfiguration getServerEngineConfig()
   {
      return (EngineConfiguration)getAttribute("ServerEngineConfiguration");
   }

   /**
    * static method to create a new jmx factory
    * @param param objectname of the server mbean
    * @return a new factory bound to that mbean, if it exists
    */
   public static JMXEngineConfigurationFactory newJMXFactory(String param)
   {
      try
      {
         return new JMXEngineConfigurationFactory((String)param);
      }
      catch (MalformedObjectNameException e)
      {
         return null;
      }
   }

   /**
    * static method to create a new factory along the Axis spec
    * @param param specification of the configuration
    * @return new factory, if the param represents an mbean object name
    */

   public static EngineConfigurationFactory newFactory(Object param)
   {
      if (param instanceof String)
      {
         return newJMXFactory((String)param);
      }
      else
      {
         return null;
      }
   }

}
