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

ludovicp
29.35.2010 7c30dbb5403772b323df3ad907d9ed15d23b5aee
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * 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-2010 Sun Microsystems, Inc.
 */
 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Enumeration;
import netscape.ldap.LDAPAttribute;
import netscape.ldap.LDAPAttributeSet;
import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPEntry;
import netscape.ldap.LDAPException;
import netscape.ldap.LDAPSearchConstraints;
import netscape.ldap.LDAPSearchResults;
import netscape.ldap.LDAPv3;
import netscape.ldap.controls.LDAPPersistSearchControl;
 
 
public class PSearchOperations extends Thread {
 
    public static final String ADD = "ADD";
    public static final String MODIFY = "MODIFY";
    public static final String DELETE = "DELETE";
    public static final String MODDN = "MODDN";
    public static final String ALL = "ALL";
    private LDAPConnection connection;
    private String hostname;
    private int portnumber;
    private String bindDN;
    private String bindPW;
    private String suffix;
    private int threadId;
    private String fileName;
    private boolean output;
    private boolean ldifFormat;
    private boolean logFile;
    private String operation;
    /**
     * constructor
     * @param id
     * @param hostname
     * @param portnumber
     * @param bindDN
     * @param bindPW
     * @param suffix
     */
    public PSearchOperations(int id, String hostname, int portnumber, String bindDN, String bindPW, String suffix) {
        this.hostname = hostname;
        this.portnumber = portnumber;
        this.bindDN = bindDN;
        this.bindPW = bindPW;
        this.suffix = suffix;
        this.threadId = id;
        this.output = false;
        this.logFile = false;
        this.ldifFormat = false;
        //by default all operation
        this.operation = ALL;
 
    }
    /**
     * to use systeme.out
     * @param output boolean
     */
    public void setOutput(boolean output) {        
        this.output = output;
    }
    /**
     * to use the log file
     * @param logFile boolean
     */
    public void useLogFile(boolean logFile) { 
        this.logFile = logFile;
    }
    /**
     * to define the log file and URI
     * @param file String
     */
    public void setLogFile(String file) {        
        //if there one thread the thread id are not add in the file name
        this.fileName = file;
        //in multy thread for each thread the thread id are add in the file name
        if (threadId!=0) {
            String ext = file.substring(file.lastIndexOf("."), file.length());
            this.fileName = file.substring(0, file.lastIndexOf(".")) + threadId + ext;
        }
        //delete old log file if logFile is present and enable
        File fileToDelete = new File(fileName);
        if (fileToDelete.isFile() && logFile) {
            fileToDelete.delete();
        }
    }
    /**
     * to define the PSearch operation
     * @param operation String
     */
    public void setOperation(String operation) {
        this.operation = operation;
    }
 
    public void setLdifFormat(boolean ldifFormat) {
        this.ldifFormat = ldifFormat;
    }
 
