james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [09/13] james-project git commit: JAMES-2448 Avoid pushing logic into Apache Camel
Date Thu, 05 Jul 2018 02:01:31 GMT
JAMES-2448 Avoid pushing logic into Apache Camel

This comes at a huge price in readability, and when starting Apache Camel


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/85a44046
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/85a44046
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/85a44046

Branch: refs/heads/master
Commit: 85a440469094330beb7d7c8c152d9e9296ae11cb
Parents: 32d59cc
Author: benwa <btellier@linagora.com>
Authored: Mon Jul 2 00:13:36 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Thu Jul 5 08:58:13 2018 +0700

----------------------------------------------------------------------
 .../mailetcontainer/impl/MatcherMailetPair.java |   5 +
 .../impl/camel/CamelMailetProcessor.java        | 104 +++++++++----------
 .../impl/camel/CamelProcessor.java              |  16 ++-
 .../impl/camel/MatcherSplitter.java             |   7 +-
 .../lib/AbstractStateMailetProcessor.java       |   2 +-
 5 files changed, 66 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/85a44046/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/MatcherMailetPair.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/MatcherMailetPair.java
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/MatcherMailetPair.java
index 0c25977..b2d1540 100644
--- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/MatcherMailetPair.java
+++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/MatcherMailetPair.java
@@ -51,4 +51,9 @@ public class MatcherMailetPair {
         return mailet;
     }
 
