/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2008 INRIA/University of Nice-Sophia Antipolis
 * Contact: proactive@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU 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
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 * $$PROACTIVE_INITIAL_DEV$$
 */
package org.objectweb.proactive.core.process.ssh;

import org.objectweb.proactive.core.process.AbstractExternalProcessDecorator;
import org.objectweb.proactive.core.process.ExternalProcess;
import org.objectweb.proactive.core.process.SimpleExternalProcess;
import org.objectweb.proactive.core.process.UniversalProcess;


/**
 * <p>
 * The SSHProcess class is able to start any class, of the ProActive library,
 * using ssh protocol.
 * </p><p>
 * For instance:
 * </p><pre>
 * .......
 * SSHProcess ssh = new SSHProcess(new SimpleExternalProcess("ls -lsa"));
 * ssh.setHostname("hostname.domain.fr");
 * ssh.startProcess();
 * .....
 * </pre>
 * @author The ProActive Team
 * @version 1.0,  2002/09/20
 * @since   ProActive 0.9.4
 */
public class SSHProcess extends AbstractExternalProcessDecorator {
    public final static String DEFAULT_SSHPATH = "ssh";
    public final static String DEFAULT_SSH_COPYPROTOCOL = "scp";

    //
    // -- CONSTRUCTORS -----------------------------------------------
    //

    /**
     * Creates a new SSHProcess
     * Used with XML Descriptor
     */
    public SSHProcess() {
        super();

        FILE_TRANSFER_DEFAULT_PROTOCOL = DEFAULT_SSH_COPYPROTOCOL;
        this.command_path = DEFAULT_SSHPATH;
    }

    /**
     * Creates a new SSHProcess
     * @param targetProcess The target process associated to this process. The target process
     * represents the process that will be launched after logging remote host with ssh protocol
     */
    public SSHProcess(ExternalProcess targetProcess) {
        super(targetProcess);

        FILE_TRANSFER_DEFAULT_PROTOCOL = DEFAULT_SSH_COPYPROTOCOL;
        this.command_path = DEFAULT_SSHPATH;
    }

    //
    // -- PUBLIC METHODS -----------------------------------------------
    //

    /**
     * @see org.objectweb.proactive.core.process.UniversalProcess#getProcessId()
     */
    public String getProcessId() {
        return "ssh_" + targetProcess.getProcessId();
    }

    /**
     * @see org.objectweb.proactive.core.process.UniversalProcess#getNodeNumber()
     */
    public int getNodeNumber() {
        return targetProcess.getNodeNumber();
    }

    /**
     * @see org.objectweb.proactive.core.process.UniversalProcess#getFinalProcess()
     */
    public UniversalProcess getFinalProcess() {
        checkStarted();
        return targetProcess.getFinalProcess();
    }

    public static void main(String[] args) {
        try {
            SSHProcess ssh = new SSHProcess(new SimpleExternalProcess("ls -lsa"));
            ssh.setHostname("galere1.inria.fr");
            ssh.startProcess();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //
    // -- PROTECTED METHODS -----------------------------------------------
    //
    @Override
    protected String internalBuildCommand() {
        return buildSSHCommand() + buildEnvironmentCommand();
    }

    protected String buildSSHCommand() {
        StringBuilder command = new StringBuilder();
        command.append(command_path);
        // append username
        if (username != null) {
            command.append(" -l ");
            command.append(username);
        }

        // append host
        command.append(" ");
        command.append(hostname);
        command.append(" ");
        if (logger.isDebugEnabled()) {
            logger.debug(command.toString());
        }
        command.append(" -- ");
        return command.toString();
    }

    //
    // -- PRIVATE METHODS -----------------------------------------------
    //
}

@@ -1431,32 +1431,88 @@
 xternalProcess;%0A
+import org.objectweb.proactive.core.process.JVMProcess;%0A
 import org.objec
@@ -1624,16 +1624,74 @@
 rocess;%0A
+import org.objectweb.proactive.core.util.OperatingSystem;%0A
 %0A%0A/**%0A *
@@ -4756,24 +4756,418 @@
 ;%0A        %7D%0A
+%0A        // Fix for PROACTIVE-472%0A        // Only the local operating is checked so it will not work if several SSHProcess are chained %0A        // with different type of Operating Systems. For example: Unix -SSH-%3E Windows -SSH-%3E Unix%0A        // We don't have enough information to be able to handle all the cases.%0A        if (OperatingSystem.getOperatingSystem() == OperatingSystem.unix) %7B%0A    
         comm
@@ -5182,24 +5182,34 @@
 nd(%22 -- %22);%0A
+        %7D%0A
         retu

Fix for PROACTIVE-472