// (c) Copyright 2014 by PTC Inc. All rights reserved.

/**
 * Example Integration Command Extension demonstrating how to return
 * a custom exit code by overriding getExitCode()
 */

package ptc.integrationcommandextensions.example;

import com.mks.api.Command;
import com.mks.api.Option;
import com.mks.api.ext.*;
import com.mks.api.response.APIException;
import com.mks.api.util.APIVersion;

public class ExitCodeExampleIntegrationCommand extends SimpleIntegrationCommand
{
    private static final String HOSTNAME = "myhost";
    private static final int PORT = 7002;
    private int exitCode = 0;
    
    public ExitCodeExampleIntegrationCommand()
    {
	super(APIVersion.API_4_13);
    }
    
    @Override
    protected void execute(ResponseWriter apiout, CommandOptions options,
	    CommandSelection selection) throws APIException
    {
	Command connectCommand = new Command(Command.SI, "connect");
	connectCommand.addOption(new Option("hostname", HOSTNAME));
	connectCommand.addOption(new Option("port", Integer.toString(PORT)));
	connectCommand.addOption(new Option("user", options
		.getOptionValue("user")));
	connectCommand.addOption(new Option("password", options
		.getOptionValue("password")));
	
	try {
	    runLocalCommand(connectCommand);
	} catch (APIException ex){
	    exitCode = -1;
	}
    }
    
    @Override
    public int getExitCode()
    {
	return exitCode;
    }
}
