/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2007 Bull S.A.S.
 * 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 (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
/**
 * this servlet will :
 * - get the server to configure
 * - get its current conf infos
 * - forward the request to a jsp
 */
package org.ow2.jonas.webapp.jonasadmin.clusterd;

import java.io.IOException;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.webapp.jonasadmin.JonasBaseAction;
import org.ow2.jonas.webapp.jonasadmin.JonasManagementRepr;
import org.ow2.jonas.webapp.jonasadmin.domain.ServerItem;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DaemonProxyClusterConfigureServerAction extends JonasBaseAction {
    public static final String DEF_JAVA_HOME = "/usr/java/jdk-ia32/sun/j2sdk1.4.2_10";
    public static final String DEF_JONAS_ROOT = "/home/zengeyil/pkg/jonas_root_5";
    public static final String DEF_JONAS_BASE = "/home/zengeyil/pkg/jonas_base_5";
    public static final String DEF_XPREM = "-Djava.net.preferIPv4Stack=true";
    public static final String DEF_DESC = "jonas server";
    public static final String DEF_AUTO_BOOT = "false";



    @Override
    public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form, HttpServletRequest p_Request, HttpServletResponse p_Response) throws IOException, ServletException {
        String sDomainName = m_WhereAreYou.getCurrentDomainName();
        String serverName = m_WhereAreYou.getAdminJonasServerName();
        @SuppressWarnings("unused")
        String cdName = m_WhereAreYou.getCurrentClusterDaemonName();
        //getting the server name
        String sName = p_Request.getParameter("server");
        ClusterdServerForm oform = (ClusterdServerForm) p_Form;

        oform.setJavaHome(DEF_JAVA_HOME);
        oform.setJonasRoot(DEF_JONAS_ROOT);
        oform.setJonasBase(DEF_JONAS_BASE);
        oform.setDescription(DEF_DESC);
        oform.setAutoBoot(DEF_AUTO_BOOT);
        oform.setXprem(DEF_XPREM);
        oform.setName(sName);
        oform.setCdName(cdName);
        //getting current server infos
        try {
            ObjectName on = new ObjectName(sDomainName
                    + ":type=ServerProxy,name=" + sName);

            if (on != null) {
                //that means the server is in the domain.xml but a server should be affected to the domain without being in the domain.xml file on start.
                //think about adding the server in domain manager when adding it into a domain by jonasAdmin console
                String state = (String) JonasManagementRepr.getAttribute(on,
                        "State", serverName);

                ServerItem srv = m_WhereAreYou.getSrvConfiguredForClusterd(sName);
                if (srv == null) {
                    //the server is not configured for clusterd affectation
                    if (state.equals("RUNNING")) {
                        //the server is running
                        String[] attributes = new String[] {
                                "JavaHome", "JonasRoot", "JonasBase"
                        };
                        AttributeList al = (AttributeList) JonasManagementRepr.getAttributes(on, attributes, serverName);
                        for (int i = 0; i < al.size(); i++) {
                            Attribute at = (Attribute) al.get(i);
                            String name = at.getName();
                            if ("JavaHome".equals(name)) {
                                oform.setJavaHome((String) at.getValue());
                            } else if ("JonasRoot".equals(name)) {
                                oform.setJonasRoot((String) at.getValue());
                            } else if ("JonasBase".equals(name)) {
                                 oform.setJonasBase((String) at.getValue());
                            }
                        }
                    }
                }
                oform.setState(state);
                //Configuration mode
                p_Request.setAttribute("isNotMonitoring", "true");
                //set this server as current server to configure
                m_WhereAreYou.setCurrentSrvToConfigure(sName);
            }
        } catch (Throwable t) {
            addGlobalError(t);
            saveErrors(p_Request, m_Errors);
            return (p_Mapping.findForward("Global Error"));
        }
       //next operation will set selected server's properties that are needed
       //to affect it to the current cluster daemon.
        return (p_Mapping.findForward("DaemonProxyClusterDisplayServerConfig"));
    }

}
