You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2008/02/25 13:24:41 UTC

svn commit: r630828 - in /james/jspf/trunk/src/main/java/org/apache/james/jspf: core/SPFSession.java executor/StagedMultipleSPFExecutor.java executor/SynchronousSPFExecutor.java impl/SPF.java terms/IncludeMechanism.java terms/RedirectModifier.java

Author: bago
Date: Mon Feb 25 04:24:24 2008
New Revision: 630828

URL: http://svn.apache.org/viewvc?rev=630828&view=rev
Log:
Made a refactoring so that there is no more an ExceptionCatcher stack in SPFSession.
Now every SPFChecker can implement ExceptionCatcher so that a single checker stack is needed/used and it seem a little clearer and the logic to remove "skipped" checkers can be moved to the executors (much better).

Modified:
    james/jspf/trunk/src/main/java/org/apache/james/jspf/core/SPFSession.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutor.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/impl/SPF.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/core/SPFSession.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/core/SPFSession.java?rev=630828&r1=630827&r2=630828&view=diff
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/core/SPFSession.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/core/SPFSession.java Mon Feb 25 04:24:24 2008
@@ -72,8 +72,6 @@
     
     private Stack checkers = new Stack();
     
-    private Stack catchers = new Stack();
-
     private String currentResultExpanded;
     
     /**
@@ -353,6 +351,7 @@
      * @param checker  
      */
     public void pushChecker(SPFChecker checker) {
+        System.out.println(System.identityHashCode(this)+"/pushChecker: "+checker+" "+System.identityHashCode(checker));
         checkers.push(checker);
     }
     
@@ -367,44 +366,8 @@
             return null;
         } else {
             SPFChecker checker = (SPFChecker) checkers.pop();
+            System.out.println(System.identityHashCode(this)+"/popChecker: "+checker+" "+System.identityHashCode(checker));
             return checker;
-        }
-    }
-
-    /**
-     * Add the given SPFCheckerExceptionCatcher on top of the stack
-     * 
-     * @param catcher
-     */
-    public void pushExceptionCatcher(SPFCheckerExceptionCatcher catcher) {
-        catchers.push(catcher);
-    }
-    
-    /**
-     * Remove the SPFCheckerExceptionCatcher on the top and return it. If no SPFCheckerExceptionCatcher is left
-     * null is returned
-     * 
-     * @return the last catcher
-     */
-    public SPFCheckerExceptionCatcher popExceptionCatcher() {
-        if (catchers.isEmpty()) {
-            return null;
-        } else {
-            return (SPFCheckerExceptionCatcher) catchers.pop();
-        }
-    }
-
-    /**
-     * Return the SPFCheckerExceptionCatcher on the top of the Stack, but not 
-     * remove it. If no SPFCheckerExceptionCatcher is left null is returned
-     * 
-     * @return the last catcher
-     */
-    public SPFCheckerExceptionCatcher getExceptionCatcher() {
-        if (catchers.isEmpty()) {
-            return null;
-        } else {
-            return (SPFCheckerExceptionCatcher) catchers.peek();
         }
     }
 

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutor.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutor.java?rev=630828&r1=630827&r2=630828&view=diff
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutor.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutor.java Mon Feb 25 04:24:24 2008
@@ -130,13 +130,16 @@
                 }
             } catch (Exception e) {
                 while (e != null) {
-                    SPFCheckerExceptionCatcher catcher = session
-                            .getExceptionCatcher();
+                    while (checker == null || !(checker instanceof SPFCheckerExceptionCatcher)) {
+                        checker = session.popChecker();
+                    }
                     try {
-                        catcher.onException(e, session);
+                        ((SPFCheckerExceptionCatcher) checker).onException(e, session);
                         e = null;
                     } catch (SPFResultException ex) {
                         e = ex;
+                    } finally {
+                        checker = null;
                     }
                 }
             }
