PSearch.java revision 76ad422356a176a459b251dab22660cdc0bb5195
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE
* or https://OpenDS.dev.java.net/OpenDS.LICENSE.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2009 Sun Microsystems, Inc.
*/
import netscape.ldap.util.GetOpt;
public class PSearch {
public static void main(String[] args) {
String usage = "Usage: psearch -h <hostname> -p <port> -b <suffix>" + "[-D bindDN] [-w bindPW]" + "-f <fileURL+file name>" + "-l <y>" + "-n <number of thread>" + " -o <add,modify,delete,moddn>";
String hostname = "localhost";
int portnumber = 1389; //LDAPv3.DEFAULT_PORT;
int nbThreads = 1;//number of thread by default
// Check for these options. -H means to print out a usage message.
GetOpt options = new GetOpt("h:p:b:D:w:H:f:n:o:l", args);
// Get the arguments specified for each option.
String host = options.getOptionParam('h');
// host
if (options.hasOption('h')) {
if (host == null) {
// usage
System.out.println(usage);
System.exit(1);
} else {
hostname = host;
}
}
String port = options.getOptionParam('p');
// If a port number was specified, convert the port value
// to an integer.
if (port != null) {
try {
portnumber = java.lang.Integer.parseInt(port);
} catch (java.lang.Exception e) {
System.out.println("Invalid port number: " + port);
System.out.println(usage);
System.exit(1);
}
}
//number of thread
String nbT = options.getOptionParam('n');
if (nbT != null) {
try {
nbThreads = java.lang.Integer.parseInt(nbT);
} catch (java.lang.Exception e) {
System.out.println("Invalid Thread number: " + nbT);
System.out.println(usage);
System.exit(1);
}
}
// PSearch suffix
String suffix = options.getOptionParam('b');
String bindDN = options.getOptionParam('D');
String bindPW = options.getOptionParam('w');
//operations all by default
String operation = PSearchOperations.ALL;
if (options.hasOption('o')) {
String opParam = options.getOptionParam('o');
if (opParam.equals("add")) {
operation = PSearchOperations.ADD;
} else if (opParam.equals("modify")) {
operation = PSearchOperations.MODIFY;
} else if (opParam.equals("delete")) {
operation = PSearchOperations.DELETE;
} else if (opParam.equals("moddn")) {
operation = PSearchOperations.MODDN;
}else if (opParam.equals("all")) {
operation = PSearchOperations.ALL;;
} else {
System.out.println("Invalid operation type: " + opParam);
System.out.println(usage);
System.exit(1);
}
}
// to disable the log files
boolean useFile = false;
String fileName = "logLile";
if (options.hasOption('f')) {
useFile = options.hasOption('f');
fileName = options.getOptionParam('f');
}
// to enable system out logs
boolean output = options.hasOption('l');
System.out.println("Connecting to " + hostname + ":" + portnumber +
" as \"" + bindDN + "\"" +
" on suffix \"" + suffix + "\"" +
" on operation \"" + operation + "\"" +
" use file: \"" + useFile + "\" output: \"" + output + "\"");
//start all thread
for (int i = 0; i < nbThreads; i++) {
PSearchOperations ps = new PSearchOperations(i, hostname, portnumber, bindDN, bindPW, suffix);
if (useFile) {
ps.useLogFile(useFile);
ps.setLogFile(fileName);
}
ps.setOutput(output);
ps.setOperation(operation);
ps.start();
}
}
}