CreateDocBase.java sample program
How to login, create a document base, release memory, and logout.
package com.ixiasoft.samples;
/**
* Title: CreateDocbase - 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-07
*/
import com.ixia.textmlserver.*;
import java.util.*;
public class CreateDocbase
{
final static String TOKEN_USER = "USER";
final static String TOKEN_PASSWORD = "PASSWORD";
final static String TOKEN_SERVER = "SERVER";
final static String TOKEN_DOCBASE = "DOCBASE";
final static String TOKEN_PATH = "PATH";
// Valid parameters accepted from command-line
final static String [] validTokens =
{ TOKEN_USER, TOKEN_PASSWORD, TOKEN_SERVER, TOKEN_DOCBASE, TOKEN_PATH};
// Which parameters must be specified
final static boolean[] mandatory =
{ true , true , true , true , true };
// Extract run-time parameters from command-line
private static HashMap Extract(String [] args)
{
HashMap retval = new HashMap(10);
for (int i = 0; i < args.length; ++i)
{
StringTokenizer tokens = new StringTokenizer(args[i], "=", false);
String token = null, value = null;
if (tokens.hasMoreElements())
token = tokens.nextToken();
if (tokens.hasMoreElements())
value = tokens.nextToken();
if (token == null || value == null)
{
retval.clear();
return retval;
}
boolean found = false;
for (int j = 0; j < validTokens.length && !found; ++j)
{
if (validTokens[j].equalsIgnoreCase(token) &&
!retval.containsKey(validTokens[j]))
{
retval.put(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()
{
System.out.println
("CreateDocbase user=<domain\\user> password=<password> " +
"server=<ServerName> docbase=<DocBaseName> path=<path>");
System.out.println
("\t<domain\\user> Domain and username used to login to server");
System.out.println
("\t<password> Password of the user");
System.out.println
("\t<ServerName> Name of the server hosting an instance" +
"of TEXTML Server");
System.out.println
("\t<DocBaseName> Name of the document base name to be created");
System.out.println
("\t<path> The path, on the server, where the docbase " +
"will be created");
}
public static void main(String[] args)
{
// Parse the command line
HashMap map = Extract(args);
// Validate the parameters
if (map.isEmpty())
{
Usage(); // Display Help
return;
}
String user = (String) map.get(TOKEN_USER);
if (user.indexOf("\\") == -1) // Domain not specified
{
Usage();
return;
}
// Prepare parameters for ClientServicesFactory
HashMap parms = new HashMap(1);
try
{
// Get the ClientServices object from ClientServicesFactory.
// Always use the two parameters shown below
// when you call ClientServicesFactory.getInstance().
ClientServices cs =
com.ixia.textmlserver.ClientServicesFactory.getInstance
("CORBA", parms);
// 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.get(TOKEN_PASSWORD));
try
{
// Get PowerServices object for the TEXTML Server instance.
// PowerServices are required to administer the server.
IxiaPowerServices ps =
cs.GetPowerServices((String) map.get(TOKEN_SERVER));
// Waiting for transition to Constants.TEXTML_RUNNING
try
{
long state;
do
{
state = 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
synchronized (ps) { ps.wait(500); }
break;
case (int)Constants.TEXTML_PAUSED:
ps.Continue();
break;
case (int)Constants.TEXTML_RUNNING:
break;
}
}
while (state != Constants.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.get(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.get(TOKEN_DOCBASE),
(String) map.get(TOKEN_PATH));
System.out.println("Document base '" +
map.get(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)
{
System.err.println("Exception occurred: ");
e.printStackTrace(System.err);
}
}
}