/*
 * 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.web.loadbalancer;

import java.io.IOException;
import javax.management.ObjectName;
import javax.servlet.ServletException;

import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.system.ServiceMBeanSupport;
import org.jboss.web.loadbalancer.scheduler.NoHostAvailableException;
import org.jboss.web.loadbalancer.scheduler.SchedulerMBean;
import org.jboss.web.loadbalancer.util.Request;
import org.w3c.dom.Element;

/**
 *
 * @jmx:mbean name="jboss.web.loadbalancer: service=Loadbalancer"
 *            extends="org.jboss.system.ServiceMBean"
 *
 * @author Thomas Peuss <jboss@peuss.de>
 * @version $Revision: 1.1.2.3 $
 */
public class LoadbalancerService
    extends ServiceMBeanSupport
    implements LoadbalancerServiceMBean
{
  protected Element config;
  protected Loadbalancer loadbalancer;
  protected ObjectName schedulerObjectName;
  protected SchedulerMBean scheduler;
  protected int timeout = 20000;

  protected void startService() throws java.lang.Exception
  {
    scheduler = (SchedulerMBean)
        MBeanProxyExt.create(SchedulerMBean.class,
                             schedulerObjectName);
    loadbalancer = new Loadbalancer(scheduler, timeout);
  }

  protected void destroyService() throws java.lang.Exception
  {
    loadbalancer = null;
  }

  /**
   * Set the scheduler for this Loadbalancer.
   * @jmx:managed-attribute
   * @param schedulerObjectName
   */
  public void setScheduler(ObjectName schedulerObjectName)
  {
    this.schedulerObjectName = schedulerObjectName;
  }

  /**
   * Get the scheduler for this Loadbalancer.
   * @jmx:managed-attribute
   */
  public ObjectName getScheduler()
  {
    return schedulerObjectName;
  }

  /**
   * Get the currently used connection timeout to slave hosts.
   * @jmx:managed-attribute
   */
  public int getConnectionTimeout()
  {
    return timeout;
  }

  /**
   * Set the currently used connection timeout to slave hosts.
   * @jmx:managed-attribute
   */
  public void setConnectionTimeout(int newTimeout)
  {
    this.timeout = newTimeout;
    if (loadbalancer != null)
    {
      loadbalancer.setConnectionTimeout(newTimeout);
    }
  }

  /**
   * Get the currently used connections to slave hosts.
   * @jmx:managed-attribute
   */
  public int getConnectionsInUse()
  {
    return loadbalancer.getConnectionsInUse();
  }

  /**
   * @jmx:managed-operation
   */
  public void createMethod(Request schedRequest) throws NoHostAvailableException
  {
    loadbalancer.createMethod(schedRequest);
  }

  /**
   * @jmx:managed-operation
   */
  public void addRequestData(Request schedRequest)
  {
    loadbalancer.addRequestData(schedRequest);
  }

  /**
   * @jmx:managed-operation
   */
  public void handleRequest(Request schedRequest) throws ServletException, IOException
  {
    loadbalancer.handleRequest(schedRequest);
  }
}