/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * 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.webapp.jonasadmin.jonasserver;

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.J2eeObjectName;
import org.objectweb.jonas.jmx.JonasManagementRepr;
import org.objectweb.jonas.jmx.JonasObjectName;
import org.objectweb.jonas.jmx.ManagementReprLoader;
import org.objectweb.jonas.webapp.jonasadmin.JonasBaseAction;
import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;

/**
 * @author Michel-Ange ANTON
 */

public class EditJonasServerAction extends JonasBaseAction {

// --------------------------------------------------------- Public Methods
    /**
     * Execute action for a JOnAS server instance management
     * @param pMapping mapping info
     * @param pForm form object
     * @param pRequest HTTP request
     * @param pResponse HTTP response
     *
     * @return An <code>ActionForward</code> instance or <code>null</code>
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet exception occurs
     */
    public ActionForward executeAction(ActionMapping pMapping, ActionForm pForm
        , HttpServletRequest pRequest, HttpServletResponse pResponse)
        throws IOException, ServletException {

        String requestedObjectName = (String) pRequest.getParameter("select");
        boolean changeManagement = false;
        try {
            ObjectName on = ObjectName.getInstance(requestedObjectName);
            String requestedJonasServerName = on.getKeyProperty("name");
            String currentJonasServerName = m_WhereAreYou.getCurrentJonasServerName();
            if (!currentJonasServerName.equals(requestedJonasServerName)) {
                // change management conext
                ManagementReprLoader.loadServerRepr(requestedJonasServerName);
                // Update management context m_WhereAreYou
                m_WhereAreYou.refreshServers(pRequest, requestedJonasServerName);
                if (m_WhereAreYou.getCurrentJonasServerName().equals(requestedJonasServerName)) {
                    changeManagement = true;
                } else {
                    addGlobalError(new Error("Can't switch management context to server " + requestedJonasServerName));
                    saveErrors(pRequest, m_Errors);
                    return (pMapping.findForward("Global Error"));
                }
            }

            if (changeManagement) {
                refreshServerTree(pRequest);
            }

            // Force the node selected in tree
            m_WhereAreYou.selectNameNode("domain"
                    + WhereAreYou.NODE_SEPARATOR
                    + requestedJonasServerName, true);

            // Form used
            JonasServerForm oForm = (JonasServerForm) pForm;
            //try {
            // Object name used
            ObjectName oObjectName = J2eeObjectName.J2EEServer(m_WhereAreYou.getCurrentDomainName()
                , m_WhereAreYou.getCurrentJonasServerName());
            oForm.setJonasName(m_WhereAreYou.getCurrentJonasServerName());
            oForm.setJonasVersion(getStringAttribute(oObjectName, "serverVersion"));
            oForm.setProtocols(getStringAttribute(oObjectName, "protocols"));
            oForm.setVersions(getStringAttribute(oObjectName, "versions"));
            if (JonasManagementRepr.isRegistered(JonasObjectName.webContainerService(), m_WhereAreYou.getCurrentJonasServerName())) {
                oForm.setPresentServletContainer(true);
                if (m_WhereAreYou.isCatalinaServer()) {
                    oForm.setServerServletContainerInfo("Apache Tomcat");
                    //oForm.setServerServletContainerInfo(ServerInfo.getServerInfo());
                }
            } else {
                oForm.setPresentServletContainer(false);
            }
        } catch (Throwable t) {
            addGlobalError(t);
            saveErrors(pRequest, m_Errors);
            return (pMapping.findForward("Global Error"));
        }
        // Forward to the jsp.
        return (pMapping.findForward("JonasServer"));
    }

}
