CreateDocBase.cs sample program

Shows how to login, create a document base, release memory, and logout.

/**
 * Title: CreateDocbase.cs - a sample to create a document base on a server
 *
 * Description:
 *
 *               This sample demonstrates how to:
 *               -- Connect and login to a TEXTML Server instance.
 *               -- Create a document base.
 *
 * Syntax:       CreateDocbase  user=<domain\\user>
 *                              password=<password>
 *                              server=<ServerName>
 *                              docbase=<DocBaseName>
 *                              path=<Path>
 *
 * Copyright:    Copyright (c) 2003, 2010
 * Company:      Ixiasoft Technologies Inc.
 *
 * @version 2.0
 * Modified      2010-06-10
 */

using Ixiasoft.TextmlServer41;
using System;
using System.Threading;
using System.Text;
using System.Collections;
using System.IO;


namespace textmlsample
{

    public class CreateDocBase
    {

        static String TOKEN_USER      = "USER";
        static String TOKEN_PASSWORD  = "PASSWORD";
        static String TOKEN_SERVER    = "SERVER";
        static String TOKEN_DOCBASE   = "DOCBASE";
        static String TOKEN_PATH      = "PATH";

        // Valid parameters accepted from command-line
        static String[] validTokens =
            { TOKEN_USER, TOKEN_PASSWORD, TOKEN_SERVER,
              TOKEN_DOCBASE, TOKEN_PATH};

    // Which parameters must be specified
        static bool[] mandatory =
            { true,       true,           true,
              true,       true};

            // Extract run-time parameters from command-line
            private static Hashtable Extract(String[] args)
        {
            Hashtable retval = new Hashtable(10);

            for (int i = 0; i < args.Length; ++i)
            {
               // StringTokenizer tokens = new StringTokenizer(args[i], "=", false);
                String[] tokens = args[i].Split('=');
                String token = null, value = null;

                if (tokens.Length > 1)
                    token = tokens[0];
                if (tokens.Length > 1)
                    value = tokens[1];

                if (token == null || value == null)
                {
                    retval.Clear();
                    return retval;
                }

                bool found = false;
                for (int j = 0; j < validTokens.Length && !found; ++j)
                {
                    if (validTokens[j].CompareTo(token.ToUpperInvariant()) == 0 &&
                        !retval.ContainsKey(validTokens[j]))
                    {
                        retval.Add(validTokens[j], value);
                        found = true;
                    }
                }

                if (!found)
                {
                    retval.Clear();
                    return retval;
                }
            }

            for (int i = 0; i < validTokens.Length; ++i)
            {
                if (mandatory[i] && !retval.ContainsKey(validTokens[i]))
                {
                    retval.Clear();
                    return retval;
                }
            }

            return retval;
        }

        // If run-time parameters are missing or invalid, display Help
        private static void Usage()
        {
            Console.WriteLine
                ("CreateDocbase user=<domain\\user> password=<password> " +
                  "docbase=<DocBaseName> path=<path>");
            Console.WriteLine
                ("\t<domain\\user> Domain and username used to login to server");
            Console.WriteLine
                ("\t<password> Password of the user");
            Console.WriteLine
                ("\t<ServerName> Name of the server hosting an instance" +
                 "of TEXTML Server");
            Console.WriteLine
                ("\t<DocBaseName> Name of the document base " +
                 "to be created");
            Console.WriteLine
                ("\t<path> The path, on the server, where the docbase " +
                 "will be created");
            Console.WriteLine();
            Console.WriteLine
                ("Press any key to exit");
            Console.ReadKey();
        }

        static void Main(string[] args)
        {
            String user = "";

            // Parse the command line

            Hashtable map = Extract(args);

            // Validate the parameters

            if (map.Count == 0)
            {
                Usage();  // Display Help
                return;
            }

            if (map.ContainsKey(TOKEN_USER))
                user = (String)map[TOKEN_USER];

            if (user.IndexOf("\\") == -1)  // Domain not specified
            {
                Usage();
                return;
            }


            try
            {
                // Get the ClientServices object from ClientServicesFactory.
                IxiaClientServices cs =
                   Ixiasoft.TextmlServer41.ClientServicesFactory.getInstance();

                // Extract domain (or machine-name) from user
                String domain = user.Substring(0, user.IndexOf("\\"));
                String userName = user.Substring(user.IndexOf("\\") + 1);

                // Prepare to login to the TEXTML Server instance
                cs.Login(domain, userName, (String)map[TOKEN_PASSWORD]);

                try
                {
                    // Get PowerServices object for the TEXTML Server instance.
                    // PowerServices are required to administer the server.
                    IxiaPowerServices ps =
                        cs.GetPowerServices((String)map[TOKEN_SERVER]);

                    // Waiting for transition to Constants.TEXTML_RUNNING
                    try
                    {
                        int state;

                        do
                        {
                            state = (int) ps.Status;
                            switch ((int)state)
                            {
                            default:    // do nothing for these states:
                                        // Constants.TEXTML_CONTINUE_PENDING
                                        // Constants.TEXTML_PAUSE_PENDING
                                        // Constants.TEXTML_START_PENDING
                                        // Constants.TEXTML_STOP_PENDING

                             // Wait half a second
                                lock(ps) { Monitor.Wait(ps); }
                                break;
                             case (int)TextmlConstants.TEXTML_PAUSED:
                                 ps.Continue();
                                 break;
                            case (int)TextmlConstants.TEXTML_RUNNING:
                                break;
                            }
                        }
                        while (state != TextmlConstants.TEXTML_RUNNING);

                        // Get the ServerServices object for the instance running
                        // on the specified server. The object provides
                        // access to the server’s administration services.
                        IxiaServerServices ss =
                            cs.ConnectServer((String)map[TOKEN_SERVER]);

                        try
                        {
                            // Get the ServerAdminService object.
                            // You need it to create (install) document bases.
                           IxiaServerAdminServices sas = ss.AdminServices;

                            // Install the specified document base
                            try
                            {

                                sas.InstallDocBase((String) map[TOKEN_DOCBASE],
                                                   (String) map[TOKEN_PATH]);

                                Console.WriteLine
                                    ("Document base '" + map[TOKEN_DOCBASE] +
                                     "' successfully created.");
                            }
                            finally
                            {
                                sas.Release(); // Free memory for this object
                            }
                        }
                        finally
                        {
                            ss.Release();
                        }

                    }
                    finally
                    {
                        ps.Release();
                    }
                }
                finally
                {
                    // Don't forget to logout
                    cs.Logout();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine ("**Exception CreateDocBase.main: " + e);
                Console.WriteLine (e.StackTrace);
            }

        }
    }
}