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/16 01:01:40 UTC
svn commit: r406765 -
/directory/branches/elecharny/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java
Author: elecharny
Date: Mon May 15 16:01:39 2006
New Revision: 406765
URL: http://svn.apache.org/viewcvs?rev=406765&view=rev
Log:
Updated the Import command. Bind and Unbind are ok, Add has been adde d(not fully functionnal though ...)
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/viewcvs/directory/branches/elecharny/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java?rev=406765&r1=406764&r2=406765&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 Mon May 15 16:01:39 2006
@@ -16,14 +16,9 @@
*/
package org.apache.directory.server.tools;
-import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.InetAddress;
import java.net.InetSocketAddress;
-import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
@@ -35,6 +30,7 @@
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
+import javax.naming.directory.ModificationItem;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
@@ -59,6 +55,7 @@
import org.apache.directory.shared.ldap.codec.bind.LdapAuthentication;
import org.apache.directory.shared.ldap.codec.bind.SimpleAuthentication;
import org.apache.directory.shared.ldap.codec.extended.ExtendedResponse;
+import org.apache.directory.shared.ldap.codec.unbind.UnBindRequest;
import org.apache.directory.shared.ldap.codec.util.LdapResultEnum;
/**
@@ -94,22 +91,10 @@
*/
private SocketChannel channel;
private SocketAddress serverAddress;
- private OutputStream serverOut;
- private InputStream serverIn;
private IAsn1Container ldapMessageContainer = new LdapMessageContainer();
private Asn1Decoder ldapDecoder = new LdapDecoder();
/**
- * Stream used to talk to the server
- */
- private InputStream in = null;
-
- /**
- * Stream used to read from the server
- */
- private OutputStream out = null;
-
- /**
* The constructor save the command's name into it's super class
*
*/
@@ -127,7 +112,7 @@
* @throws IOException
* There was a error opening or establishing the socket
*/
- public void connect() throws UnknownHostException, IOException {
+ private void connect() throws UnknownHostException, IOException {
serverAddress = new InetSocketAddress( host, port );
channel = SocketChannel.open( serverAddress );
channel.configureBlocking( true );
@@ -197,22 +182,6 @@
}
- private LdapMessage decode(ByteBuffer buffer) throws DecoderException {
- Asn1Decoder ldapDecoder = new LdapDecoder();
-
- // Allocate a LdapMessageContainer Container
- IAsn1Container ldapMessageContainer = new LdapMessageContainer();
-
- // Decode the PDU
- ldapDecoder.decode(buffer, ldapMessageContainer);
-
- // Check that everything is OK
- LdapMessage ldapMessage = ((LdapMessageContainer) ldapMessageContainer)
- .getLdapMessage();
-
- return ldapMessage;
- }
-
/**
* Prepare the LDIF entry and send it to the encoder, then wait for a
* reponse from the LDAP server on the results of the operation.
@@ -222,24 +191,32 @@
* @param msgId
* message id number
*/
- private void add(Entry entry, int messageId) throws IOException,
+ private void addEntry( Entry entry, int messageId ) throws IOException,
DecoderException, InvalidNameException, NamingException,
- EncoderException {
+ EncoderException
+ {
AddRequest addRequest = new AddRequest();
String dn = entry.getDn();
+
+ if ( isDebugEnabled() )
+ {
+ System.out.println( "Adding entry " + dn );
+ }
+
Attributes attributes = entry.getAttributes();
- addRequest.setEntry(new LdapDN(dn));
+ addRequest.setEntry( new LdapDN( dn ) );
- for (NamingEnumeration attrs = attributes.getAll(); attrs
- .hasMoreElements();) {
+ addRequest.initAttributes();
+
+ for (NamingEnumeration attrs = attributes.getAll(); attrs.hasMoreElements();) {
Attribute attribute = (Attribute) attrs.nextElement();
- addRequest.addAttributeType(attribute.getID());
+ addRequest.addAttributeType( attribute.getID() );
- for (NamingEnumeration values = attribute.getAll(); values
- .hasMoreElements();) {
+ for (NamingEnumeration values = attribute.getAll(); values.hasMoreElements();)
+ {
Object value = values.nextElement();
addRequest.addAttributeValue(value);
}
@@ -273,7 +250,7 @@
}
}
- private void bind(int messageId) throws InvalidNameException, EncoderException,
+ private void bind( int messageId ) throws InvalidNameException, EncoderException,
DecoderException, IOException
{
BindRequest bindRequest = new BindRequest();
@@ -302,7 +279,7 @@
LdapMessage response = readResponse( bb );
- LdapResult result = response.getAddResponse().getLdapResult();
+ LdapResult result = response.getBindResponse().getLdapResult();
if (result.getResultCode() == LdapResultEnum.SUCCESS) {
if (isDebugEnabled()) {
@@ -315,10 +292,54 @@
+ user
+ " failed for the following reasons provided by the server:\n"
+ result.getErrorMessage());
+ System.exit( 1 );
}
}
/**
+ * Unbind from the server
+ * @param messageId The message Id
+ * @throws InvalidNameException
+ * @throws EncoderException
+ * @throws DecoderException
+ * @throws IOException
+ */
+ private void unbind( int messageId ) throws InvalidNameException, EncoderException,
+ DecoderException, IOException
+ {
+ UnBindRequest unbindRequest = new UnBindRequest();
+ LdapMessage message = new LdapMessage();
+
+ message.setProtocolOP( unbindRequest );
+ message.setMessageId( messageId );
+ ByteBuffer bb = message.encode(null);
+ bb.flip();
+
+ connect();
+ sendMessage(bb);
+
+ bb.clear();
+
+ LdapMessage response = readResponse( bb );
+
+ LdapResult result = response.getAddResponse().getLdapResult();
+
+ if (result.getResultCode() == LdapResultEnum.SUCCESS) {
+ if (isDebugEnabled()) {
+ System.out.println("Unbinding of user " + user
+ + " was successful");
+ }
+ } else {
+ System.err
+ .println("Uninding of user "
+ + user
+ + " failed for the following reasons provided by the server:\n"
+ + result.getErrorMessage());
+ System.exit( 1 );
+ }
+ }
+
+ /**
* Execute the command
*
* @param cmd
@@ -347,14 +368,33 @@
+ "Importing data ... ");
}
- LdifReader ldifReader = new LdifReader(ldifFile);
-
- // Parse the file and inject every entri or every modification
- Iterator entries = ldifReader.iterator();
+ LdifReader ldifReader = new LdifReader( ldifFile );
+
+ if ( ldifReader.containsEntries() )
+ {
+ // Parse the file and inject every entri or every modification
+ Iterator entries = ldifReader.iterator();
- while (entries.hasNext()) {
+ while ( entries.hasNext() ) {
+ Entry entry = (Entry)entries.next();
+
+ addEntry( entry, messageId++ );
+ }
+ }
+ else
+ {
+ // Parse the file and inject every entri or every modification
+ Iterator changes = ldifReader.iterator();
+ while ( changes.hasNext() ) {
+ ModificationItem modification = (ModificationItem)changes.next();
+
+ //addchange( modification, messageId++ );
+ }
}
+
+ // Logout to the server
+ unbind( messageId++ );
}