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

import java.rmi.RemoteException;
import java.util.ResourceBundle;

import org.logical_paradox.common.util.StringUtils;
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;

/**
 * 適当にドメインにノードを追加するツール
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.1 $
 */
public class RandomDomainAppender {
	public static final String TEST_RESOURCE_PFX = "testenv";

	private final String domain;
	private final DomainController dc;

	/**
	 * constructor.
	 * @param dcn domain controller name
	 * @param name domain name
	 * @throws LookupException failed in service lookup process.
	 */	
	public RandomDomainAppender(String dcn, String name) throws LookupException {
		domain = name;

		// get stub of domain controller
		dc = LookupFactory.getLookup().lookupDomainController(dcn);
	}

	/**
	 * append node to the domain.
	 * @throws RemoteException caused exception in process.
	 */
	public void append() throws RemoteException {
		// create a new node
		DiskArrayNode node = new DiskArrayNode(StringUtils.randomText(16, true));
		dc.join(domain, node);
	}

	/**
	 * disconnect domain connection.
	 * @throws caused exception in process.
	 */
	public void close() throws Exception {
		// nothing to do.
	}

	/**
	 * main method
	 * @param args program variables
	 * @throws Exception unhandled exception
	 */
	public static final void main(String[] args) throws Exception {
		// read test property file.
		ResourceBundle rb = ResourceBundle.getBundle(TEST_RESOURCE_PFX);
		String dcn = rb.getString("domain.controller.name");
		String domain = rb.getString("domain.name");

		if(StringUtils.isEmpty(dcn)) {
			throw new Exception("resource 'domain.controller.name' is empty");
		}
		if(StringUtils.isEmpty(domain)) {
			throw new Exception("resource 'domain.name' is empty");
		}
		// create random domain appender
		RandomDomainAppender appender = new RandomDomainAppender(dcn, domain);
		appender.append();

		appender.close();
	}
}