+    public String getOnMatchException() {
+        return mailet.getMailetConfig()
+            .getInitParameter("onMatchException");
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/85a44046/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelMailetProcessor.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelMailetProcessor.java
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelMailetProcessor.java
index 2410374..1c9a473 100644
--- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelMailetProcessor.java
+++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelMailetProcessor.java
@@ -28,17 +28,16 @@ import org.apache.camel.CamelContextAware;
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
-import org.apache.camel.Processor;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
+import org.apache.james.lifecycle.api.LifecycleUtil;
 import org.apache.james.mailetcontainer.impl.MatcherMailetPair;
 import org.apache.james.mailetcontainer.lib.AbstractStateMailetProcessor;
 import org.apache.james.metrics.api.MetricFactory;
 import org.apache.mailet.Mail;
 import org.apache.mailet.Mailet;
-import org.apache.mailet.MailetConfig;
 import org.apache.mailet.Matcher;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -56,7 +55,6 @@ public class CamelMailetProcessor extends AbstractStateMailetProcessor implement
 
     private ProducerTemplate producerTemplate;
 
-    private final UseLatestAggregationStrategy aggr = new UseLatestAggregationStrategy();
     private final MetricFactory metricFactory;
     private List<MatcherMailetPair> pairs;
 
@@ -114,7 +112,7 @@ public class CamelMailetProcessor extends AbstractStateMailetProcessor
implement
     protected void setupRouting(List<MatcherMailetPair> pairs) throws MessagingException
{
         try {
             this.pairs = pairs;
-            context.addRoutes(new MailetContainerRouteBuilder(pairs));
+            context.addRoutes(new MailetContainerRouteBuilder(this, metricFactory, pairs));
         } catch (Exception e) {
             throw new MessagingException("Unable to setup routing for MailetMatcherPairs",
e);
         }
@@ -124,84 +122,82 @@ public class CamelMailetProcessor extends AbstractStateMailetProcessor
implement
      * {@link RouteBuilder} which construct the Matcher and Mailet routing use
      * Camel DSL
      */
-    private final class MailetContainerRouteBuilder extends RouteBuilder {
+    private static class MailetContainerRouteBuilder extends RouteBuilder {
+
+        private final CamelMailetProcessor container;
 
         private final List<MatcherMailetPair> pairs;
+        private final MetricFactory metricFactory;
 
-        public MailetContainerRouteBuilder(List<MatcherMailetPair> pairs) {
+        private MailetContainerRouteBuilder(CamelMailetProcessor container, MetricFactory
metricFactory, List<MatcherMailetPair> pairs) {
+            this.container = container;
+            this.metricFactory = metricFactory;
             this.pairs = pairs;
         }
 
         @Override
         public void configure() {
-            Processor disposeProcessor = new DisposeProcessor();
-            Processor completeProcessor = new CompleteProcessor();
-            Processor stateChangedProcessor = new StateChangedProcessor();
-
-            String state = getState();
+            String state = container.getState();
+            CamelProcessor terminatingMailetProcessor = new CamelProcessor(metricFactory,
container, new TerminatingMailet());
 
-            RouteDefinition processorDef = from(getEndpoint())
+            RouteDefinition processorDef = from(container.getEndpoint())
                 .routeId(state)
                 .setExchangePattern(ExchangePattern.InOnly);
 
             for (MatcherMailetPair pair : pairs) {
-                Matcher matcher = pair.getMatcher();
-                Mailet mailet = pair.getMailet();
-
-                MailetConfig mailetConfig = mailet.getMailetConfig();
-                String onMatchException = mailetConfig.getInitParameter("onMatchException");
-
-                CamelProcessor mailetProccessor = new CamelProcessor(metricFactory, mailet,
CamelMailetProcessor.this);
-                // Store the matcher to use for splitter in properties
-                MatcherSplitter matcherSplitter = new MatcherSplitter(metricFactory, CamelMailetProcessor.this,
matcher, onMatchException);
+                CamelProcessor mailetProccessor = new CamelProcessor(metricFactory, container,
pair.getMailet());
+                MatcherSplitter matcherSplitter = new MatcherSplitter(metricFactory, container,
pair);
 
                 processorDef
                         // do splitting of the mail based on the stored matcher
-                        .split().method(matcherSplitter).aggregationStrategy(aggr)
-
-                        .choice().when(new MatcherMatch()).process(mailetProccessor).end()
-
-                        .choice().when(new MailStateEquals(Mail.GHOST)).process(disposeProcessor).stop().end()
-
-                        .choice().when(new MailStateNotEquals(state)).process(stateChangedProcessor).process(completeProcessor).stop().end();
+                        .split().method(matcherSplitter)
+                            .aggregationStrategy(new UseLatestAggregationStrategy())
+                        .process(exchange -> handleMailet(exchange, container, mailetProccessor));
             }
 
-            Processor terminatingMailetProcessor = new CamelProcessor(metricFactory, new
TerminatingMailet(), CamelMailetProcessor.this);
-
             processorDef
-                    // start choice
-                    .choice()
-
-                    // when the mail state did not change till yet ( the end of
-                    // the route) we need to call the TerminatingMailet to
-                    // make sure we don't fall into a endless loop
-                    .when(new MailStateEquals(state)).process(terminatingMailetProcessor).stop()
-
-                    // dispose when needed
-                    .when(new MailStateEquals(Mail.GHOST)).process(disposeProcessor).stop()
-
-                    // this container is complete
-                    .otherwise().process(completeProcessor).stop();
+                .process(exchange -> terminateSmoothly(exchange, container, terminatingMailetProcessor));
 
         }
 
-        private final class CompleteProcessor implements Processor {
+        private void terminateSmoothly(Exchange exchange, CamelMailetProcessor container,
CamelProcessor terminatingMailetProcessor) throws Exception {
+            Mail mail = exchange.getIn().getBody(Mail.class);
+            if (mail.getState().equals(container.getState())) {
+                terminatingMailetProcessor.process(mail);
+            }
+            if (mail.getState().equals(Mail.GHOST)) {
+                dispose(exchange, mail);
+            }
+            complete(exchange, container);
+        }
 
-            @Override
-            public void process(Exchange ex) {
-                LOGGER.debug("End of mailetprocessor for state {} reached", getState());
-                ex.setProperty(Exchange.ROUTE_STOP, true);
+        private void handleMailet(Exchange exchange, CamelMailetProcessor container, CamelProcessor
mailetProccessor) throws Exception {
+            Mail mail = exchange.getIn().getBody(Mail.class);
+            boolean isMatched = mail.removeAttribute(MatcherSplitter.MATCHER_MATCHED_ATTRIBUTE)
!= null;
+            if (isMatched) {
+                mailetProccessor.process(mail);
+            }
+            if (mail.getState().equals(Mail.GHOST)) {
+                dispose(exchange, mail);
+                return;
+            }
+            if (!mail.getState().equals(container.getState())) {
+                container.toProcessor(mail);
+                complete(exchange, container);
             }
         }
 
-        private final class StateChangedProcessor implements Processor {
+        private void complete(Exchange exchange, CamelMailetProcessor container) {
+            LOGGER.debug("End of mailetprocessor for state {} reached", container.getState());
+            exchange.setProperty(Exchange.ROUTE_STOP, true);
+        }
 
-            @Override
-            public void process(Exchange arg0) throws Exception {
-                Mail mail = arg0.getIn().getBody(Mail.class);
-                toProcessor(mail);
-            }
+        private void dispose(Exchange exchange, Mail mail) throws MessagingException {
+            LifecycleUtil.dispose(mail.getMessage());
+            LifecycleUtil.dispose(mail);
 
+            // stop routing
+            exchange.setProperty(Exchange.ROUTE_STOP, true);
         }
 
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/85a44046/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelProcessor.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelProcessor.java
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelProcessor.java
index 3b3eba8..a1ae59a 100644
--- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelProcessor.java
+++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/CamelProcessor.java
@@ -22,8 +22,6 @@ import java.io.Closeable;
 import java.util.List;
 import java.util.Locale;
 
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 import org.apache.james.mailetcontainer.impl.MailetConfigImpl;
 import org.apache.james.mailetcontainer.impl.ProcessorUtil;
 import org.apache.james.mailetcontainer.lib.AbstractStateMailetProcessor.MailetProcessorListener;
@@ -42,7 +40,7 @@ import com.google.common.collect.ImmutableList;
 /**
  * Mailet wrapper which execute a Mailet in a Processor
  */
-public class CamelProcessor implements Processor {
+public class CamelProcessor {
     private static final Logger LOGGER = LoggerFactory.getLogger(CamelProcessor.class);
 
     private final MetricFactory metricFactory;
@@ -51,22 +49,20 @@ public class CamelProcessor implements Processor {
 
     /**
      * Mailet to call on process
-     *
-     * @param metricFactory
+     *  @param metricFactory
+     * @param processor
      * @param mailet
      */
-    public CamelProcessor(MetricFactory metricFactory, Mailet mailet, CamelMailetProcessor
processor) {
+    public CamelProcessor(MetricFactory metricFactory, CamelMailetProcessor processor, Mailet
mailet) {
         this.metricFactory = metricFactory;
-        this.mailet = mailet;
         this.processor = processor;
+        this.mailet = mailet;
     }
 
     /**
      * Call the wrapped mailet for the exchange
      */
-    @Override
-    public void process(Exchange exchange) throws Exception {
-        Mail mail = exchange.getIn().getBody(Mail.class);
+    public void process(Mail mail) throws Exception {
         long start = System.currentTimeMillis();
         TimeMetric timeMetric = metricFactory.timer(mailet.getClass().getSimpleName());
         Exception ex = null;

http://git-wip-us.apache.org/repos/asf/james-project/blob/85a44046/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/MatcherSplitter.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/MatcherSplitter.java
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/MatcherSplitter.java
index 016a0c9..21d721a 100644
--- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/MatcherSplitter.java
+++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/camel/MatcherSplitter.java
@@ -32,6 +32,7 @@ import org.apache.camel.Body;
 import org.apache.camel.Handler;
 import org.apache.camel.InOnly;
 import org.apache.james.core.MailAddress;
+import org.apache.james.mailetcontainer.impl.MatcherMailetPair;
 import org.apache.james.mailetcontainer.impl.ProcessorUtil;
 import org.apache.james.mailetcontainer.lib.AbstractStateMailetProcessor.MailetProcessorListener;
 import org.apache.james.metrics.api.MetricFactory;
@@ -61,11 +62,11 @@ public class MatcherSplitter {
     private final Matcher matcher;
     private final String onMatchException;
 
-    public MatcherSplitter(MetricFactory metricFactory, CamelMailetProcessor container, Matcher
matcher, String onMatchException) {
+    public MatcherSplitter(MetricFactory metricFactory, CamelMailetProcessor container, MatcherMailetPair
pair) {
         this.metricFactory = metricFactory;
         this.container = container;
-        this.matcher = matcher;
-        this.onMatchException = Optional.ofNullable(onMatchException)
+        this.matcher = pair.getMatcher();
+        this.onMatchException = Optional.ofNullable(pair.getOnMatchException())
             .map(s -> s.trim().toLowerCase(Locale.US))
             .orElse(Mail.ERROR);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/85a44046/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessor.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessor.java
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessor.java
index 4666e98..99f3b1b 100644
--- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessor.java
+++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessor.java
@@ -383,7 +383,7 @@ public abstract class AbstractStateMailetProcessor implements MailProcessor,
Con
      * Mailet which protect us to not fall into an endless loop caused by an
      * configuration error
      */
-    public final class TerminatingMailet extends GenericMailet {
+    public static class TerminatingMailet extends GenericMailet {
         /**
          * The name of the mailet used to terminate the mailet chain. The end of
          * the matcher/mailet chain must be a matcher that matches all mails and


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


Mime
View raw message