@@ -195,14 +198,18 @@
                 }
 
             } catch (Exception e) {
+                SPFChecker checker = null;
                 while (e != null) {
-                    SPFCheckerExceptionCatcher catcher = session
-                            .getExceptionCatcher();
+                    while (checker == null || !(checker instanceof SPFCheckerExceptionCatcher)) {
+                        checker = session.popChecker();
+                    }
                     try {
-                        catcher.onException(e, session);
+                        ((SPFCheckerExceptionCatcher) checker).onException(e, session);
                         e = null;
                     } catch (SPFResultException ex) {
                         e = ex;
+                    } finally {
+                        checker = null;
                     }
                 }
                 execute(session, result, false);

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java?rev=630828&r1=630827&r2=630828&view=diff
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java Mon Feb 25 04:24:24 2008
@@ -65,13 +65,16 @@
                 }
             } catch (Exception e) {
                 while (e != null) {
-                    SPFCheckerExceptionCatcher catcher = session
-                            .getExceptionCatcher();
+                    while (checker == null || !(checker instanceof SPFCheckerExceptionCatcher)) {
+                        checker = session.popChecker();
+                    }
                     try {
-                        catcher.onException(e, session);
+                        ((SPFCheckerExceptionCatcher) checker).onException(e, session);
                         e = null;
                     } catch (SPFResultException ex) {
                         e = ex;
+                    } finally {
+                        checker = null;
                     }
                 }
             }

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/impl/SPF.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/impl/SPF.java?rev=630828&r1=630827&r2=630828&view=diff
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/impl/SPF.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/impl/SPF.java Mon Feb 25 04:24:24 2008
@@ -68,46 +68,6 @@
  */
 public class SPF implements SPFChecker {
 
-    private final static class SPFCheckerExceptionCatcherImplementation implements
-            SPFCheckerExceptionCatcher {
-        private SPFChecker resultHandler;
-        private Logger log;
-
-        public SPFCheckerExceptionCatcherImplementation(SPFChecker resultHandler, Logger log) {
-            this.resultHandler = resultHandler;
-            this.log = log;
-        }
-
-        /**
-         * @see org.apache.james.jspf.core.SPFCheckerExceptionCatcher#onException(java.lang.Exception, org.apache.james.jspf.core.SPFSession)
-         */
-        public void onException(Exception exception, SPFSession session)
-                throws PermErrorException, NoneException, TempErrorException,
-                NeutralException {
-
-            SPFChecker checker;
-            while ((checker = session.popChecker())!=resultHandler) {
-                log.debug("Redirect resulted in exception. Removing checker: "+checker);
-            }
-
-            String result;
-            if (exception instanceof SPFResultException) {
-                result = ((SPFResultException) exception).getResult();
-                if (!SPFErrorConstants.NEUTRAL_CONV.equals(result)) {
-                    log.warn(exception.getMessage(),exception);
-                }
-            } else {
-                // this should never happen at all. But anyway we will set the
-                // result to neutral. Safety first ..
-                log.error(exception.getMessage(),exception);
-                result = SPFErrorConstants.NEUTRAL_CONV;
-            }
-            session.setCurrentResultExpanded(result);
-            
-        }
-
-    }
-
     private static final class SPFRecordChecker implements SPFChecker {
         
         /**
@@ -280,7 +240,13 @@
     }
 
     
-    private static final class DefaultSPFChecker implements SPFChecker {
+    private static final class DefaultSPFChecker implements SPFChecker, SPFCheckerExceptionCatcher {
+        
+        private Logger log;
+
+        public DefaultSPFChecker(Logger log) {
+            this.log = log;
+        }
 
         /**
          * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
@@ -295,6 +261,30 @@
             }
             return null;
         }
+        
+
+        /**
+         * @see org.apache.james.jspf.core.SPFCheckerExceptionCatcher#onException(java.lang.Exception, org.apache.james.jspf.core.SPFSession)
+         */
+        public void onException(Exception exception, SPFSession session)
+                throws PermErrorException, NoneException, TempErrorException,
+                NeutralException {
+
+            String result;
+            if (exception instanceof SPFResultException) {
+                result = ((SPFResultException) exception).getResult();
+                if (!SPFErrorConstants.NEUTRAL_CONV.equals(result)) {
+                    log.warn(exception.getMessage(),exception);
+                }
+            } else {
+                // this should never happen at all. But anyway we will set the
+                // result to neutral. Safety first ..
+                log.error(exception.getMessage(),exception);
+                result = SPFErrorConstants.NEUTRAL_CONV;
+            }
+            session.setCurrentResultExpanded(result);
+        }
+
     }
     
     /**
@@ -315,11 +305,10 @@
         spfData = new SPFSession(mailFrom, hostName, ipAddress);
       
 
-        SPFChecker resultHandler = new DefaultSPFChecker();
+        SPFChecker resultHandler = new DefaultSPFChecker(log);
         
         spfData.pushChecker(resultHandler);
         spfData.pushChecker(this);
-        spfData.pushExceptionCatcher(new SPFCheckerExceptionCatcherImplementation(resultHandler, log));
         
         FutureSPFResult ret = new FutureSPFResult();
         
@@ -341,12 +330,12 @@
             NoneException, TempErrorException, NeutralException {
 
         // if we already have a result we don't need to add further processing.
-        if (spfData.getCurrentResultExpanded() == null) {
-        SPFChecker policyChecker = new PolicyChecker(getPolicies());
-        SPFChecker recordChecker = new SPFRecordChecker();
-        
-        spfData.pushChecker(recordChecker);
-        spfData.pushChecker(policyChecker);
+        if (spfData.getCurrentResultExpanded() == null && spfData.getCurrentResult() == null) {
+            SPFChecker policyChecker = new PolicyChecker(getPolicies());
+            SPFChecker recordChecker = new SPFRecordChecker();
+            
+            spfData.pushChecker(recordChecker);
+            spfData.pushChecker(policyChecker);
         }
         
         return null;

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java?rev=630828&r1=630827&r2=630828&view=diff
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java Mon Feb 25 04:24:24 2008
@@ -42,49 +42,6 @@
  */
 public class IncludeMechanism implements Mechanism, ConfigurationEnabled, LogEnabled, SPFCheckEnabled, MacroExpandEnabled {
 
-    private final class ExceptionCatcher implements SPFCheckerExceptionCatcher {
-        private SPFChecker spfChecker;
-
-        private SPFChecker finallyChecker;
-
-        /**
-         * @see org.apache.james.jspf.core.SPFCheckerExceptionCatcher#onException(java.lang.Exception, org.apache.james.jspf.core.SPFSession)
-         */
-        public void onException(Exception exception, SPFSession session)
-                throws PermErrorException, NoneException,
-                TempErrorException, NeutralException {
-            
-            // remove every checker until the initialized one
-            SPFChecker checker;
-            while ((checker = session.popChecker())!=spfChecker) {
-                log.debug("Include resulted in exception. Removing checker: "+checker);
-            }
-            
-            finallyChecker.checkSPF(session);
-            
-            if (exception instanceof NeutralException) {
-                throw new PermErrorException("included checkSPF returned NeutralException");
-            } else if (exception instanceof NoneException) {
-                throw new PermErrorException("included checkSPF returned NoneException");
-            } else if (exception instanceof PermErrorException){
-                throw (PermErrorException) exception;
-            } else if (exception instanceof TempErrorException){
-                throw (TempErrorException) exception;
-            } else if (exception instanceof RuntimeException){
-                throw (RuntimeException) exception;
-            } else {
-                throw new IllegalStateException(exception.getMessage());
-            }
-        }
-
-        public SPFCheckerExceptionCatcher setExceptionHandlerChecker(
-                SPFChecker checker, SPFChecker finallyChecker) {
-            this.spfChecker = checker;
-            this.finallyChecker = finallyChecker;
-            return this;
-        }
-    }
-
     private final class ExpandedChecker implements SPFChecker {
       
         /**
@@ -109,41 +66,16 @@
         }
     }
 
-    private final class FinallyChecker implements SPFChecker {
+    private final class CleanupAndResultChecker implements SPFChecker, SPFCheckerExceptionCatcher {
         private String previousResult;
-
         private String previousDomain;
 
-        /**
-         * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
-         */
-        public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException,
-                TempErrorException, NeutralException, NoneException {
-            
+        private void restoreSession(SPFSession spfData) {
             spfData.setIgnoreExplanation(false);
             spfData.setCurrentDomain(previousDomain);
             spfData.setCurrentResult(previousResult);
-
-            spfData.popExceptionCatcher();
-            
-            return null;
         }
-
-        public SPFChecker init(SPFSession spfSession) {
-
-            // TODO understand what exactly we have to do now that spfData is a session
-            // and contains much more than the input data.
-            // do we need to create a new session at all?
-            // do we need to backup the session attributes and restore them?
-            this.previousResult = spfSession.getCurrentResult();
-            this.previousDomain = spfSession.getCurrentDomain();
-            return this;
-        }
-    }
-
-    private final class CleanupAndResultChecker implements SPFChecker {
-        private SPFChecker finallyChecker;
-
+        
         /**
          * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
          */
@@ -152,7 +84,7 @@
             
             String currentResult = spfData.getCurrentResult();
             
-            finallyChecker.checkSPF(spfData);
+            restoreSession(spfData);
             
             if (currentResult == null) {
                 throw new TempErrorException("included checkSPF returned null");
@@ -169,8 +101,37 @@
             return null;
         }
 
-        public SPFChecker init(SPFChecker finallyChecker) {
-            this.finallyChecker = finallyChecker;
+        /**
+         * @see org.apache.james.jspf.core.SPFCheckerExceptionCatcher#onException(java.lang.Exception, org.apache.james.jspf.core.SPFSession)
+         */
+        public void onException(Exception exception, SPFSession session)
+                throws PermErrorException, NoneException,
+                TempErrorException, NeutralException {
+            
+            restoreSession(session);
+            
+            if (exception instanceof NeutralException) {
+                throw new PermErrorException("included checkSPF returned NeutralException");
+            } else if (exception instanceof NoneException) {
+                throw new PermErrorException("included checkSPF returned NoneException");
+            } else if (exception instanceof PermErrorException){
+                throw (PermErrorException) exception;
+            } else if (exception instanceof TempErrorException){
+                throw (TempErrorException) exception;
+            } else if (exception instanceof RuntimeException){
+                throw (RuntimeException) exception;
+            } else {
+                throw new IllegalStateException(exception.getMessage());
+            }
+        }
+
+        public SPFChecker init(SPFSession spfSession) {
+            // TODO understand what exactly we have to do now that spfData is a session
+            // and contains much more than the input data.
+            // do we need to create a new session at all?
+            // do we need to backup the session attributes and restore them?
+            this.previousResult = spfSession.getCurrentResult();
+            this.previousDomain = spfSession.getCurrentDomain();
             return this;
         }
     }
@@ -196,11 +157,8 @@
         // update currentDepth
         spfData.increaseCurrentDepth();
         
-        SPFChecker finallyChecker = new FinallyChecker().init(spfData);
-        
-        SPFChecker cleanupAndResultHandler = new CleanupAndResultChecker().init(finallyChecker);
+        SPFChecker cleanupAndResultHandler = new CleanupAndResultChecker().init(spfData);
         spfData.pushChecker(cleanupAndResultHandler);
-        spfData.pushExceptionCatcher(new ExceptionCatcher().setExceptionHandlerChecker(cleanupAndResultHandler, finallyChecker));
         
         spfData.pushChecker(new ExpandedChecker());
         return macroExpand.checkExpand(getHost(), spfData, MacroExpand.DOMAIN);

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java?rev=630828&r1=630827&r2=630828&view=diff
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java Mon Feb 25 04:24:24 2008
@@ -39,53 +39,6 @@
 public class RedirectModifier extends GenericModifier implements
         SPFCheckEnabled, MacroExpandEnabled {
 
-    private final class ExceptionCatcher implements SPFCheckerExceptionCatcher {
-        private SPFChecker spfChecker;
-
-        private SPFChecker finallyChecker;
-
-        public ExceptionCatcher(SPFChecker spfChecker,
-                SPFChecker finallyChecker) {
-            this.spfChecker = spfChecker;
-            this.finallyChecker = finallyChecker;
-        }
-
-        /**
-         * @see org.apache.james.jspf.core.SPFCheckerExceptionCatcher#onException(java.lang.Exception, org.apache.james.jspf.core.SPFSession)
-         */
-        public void onException(Exception exception, SPFSession session)
-                throws PermErrorException, NoneException,
-                TempErrorException, NeutralException {
-            
-            finallyChecker.checkSPF(session);
-
-            // remove every checker until the initialized one
-            SPFChecker checker;
-            while ((checker = session.popChecker())!=spfChecker) {
-                log.debug("Redirect resulted in exception. Removing checker: "+checker);
-            }
-
-            if (exception instanceof NeutralException) {
-                throw new PermErrorException(
-                "included checkSPF returned NeutralException");
-
-            } else if (exception instanceof NoneException) {
-                // no spf record assigned to the redirect domain
-                throw new PermErrorException(
-                        "included checkSPF returned NoneException");
-            } else if (exception instanceof PermErrorException){
-                throw (PermErrorException) exception;
-            } else if (exception instanceof TempErrorException){
-                throw (TempErrorException) exception;
-            } else if (exception instanceof RuntimeException){
-                throw (RuntimeException) exception;
-            } else {
-                throw new IllegalStateException(exception.getMessage());
-            }
-        }
-
-    }
-
     private final class ExpandedChecker implements SPFChecker {
         
         /**
@@ -108,7 +61,7 @@
         }
     }
 
-    private final class CleanupChecker implements SPFChecker {
+    private final class CleanupChecker implements SPFChecker, SPFCheckerExceptionCatcher {
       
         /**
         * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
@@ -119,10 +72,38 @@
             // After the redirect we should not use the
             // explanation from the orginal record
             spfData.setIgnoreExplanation(true);
-            
-            spfData.popExceptionCatcher();
             return null;
         }
+        
+        /**
+         * @see org.apache.james.jspf.core.SPFCheckerExceptionCatcher#onException(java.lang.Exception, org.apache.james.jspf.core.SPFSession)
+         */
+        public void onException(Exception exception, SPFSession session)
+                throws PermErrorException, NoneException,
+                TempErrorException, NeutralException {
+            
+            checkSPF(session);
+
+            // remove every checker until the initialized one
+            
+            if (exception instanceof NeutralException) {
+                throw new PermErrorException(
+                "included checkSPF returned NeutralException");
+
+            } else if (exception instanceof NoneException) {
+                // no spf record assigned to the redirect domain
+                throw new PermErrorException(
+                        "included checkSPF returned NoneException");
+            } else if (exception instanceof PermErrorException){
+                throw (PermErrorException) exception;
+            } else if (exception instanceof TempErrorException){
+                throw (TempErrorException) exception;
+            } else if (exception instanceof RuntimeException){
+                throw (RuntimeException) exception;
+            } else {
+                throw new IllegalStateException(exception.getMessage());
+            }
+        }
     }
 
     /**
@@ -139,8 +120,6 @@
 
     private SPFChecker expandedChecker = new ExpandedChecker();
 
-    private ExceptionCatcher exceptionCatcher = new ExceptionCatcher(cleanupChecker, cleanupChecker);
-
     /**
      * Set the host which should be used for redirection and set it in SPF1Data
      * so it can be accessed easy later if needed
@@ -166,8 +145,6 @@
             
             spfData.pushChecker(cleanupChecker);
             
-            spfData.pushExceptionCatcher(exceptionCatcher);
-
             spfData.pushChecker(expandedChecker);
             return macroExpand.checkExpand(getHost(), spfData, MacroExpand.DOMAIN);
         }



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