/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2008 Bull S.A.S.
 * Contact: jonas-team@objectweb.org
 *
 * This library 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 any later version.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.container;

import org.objectweb.jonas_ejb.container.JSessionFactory;

/**
 * This class implements the SessionBean type specified in JSR77.
 *
 * @author Adriana Danes
 * @author S. Ali Tokmen, Malek Chahine: EJB statistics
 */
public class SessionBean extends EJB {

    /**
     * @param objectName JMX object name
     * @param factoryToManage JSessionFactory factory to manage
     */
    public SessionBean(final String objectName, final JSessionFactory factoryToManage) {
        super(objectName, factoryToManage);
    }

    /**
     * @return session time out for this session ejb
     */

    public int getSessionTimeOut() {
        return ((JSessionFactory) ejbToManage).getTimeout();
    }

    /**
     * @param timeOut new session time out for this session bean
     */
    public void setSessionTimeOut(final int timeOut) {
        ((JSessionFactory) ejbToManage).setTimeout(timeOut);
    }

    /**
     * @return true if EJB monitoring settings have been defined in the
     *              deployment descriptor
     */
    public boolean getMonitoringSettingsDefinedInDD() {
        return ((JSessionFactory) ejbToManage).getMonitoringSettingsDefinedInDD();
    }

    /**
     * @return true if ejb monitoring active
     */
    public boolean getMonitoringEnabled() {
        return ((JSessionFactory) ejbToManage).getMonitoringEnabled();
    }

    /**
     * @param monitoringEnabled whether ejb monitoring will be active
     */
    public void setMonitoringEnabled(final boolean monitoringEnabled) {
        ((JSessionFactory) ejbToManage).setMonitoringEnabled(monitoringEnabled);
    }

    /**
     * @return the method time threshold (in ms)
     */
    public int getWarningThreshold() {
        return ((JSessionFactory) ejbToManage).getWarningThreshold();
    }

    /**
     * @param warningThreshold new method time threshold (in ms)
     */
    public void setWarningThreshold(final int warningThreshold) {
        ((JSessionFactory) ejbToManage).setWarningThreshold(warningThreshold);
    }

    /**
     * @return the total number of calls on this ejb
     */
    public int getNumberOfCalls() {
        return ((JSessionFactory) ejbToManage).getNumberOfCalls();
    }

    /**
     * @return the total time spent in business execution
     */
    public long getTotalBusinessProcessingTime() {
        return ((JSessionFactory) ejbToManage).getTotalBusinessProcessingTime();
    }

    /**
     * @return the total time spent in container + business execution
     */
    public long getTotalProcessingTime() {
        return ((JSessionFactory) ejbToManage).getTotalProcessingTime();
    }

    /**
     * @return the average time per request spent in business execution
     */
    public long getAverageBusinessProcessingTime() {
        int numberOfCalls = getNumberOfCalls();
        if (numberOfCalls > 0) {
            return getTotalBusinessProcessingTime() / numberOfCalls;
        } else {
            return 0;
        }
    }

    /**
     * @return the average total time per request spent in container + business
     *         execution
     */
    public long getAverageProcessingTime() {
        int numberOfCalls = getNumberOfCalls();
        if (numberOfCalls > 0) {
            return getTotalProcessingTime() / numberOfCalls;
        } else {
            return 0;
        }
    }
}
