directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r325856 - in /directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol: ExtendedOperationHandler.java LdapProtocolProvider.java support/ExtendedHandler.java
Date Mon, 17 Oct 2005 07:47:24 GMT
Author: trustin
Date: Mon Oct 17 00:47:18 2005
New Revision: 325856

URL: http://svn.apache.org/viewcvs?rev=325856&view=rev
Log:
Implemented an extension mechanism for LDAP extended operations.

Modified:
    directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/ExtendedOperationHandler.java
    directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/LdapProtocolProvider.java
    directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java

Modified: directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/ExtendedOperationHandler.java
URL: http://svn.apache.org/viewcvs/directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/ExtendedOperationHandler.java?rev=325856&r1=325855&r2=325856&view=diff
==============================================================================
--- directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/ExtendedOperationHandler.java
(original)
+++ directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/ExtendedOperationHandler.java
Mon Oct 17 00:47:18 2005
@@ -18,9 +18,9 @@
  */
 package org.apache.ldap.server.protocol;
 
-import javax.naming.ldap.ExtendedResponse;
-
 import org.apache.ldap.common.message.ExtendedRequest;
+import org.apache.ldap.common.message.ExtendedResponse;
+import org.apache.mina.protocol.ProtocolSession;
 
 /**
  * An extension (hook) point that enables an implementor to provide his or her
@@ -38,12 +38,15 @@
     String getOid();
     
     /**
-     * Handles the specified extended operation and returns its response.
-     * Don't lock the returning response; messageID and resultCode will
+     * Handles the specified extended operation and fills its response to
+     * <tt>res</tt>.  Don't lock the response; messageID and resultCode might
be
      * be filled by caller automatically.
      * 
-     * @param request the LDAP Extended operation
+     * @param session the MINA session object related with current connection
+     * @param req the LDAP Extended operation request
+     * @param res the LDAP Extended operation response you have to fill its payload
+     * 
      * @throws Exception if failed to handle the operation
      */
-    ExtendedResponse handleExtendedOperation( ExtendedRequest request ) throws Exception;
+    void handleExtendedOperation( ProtocolSession session, SessionRegistry registry, ExtendedRequest
req, ExtendedResponse res ) throws Exception;
 }

Modified: directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/LdapProtocolProvider.java
URL: http://svn.apache.org/viewcvs/directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/LdapProtocolProvider.java?rev=325856&r1=325855&r2=325856&view=diff
==============================================================================
--- directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/LdapProtocolProvider.java
(original)
+++ directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/LdapProtocolProvider.java
Mon Oct 17 00:47:18 2005
@@ -61,7 +61,6 @@
 import org.apache.ldap.server.protocol.support.ModifyHandler;
 import org.apache.ldap.server.protocol.support.SearchHandler;
 import org.apache.ldap.server.protocol.support.UnbindHandler;
-import org.apache.mina.common.IdleStatus;
 import org.apache.mina.protocol.ProtocolCodecFactory;
 import org.apache.mina.protocol.ProtocolDecoder;
 import org.apache.mina.protocol.ProtocolEncoder;
@@ -84,7 +83,7 @@
     public static final String SERVICE_NAME = "ldap";
 
     /** a map of the default request object class name to the handler class name */
