/*
 * LOGICAL-PARADOX.ORG
 * Copyright (C)2005 satoshi akabane(akabane@logical-paradox.org)
 * $Id: domain.java,v 1.3 2005/09/28 16:13:56 rampil Exp $
 */
package org.logical_paradox.vsan.tools.console.command;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Map;

import org.logical_paradox.shell.Argument;
import org.logical_paradox.shell.Command;
import org.logical_paradox.shell.Option;
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.Domain;
import org.logical_paradox.vsan.domain.DomainController;
import org.logical_paradox.vsan.lookup.LookupException;
import org.logical_paradox.vsan.lookup.LookupFactory;
import org.logical_paradox.vsan.node.DiskArrayNode;
import org.logical_paradox.vsan.tools.console.DacConsoleConstant;

/**
 * <pre>
 * ■domain
 * 
 * 【動作仕様】
 * ドメインの情報を表示します。
 * 
 * 【書式】
 * domain <ドメイン名> [<ドメインコントローラー]>
 * 
 * 【例】
 * </pre>
 * 
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.3 $
 */
public class domain implements Command {
	public static final String USAGE = "domain <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 = new Argument();
		}
		String[] args = a.getArguments();
		Option[] opts = (Option[])a.getOptions();

		if(args.length < 1 || args.length > 2 || opts.length > 0) {
			// 引数の数が不正
			throw new CommandExecutionException();
		}

		String dci = null;
		if(args.length == 1) {
			// ドメインコントローラー指定なしの場合
			dci = env.getProperty(DacConsoleConstant.ENV_DC);
			if(dci == null) {
				throw new CommandExecutionException("no domain controller id is specified.");
			}
		} else {
			dci = args[1];
		}

		try {
			// ドメインコントローラーに接続する
			DomainController dc = LookupFactory.getLookup().lookupDomainController(dci);
			// ドメイン情報を取得する
			Domain domain = dc.getDomain(args[0]);

			PrintWriter pw = new PrintWriter(o);

			// レポートの表示
			pw.println("----------------------------------------");
			pw.println("DOMAIN NAME   : [" + domain.getDomainName() + "]");
			pw.println("NUMBER OF NODE: [" + domain.getNodeList().size() + "]");
			Map nodeMap = domain.getNodeList();

			for(Iterator it = nodeMap.keySet().iterator(); it.hasNext();) {
				String nodeid = (String)it.next();
				DiskArrayNode node = (DiskArrayNode)nodeMap.get(nodeid);

				pw.println("              NODE ID - [" + nodeid + "] / " + node.toString());
			}
			pw.println("----------------------------------------");

			pw.println("done.");
			pw.flush();
			pw.close();

		} catch (LookupException e) {
			throw new CommandExecutionException("domain controller seems be down.");
		} catch (RemoteException e) {
			e.printStackTrace();
			throw new CommandExecutionException("specified domain is not exist.");
		}

		return new ResultCode(ResultCode.STATUS_OK);
	}

}
