4710N/A/*
4710N/A * CDDL HEADER START
4710N/A *
4710N/A * The contents of this file are subject to the terms of the
4710N/A * Common Development and Distribution License, Version 1.0 only
4710N/A * (the "License"). You may not use this file except in compliance
4710N/A * with the License.
4710N/A *
4710N/A * You can obtain a copy of the license at
4710N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
4710N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
4710N/A * See the License for the specific language governing permissions
4710N/A * and limitations under the License.
4710N/A *
4710N/A * When distributing Covered Code, include this CDDL HEADER in each
4710N/A * file and include the License file at
4710N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
4710N/A * add the following below this CDDL HEADER, with the fields enclosed
4710N/A * by brackets "[]" replaced with your own identifying information:
4710N/A * Portions Copyright [yyyy] [name of copyright owner]
4710N/A *
4710N/A * CDDL HEADER END
4710N/A *
4710N/A *
5065N/A * Copyright 2009-2010 Sun Microsystems, Inc.
4710N/A */
4710N/A
4710N/Aimport java.io.File;
4710N/Aimport java.io.FileWriter;
4710N/Aimport java.io.IOException;
4710N/Aimport java.util.Calendar;
4710N/Aimport java.util.Enumeration;
4710N/Aimport netscape.ldap.LDAPAttribute;
4710N/Aimport netscape.ldap.LDAPAttributeSet;
4710N/Aimport netscape.ldap.LDAPConnection;
4710N/Aimport netscape.ldap.LDAPEntry;
4710N/Aimport netscape.ldap.LDAPException;
4710N/Aimport netscape.ldap.LDAPSearchConstraints;
4710N/Aimport netscape.ldap.LDAPSearchResults;
4710N/Aimport netscape.ldap.LDAPv3;
4710N/Aimport netscape.ldap.controls.LDAPPersistSearchControl;
4710N/A
4710N/A
4710N/Apublic class PSearchOperations extends Thread {
4710N/A
4710N/A public static final String ADD = "ADD";
4710N/A public static final String MODIFY = "MODIFY";
4710N/A public static final String DELETE = "DELETE";
4710N/A public static final String MODDN = "MODDN";
4710N/A public static final String ALL = "ALL";
4710N/A private LDAPConnection connection;
4710N/A private String hostname;
4710N/A private int portnumber;
4710N/A private String bindDN;
4710N/A private String bindPW;
4710N/A private String suffix;
4710N/A private int threadId;
4710N/A private String fileName;
4710N/A private boolean output;
5065N/A private boolean ldifFormat;
4710N/A private boolean logFile;
4710N/A private String operation;
4710N/A /**
4710N/A * constructor
4710N/A * @param id
4710N/A * @param hostname
4710N/A * @param portnumber
4710N/A * @param bindDN
4710N/A * @param bindPW
4710N/A * @param suffix
4710N/A */
4710N/A public PSearchOperations(int id, String hostname, int portnumber, String bindDN, String bindPW, String suffix) {
4710N/A this.hostname = hostname;
4710N/A this.portnumber = portnumber;
4710N/A this.bindDN = bindDN;
4710N/A this.bindPW = bindPW;
4710N/A this.suffix = suffix;
4710N/A this.threadId = id;
4710N/A this.output = false;
4710N/A this.logFile = false;
5065N/A this.ldifFormat = false;
4710N/A //by default all operation
4710N/A this.operation = ALL;
4710N/A
4710N/A }
4710N/A /**
4710N/A * to use systeme.out
4710N/A * @param output boolean
4710N/A */
4710N/A public void setOutput(boolean output) {
4710N/A this.output = output;
4710N/A }
4710N/A /**
4710N/A * to use the log file
4710N/A * @param logFile boolean
4710N/A */
4710N/A public void useLogFile(boolean logFile) {
4710N/A this.logFile = logFile;
4710N/A }
4710N/A /**
4710N/A * to define the log file and URI
4710N/A * @param file String
4710N/A */
4710N/A public void setLogFile(String file) {
4710N/A //if there one thread the thread id are not add in the file name
4710N/A this.fileName = file;
4710N/A //in multy thread for each thread the thread id are add in the file name
4710N/A if (threadId!=0) {
4710N/A String ext = file.substring(file.lastIndexOf("."), file.length());
4710N/A this.fileName = file.substring(0, file.lastIndexOf(".")) + threadId + ext;
4710N/A }
4710N/A //delete old log file if logFile is present and enable
4710N/A File fileToDelete = new File(fileName);
4710N/A if (fileToDelete.isFile() && logFile) {
4710N/A fileToDelete.delete();
4710N/A }
4710N/A }
4710N/A /**
4710N/A * to define the PSearch operation
4710N/A * @param operation String
4710N/A */
4710N/A public void setOperation(String operation) {
4710N/A this.operation = operation;
4710N/A }
4710N/A
5065N/A public void setLdifFormat(boolean ldifFormat) {
5065N/A this.ldifFormat = ldifFormat;
5065N/A }
5065N/A
4710N/A /**
4710N/A *Connect to server.
4710N/A */
4710N/A private void connect() {
4710N/A try {
4710N/A connection = new LDAPConnection();
4710N/A connection.connect(3, hostname, portnumber, "", "");
4710N/A connection.authenticate(3, bindDN, bindPW);
5065N/A if(!ldifFormat)
5065N/A write("[Thread id: " + threadId + "] \n" + getDate() + connection);
4710N/A } catch (LDAPException ex) {
5098N/A System.out.println("[Thread id: " + threadId + "] Connection :" + ex.getMessage());
5098N/A System.exit(1);
4710N/A }
4710N/A }
4710N/A /**
4710N/A * to instanciate new LDAPPersistSearchControl
4710N/A * @return LDAPPersistSearchControl
4710N/A */
4710N/A private LDAPPersistSearchControl PSearchControl() {
4710N/A int op = 0;
4710N/A if (operation.equals(ALL)) {
4710N/A op = LDAPPersistSearchControl.ADD |
4710N/A LDAPPersistSearchControl.MODIFY |
4710N/A LDAPPersistSearchControl.DELETE |
4710N/A LDAPPersistSearchControl.MODDN;
4710N/A } else if (operation.equals(ADD)) {
4710N/A op = LDAPPersistSearchControl.ADD;
4710N/A } else if (operation.equals(MODIFY)) {
4710N/A op = LDAPPersistSearchControl.MODIFY;
4710N/A } else if (operation.equals(DELETE)) {
4710N/A op = LDAPPersistSearchControl.DELETE;
4710N/A } else if (operation.equals(MODDN)) {
4710N/A op = LDAPPersistSearchControl.MODDN;
4710N/A }
4710N/A
4710N/A boolean changesOnly = true;
4710N/A boolean returnControls = true;
4710N/A boolean isCritical = true;
4710N/A
4710N/A LDAPPersistSearchControl persistCtrl =
4710N/A new LDAPPersistSearchControl(
4710N/A op,
4710N/A changesOnly,
4710N/A returnControls,
4710N/A isCritical);
4710N/A return persistCtrl;
4710N/A }
4710N/A /**
4710N/A * LDAP Search
4710N/A * @return LDAPSearchResults
4710N/A */
4710N/A public LDAPSearchResults LDAPSearch() {
4710N/A LDAPSearchResults res = null;
4710N/A try {
4710N/A LDAPPersistSearchControl persistCtrl = PSearchControl();
4710N/A LDAPSearchConstraints cons = connection.getSearchConstraints();
4710N/A cons.setBatchSize(1);
4710N/A cons.setServerControls(persistCtrl);
4710N/A // Start the persistent search.
4710N/A res = connection.search(suffix, LDAPv3.SCOPE_SUB, "(objectclass=*)", null, false, cons);
4710N/A } catch (LDAPException ex) {
5098N/A System.out.println("[Thread id: " + threadId + "] LDAPSearch :" + ex.getMessage());
5098N/A System.exit(1);
4710N/A }
4710N/A return res;
4710N/A }
4710N/A /**
4710N/A * return the date and time
4710N/A * @return String
4710N/A */
4710N/A public static String getDate() {
4710N/A // Initialize the today's date string
4710N/A String DATE_FORMAT = "yyyy/MM/dd:HH:mm:ss";
4710N/A java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
4710N/A Calendar c1 = Calendar.getInstance(); // today
4710N/A return ("[" + sdf.format(c1.getTime()) + "]");
4710N/A }
4710N/A /**
4710N/A *
4710N/A * @param b byte off control operation
4710N/A * @return
4710N/A */
4710N/A public String controlName(byte b) {
4710N/A String control;
4710N/A switch (b) {
4710N/A case LDAPPersistSearchControl.ADD:
4710N/A control = "ADD";
4710N/A break;
4710N/A case LDAPPersistSearchControl.DELETE:
4710N/A control = "DELETE";
4710N/A break;
4710N/A case LDAPPersistSearchControl.MODDN:
4710N/A control = "MODDN";
4710N/A break;
4710N/A case LDAPPersistSearchControl.MODIFY:
4710N/A control = "MODIFY";
4710N/A break;
4710N/A default:
4710N/A control = String.valueOf(b);
4710N/A break;
4710N/A }
4710N/A return control;
4710N/A }
4710N/A /**
4710N/A * to write on the log file or to use syteme out
4710N/A * @param msg String
4710N/A */
4710N/A public void write(String msg) {
4710N/A if (output) {
4710N/A System.out.println(msg);
4710N/A }
4710N/A if (logFile) {
4710N/A FileWriter aWriter = null;
4710N/A try {
4710N/A aWriter = new FileWriter(fileName, true);
4710N/A aWriter.write(msg + "\n");
4710N/A aWriter.flush();
4710N/A aWriter.close();
4710N/A } catch (IOException ex) {
4710N/A System.out.println("[Thread id: " + threadId + "]Write :" + ex.getMessage());
4710N/A } finally {
4710N/A try {
4710N/A aWriter.close();
4710N/A } catch (IOException ex) {
4710N/A System.out.println("[Thread id: " + threadId + "]Write :" + ex.getMessage());
4710N/A }
4710N/A }
4710N/A }
4710N/A }
4710N/A /**
4710N/A * run thread methode
4710N/A */
4710N/A public void run() {
4710N/A connect();
4710N/A LDAPSearchResults result = LDAPSearch();
4710N/A while (result.hasMoreElements() && connection.isConnected()) {
4710N/A byte[] arr = result.getResponseControls()[0].getValue();
4710N/A LDAPEntry entry = (LDAPEntry) result.nextElement();
4710N/A LDAPAttributeSet attrSet = entry.getAttributeSet();
4710N/A Enumeration attrs = attrSet.getAttributes();
4710N/A if (entry.getDN().contains("break")) {
4710N/A String message = "\n[Thread id: " + threadId + "] " + getDate() + " [BREAK]";
5065N/A if(!ldifFormat)
5065N/A write(message);
4710N/A System.exit(0);
4710N/A } else if (entry.getDN().contains("stop")) {
4710N/A try {
4710N/A connection.disconnect();
4710N/A String message = "\n[Thread id: " + threadId + "] " + getDate() + "[STOP]";
5065N/A if(!ldifFormat)
5065N/A write(message);
4710N/A System.exit(0);
4710N/A } catch (LDAPException ex) {
4710N/A System.out.println("[Thread id: " + threadId + "]run :" + ex.getLDAPErrorMessage());
4710N/A }
4710N/A }
4710N/A String message = "[Thread id: " + threadId + "] " + getDate() + " [" + controlName(arr[4]) + "]";
5065N/A if(!ldifFormat)
5065N/A write("\n" + message);
5065N/A else
5065N/A write("\n");
4710N/A String dn = "dn: " + entry.getDN();
4710N/A write(dn);
4710N/A while (attrs.hasMoreElements()) {
4710N/A LDAPAttribute attr = (LDAPAttribute) attrs.nextElement();
4710N/A String name = attr.getName();
4710N/A Enumeration values = attr.getStringValues();
4710N/A while (values.hasMoreElements()) {
4710N/A String attribute = name + ": " + values.nextElement();
4710N/A write(attribute);
4710N/A }
4710N/A }
4710N/A }
4710N/A if (!connection.isConnected()) {
4710N/A String message = "\n[Thread id: " + threadId + "] " + getDate() + "[CONNECTION CLOSE]";
4710N/A write(message);
4710N/A }
4710N/A }
4710N/A}