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 2005/09/03 14:54:49 UTC

svn commit: r267455 - /directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixEncoder.java

Author: elecharny
Date: Sat Sep  3 05:54:45 2005
New Revision: 267455

URL: http://svn.apache.org/viewcvs?rev=267455&view=rev
Log:
- Added Javadoco
- Added logs
- Deprecated setEncodeMonitor

Modified:
    directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixEncoder.java

Modified: directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixEncoder.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixEncoder.java?rev=267455&r1=267454&r2=267455&view=diff
==============================================================================
--- directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixEncoder.java (original)
+++ directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixEncoder.java Sat Sep  3 05:54:45 2005
@@ -17,14 +17,17 @@
 package org.apache.asn1new.ldap;
 
 import org.apache.asn1.ber.digester.rules.ByteAccumulator;
+import org.apache.asn1.codec.DecoderException;
 import org.apache.asn1.codec.EncoderException;
 import org.apache.asn1.codec.stateful.EncoderCallback;
 import org.apache.asn1.codec.stateful.EncoderMonitor;
 import org.apache.asn1.codec.stateful.StatefulEncoder;
 import org.apache.asn1new.ldap.pojo.LdapMessage;
+import org.apache.asn1new.util.StringUtils;
 import org.apache.ldap.common.message.spi.Provider;
 import org.apache.ldap.common.message.spi.ProviderEncoder;
 import org.apache.ldap.common.message.spi.ProviderException;
+import org.apache.log4j.Logger;
 
 import java.io.IOException;
 import java.io.OutputStream;
