/*
 * LOGICAL-PARADOX.ORG
 * Copyright (C)2005 satoshi akabane(akabane@logical-paradox.org)
 * $Id: nodeadd.java,v 1.2 2005/09/07 13:38:06 rampil Exp $
 */
package org.logical_paradox.vsan.tools.console.command;

import java.io.OutputStream;
import java.io.PrintWriter;

import org.logical_paradox.shell.Argument;
import org.logical_paradox.shell.Command;
import org.logical_paradox.shell.ResultCode;
import org.logical_paradox.shell.ShellEnv;
import org.logical_paradox.shell.exception.CommandExecutionException;
import org.logical_paradox.shell.exception.TerminationException;
import org.logical_paradox.vsan.domain.DomainController;
import org.logical_paradox.vsan.lookup.LookupFactory;
import org.logical_paradox.vsan.tools.console.DacConsoleConstant;

/**
 * <pre>
 * ■nodeadd
 * 
 * 【動作仕様】
 * 指定ドメインに対して、ストレージ領域を追加します。
 * 
 * 【書式】
 * nodeadd <node-id> <domain-name> [<domain-controller>]
 * </pre>
 * 
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.2 $
 */
public class nodeadd implements Command {
	public static final String USAGE = "nodeadd <node-id> <domain-name> [<domain-controller>]";

	public String usage() {
		return USAGE;
	}

	public ResultCode execute(OutputStream o, ShellEnv env, Argument a) throws CommandExecutionException, TerminationException {
		if(a == null || a.getArguments() == null || a.getArguments().length < 2 || a.getArguments().length > 3) {
			throw new CommandExecutionException("illegal arguments.");
		}

		String[] args = a.getArguments();
		String nodeid = args[0];
		String domain = args[1];
		String dci = null;

		if(args.length == 2) {
			// 環境変数からドメインコントローラーを取り込む
			dci = env.getProperty(DacConsoleConstant.ENV_DC);
		} else {
			dci = args[2];
		}
		if(dci == null) {
			throw new CommandExecutionException("no domain controller id is specified.");
		}

		try {
			// ドメインコントローラーを検索する
			DomainController dc = LookupFactory.getLookup().lookupDomainController(dci);
			// ドメインにノードを追加する
//			dc.join(domain, nodeid);

			PrintWriter pw = new PrintWriter(o);
			pw.println("done.");
			pw.flush();
			pw.close();
		} catch(Exception e) {
			throw new CommandExecutionException("exception has been caused in update process.");
		}

		return new ResultCode(ResultCode.STATUS_OK);
	}

}
