/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2008 Bull S.A.
 * Contact: jonas-team@ow2.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.ow2.jonas.lib.management.extensions.server;

import org.ow2.jonas.lib.management.domain.DomainMonitor;
import org.ow2.jonas.lib.management.domain.J2EEDomain;
import org.ow2.jonas.lib.management.domain.proxy.server.ServerProxy;
import org.ow2.jonas.lib.management.extensions.base.BaseManagement;
import org.ow2.jonas.management.extensions.server.api.IServer;

/**
 *
 * Implements server management functions.
 * @author Adriana Danes
 * @author Oualaa Hani
 *
 */
public class ServerManagement extends BaseManagement implements IServer {

    private final static String getCurrentUsedMemory = "currentUsedMemory";
    private final static String getServerVersion = "serverVersion";
    //private final String domain = this.getDomainName();
    private DomainMonitor dm = null;

    public ServerManagement(){
        super();
        dm = J2EEDomain.getInstance().getDomainMonitor();
    }

    public String getServerVersion(final String server) {
         /*
         ObjectName serverProxyOn = null;
        try {
            serverProxyOn = JonasObjectName.serverProxy(domain, server);
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String serverVersion = (String) ManagementRepr.getAttribute(serverProxyOn, getServerVersion, server);
        return serverVersion;*/
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJOnASVersion();
    }

    /**
     * Get the JMX connection URL of the server.
     * @param pServer the server's name.
     * @return the JMX connection URL of the server
     */
    public String getConnectionUrl(final String server){
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getConnectionUrl();
    }

    /**
     * Get the memory used by a server in the domain.
     * @param pServer the server's name.
     * @return the used memory
     */
    public long getCurrentUsedMemory(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentUsedMemory();
    }

    /**
     * Get the number of threads used by a server in the domain.
     * @param pServer the server's name.
     * @return the number of threads
     */
    public int getAllThreadsCount(final String server){
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getAllThreadsCount();
    }

    /**
     * @return bytes received by connector tomcat.
     * @param pServer Server name
     */
    public long getBytesReceivedByConnectorTomcat(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getBytesReceivedByConnectorTomcat();
    }

    /**
     * @return bytes sent by connector tomcat.
     * @param pServer Server name
     */
    public long getBytesSentByConnectorTomcat(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getBytesSentByConnectorTomcat();
    }

    /**
     * @return current number of Entity bean.
     * @param pServer Server name
     */
    public int getCurrentNumberOfEntityBean(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentNumberOfEntityBean();
    }

    /**
     * @return current number of EJB.
     * @param pServer Server name
     */
    public int getCurrentNumberOfEJB(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentNumberOfEJB();
    }

    /**
     * @return current number of message driven bean.
     * @param pServer Server name
     */
    public int getCurrentNumberOfMDB(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentNumberOfMDB();
    }

    /**
     * @return current number of state full bean.
     * @param pServer Server name
     */
    public int getCurrentNumberOfSBF(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentNumberOfSBF();
    }

    /**
     * @return current number of state less bean.
     * @param pServer Server name
     */
    public int getCurrentNumberOfSBL(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentNumberOfSBL();
    }

    /**
     * @return current threads busy by connector tomcat.
     * @param pServer Server name
     */
    public int getCurrentThreadBusyByConnectorTomcat(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentThreadBusyByConnectorTomcat();
    }

    /**
     * @return current threads count by tomcat connector
     * @param pServer Server name
     */
    public int getCurrentThreadCountByConnectorTomcat(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentThreadCountByConnectorTomcat();
    }

    /**
     * Get the total memory of the server in the domain.
     * @param pServer the server's name.
     * @return the total memory
     */
    public Long getCurrentTotalMemory(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentTotalMemory();
    }

    /**
     * @return error count by connector tomcat.
     * @param pServer Server name
     */
    public int getErrorCountByConnectorTomcat(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getErrorCountByConnectorTomcat();
    }

    /**
     * Get the JVM vendor.
     * @param pServer the server's name.
     * @return the JVM vendor
     */
    public String getJavaVendor(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJavaVendor();
    }

    /**
     * Get the java version.
     * @param pServer the server's name.
     * @return the java version
     */
    public String getJavaVersion(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJavaVersion();
    }

    /**
     * @return the max threads by connector tomcat.
     * @param pServer Server name
     */
    public int getMaxThreadsByConnectorTomcat(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getMaxThreadsByConnectorTomcat();
    }

    /**
     * @return processing time by connector tomcat.
     * @param pServer Server name
     */

    public long getProcessingTimeByConnectorTomcat(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getProcessingTimeByConnectorTomcat();
    }

    /**
     * Get the the protocols used bye the server.
     * @param pServer the server's name.
     * @return the protocols used bye the server
     */
    public String getProtocols(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getProtocols();
    }

    /**
     * @return request count by connector tomcat.
     * @param pServer Server name
     */
    public int getRequestCountByConnectorTomcat(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getRequestCountByConnectorTomcat();
    }

    /**
     * @return total begun transactions.
     * @param pServer Server name
     */
    public int getTotalBegunTransactions(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getTotalBegunTransactions();
    }

    /**
     * @return total commited transactions.
     * @param pServer Server name
     */
    public int getTotalCommittedTransactions(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getTotalCommittedTransactions();
    }

