You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2005/03/18 16:20:16 UTC

svn commit: r158078 [2/2] - in directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server: exception/ jndi/ jndi/ibs/ jndi/request/ jndi/request/processor/

Added: directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorChain.java
URL: http://svn.apache.org/viewcvs/directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorChain.java?view=auto&rev=158078
==============================================================================
--- directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorChain.java (added)
+++ directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorChain.java Fri Mar 18 07:20:09 2005
@@ -0,0 +1,307 @@
+package org.apache.ldap.server.jndi.request.processor;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.naming.NamingException;
+
+import org.apache.ldap.server.jndi.request.Request;
+
+/**
+ * Manages {@link RequestProcessor} stack.  The first {@link RequestProcessor} is
+ * invoked and then invocation chain starts.
+ * 
+ * TODO imeplement me.
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class RequestProcessorChain
+{
+    private static final RequestProcessor FINAL_PROCESSOR = new RequestProcessor()
+    {
+        public void init(Properties config) throws NamingException
+        {
+            // do nothing
+        }
+
+        public void destroy()
+        {
+            // do nothing
+        }
+
+        public void process(NextRequestProcessor nextProcessor, Request request)
+                throws NamingException
+        {
+            // do nothing
+        }
+    };
+
+    private final Map name2entry = new HashMap();
+    private Entry head = new Entry( null, null, "", FINAL_PROCESSOR );
+    private final Entry tail = head;
+
+    /**
+     * Create a new processor chain.
+     */
+    public RequestProcessorChain()
+    {
+    }
+
+    /**
+     * Returns the processor with the specified <code>name</code>.
+     * 
+     * @return <code>null</code> if there is no processor with the specified
+     *         <code>name</code>.
+     */
+    public RequestProcessor get( String name )
+    {
+        Entry e = ( Entry ) name2entry.get( name );
+        if( e == null )
+        {
+            return null;
+        }
+        return e.processor;
+    }
+
+    /**
+     * Adds the specified processor with the specified name at the beginning
+     * of this chain.
+     */
+    public synchronized void addFirst( String name,
+                                       RequestProcessor processor )
+    {
+        checkNewName( name );
+        
+        Entry newEntry = new Entry( null, head, name, processor );
+        head.prevEntry = newEntry;
+        head = newEntry;
+    }
+
+    /**
+     * Adds the specified processor with the specified name at the end
+     * of this chain.
+     */
+    public synchronized void addLast( String name,
+                                      RequestProcessor processor )
+    {
+        checkNewName( name );
+        
+        Entry newEntry = new Entry( tail.prevEntry, tail, name, processor );
+        tail.prevEntry.nextEntry = newEntry;
+        tail.prevEntry = newEntry;
+    }
+
+    /**
+     * Adds the specified processor with the specified name just before
+     * the processor whose name is <code>baseName</code> in this chain.
+     */
+    public synchronized void addBefore( String baseName,
+                                        String name,
+                                        RequestProcessor processor )
+    {
+        Entry baseEntry = checkOldName( baseName );
+        checkNewName( name );
+
+        Entry prevEntry = baseEntry.prevEntry;
+        Entry newEntry = new Entry( prevEntry, baseEntry, name, processor );
+        if( prevEntry == null )
+        {
+            head = newEntry;
+        }
+        else
+        {
+            prevEntry.nextEntry.prevEntry = newEntry;
+            prevEntry.nextEntry = newEntry;
+        }
+        
+        name2entry.put( name, newEntry );
+    }
+    
+    /**
+     * Adds the specified processor with the specified name just after
+     * the processor whose name is <code>baseName</code> in this chain.
+     */
+    public synchronized void addAfter( String baseName,
+                                       String name,
+                                       RequestProcessor processor )
+    {
+        Entry baseEntry = checkOldName( baseName );
+        checkNewName(name);
+
+        Entry nextEntry = baseEntry.nextEntry;
+        Entry newEntry = new Entry( baseEntry, nextEntry, name, processor );
+        if( nextEntry == null )
+        {
+            throw new IllegalStateException();
+        }
+        else
+        {
+            nextEntry.prevEntry.nextEntry = newEntry;
+            nextEntry.prevEntry = newEntry;
+        }
+
+        name2entry.put( name, newEntry );
+    }
+    
+    /**
+     * Removes the processor with the specified name from this chain.
+     */
+    public synchronized void remove( String name )
+    {
+        Entry entry = checkOldName( name );
+        Entry prevEntry = entry.prevEntry;
+        Entry nextEntry = entry.nextEntry;
+        if( prevEntry == null )
+        {
+            nextEntry.prevEntry = null;
+            head = entry;
+        }
+        else
+        {
+            prevEntry.nextEntry = nextEntry;
+            nextEntry.prevEntry = prevEntry;
+        }
+    }
+
+    /**
+     * Removed all processors added to this chain.
+     */
+    public synchronized void clear()
+    {
+        tail.prevEntry = null;
+        tail.nextEntry = null;
+        head = tail;
+    }
+
+    private Entry checkOldName( String baseName )
+    {
+        Entry e = ( Entry ) name2entry.get( baseName );
+        if( e == null )
+        {
+            throw new IllegalArgumentException( "Unknown processor name:" +
+                                                baseName );
+        }
+        return e;
+    }
+
+    private void checkNewName( String name )
+    {
+        if( name2entry.containsKey( name ) )
+        {
+            throw new IllegalArgumentException(
+                    "Other processor is using name '" + name + "'" );
+        }
+    }
+    
+    /**
+     * Start invocation chain with the specified invocation.
+     * @throws NamingException if invocation failed
+     */
+    public void process( Request request ) throws NamingException
+    {
+        Entry head = this.head;
+        try
+        {
+            head.processor.process(
+                    head.nextProcessor, request );
+        }
+        catch( NamingException ne )
+        {
+            throw ne;
+        }
+        catch( Throwable e )
+        {
+            throw new RequestProcessorException( head.processor, request,
+                                            "Unexpected exception.", e );
+        }
+    }
+
+    /**
+     * Returns the list of processors this chain contains in the order of
+     * evaluation.
+     */
+    public List getAll()
+    {
+        List list = new ArrayList();
+        Entry e = head;
+        do
+        {
+            list.add( e.processor );
+            e = e.nextEntry;
+        }
+        while( e != null );
+
+        return list;
+    }
+
+    /**
+     * Returns the list of processors this chain contains in the reversed
+     * order of evaluation.
+     */
+    public List getAllReversed()
+    {
+        List list = new ArrayList();
+        Entry e = tail;
+        do
+        {
+            list.add( e.processor );
+            e = e.prevEntry;
+        }
+        while( e != null );
+
+        return list;
+    }
+
+    private class Entry
+    {
+        private Entry prevEntry;
+        private Entry nextEntry;
+        private final String name;
+        private final RequestProcessor processor;
+        private final NextRequestProcessor nextProcessor;
+
+        private Entry( Entry prevEntry, Entry nextEntry,
+                       String name, RequestProcessor processor )
+        {
+            if( processor == null )
+            {
+                throw new NullPointerException( "processor" );
+            }
+            if( name == null )
+            {
+                throw new NullPointerException( "name" );
+            }
+
+            this.prevEntry = prevEntry;
+            this.nextEntry = nextEntry;
+            this.name = name;
+            this.processor = processor;
+            this.nextProcessor = new NextRequestProcessor()
+            {
+                public void process(Request request)
+                        throws NamingException {
+                    RequestProcessor processor = Entry.this.nextEntry.processor;
+                    try
+                    {
+                        processor.process(
+                                Entry.this.nextEntry.nextProcessor, request );
+                    }
+                    catch( NamingException ne )
+                    {
+                        throw ne;
+                    }
+                    catch( Throwable e )
+                    {
+                        throw new RequestProcessorException( processor, request,
+                                                             "Unexpected exception.", e );
+                    }
+                }
+            };
+        }
+    }
+}