-    public static final Map DEFAULT_HANDLERS;
+    private static final Map DEFAULT_HANDLERS;
 
     static
     {
@@ -144,7 +143,7 @@
     // ------------------------------------------------------------------------
 
     /**
-     * Creates a SEDA LDAP protocol provider.
+     * Creates a MINA LDAP protocol provider.
      *
      * @param env environment properties used to configure the provider and
      * underlying codec providers if any
@@ -188,7 +187,7 @@
             {
                 Class typeClass = Class.forName( type );
                 handler = ( MessageHandler ) clazz.newInstance();
-                this.handler.registerMessageType( typeClass, handler );
+                this.handler.addMessageHandler( typeClass, handler );
             }
             catch( Exception e )
             {
@@ -205,7 +204,7 @@
     }
 
     /**
-     * Creates a SEDA LDAP protocol provider.
+     * Creates a MINA LDAP protocol provider.
      */
     public LdapProtocolProvider() throws LdapNamingException
     {
@@ -225,7 +224,7 @@
             {
                 Class typeClass = Class.forName( type );
                 handler = ( MessageHandler ) clazz.newInstance();
-                this.handler.registerMessageType( typeClass, handler );
+                this.handler.addMessageHandler( typeClass, handler );
             }
             catch( Exception e )
             {
@@ -259,7 +258,46 @@
     {
         return handler;
     }
-
+    
+    /**
+     * Registeres the specified {@link ExtendedOperationHandler} to this
+     * protocol provider to provide a specific LDAP extended operation.
+     */
+    public void addExtendedOperationHandler( ExtendedOperationHandler eoh )
+    {
+        ExtendedHandler eh = ( ExtendedHandler ) handler.getMessageHandler( ExtendedRequest.class
);
+        eh.addHandler( eoh );
+    }
+    
+    /**
+     * Deregisteres an {@link ExtendedOperationHandler} with the specified <tt>oid</tt>
+     * from this protocol provider.
+     */
+    public void removeExtendedOperationHandler( String oid )
+    {
+        ExtendedHandler eh = ( ExtendedHandler ) handler.getMessageHandler( ExtendedRequest.class
);
+        eh.removeHandler( oid );
+    }
+    
+    /**
+     * Returns an {@link ExtendedOperationHandler} with the specified <tt>oid</tt>
+     * which is registered to this protocol provider.
+     */
+    public ExtendedOperationHandler getExtendedOperationHandler( String oid )
+    {
+        ExtendedHandler eh = ( ExtendedHandler ) handler.getMessageHandler( ExtendedRequest.class
);
+        return eh.getHandler( oid );
+    }
+    
+    /**
+     * Returns a {@link Map} of all registered OID-{@link ExtendedOperationHandler}
+     * pairs.
+     */
+    public Map getExtendedOperationHandlerMap()
+    {
+        ExtendedHandler eh = ( ExtendedHandler ) handler.getMessageHandler( ExtendedRequest.class
);
+        return eh.getHandlerMap();
+    }
     /**
      * A snickers based BER Decoder factory.
      */
@@ -305,20 +343,6 @@
 
     private class LdapProtocolHandler extends DemuxingProtocolHandler
     {
-        private LdapProtocolHandler()
-        {
-        }
-
-        public void deregisterMessageType( Class arg0 )
-        {
-            super.deregisterMessageType( arg0 );
-        }
-
-        public void registerMessageType( Class arg0, MessageHandler arg1 )
-        {
-            super.registerMessageType( arg0, arg1 );
-        }
-
         public void sessionClosed( ProtocolSession session )
         {
             SessionRegistry.getSingleton().remove( session );
@@ -327,22 +351,6 @@
         public void exceptionCaught( ProtocolSession session, Throwable cause )
         {
             cause.printStackTrace();
-        }
-
-        public void messageSent( ProtocolSession arg0, Object arg1 )
-        {
-        }
-
-        public void sessionIdle( ProtocolSession arg0, IdleStatus arg1 )
-        {
-        }
-
-        public void sessionOpened( ProtocolSession arg0 )
-        {
-        }
-
-        public void sessionCreated( ProtocolSession arg0 )
-        {
         }
     }
     

Modified: directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java
URL: http://svn.apache.org/viewcvs/directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java?rev=325856&r1=325855&r2=325856&view=diff
==============================================================================
--- directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java
(original)
+++ directory/protocol-providers/ldap/trunk/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java
Mon Oct 17 00:47:18 2005
@@ -17,12 +17,19 @@
 package org.apache.ldap.server.protocol.support;
 
 
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.ldap.common.message.ExtendedRequest;
 import org.apache.ldap.common.message.ExtendedResponse;
 import org.apache.ldap.common.message.ExtendedResponseImpl;
 import org.apache.ldap.common.message.LdapResult;
 import org.apache.ldap.common.message.LdapResultImpl;
 import org.apache.ldap.common.message.ResultCodeEnum;
+import org.apache.ldap.common.util.ExceptionUtils;
+import org.apache.ldap.server.protocol.ExtendedOperationHandler;
+import org.apache.ldap.server.protocol.SessionRegistry;
 import org.apache.mina.protocol.ProtocolSession;
 import org.apache.mina.protocol.handler.MessageHandler;
 
@@ -34,20 +41,74 @@
  */
 public class ExtendedHandler implements MessageHandler
 {
+    private Map handlers = new HashMap();
+    
+    public ExtendedOperationHandler addHandler( ExtendedOperationHandler eoh )
+    {
+        synchronized( handlers )
+        {
+            return ( ExtendedOperationHandler ) handlers.put( eoh.getOid(), eoh );
+        }
+    }
+    
+    public ExtendedOperationHandler removeHandler( String oid )
+    {
+        synchronized( handlers )
+        {
+            return ( ExtendedOperationHandler ) handlers.remove( oid );
+        }
+    }
+    
+    public ExtendedOperationHandler getHandler( String oid )
+    {
+        return ( ExtendedOperationHandler ) handlers.get( oid );
+    }
+    
+    public Map getHandlerMap()
+    {
+        return Collections.unmodifiableMap( handlers );
+    }
+    
     public void messageReceived( ProtocolSession session, Object request )
     {
         ExtendedRequest req = ( ExtendedRequest ) request;
-        ExtendedResponse resp = new ExtendedResponseImpl( req.getMessageId() );
-        resp.setLdapResult( new LdapResultImpl( resp ) );
-        
-        // As long as no extended operations are implemented, send appropriate
-        // error back to the client.        
-        String msg = "Unrecognized extended operation";
-        
-        LdapResult result = resp.getLdapResult();        
-        result.setResultCode( ResultCodeEnum.PROTOCOLERROR );
-        result.setErrorMessage( msg );
+        ExtendedResponse res = new ExtendedResponseImpl( req.getMessageId() );
+
+        ExtendedOperationHandler handler = ( ExtendedOperationHandler ) handlers.get( req.getOid()
);
+        if( handler == null )
+        {
+            res.setLdapResult( new LdapResultImpl( res ) );
+            // As long as no extended operations are implemented, send appropriate
+            // error back to the client.        
+            String msg = "Unrecognized extended operation OID: " + req.getOid();
+            
+            LdapResult result = res.getLdapResult();        
+            result.setResultCode( ResultCodeEnum.PROTOCOLERROR );
+            result.setErrorMessage( msg );
+        }
+        else
+        {
+            try
+            {
+                handler.handleExtendedOperation(
+                        session,
+                        SessionRegistry.getSingleton(),
+                        req,
+                        res );
+            }
+            catch( Exception e )
+            {
+                LdapResult result = new LdapResultImpl( res );
+                result.setResultCode( ResultCodeEnum.UNAVAILABLE );
+                result.setErrorMessage(
+                        "Extended operation handler for the specified OID (" +
+                        req.getOid() + ") has failed to process your request:\n" +
+                        ExceptionUtils.getStackTrace( e ) );
+                res.setLdapResult( result );
+                res.setResponse( new byte[ 0 ] );
+            }
+        }
         
-        session.write( resp );
+        session.write( res );
     }
 }



Mime
View raw message