    /**
     * @return total current transactions.
     * @param pServer Server name
     */
    public int getTotalCurrentTransactions(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getTotalCurrentTransactions();
    }

    /**
     * @return total expired transactions.
     * @param pServer Server name
     */
    public int getTotalExpiredTransactions(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getTotalExpiredTransactions();
    }

    /**
     * @return total global transactions.
     * @param pServer Server name
     */
    public int getTotalRolledbackTransactions(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getTotalRolledbackTransactions();
    }

    /**
     * @return the description
     */
    public String getDescription(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getDescription();
    }

    /**
     * @param description the description to set
     */
    public void setDescription(final String description,final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        srvProxy.setDescription(description);
    }

    /**
     * @return the javaHome
     */
    public String getJavaHome(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJavaHome();
    }

    /**
     * @param javaHome the javaHome to set
     */
    public void setJavaHome(final String javaHome, final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        srvProxy.setJavaHome(javaHome);
    }

    /**
     * @return the jonasRoot
     */
    public String getJonasRoot(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJonasRoot();
    }

    /**
     * @param jonasRoot the jonasRoot to set
     */
    public void setJonasRoot(final String jonasRoot, final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        srvProxy.setJonasRoot(jonasRoot);
    }

    /**
     * @return the jonasBase
     */
    public String getJonasBase(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJonasRoot();
    }

    /**
     * @param jonasBase the jonasBase to set
     */
    public void setJonasBase(final String jonasBase,final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        srvProxy.setJonasBase(jonasBase);
    }

    /**
     * @return the xprem
     */
    public String getXprem(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getXprem();
    }

    /**
     * @param xprem the xprem to set
     */
    public void setXprem(final String xprem, final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        srvProxy.setXprem(xprem);
    }

    /**
     * @return the autoBoot
     */
    public String getAutoBoot(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getAutoBoot();
    }

    /**
     * @param autoBoot the autoBoot to set
     */
    public void setAutoBoot(final String autoBoot, final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        srvProxy.setAutoBoot(autoBoot);
    }

    public String getJ2eeObjectName(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJ2eeObjectName();
    }

    public int getConnectionFailuresJCAConnection(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getConnectionFailuresJCAConnection();
    }

    public int getConnectionLeaksJCAConnection(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getConnectionLeaksJCAConnection();
    }

    public int getCurrentBusyJCAConnection(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentBusyJCAConnection();
    }

    public int getCurrentWorkerPoolSize(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentWorkerPoolSize();
    }

    public int getCurrentOpenedJCAConnection(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentOpenedJCAConnection();
    }

    public int getWaiterCountJCAConnection(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getWaiterCountJCAConnection();
    }

    public long getWaitingTimeJCAConnection(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getWaitingTimeJCAConnection();
    }

    public boolean getTomcat(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getTomcat();
    }

    public boolean getTransaction(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getTransaction();
    }

    public boolean getWorkers(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getWorkers();
    }

    public int getMaxWorkerPoolSize(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getMaxWorkerPoolSize();
    }

    public int getMinWorkerPoolSize(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getMinWorkerPoolSize();
    }

    public int getConnectionFailuresJDBCResource(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getConnectionFailuresJDBCResource();
    }

    public int getConnectionLeaksJDBCResource(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getConnectionLeaksJDBCResource();
    }

    public int getCurrentBusyJDBCResource(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentBusyJDBCResource();
    }

    public int getCurrentOpenedJDBCResource(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getCurrentOpenedJDBCResource();
    }

    public int getRejectedOpenJDBCResource(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getRejectedOpenJDBCResource();
    }

    public int getServedOpenJDBCResource(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getServedOpenJDBCResource();
    }

    public int getWaiterCountJDBCResource(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getWaiterCountJDBCResource();
    }

    public long getWaitingTimeJDBCResource(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getWaitingTimeJDBCResource();
    }

    public int getServedOpenJCAConnection(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getServedOpenJCAConnection();
    }

    public int getRejectedOpenJCAConnection(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getRejectedOpenJCAConnection();
    }

    public boolean getJcaConnection(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJcaConnection();
    }

    public boolean getJdbcDatasource(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJdbcDatasource();
    }

    public boolean getJmsJoram(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJmsJoram();
    }

    public int getJmsQueuesNbMsgsDeliverSinceCreation(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJmsQueuesNbMsgsDeliverSinceCreation();
    }

    public int getJmsQueuesNbMsgsReceiveSinceCreation(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJmsQueuesNbMsgsReceiveSinceCreation();
    }

    public int getJmsQueuesNbMsgsSendToDMQSinceCreation(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJmsQueuesNbMsgsSendToDMQSinceCreation();
    }

    public int getJmsTopicsNbMsgsDeliverSinceCreation(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJmsTopicsNbMsgsDeliverSinceCreation();
    }

    public int getJmsTopicsNbMsgsReceiveSinceCreation(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJmsTopicsNbMsgsReceiveSinceCreation();
    }

    public int getJmsTopicsNbMsgsSendToDMQSinceCreation(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJmsTopicsNbMsgsSendToDMQSinceCreation();
    }

    public String getHostName(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getHostName();
    }

    public String getJOnASVersion(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getJOnASVersion();
    }

    public String getLoadCPU(final String server) {
        ServerProxy srvProxy = dm.findServerProxy(server);
        return srvProxy.getLoadCPU();
    }
}
