Author: norman
Date: Fri Nov 18 17:15:10 2011
New Revision: 1203750

URL: http://svn.apache.org/viewvc?rev=1203750&view=rev
Log:
Add some generic mutable ProtocolHandlerChain implementation and fix some 
javadocs

Added:
    
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/ProtocolHandlerChainImpl.java
Modified:
    
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/Protocol.java
    
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolSession.java
    
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java
    
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/AbstractProtocolHandlerChain.java
    
james/protocols/trunk/lmtp/src/main/java/org/apache/james/protocols/lmtp/LMTPProtocolHandlerChain.java
    
james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/SMTPProtocolHandlerChain.java

Modified: 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/Protocol.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/Protocol.java?rev=1203750&r1=1203749&r2=1203750&view=diff
==============================================================================
--- 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/Protocol.java
 (original)
+++ 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/Protocol.java
 Fri Nov 18 17:15:10 2011
@@ -41,6 +41,11 @@ public interface Protocol {
      */
     ProtocolSession newSession(ProtocolTransport transport);
     
+    /**
+     * Returns <code>true</code> if STARTTLS is supported
+     * 
+     * @return starttlsSupported
+     */
     boolean isStartTLSSupported();
 
 }

Modified: 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolSession.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolSession.java?rev=1203750&r1=1203749&r2=1203750&view=diff
==============================================================================
--- 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolSession.java
 (original)
+++ 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolSession.java
 Fri Nov 18 17:15:10 2011
