mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Christophe Sovant
10.14.2009 f399b654e5474a65b0ee5a17959c6a3ef643a7aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * 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();
        }
    }
}