SearchIndexes.java sample program
Queries a docbase for the values stored in its indexes.
package com.ixiasoft.samples;
/**
* Title: SearchIndexes.java - a sample "index search" program.
*
* Description: This sample shows how to use an "index query" to search for the
* values that appear in a specified index of a specified docbase.
*
* For a sample index query file, see SampleSearchIndexQuery.xml,
* which works with the sample docbase distributed with TEXTML
* Server.
*
* Syntax: DoSearch user=<domain\\user>
* password=<password>
* server=<ServerName>
* docbase=<docBaseName>
* queryfile=<queryFile>
*
* Copyright: Copyright (c) 2003, 2010
* Company: Ixiasoft Technologies Inc.
*
* @version 2.0
* Modified: 2010-08-16
*/
import com.ixia.textmlserver.*;
import java.io.*;
import java.util.*;
public class SearchIndexes
{
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_QUERYFILE = "QUERYFILE";
// Valid parameters accepted from command-line
final static String [] validTokens =
{ TOKEN_USER, TOKEN_PASSWORD, TOKEN_SERVER, TOKEN_DOCBASE,
TOKEN_QUERYFILE };
// Which parameters must be specified
final static boolean[] mandatory =
{ true , true , true , true ,
true };
final static String newLine = System.getProperty("line.separator") ;
// If run-time parameters are missing or invalid, display Help
private static void Usage()
{
System.out.println
("SearchIndexes user=<domain\\user> password=<password> " +
"server=<serverName> docbase=<docBaseName> " +
"queryfile=<[path\\]queryFileName>");
System.out.println
("\t<domain\\user> Name of the user used for security purpose");
System.out.println
("\t<password> Password of the user");
System.out.println
("\t<ServerName> Name of TEXTML Server instance");
System.out.println
("\t<DocBaseName> Document base name");
System.out.println
("\t<[path\\]queryFileName> XML file containing query");
}
// 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;
}
// Read the index query file, and return its contents as a String object
private static String ReadQuery(String fileName) throws java.io.IOException
{
FileInputStream stream = new FileInputStream(fileName);
InputStreamReader reader = new InputStreamReader(stream, "UTF-16");
StringBuffer buff = new StringBuffer(stream.available());
try
{
while (reader.ready())
buff.append((char)reader.read());
}
finally
{
reader.close();
}
return buff.toString();
}
//*** This function is never used ***
private static byte[] ReadFile(File file) throws java.io.IOException
{
FileInputStream f = new FileInputStream(file);
byte[] retval = new byte[f.available()];
try
{
f.read(retval);
}
finally
{
f.close();
}
return retval;
}
public static void main(String[] args)
{
String user = null;
// Parse the command line
HashMap map = Extract(args);
if (map.isEmpty())
{
Usage();
return;
}
// Validate the command-line parameters
user = (String) map.get(TOKEN_USER);
if (user.indexOf("\\") == -1)
{
Usage();
return;
}
// Prepare parameters for ClientServicesFactory
HashMap<String, String> parms = new HashMap<String, String>(1);
try
{
// Get the ClientServices object
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));
// Read the query from the query file
String query = ReadQuery((String)map.get(TOKEN_QUERYFILE));
try
{
// Get the ServerServices for the server specified by the user
IxiaServerServices ss =
cs.ConnectServer((String) map.get(TOKEN_SERVER));
try
{
// Get the DocbaseServices for the specified docbase
// on that server
IxiaDocBaseServices docbase =
ss.ConnectDocBase((String) map.get(TOKEN_DOCBASE));
try
{
// then, the SearchServices for that docbase
IxiaSearchServices search = docbase.SearchServices();
try
{
// Display the index query. The query
// must be in the following format:
// SEARCH_INDEX.DTD.
System.out.println("Executing query:");
System.out.println(query);
System.out.print(newLine) ;
// Search the specified docbase with the
// specified query. Store the results in
// a String.
String result =
search.SearchIndexes(query);
// If you got this far, then your query did
// not throw an exception.
// Display the results, which are in XML that is
// in the following format:
// SEARCH_INDEXRESULT.DTD.
System.out.println ("Result of query:");
System.out.println (result);
}
finally
{
search.Release();
}
}
finally
{
docbase.Release();
}
}
finally
{
ss.Release();
}
}
finally
{
// Don't forget to logout
cs.Logout();
}
}
catch (Exception e)
{
System.err.println("Exception occurred: ");
e.printStackTrace(System.err);
}
}
}