@@ -79,9 +79,19 @@ public interface ProtocolSession {
     String getRemoteIPAddress();
     
     
-    InetSocketAddress getLocalAddress();
-    
+    /**
+     * Return the {@link InetSocketAddress} of the remote peer
+     * 
+     * @return address
+     */
     InetSocketAddress getRemoteAddress();
+
+    /**
+     * Return the {@link InetSocketAddress} of the local bound address
+     * 
+     * @return local
+     */
+    InetSocketAddress getLocalAddress();
     
     /**
      * Return the ID for the session

Modified: 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java?rev=1203750&r1=1203749&r2=1203750&view=diff
==============================================================================
--- 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java
 (original)
+++ 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java
 Fri Nov 18 17:15:10 2011
@@ -36,6 +36,11 @@ public interface ProtocolTransport {
      */
     InetSocketAddress getRemoteAddress();
 
+    /**
+     * Return the {@link InetSocketAddress} of the local bound address
+     * 
+     * @return local
+     */
     InetSocketAddress getLocalAddress();
 
     

Modified: 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/AbstractProtocolHandlerChain.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/AbstractProtocolHandlerChain.java?rev=1203750&r1=1203749&r2=1203750&view=diff
==============================================================================
--- 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/AbstractProtocolHandlerChain.java
 (original)
+++ 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/AbstractProtocolHandlerChain.java
 Fri Nov 18 17:15:10 2011
@@ -31,11 +31,11 @@ import java.util.List;
 public abstract class AbstractProtocolHandlerChain implements 
ProtocolHandlerChain{
 
        /**
-        * Return a List of all Handlers
+        * Return an immutable List of all Handlers
         * 
         * @return handlerList
         */
-       protected abstract List<Object> getHandlers();
+       protected abstract List<ProtocolHandler> getHandlers();
        
     /**
         * @see 
org.apache.james.protocols.api.handler.ProtocolHandlerChain#getHandlers(java.lang.Class)
@@ -43,7 +43,7 @@ public abstract class AbstractProtocolHa
     @SuppressWarnings("unchecked")
     public <T> LinkedList<T> getHandlers(Class<T> type) {
         LinkedList<T> result = new LinkedList<T>();
-        List<Object> handlers = getHandlers();
+        List<ProtocolHandler> handlers = getHandlers();
         for (Iterator<?> i = handlers.iterator(); i.hasNext(); ) {
             Object handler = i.next();
             if (type.isInstance(handler)) {
@@ -59,8 +59,8 @@ public abstract class AbstractProtocolHa
      * 
      * @throws WiringException 
      */
-    public final void wireExtensibleHandlers() throws WiringException {
-        List<Object> handlers = getHandlers();
+    public void wireExtensibleHandlers() throws WiringException {
+        List<ProtocolHandler> handlers = getHandlers();
         for (Iterator<?> h = handlers.iterator(); h.hasNext(); ) {
             Object handler = h.next();
             if (handler instanceof ExtensibleHandler) {

Added: 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/ProtocolHandlerChainImpl.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/ProtocolHandlerChainImpl.java?rev=1203750&view=auto
==============================================================================
--- 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/ProtocolHandlerChainImpl.java
 (added)
+++ 
james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/handler/ProtocolHandlerChainImpl.java
 Fri Nov 18 17:15:10 2011
@@ -0,0 +1,273 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.protocols.api.handler;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+/**
+ * {@link AbstractProtocolHandlerChain} which is mutable till the {@link 
#wireExtensibleHandlers()} is called. After that all operations
+ * which try to modify the instance will throw and {@link 
UnsupportedOperationException}
+ * 
+ *
+ */
+public class ProtocolHandlerChainImpl extends AbstractProtocolHandlerChain 
implements List<ProtocolHandler>{
+
+       private final List<ProtocolHandler> handlers = new 
ArrayList<ProtocolHandler>();
+       private volatile boolean readyOnly = false;
+       
+       
+       /**
+        * Once this is called all tries to modify this {@link 
ProtocolHandlerChainImpl} will throw an {@link UnsupportedOperationException}
+        */
+       @Override
+       public void wireExtensibleHandlers() throws WiringException {
+               super.wireExtensibleHandlers();
+               readyOnly = true;
+       }
+
+       protected final boolean isReadyOnly() {
+               return readyOnly;
+       }
+       
+       public boolean add(ProtocolHandler handler) {
+               if (readyOnly) {
+                       throw new UnsupportedOperationException("Ready-only");
+               }
+               return handlers.add(handler);
+       }
+       
+       @Override
+       protected List<ProtocolHandler> getHandlers() {
+               if (!readyOnly) {
+                       throw new IllegalStateException("wireExtensibleHandler 
must be called first!");
+               }
+               return Collections.unmodifiableList(handlers);
+       }
+
+       @Override
+       public int size() {
+               return handlers.size();
+       }
+
+       @Override
+       public boolean isEmpty() {
+               return handlers.isEmpty();
+       }
+
+       @Override
+       public boolean contains(Object o) {
+               return handlers.contains(o);
+       }
+
+       @Override
+       public Iterator<ProtocolHandler> iterator() {
+               return new ProtocolHandlerIterator(handlers.listIterator());
+       }
+
+       @Override
+       public Object[] toArray() {
+               return handlers.toArray();
+       }
+
+       @Override
+       public <T> T[] toArray(T[] a) {
+               return handlers.toArray(a);
+       }
+
+       @Override
+       public boolean remove(Object o) {
+               if (readyOnly) {
+                       throw new UnsupportedOperationException("Ready-only");
+               }
+               return handlers.remove(o);
+       }
+
+       @Override
+       public boolean containsAll(Collection<?> c) {
+               return handlers.containsAll(c);
+       }
+
+       @Override
+       public boolean addAll(Collection<? extends ProtocolHandler> c) {
+               if (readyOnly) {
+                       throw new UnsupportedOperationException("Ready-only");
+               }
+               return handlers.addAll(c);
+       }
+
+       @Override
+       public boolean addAll(int index, Collection<? extends ProtocolHandler> 
c) {
+               if (readyOnly) {
+                       throw new UnsupportedOperationException("Ready-only");
+               }
+               return handlers.addAll(index, c);
+       }
+
+       @Override
+       public boolean removeAll(Collection<?> c) {
+               if (readyOnly) {
+                       throw new UnsupportedOperationException("Ready-only");
+               }
+               return handlers.removeAll(c);
+       }
+
+       @Override
+       public boolean retainAll(Collection<?> c) {
+               return handlers.retainAll(c);
+       }
+
+       @Override
+       public void clear() {
+               if (readyOnly) {
+                       throw new UnsupportedOperationException("Ready-only");
+               }
+               handlers.clear();
+       }
+
+       @Override
+       public ProtocolHandler get(int index) {
+               return (ProtocolHandler) handlers.get(index);
+       }
+
+       @Override
+       public ProtocolHandler set(int index, ProtocolHandler element) {
+               if (readyOnly) {
+                       throw new UnsupportedOperationException("Ready-only");
+               }
+               return (ProtocolHandler) handlers.set(index, element);
+       }
+
+       @Override
+       public void add(int index, ProtocolHandler element) {
+               if (readyOnly) {
+                       throw new UnsupportedOperationException("Ready-only");
+               }
+               handlers.add(index, element);
+       }
+
+       @Override
+       public ProtocolHandler remove(int index) {
+               if (readyOnly) {
+                       throw new UnsupportedOperationException("Ready-only");
+               }
+               return (ProtocolHandler) handlers.remove(index);
+       }
+
+       @Override
+       public int indexOf(Object o) {
+               return handlers.indexOf(o);
+       }
+
+       @Override
+       public int lastIndexOf(Object o) {
+               return handlers.lastIndexOf(o);
+       }
+
+       @Override
+       public ListIterator<ProtocolHandler> listIterator() {
+               return new ProtocolHandlerIterator(handlers.listIterator());
+       }
+
+       @Override
+       public ListIterator<ProtocolHandler> listIterator(int index) {
+               return new 
ProtocolHandlerIterator(handlers.listIterator(index));
+       }
+
+       @Override
+       public List<ProtocolHandler> subList(int fromIndex, int toIndex) {
+               List<ProtocolHandler> sList = new ArrayList<ProtocolHandler>();
+               for (Object handler: handlers.subList(fromIndex, toIndex)) {
+                       sList.add((ProtocolHandler) handler);
+               }
+               if (readyOnly) {
+                       return Collections.unmodifiableList(sList);
+               }
+               return sList;
+       }
+       
+       private final class ProtocolHandlerIterator implements 
ListIterator<ProtocolHandler> {
+               private final ListIterator<ProtocolHandler> handlers;
+
+               public ProtocolHandlerIterator(ListIterator<ProtocolHandler> 
handlers) {
+                       this.handlers = handlers;
+               }
+               
+               @Override
+               public boolean hasNext() {
+                       return handlers.hasNext();
+               }
+
+               @Override
+               public ProtocolHandler next() {
+                       return (ProtocolHandler) handlers.next();
+               }
+
+               @Override
+               public boolean hasPrevious() {
+                       return handlers.hasPrevious();
+               }
+
+               @Override
+               public ProtocolHandler previous() {
+                       return handlers.previous();
+               }
+
+               @Override
+               public int nextIndex() {
+                       return handlers.nextIndex();
+               }
+
+               @Override
+               public int previousIndex() {
+                       return handlers.previousIndex();
+               }
+
+               @Override
+               public void remove() {
+                       if (readyOnly) {
+                               throw new 
UnsupportedOperationException("Ready-only");
+                       }
+                       handlers.previousIndex();
+               }
+
+               @Override
+               public void set(ProtocolHandler e) {
+                       if (readyOnly) {
+                               throw new 
UnsupportedOperationException("Ready-only");
+                       }
+                       handlers.set(e);
+               }
+
+               @Override
+               public void add(ProtocolHandler e) {
+                       if (readyOnly) {
+                               throw new 
UnsupportedOperationException("Ready-only");
+                       }
+                       handlers.add(e);
+               }
+               
+       }
+
+}

Modified: 
james/protocols/trunk/lmtp/src/main/java/org/apache/james/protocols/lmtp/LMTPProtocolHandlerChain.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/lmtp/src/main/java/org/apache/james/protocols/lmtp/LMTPProtocolHandlerChain.java?rev=1203750&r1=1203749&r2=1203750&view=diff
==============================================================================
--- 
james/protocols/trunk/lmtp/src/main/java/org/apache/james/protocols/lmtp/LMTPProtocolHandlerChain.java
 (original)
+++ 
james/protocols/trunk/lmtp/src/main/java/org/apache/james/protocols/lmtp/LMTPProtocolHandlerChain.java
 Fri Nov 18 17:15:10 2011
@@ -21,6 +21,7 @@ package org.apache.james.protocols.lmtp;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.james.protocols.api.handler.ProtocolHandler;
 import org.apache.james.protocols.api.handler.WiringException;
 import org.apache.james.protocols.smtp.SMTPProtocolHandlerChain;
 import org.apache.james.protocols.smtp.core.DataCmdHandler;
@@ -44,8 +45,8 @@ public class LMTPProtocolHandlerChain ex
     }
 
     @Override
-    protected List<Object> initDefaultHandlers() {
-        List<Object> defaultHandlers = new ArrayList<Object>();
+    protected List<ProtocolHandler> initDefaultHandlers() {
+        List<ProtocolHandler> defaultHandlers = new 
ArrayList<ProtocolHandler>();
         defaultHandlers.add(new SMTPCommandDispatcherLineHandler());
         defaultHandlers.add(new ExpnCmdHandler());
         defaultHandlers.add(new LhloCmdHandler());

Modified: 
james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/SMTPProtocolHandlerChain.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/SMTPProtocolHandlerChain.java?rev=1203750&r1=1203749&r2=1203750&view=diff
==============================================================================
--- 
james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/SMTPProtocolHandlerChain.java
 (original)
+++ 
james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/SMTPProtocolHandlerChain.java
 Fri Nov 18 17:15:10 2011
@@ -19,11 +19,12 @@
 package org.apache.james.protocols.smtp;
 
 import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Collection;
 import java.util.List;
 
-import org.apache.james.protocols.api.handler.AbstractProtocolHandlerChain;
+import org.apache.james.protocols.api.handler.ProtocolHandler;
 import org.apache.james.protocols.api.handler.ProtocolHandlerChain;
+import org.apache.james.protocols.api.handler.ProtocolHandlerChainImpl;
 import org.apache.james.protocols.api.handler.WiringException;
 import org.apache.james.protocols.smtp.core.DataCmdHandler;
 import org.apache.james.protocols.smtp.core.DataLineMessageHookHandler;
@@ -46,34 +47,48 @@ import org.apache.james.protocols.smtp.c
 import org.apache.james.protocols.smtp.core.esmtp.StartTlsCmdHandler;
 import org.apache.james.protocols.smtp.hook.AuthHook;
 import org.apache.james.protocols.smtp.hook.Hook;
-import org.apache.james.protocols.smtp.hook.MessageHook;
 
 /**
  * This {@link ProtocolHandlerChain} implementation add all needed handlers to
  * the chain to act as full blown SMTPServer. By default messages will just get
  * rejected after the DATA command.
  * 
- * If you want to accept the messagejust add a {@link MessageHook}
- * implementation to the chain and handle the queuing
+ * 
+ * 
  * 
  * 
  * 
  */
-public class SMTPProtocolHandlerChain extends AbstractProtocolHandlerChain {
-    private final List<Object> defaultHandlers = new ArrayList<Object>();
-    private final List<Hook> hooks = new ArrayList<Hook>();
-    private final List<Object> handlers = new ArrayList<Object>();
-    private boolean authHandler = false;
+public class SMTPProtocolHandlerChain extends ProtocolHandlerChainImpl {
+    private volatile boolean authHandler = false;
     
-    public SMTPProtocolHandlerChain() throws WiringException {
-        defaultHandlers.addAll(initDefaultHandlers());
-        copy();
+    public SMTPProtocolHandlerChain() {
+        this(true);
+    }
+
         
-        wireExtensibleHandlers();
+    public SMTPProtocolHandlerChain(boolean addDefault) {
+        if (addDefault) {
+            addAll(initDefaultHandlers());      
+        }
     }
 
-    protected List<Object> initDefaultHandlers() {
-        List<Object> defaultHandlers = new ArrayList<Object>();
+    /**
+     * Add all default handlers to the chain and the given {@link Hook}'s. 
After that {@link #wireExtensibleHandlers()} is called
+     * 
+     * @param hooks
+     * @throws WiringException
+     */
+    public SMTPProtocolHandlerChain(Hook... hooks) throws WiringException {
+        this(true);    
+        for (int i = 0; i < hooks.length; i++) {
+            add(hooks[i]);
+        }
+        wireExtensibleHandlers();
+    }
+    
+    protected List<ProtocolHandler> initDefaultHandlers() {
+        List<ProtocolHandler> defaultHandlers = new 
ArrayList<ProtocolHandler>();
         defaultHandlers.add(new SMTPCommandDispatcherLineHandler());
         defaultHandlers.add(new ExpnCmdHandler());
         defaultHandlers.add(new EhloCmdHandler());
@@ -96,74 +111,44 @@ public class SMTPProtocolHandlerChain ex
     }
     
 
-    /**
-     * Add the hook to the chain
-     * 
-     * @param hook
-     * @throws WiringException
-     */
-    public final synchronized void addHook(Hook hook) throws WiringException {
-        if (hook instanceof AuthHook && !authHandler) {
-            defaultHandlers.add(new AuthCmdHandler());
+    private boolean checkForAuth(ProtocolHandler handler) {
+        if (handler instanceof AuthHook && !authHandler) {
+            if (!add(new AuthCmdHandler())) {
+                return false;
+            }
             authHandler = true;
         }
-        addHook(hooks.size(), hook);
-    }
-
-    /**
-     * Add the hook to the chain on the given index
-     * 
-     * @param index
-     * @param hook
-     * @throws WiringException
-     */
-    public final synchronized void addHook(int index, Hook hook) throws 
WiringException {
-        hooks.add(index, hook);
-        copy();
-        wireExtensibleHandlers();
-
+        return true;
     }
-
-    /**
-     * Remove the Hook found on the given index from the chain
-     * 
-     * @param index
-     * @return hook
-     * @throws WiringException
-     */
-    public final synchronized Hook removeHook(int index) throws 
WiringException {
-        Hook hook = hooks.remove(index);
-        handlers.remove(hook);
-        wireExtensibleHandlers();
-        return hook;
-
+    public boolean add(ProtocolHandler handler) {
+        checkForAuth(handler);
+        return super.add(handler);
     }
 
-    /**
-     * Return the index of the given hook
-     * 
-     * @param hook
-     * @return index
-     */
-    public synchronized int getIndexOfHook(Hook hook) {
-        return hooks.indexOf(hook);
+    @Override
+    public boolean addAll(Collection<? extends ProtocolHandler> c) {
+        for (ProtocolHandler handler: c) {
+            if (!checkForAuth(handler)) {
+                return false;
+            }
+        }
+        return super.addAll(c);
     }
 
-    /**
-     * @see
-     * 
org.apache.james.protocols.api.handler.AbstractProtocolHandlerChain#getHandlers()
-     */
     @Override
-    protected synchronized List<Object> getHandlers() {
-        return Collections.unmodifiableList(handlers);
+    public boolean addAll(int index, Collection<? extends ProtocolHandler> c) {
+        for (ProtocolHandler handler: c) {
+            if (!checkForAuth(handler)) {
+                return false;
+            }
+        }
+        return super.addAll(index, c);
     }
 
-    /**
-     * Copy the lists
-     */
-    private void copy() {
-        handlers.clear();
-        handlers.addAll(defaultHandlers);
-        handlers.addAll(hooks);
+    @Override
+    public void add(int index, ProtocolHandler element) {
+        checkForAuth(element);
+        super.add(index, element);
     }
+  
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to