    /**
     *Connect to server.
     */
    private void connect() {
        try {
            connection = new LDAPConnection();            
            connection.connect(3, hostname, portnumber, "", "");
            connection.authenticate(3, bindDN, bindPW);
            if(!ldifFormat)
              write("[Thread id: " + threadId + "] \n" + getDate() + connection);
        } catch (LDAPException ex) {
            System.out.println("[Thread id: " + threadId + "]Connection :" + ex.getMessage());
            System.exit(0);
        }
    }
    /**
     * to instanciate new LDAPPersistSearchControl
     * @return LDAPPersistSearchControl
     */
    private LDAPPersistSearchControl PSearchControl() {
        int op = 0;
        if (operation.equals(ALL)) {
            op = LDAPPersistSearchControl.ADD |
                    LDAPPersistSearchControl.MODIFY |
                    LDAPPersistSearchControl.DELETE |
                    LDAPPersistSearchControl.MODDN;
        } else if (operation.equals(ADD)) {
            op = LDAPPersistSearchControl.ADD;
        } else if (operation.equals(MODIFY)) {
            op = LDAPPersistSearchControl.MODIFY;
        } else if (operation.equals(DELETE)) {
            op = LDAPPersistSearchControl.DELETE;
        } else if (operation.equals(MODDN)) {
            op = LDAPPersistSearchControl.MODDN;
        }
 
        boolean changesOnly = true;
        boolean returnControls = true;
        boolean isCritical = true;
 
        LDAPPersistSearchControl persistCtrl =
                new LDAPPersistSearchControl(
                op,
                changesOnly,
                returnControls,
                isCritical);
        return persistCtrl;
    }
    /**
     * LDAP Search
     * @return LDAPSearchResults
     */
    public LDAPSearchResults LDAPSearch() {
        LDAPSearchResults res = null;
        try {
            LDAPPersistSearchControl persistCtrl = PSearchControl();
            LDAPSearchConstraints cons = connection.getSearchConstraints();
            cons.setBatchSize(1);
            cons.setServerControls(persistCtrl);
            // Start the persistent search.
            res = connection.search(suffix, LDAPv3.SCOPE_SUB, "(objectclass=*)", null, false, cons);
        } catch (LDAPException ex) {
            System.out.println("[Thread id: " + threadId + "]LDAPSearch :" + ex.getMessage());
            System.exit(0);
        }
        return res;
    }
    /**
     * return the date and time
     * @return String
     */
    public static String getDate() {
        // Initialize the today's date string
        String DATE_FORMAT = "yyyy/MM/dd:HH:mm:ss";
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        Calendar c1 = Calendar.getInstance(); // today
        return ("[" + sdf.format(c1.getTime()) + "]");
    }
    /**
     *
     * @param b byte off control operation
     * @return
     */
    public String controlName(byte b) {
        String control;
        switch (b) {
            case LDAPPersistSearchControl.ADD:
                control = "ADD";
                break;
            case LDAPPersistSearchControl.DELETE:
                control = "DELETE";
                break;
            case LDAPPersistSearchControl.MODDN:
                control = "MODDN";
                break;
            case LDAPPersistSearchControl.MODIFY:
                control = "MODIFY";
                break;
            default:
                control = String.valueOf(b);
                break;
        }
        return control;
    }
    /**
     * to write on the log file or to use syteme out
     * @param msg String
     */
    public void write(String msg) {
        if (output) {
            System.out.println(msg);
        }
        if (logFile) {
            FileWriter aWriter = null;
            try {
                aWriter = new FileWriter(fileName, true);
                aWriter.write(msg + "\n");
                aWriter.flush();
                aWriter.close();
            } catch (IOException ex) {
                System.out.println("[Thread id: " + threadId + "]Write :" + ex.getMessage());
            } finally {
                try {
                    aWriter.close();
                } catch (IOException ex) {
                    System.out.println("[Thread id: " + threadId + "]Write :" + ex.getMessage());
                }
            }
        }
    }
    /**
     * run thread methode
     */
    public void run() {
        connect();
        LDAPSearchResults result = LDAPSearch();
        while (result.hasMoreElements() && connection.isConnected()) {
            byte[] arr = result.getResponseControls()[0].getValue();
            LDAPEntry entry = (LDAPEntry) result.nextElement();
            LDAPAttributeSet attrSet = entry.getAttributeSet();
            Enumeration attrs = attrSet.getAttributes();
            if (entry.getDN().contains("break")) {
                String message = "\n[Thread id: " + threadId + "] " + getDate() + " [BREAK]";
                if(!ldifFormat)
                  write(message);
                System.exit(0);
            } else if (entry.getDN().contains("stop")) {
                try {
                    connection.disconnect();
                    String message = "\n[Thread id: " + threadId + "] " + getDate() + "[STOP]";
                    if(!ldifFormat)
                      write(message);
                    System.exit(0);
                } catch (LDAPException ex) {
                    System.out.println("[Thread id: " + threadId + "]run :" + ex.getLDAPErrorMessage());
                }
            }
            String message = "[Thread id: " + threadId + "] " + getDate() + " [" + controlName(arr[4]) + "]";
           if(!ldifFormat)
             write("\n" + message);
           else
             write("\n");
            String dn = "dn: " + entry.getDN();
            write(dn);
            while (attrs.hasMoreElements()) {
                LDAPAttribute attr = (LDAPAttribute) attrs.nextElement();
                String name = attr.getName();
                Enumeration values = attr.getStringValues();
                while (values.hasMoreElements()) {
                    String attribute = name + ": " + values.nextElement();
                    write(attribute);
                }
            }
        }
        if (!connection.isConnected()) {
            String message = "\n[Thread id: " + threadId + "] " + getDate() + "[CONNECTION CLOSE]";
            write(message);
        }
    }
}