@@ -41,12 +44,19 @@
  */
 public class TwixEncoder implements ProviderEncoder
 {
+    /** The logger */
+    private static final Logger log = Logger.getLogger( TwixDecoder.class );
+
+    /** The associated Provider */
     private final Provider provider;
+
+    /** The callback to call when the encoding is done */
     private EncoderCallback encodeCallback;
 
     /**
+     * Creates an instance of a Twix Encoder implementation.
      * 
-     * @param provider
+     * @param provider The associated Provider
      */
     public TwixEncoder( Provider provider )
     {
@@ -55,17 +65,29 @@
     }
 
     /**
+     * Encodes a LdapMessage, and calls the callback.
+     * 
+     * @param lock Not used...
+     * @param out Not used ... 
+     * @param obj The LdapMessage to encode
      * 
+     * @throws ProviderException If anything went wrong
      */
     public void encodeBlocking( Object lock, OutputStream out, Object obj )
             throws ProviderException
     {
         try
         {
+        	if (log.isDebugEnabled())
+        	{
+        		log.debug( "Encoding this LdapMessage : " + obj );
+        	}
+        	
         	encodeCallback.encodeOccurred( null, ( (LdapMessage)obj ).encode( null ) );
         }
         catch ( EncoderException e )
         {
+        	log.error( "Twix encoder failed to encode object: " + obj );
             ProviderException pe = new ProviderException( provider,
                     "Twix encoder failed to encode object: " + obj );
             throw pe;
@@ -73,47 +95,80 @@
     }
 
     /**
+     * Encodes a LdapMessage, and return a ByteBuffer containing the
+     * resulting PDU 
      * 
+     * @param obj The LdapMessage to encode
+     * @return The ByteBuffer containing the PDU
+     * @throws ProviderException If anything went wrong
      */
     public ByteBuffer encodeBlocking( Object obj ) throws ProviderException
     {
         try
         {
-        	return ( (LdapMessage)obj ).encode( null );
+        	if (log.isDebugEnabled())
+        	{
+        		log.debug( "Encoding this LdapMessage : " + obj );
+        	}
+
+        	ByteBuffer pdu = ( (LdapMessage)obj ).encode( null );
+        	
+        	if (log.isDebugEnabled())
+        	{
+        		log.debug( "Encoded PDU : " + StringUtils.dumpBytes( pdu.array() ) );
+        		pdu.flip();
+        	}
+
+        	return pdu;
         }
         catch ( EncoderException e )
         {
+        	log.error( "Twix encoder failed to encode object: " + obj );
             ProviderException pe = new ProviderException( provider,
-                    "Snickers encoder failed to encode object: " + obj );
+                    "Twix encoder failed to encode object: " + obj );
             throw pe;
         }
     }
 
     /**
+     * Encodes a LdapMessage, and return a byte array containing the
+     * resulting PDU 
      * 
-     * @param obj
-     * @return
-     * @throws ProviderException
+     * @param obj The LdapMessage to encode
+     * @return The byte[] containing the PDU
+     * @throws ProviderException If anything went wrong
      */
     public byte[] encodeToArray( Object obj ) throws ProviderException
     {
         try
         {
-        	return ( (LdapMessage)obj ).encode( null ).array();
+        	if (log.isDebugEnabled())
+        	{
+        		log.debug( "Encoding this LdapMessage : " + obj );
+        	}
+        	
+        	byte[] pdu = ( (LdapMessage)obj ).encode( null ).array();
+        	
+        	if (log.isDebugEnabled())
+        	{
+        		log.debug( "Encoded PDU : " + StringUtils.dumpBytes( pdu ) );
+        	}
+
+        	return pdu;
         }
         catch ( EncoderException e )
         {
+        	log.error( "Twix encoder failed to encode object: " + obj );
             ProviderException pe = new ProviderException( provider,
                     "Twix encoder failed to encode object: " + obj );
             throw pe;
         }
     }
 
-
     /**
      * Gets the Provider associated with this SPI implementation object.
      *
-     * @return Provider.
+     * @return Provider The provider
      */
     public Provider getProvider()
     {
@@ -121,7 +176,10 @@
     }
 
     /**
+     * Encodes a LdapMessage, and calls the callback 
      * 
+     * @param obj The LdapMessage to encode
+     * @throws EncoderException If anything went wrong
      */
     public void encode( Object obj ) throws EncoderException
     {
@@ -130,7 +188,9 @@
     }
 
     /**
+     * Set the callback called when the encoding is done.
      * 
+     * @param cb The callback.
      */
     public void setCallback( EncoderCallback cb )
     {
@@ -138,58 +198,20 @@
     }
 
     /**
+     * Not used ...
      * 
+     * @deprecated
      */
     public void setEncoderMonitor( EncoderMonitor monitor )
     {
-        //encoder.setEncoderMonitor( monitor );
-    }
-
-    /**
-     * 
-     */
-    class AccumulatorCallback implements EncoderCallback
-    {
-        ByteAccumulator accumulator = new ByteAccumulator();
-
-        /**
-         * Callback to deliver a fully encoded object.
-         *
-         * @param encoder the stateful encoder driving the callback
-         * @param encoded the object that was encoded
-         */
-        public void encodeOccurred( StatefulEncoder encoder, Object encoded )
-        {
-            if ( encoded instanceof ByteBuffer[] )
-            {
-                ByteBuffer[] buffers = ( ByteBuffer[] ) encoded;
-
-                for ( int ii = 0; ii < buffers.length; ii++ )
-                {
-                    accumulator.fill( buffers[ii] );
-                }
-
-                return;
-            }
-
-            accumulator.fill( ( ByteBuffer ) encoded );
-        }
-
-        /**
-         * 
-         * @return
-         */
-        ByteBuffer getEncoded()
-        {
-            return accumulator.drain();
-        }
     }
 
     /**
-     * 
+     * The inner class used to write the PDU to a channel. 
      */
     class OutputCallback implements EncoderCallback
     {
+    	/** The channel in which the PDU will be written */  
         private WritableByteChannel channel = null;
 
         /**
@@ -213,8 +235,8 @@
         }
 
         /**
-         * 
-         * @param channel
+         * Associate a channel to the callback
+         * @param channel The channel to use to write a PDU
          */
         void attach( WritableByteChannel channel )
         {
@@ -222,8 +244,10 @@
         }
 
         /**
+         * Associate a OutputStream to the callback. A channel
+         * will be created.
          * 
-         * @param out
+         * @param out The OutputStream to use
          */
         void attach( OutputStream out )
         {