package org.objectweb.jonas.webapp.jonasadmin.monitoring;

import java.io.IOException;

import javax.management.ObjectName;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectweb.jonas.jmx.JonasManagementRepr;
import org.objectweb.jonas.webapp.jonasadmin.JonasBaseAction;
import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;

public class DisplayNodeInfoAction extends JonasBaseAction {

    public ActionForward executeAction(ActionMapping p_Mapping,
            ActionForm p_Form, HttpServletRequest p_Request,
            HttpServletResponse p_Response) throws IOException,
            ServletException {
        // Form used
        WhereAreYou oWhere = (WhereAreYou) p_Request.getSession().getAttribute(
                WhereAreYou.SESSION_NAME);
        String sDomainName = oWhere.getCurrentDomainName();

        String serverName = oWhere.getCurrentJonasServerName();

        NodeInfoForm oForm = (NodeInfoForm) p_Form;
        try {

            /*
             * Server related info
             */
            oForm.setServerName(p_Request.getParameter("node"));

            ObjectName on = new ObjectName(sDomainName
                    + ":type=ServerProxy,name=" + oForm.getServerName());
            try {
                oForm.setState((String) JonasManagementRepr.getAttribute(on,
                        "State", serverName));
            } catch (RuntimeException e) {
                // Attibute missing
            }
            try {
                oForm.setHostName((String) JonasManagementRepr.getAttribute(on,
                        "HostName", serverName));
            } catch (RuntimeException e) {
                // Attibute missing
            }
            try {
                oForm.setAllThreadsCount((int) new Integer(JonasManagementRepr.getAttribute(on,
                        "AllThreadsCount", serverName).toString()).intValue());
            } catch (RuntimeException e) {
                // Attibute missing
            }
            try {
                oForm.setJavaVersion((String) JonasManagementRepr.getAttribute(on,
                        "JavaVersion", serverName));
            } catch (RuntimeException e) {
                // Attibute missing
            }
            try {
                oForm.setJavaVendor((String) JonasManagementRepr.getAttribute(on,
                        "JavaVendor", serverName));
            } catch (RuntimeException e) {
                // Attibute missing
            }
            try {
                oForm.setCurrentTotalMemory((int) new Integer(JonasManagementRepr.getAttribute(on,
                        "CurrentTotalMemory", serverName).toString())
                        .intValue());
            } catch (RuntimeException e) {
                // Attibute missing
            }
            try {
                oForm.setCurrentUsedMemory((int) new Integer(JonasManagementRepr.getAttribute(on,
                        "CurrentUsedMemory", serverName).toString()).intValue());
            } catch (RuntimeException e) {
                // Attibute missing
            }
            try {
                oForm.setJOnASVersion((String) JonasManagementRepr.getAttribute(on,
                        "JOnASVersion", serverName));
            } catch (RuntimeException e) {
                // Attibute missing
            }
            try {
                oForm.setProtocols((String) JonasManagementRepr.getAttribute(on,
                        "Protocols", serverName));
            } catch (RuntimeException e) {
                // Attibute missing
            }
            try {
                oForm.setConnectionUrl((String) JonasManagementRepr.getAttribute(on,
                        "ConnectionUrl", serverName));
            } catch (RuntimeException e) {
                // Attibute missing
            }
            /*
             * JDK5
             */

            /*
             * Tomcat
             */
            try {
                oForm.setTomcat(getBooleanAttribute(on, "Tomcat"));
                if (oForm.getTomcat()) {
                    oForm.setMaxThreadsByConnectorTomcat(getIntegerAttribute(on, "MaxThreadsByConnectorTomcat"));
                    oForm.setCurrentThreadCountByConnectorTomcat(getIntegerAttribute(on, "CurrentThreadCountByConnectorTomcat"));
                    oForm.setCurrentThreadBusyByConnectorTomcat(getIntegerAttribute(on, "CurrentThreadBusyByConnectorTomcat"));
                    oForm.setBytesReceivedByConnectorTomcat(getLongAttribute(on, "BytesReceivedByConnectorTomcat"));
                    oForm.setBytesSentByConnectorTomcat(getLongAttribute(on, "BytesSentByConnectorTomcat"));
                    oForm.setErrorCountByConnectorTomcat(getIntegerAttribute(on, "ErrorCountByConnectorTomcat"));
                    oForm.setProcessingTimeByConnectorTomcat(getLongAttribute(on, "ProcessingTimeByConnectorTomcat"));
                    oForm.setRequestCountByConnectorTomcat(getIntegerAttribute(on, "RequestCountByConnectorTomcat"));
                }
            } catch (RuntimeException e) {
                // Attibute missing
            }
            /*
             * Tx
             */
            try {
                oForm.setTransaction(getBooleanAttribute(on, "Transaction"));
                if (oForm.getTransaction()) {
                    oForm.setTotalBegunTransactions(getIntegerAttribute(on, "TotalBegunTransactions"));
                    oForm.setTotalCommittedTransactions(getIntegerAttribute(on, "TotalCommittedTransactions"));
                    oForm.setTotalCurrentTransactions(getIntegerAttribute(on, "TotalCurrentTransactions"));
                    oForm.setTotalExpiredTransactions(getIntegerAttribute(on, "TotalExpiredTransactions"));
                    oForm.setTotalRolledbackTransactions(getIntegerAttribute(on, "TotalExpiredTransactions"));
                }
            } catch (RuntimeException e) {
                // Attibute missing
            }
            /*
             * Worker Pool
             */
            try {
                oForm.setWorkers(getBooleanAttribute(on, "Workers"));
                if (oForm.getWorkers()) {
                    oForm.setCurrentWorkerPoolSize(getIntegerAttribute(on, "CurrentWorkerPoolSize"));
                    oForm.setMaxWorkerPoolSize(getIntegerAttribute(on, "MaxWorkerPoolSize"));
                    oForm.setMinWorkerPoolSize(getIntegerAttribute(on, "MinWorkerPoolSize"));
                }
            } catch (RuntimeException e) {
                // Attibute missing
            }
            /*
             * JCA
             */
            try {
                oForm.setJcaConnection(getBooleanAttribute(on, "JcaConnection"));
                if (oForm.getJcaConnection()) {
                    oForm.setConnectionFailuresJCAConnection(getIntegerAttribute(on, "ConnectionFailuresJCAConnection"));
                    oForm.setConnectionLeaksJCAConnection(getIntegerAttribute(on, "ConnectionLeaksJCAConnection"));
                    oForm.setCurrentBusyJCAConnection(getIntegerAttribute(on, "CurrentBusyJCAConnection"));
                    oForm.setCurrentOpenedJCAConnection(getIntegerAttribute(on, "CurrentOpenedJCAConnection"));
                    oForm.setRejectedOpenJCAConnection(getIntegerAttribute(on, "RejectedOpenJCAConnection"));
                    oForm.setServedOpenJCAConnection(getIntegerAttribute(on, "ServedOpenJCAConnection"));
                    oForm.setWaiterCountJCAConnection(getIntegerAttribute(on, "WaiterCountJCAConnection"));
                    oForm.setWaitingTimeJCAConnection(getLongAttribute(on, "WaitingTimeJCAConnection"));
                }
            } catch (Exception e) {
                // Attibute missing
            }
            /*
             * JDBC
             */
            try {
                oForm.setJdbcDatasource(getBooleanAttribute(on, "JdbcDatasource"));
                if (oForm.getJdbcDatasource()) {
                    oForm.setConnectionFailuresJDBCDatasource(getIntegerAttribute(on, "ConnectionFailuresJDBCResource"));
                    oForm.setConnectionLeaksJDBCDatasource(getIntegerAttribute(on, "ConnectionLeaksJDBCResource"));
                    oForm.setCurrentBusyJDBCDatasource(getIntegerAttribute(on, "CurrentBusyJDBCResource"));
                    oForm.setCurrentOpenedJDBCDatasource(getIntegerAttribute(on, "CurrentOpenedJDBCResource"));
                    oForm.setRejectedOpenJDBCDatasource(getIntegerAttribute(on, "RejectedOpenJDBCResource"));
                    oForm.setServedOpenJDBCDatasource(getIntegerAttribute(on, "ServedOpenJDBCResource"));
                    oForm.setWaiterCountJDBCDatasource(getIntegerAttribute(on, "WaiterCountJDBCResource"));
                    oForm.setWaitingTimeJDBCDatasource(getLongAttribute(on, "WaitingTimeJDBCResource"));
                }
            } catch (RuntimeException e) {
                // Attibute missing
            }
            /*
             * JMS
             */
            try {
                oForm.setJmsJoram(getBooleanAttribute(on, "JmsJoram"));
                if (oForm.getJmsJoram()) {
                    oForm.setJmsQueuesNbMsgsDeliverSinceCreation(getIntegerAttribute(on, "JmsQueuesNbMsgsDeliverSinceCreation"));
                    oForm.setJmsQueuesNbMsgsReceiveSinceCreation(getIntegerAttribute(on, "JmsQueuesNbMsgsReceiveSinceCreation"));
                    int jmsQueuesNbMsgsSendToDMQSinceCreation = getIntegerAttribute(on, "JmsQueuesNbMsgsSendToDMQSinceCreation");
                    oForm.setJmsQueuesNbMsgsSendToDMQSinceCreation(jmsQueuesNbMsgsSendToDMQSinceCreation);

                    oForm.setJmsTopicsNbMsgsDeliverSinceCreation(getIntegerAttribute(on, "JmsTopicsNbMsgsDeliverSinceCreation"));
                    oForm.setJmsTopicsNbMsgsReceiveSinceCreation(getIntegerAttribute(on, "JmsTopicsNbMsgsReceiveSinceCreation"));
                    int jmsTopicsNbMsgsSendToDMQSinceCreation = getIntegerAttribute(on, "JmsTopicsNbMsgsSendToDMQSinceCreation");
                    oForm.setJmsTopicsNbMsgsSendToDMQSinceCreation(jmsTopicsNbMsgsSendToDMQSinceCreation);
                    oForm.setJmsNbMsgsSendToDMQSinceCreation(jmsQueuesNbMsgsSendToDMQSinceCreation + jmsTopicsNbMsgsSendToDMQSinceCreation);
                }
            } catch (RuntimeException e) {
                // Attibute missing
            }
            /*
             * EJB
             */
            try {
                oForm.setCurrentNumberOfEJB(getIntegerAttribute(on, "CurrentNumberOfEJB"));
                oForm.setCurrentNumberOfEntity(getIntegerAttribute(on, "CurrentNumberOfEntityBean"));
                oForm.setCurrentNumberOfMDB(getIntegerAttribute(on, "CurrentNumberOfMDB"));
                oForm.setCurrentNumberOfSBF(getIntegerAttribute(on, "CurrentNumberOfSBF"));
                oForm.setCurrentNumberOfSBL(getIntegerAttribute(on, "CurrentNumberOfSBL"));
            } catch (RuntimeException e) {
                // Attibute missing
            }

        } catch (Throwable t) {
            addGlobalError(t);
            saveErrors(p_Request, m_Errors);
            return (p_Mapping.findForward("Global Error"));
        }

        // Forward to the jsp.
        return (p_Mapping.findForward("DisplayNode"));
    }
}
