You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2006/05/21 14:18:42 UTC
svn commit: r408158 -
/directory/branches/elecharny/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java
Author: elecharny
Date: Sun May 21 05:18:42 2006
New Revision: 408158
URL: http://svn.apache.org/viewvc?rev=408158&view=rev
Log:
Added the Change modification
Modified:
directory/branches/elecharny/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java
Modified: directory/branches/elecharny/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java
URL: http://svn.apache.org/viewvc/directory/branches/elecharny/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java?rev=408158&r1=408157&r2=408158&view=diff
==============================================================================
--- directory/branches/elecharny/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java (original)
+++ directory/branches/elecharny/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java Sun May 21 05:18:42 2006
@@ -30,6 +30,7 @@
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import org.apache.commons.cli.CommandLine;
@@ -44,7 +45,9 @@
import org.apache.directory.shared.ldap.ldif.Entry;
import org.apache.directory.shared.ldap.ldif.LdifReader;
import org.apache.directory.shared.ldap.name.LdapDN;
+import org.apache.directory.shared.ldap.name.Rdn;
import org.apache.directory.shared.ldap.util.StringTools;
+import org.apache.directory.shared.ldap.codec.LdapConstants;
import org.apache.directory.shared.ldap.codec.LdapDecoder;
import org.apache.directory.shared.ldap.codec.LdapMessage;
import org.apache.directory.shared.ldap.codec.LdapMessageContainer;
@@ -56,6 +59,8 @@
import org.apache.directory.shared.ldap.codec.bind.SimpleAuthentication;
import org.apache.directory.shared.ldap.codec.del.DelRequest;
import org.apache.directory.shared.ldap.codec.extended.ExtendedResponse;
+import org.apache.directory.shared.ldap.codec.modify.ModifyRequest;
+import org.apache.directory.shared.ldap.codec.modifyDn.ModifyDNRequest;
import org.apache.directory.shared.ldap.codec.unbind.UnBindRequest;
import org.apache.directory.shared.ldap.codec.util.LdapResultEnum;
@@ -86,6 +91,9 @@
private String logs;
private boolean ignoreErrors = false;
+
+ private static final int IMPORT_ERROR = -1;
+ private static final int IMPORT_SUCCESS= 0;
/**
* Socket used to connect to the server
@@ -174,11 +182,6 @@
}
}
- // System.out.println( messageResp.toString() );
-
- // System.out.println(
- // ((LdapMessageContainer)ldapMessageContainer).getLdapMessage()
- // );
( (LdapMessageContainer) ldapMessageContainer ).clean();
break;
}
@@ -202,7 +205,7 @@
* @param msgId
* message id number
*/
- private void addEntry( Entry entry, int messageId ) throws IOException, DecoderException, InvalidNameException,
+ private int addEntry( Entry entry, int messageId ) throws IOException, DecoderException, InvalidNameException,
NamingException, EncoderException
{
AddRequest addRequest = new AddRequest();
@@ -218,6 +221,7 @@
addRequest.setEntry( new LdapDN( dn ) );
+ // Copy the attributes
addRequest.initAttributes();
for ( NamingEnumeration attrs = attributes.getAll(); attrs.hasMoreElements(); )
@@ -237,6 +241,8 @@
message.setProtocolOP( addRequest );
message.setMessageId( messageId );
+
+ // Encode and send the addRequest message
ByteBuffer bb = message.encode( null );
bb.flip();
@@ -244,6 +250,7 @@
bb.clear();
+ // Get the response
LdapMessage response = readResponse( bb );
LdapResult result = response.getAddResponse().getLdapResult();
@@ -254,11 +261,15 @@
{
System.out.println( "Add of Entry " + entry.getDn() + " was successful" );
}
+
+ return IMPORT_SUCCESS;
}
else
{
System.err.println( "Add of entry " + entry.getDn() + " failed for the following reasons provided by the server:\n"
+ result.getErrorMessage() );
+
+ return IMPORT_ERROR;
}
}
@@ -267,11 +278,11 @@
* reponse from the LDAP server on the results of the operation.
*
* @param entry
- * The entry to add
+ * The entry to delete
* @param msgId
* message id number
*/
- private void deleteEntry( Entry entry, int messageId ) throws IOException, DecoderException, InvalidNameException,
+ private int deleteEntry( Entry entry, int messageId ) throws IOException, DecoderException, InvalidNameException,
NamingException, EncoderException
{
DelRequest delRequest = new DelRequest();
@@ -283,12 +294,14 @@
System.out.println( "Deleting entry " + dn );
}
- delRequest.setEntry( new LdapDN( dn ) );
+ delRequest.setEntry( new LdapDN( dn ) );
LdapMessage message = new LdapMessage();
message.setProtocolOP( delRequest );
message.setMessageId( messageId );
+
+ // Encode and send the delete request
ByteBuffer bb = message.encode( null );
bb.flip();
@@ -296,6 +309,7 @@
bb.clear();
+ // Get the response
LdapMessage response = readResponse( bb );
LdapResult result = response.getDelResponse().getLdapResult();
@@ -306,15 +320,175 @@
{
System.out.println( "Delete of Entry " + entry.getDn() + " was successful" );
}
+
+ return IMPORT_SUCCESS;
}
else
{
System.err.println( "Delete of entry " + entry.getDn() + " failed for the following reasons provided by the server:\n"
+ result.getErrorMessage() );
+ return IMPORT_ERROR;
}
}
/**
+ * Send the entry to the encoder, then wait for a
+ * reponse from the LDAP server on the results of the operation.
+ *
+ * @param entry
+ * The entry to modify
+ * @param msgId
+ * message id number
+ */
+ private int changeModRDNEntry( Entry entry, int messageId ) throws IOException, DecoderException, InvalidNameException,
+ NamingException, EncoderException
+ {
+ ModifyDNRequest modifyDNRequest = new ModifyDNRequest();
+
+ String dn = entry.getDn();
+
+ if ( isDebugEnabled() )
+ {
+ System.out.println( "Modify DN of entry " + dn );
+ }
+
+ modifyDNRequest.setEntry( new LdapDN( dn ) );
+ modifyDNRequest.setDeleteOldRDN( entry.isDeleteOldRdn() );
+ modifyDNRequest.setNewRDN( new Rdn( entry.getNewRdn() ) );
+
+ if ( StringTools.isEmpty( entry.getNewSuperior() ) == false )
+ {
+ modifyDNRequest.setNewSuperior( new LdapDN( entry.getNewSuperior() ) );
+ }
+
+ LdapMessage message = new LdapMessage();
+
+ message.setProtocolOP( modifyDNRequest );
+ message.setMessageId( messageId );
+
+ // Encode and send the delete request
+ ByteBuffer bb = message.encode( null );
+ bb.flip();
+
+ sendMessage( bb );
+
+ bb.clear();
+
+ // Get the response
+ LdapMessage response = readResponse( bb );
+
+ LdapResult result = response.getModifyDNResponse().getLdapResult();
+
+ if ( result.getResultCode() == LdapResultEnum.SUCCESS )
+ {
+ if ( isDebugEnabled() )
+ {
+ System.out.println( "ModifyDn of Entry " + entry.getDn() + " was successful" );
+ }
+
+ return IMPORT_SUCCESS;
+ }
+ else
+ {
+ System.err.println( "ModifyDn of entry " + entry.getDn() + " failed for the following reasons provided by the server:\n"
+ + result.getErrorMessage() );
+ return IMPORT_ERROR;
+ }
+ }
+
+ /**
+ * Send the entry to the encoder, then wait for a
+ * reponse from the LDAP server on the results of the operation.
+ *
+ * @param entry
+ * The entry to modify
+ * @param msgId
+ * message id number
+ */
+ private int changeModifyEntry( Entry entry, int messageId ) throws IOException, DecoderException, InvalidNameException,
+ NamingException, EncoderException
+ {
+ ModifyRequest modifyRequest = new ModifyRequest();
+
+ String dn = entry.getDn();
+
+ if ( isDebugEnabled() )
+ {
+ System.out.println( "Modify of entry " + dn );
+ }
+
+ modifyRequest.setObject( new LdapDN( dn ) );
+ modifyRequest.initModifications();
+
+ Iterator modifications = entry.getModificationItems().iterator();
+
+ while ( modifications.hasNext() )
+ {
+ ModificationItem modification = (ModificationItem)modifications.next();
+
+ switch ( modification.getModificationOp() )
+ {
+ case DirContext.ADD_ATTRIBUTE :
+ modifyRequest.setCurrentOperation( LdapConstants.OPERATION_ADD );
+ break;
+
+ case DirContext.REMOVE_ATTRIBUTE :
+ modifyRequest.setCurrentOperation( LdapConstants.OPERATION_DELETE );
+ break;
+
+ case DirContext.REPLACE_ATTRIBUTE :
+ modifyRequest.setCurrentOperation( LdapConstants.OPERATION_REPLACE );
+ break;
+
+ default :
+ System.err.println( "Unknown modify operation for DN " + dn );
+ }
+
+ modifyRequest.addAttributeTypeAndValues( modification.getAttribute().getID() );
+
+ for ( NamingEnumeration values = modification.getAttribute().getAll(); values.hasMoreElements(); )
+ {
+ Object value = values.nextElement();
+ modifyRequest.addAttributeValue( value );
+ }
+ }
+
+ LdapMessage message = new LdapMessage();
+
+ message.setProtocolOP( modifyRequest );
+ message.setMessageId( messageId );
+
+ // Encode and send the delete request
+ ByteBuffer bb = message.encode( null );
+ bb.flip();
+
+ sendMessage( bb );
+
+ bb.clear();
+
+ // Get the response
+ LdapMessage response = readResponse( bb );
+
+ LdapResult result = response.getModifyResponse().getLdapResult();
+
+ if ( result.getResultCode() == LdapResultEnum.SUCCESS )
+ {
+ if ( isDebugEnabled() )
+ {
+ System.out.println( "Modify of Entry " + entry.getDn() + " was successful" );
+ }
+
+ return IMPORT_SUCCESS;
+ }
+ else
+ {
+ System.err.println( "Modify of entry " + entry.getDn() + " failed for the following reasons provided by the server:\n"
+ + result.getErrorMessage() );
+ return IMPORT_ERROR;
+ }
+ }
+
+ /**
* Send the change operation to the encoder, then wait for a
* reponse from the LDAP server on the results of the operation.
*
@@ -323,32 +497,27 @@
* @param msgId
* message id number
*/
- private void changeEntry( Entry entry, int messageId ) throws IOException, DecoderException, InvalidNameException,
+ private int changeEntry( Entry entry, int messageId ) throws IOException, DecoderException, InvalidNameException,
NamingException, EncoderException
{
switch ( entry.getChangeType() )
{
case Entry.ADD :
// No difference with the injection of new entries
- addEntry( entry, messageId );
- break;
+ return addEntry( entry, messageId );
case Entry.DELETE :
- deleteEntry( entry, messageId );
- break;
+ return deleteEntry( entry, messageId );
case Entry.MODIFY :
- //changeModifyEntry( entry, messageId );
- break;
+ return changeModifyEntry( entry, messageId );
case Entry.MODDN :
- //changeModDNEntry( entry, messageId );
- break;
-
case Entry.MODRDN :
- //changeModRDNEntry( entry, messageId );
- break;
+ return changeModRDNEntry( entry, messageId );
+ default :
+ return IMPORT_ERROR;
}
}
@@ -375,6 +544,8 @@
message.setProtocolOP( bindRequest );
message.setMessageId( messageId );
+
+ // Encode and send the bind request
ByteBuffer bb = message.encode( null );
bb.flip();
@@ -383,6 +554,7 @@
bb.clear();
+ // Get the bind response
LdapMessage response = readResponse( bb );
LdapResult result = response.getBindResponse().getLdapResult();
@@ -423,7 +595,11 @@
bb.flip();
sendMessage( bb );
- System.out.println( "Unbinding of user " + user + " was successful" );
+
+ if ( isDebugEnabled() )
+ {
+ System.out.println( "Unbinding of user " + user + " was successful" );
+ }
}
/**
@@ -470,7 +646,29 @@
{
Entry entry = (Entry) entries.next();
- addEntry( entry, messageId++ );
+ // Check if we have had some error, has next() does not throw any exception
+ if ( ldifReader.hasError() )
+ {
+ System.err.println( "Found an error while persing an entry : " + ldifReader.getError().getMessage() );
+
+ if ( ignoreErrors == false )
+ {
+ unbind( messageId );
+
+ System.err.println( "Import failed..." );
+ System.exit( 1 );
+ }
+ }
+
+ if ( ( addEntry( entry, messageId++ ) == IMPORT_ERROR ) &&
+ ( ignoreErrors == false ) )
+ {
+ unbind( messageId );
+
+ System.err.println( "Import failed..." );
+ System.exit( 1 );
+ }
+
nbAdd++;
if ( nbAdd % 10 == 0 )
@@ -494,30 +692,52 @@
// Parse the file and inject every modification
Iterator entries = ldifReader.iterator();
long t0 = System.currentTimeMillis();
- int nbAdd = 0;
+ int nbMod = 0;
while ( entries.hasNext() )
{
Entry entry = (Entry) entries.next();
+
+ // Check if we have had some error, has next() does not throw any exception
+ if ( ldifReader.hasError() )
+ {
+ System.err.println( "Found an error while persing an entry : " + ldifReader.getError().getMessage() );
+
+ if ( ignoreErrors == false )
+ {
+ unbind( messageId );
+
+ System.err.println( "Import failed..." );
+ System.exit( 1 );
+ }
+ }
- changeEntry( entry, messageId++ );
- nbAdd++;
+ if ( ( changeEntry( entry, messageId++ ) == IMPORT_ERROR ) &&
+ ( ignoreErrors == false ) )
+ {
+ unbind( messageId );
+
+ System.err.println( "Import failed..." );
+ System.exit( 1 );
+ }
- if ( nbAdd % 10 == 0 )
+ nbMod++;
+
+ if ( nbMod % 10 == 0 )
{
System.out.print( '.' );
}
- if ( nbAdd % 500 == 0 )
+ if ( nbMod % 500 == 0 )
{
- System.out.println( nbAdd );
+ System.out.println( nbMod );
}
}
long t1 = System.currentTimeMillis();
System.out.println( "Done!" );
- System.out.println( nbAdd + " users changed in " + ( ( t1 - t0 ) / 1000 ) + " seconds" );
+ System.out.println( nbMod + " users changed in " + ( ( t1 - t0 ) / 1000 ) + " seconds" );
}
// Logout to the server