Propchange: directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorChain.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorException.java
URL: http://svn.apache.org/viewcvs/directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorException.java?view=auto&rev=158078
==============================================================================
--- directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorException.java (added)
+++ directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorException.java Fri Mar 18 07:20:09 2005
@@ -0,0 +1,142 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.server.jndi.request.processor;
+
+
+import org.apache.ldap.common.exception.LdapException;
+import org.apache.ldap.common.exception.LdapNamingException;
+import org.apache.ldap.common.message.ResultCodeEnum;
+import org.apache.ldap.server.jndi.request.Request;
+
+
+/**
+ * Exception thrown by an Interceptor while intercepting an Invocation.
+ * Interceptor failures caught from the method are bundled as
+ * InterceptorExceptions and rethrown.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class RequestProcessorException extends LdapNamingException
+{
+    private static final long serialVersionUID = 3258690996517746233L;
+
+    /** The Invokation the Interceptor failed on */
+    private final Request request;
+    /** The Interceptor causing the failure */
+    private final RequestProcessor requestProcessor;
+
+
+    /**
+     * Creates an InterceptorException without a message.
+     *
+     * @param requestProcessor the Interceptor causing the failure
+     * @param invocation the Invocation the Interceptor failed on
+     */
+    public RequestProcessorException( RequestProcessor requestProcessor, Request request )
+    {
+        super( ResultCodeEnum.OTHER );
+        this.request = request;
+        this.requestProcessor = requestProcessor;
+    }
+
+
+    /**
+     * Creates an InterceptorException with a custom message.
+     *
+     * @param requestProcessor the Interceptor causing the failure
+     * @param invocation the Invocation the Interceptor failed on
+     * @param explanation String explanation of why the Interceptor failed
+     */
+    public RequestProcessorException( RequestProcessor requestProcessor,
+                                      Request request, String explanation )
+    {
+        super( explanation, ResultCodeEnum.OTHER );
+        this.request = request;
+        this.requestProcessor = requestProcessor;
+    }
+
+
+    /**
+     * Creates an InterceptorException without a message.
+     *
+     * @param requestProcessor the Interceptor causing the failure
+     * @param invocation the Invocation the Interceptor failed on
+     * @param rootCause the root cause of this exception
+     */
+    public RequestProcessorException( RequestProcessor requestProcessor,
+                                      Request request, Throwable rootCause )
+    {
+        this( requestProcessor, request );
+        super.setRootCause( rootCause );
+    }
+
+    /**
+     * Creates an InterceptorException without a message.
+     *
+     * @param requestProcessor the Interceptor causing the failure
+     * @param invocation the Invocation the Interceptor failed on
+     * @param explanation String explanation of why the Interceptor failed
+     * @param rootCause the root cause of this exception
+     */
+    public RequestProcessorException( RequestProcessor requestProcessor,
+                                      Request request,
+                                      String explanation,
+                                      Throwable rootCause )
+    {
+        this( requestProcessor, request, explanation );
+        super.setRootCause( rootCause );
+    }
+
+    /**
+     * Gets the invovation object this exception is associated with.
+     *
+     * @return the invovation object this exception is associated with
+     */
+    public Request getRequest()
+    {
+        return request;
+    }
+
+
+    /**
+     * Gets the interceptor this exception is associated with.
+     *
+     * @return the interceptor this exception is associated with
+     */
+    public RequestProcessor getRequestProcessor()
+    {
+        return requestProcessor;
+    }
+
+
+    /**
+     * Will return the resultCode of the root cause if the root cause
+     * implements LdapException.
+     *
+     * @see org.apache.ldap.common.exception.LdapException#getResultCode()
+     */
+    public ResultCodeEnum getResultCode()
+    {
+        if ( getRootCause() != null && ( getRootCause() instanceof LdapException ) )
+        {
+            return ( ( LdapException ) getRootCause() ).getResultCode();
+        }
+
+        return super.getResultCode();
+    }
+}

Propchange: directory/apacheds/branches/interceptor_revamp/core/src/main/java/org/apache/ldap/server/jndi/request/processor/RequestProcessorException.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision