camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/3] git commit: CAMEL-6466: Allow configuring custom log formatter from uri parameters.
Date Sat, 31 Aug 2013 12:28:16 GMT
CAMEL-6466: Allow configuring custom log formatter from uri parameters.

Conflicts:
	camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d83f6eec
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d83f6eec
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d83f6eec

Branch: refs/heads/camel-2.11.x
Commit: d83f6eec9b3113319dc9be5e99dc0ca787df7cea
Parents: f4fdeb1
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Aug 31 13:58:02 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Aug 31 14:27:54 2013 +0200

----------------------------------------------------------------------
 .../camel/component/log/LogComponent.java       | 32 +++++------
 .../apache/camel/component/log/LogEndpoint.java | 57 +++++++++++++++++++-
 .../component/log/LogCustomFormatterTest.java   | 55 ++++++++++++++++++-
 3 files changed, 126 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d83f6eec/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java b/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
index 2828d29..cd81cfe 100644
--- a/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
+++ b/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
@@ -40,25 +40,29 @@ public class LogComponent extends DefaultComponent {
     
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
         LoggingLevel level = getLoggingLevel(parameters);
-        String marker = getAndRemoveParameter(parameters, "marker", String.class);
-        Integer groupSize = getAndRemoveParameter(parameters, "groupSize", Integer.class);
-        Long groupInterval = getAndRemoveParameter(parameters, "groupInterval", Long.class);
 
-        CamelLogger camelLogger = new CamelLogger(remaining, level, marker);
+        LogEndpoint endpoint = new LogEndpoint(uri, this);
+        endpoint.setLevel(level.name());
+        setProperties(endpoint, parameters);
+
+        CamelLogger camelLogger = new CamelLogger(remaining, level, endpoint.getMarker());
         Processor logger;
-        if (groupSize != null) {
-            logger = new ThroughputLogger(camelLogger, groupSize);
-        } else if (groupInterval != null) {
-            Boolean groupActiveOnly = getAndRemoveParameter(parameters, "groupActiveOnly",
Boolean.class, Boolean.TRUE);
-            Long groupDelay = getAndRemoveParameter(parameters, "groupDelay", Long.class);
-            logger = new ThroughputLogger(camelLogger, this.getCamelContext(), groupInterval,
groupDelay, groupActiveOnly);
+        if (endpoint.getGroupSize() != null) {
+            logger = new ThroughputLogger(camelLogger, endpoint.getGroupSize());
+        } else if (endpoint.getGroupInterval() != null) {
+            Boolean groupActiveOnly = endpoint.getGroupActiveOnly() != null ? endpoint.getGroupActiveOnly()
: Boolean.TRUE;
+            Long groupDelay = endpoint.getGroupDelay();
+            logger = new ThroughputLogger(camelLogger, this.getCamelContext(), endpoint.getGroupInterval(),
groupDelay, groupActiveOnly);
         } else {
             // first, try to use the user-specified formatter (or the one picked up from
the Registry and transferred to 
             // the property by a previous endpoint initialisation); if null, try to pick
it up from the Registry now
             ExchangeFormatter localFormatter = exchangeFormatter;
             if (localFormatter == null) {
                 localFormatter = getCamelContext().getRegistry().lookupByNameAndType("logFormatter",
ExchangeFormatter.class);
-                exchangeFormatter = localFormatter;
+                if (localFormatter != null) {
+                    exchangeFormatter = localFormatter;
+                    setProperties(exchangeFormatter, parameters);
+                }
             }
             // if no formatter is available in the Registry, create a local one of the default
type, for a single use
             if (localFormatter == null) {
@@ -68,9 +72,8 @@ public class LogComponent extends DefaultComponent {
             logger = new CamelLogProcessor(camelLogger, localFormatter);
         }
 
-        LogEndpoint endpoint = new LogEndpoint(uri, this);
-        setProperties(endpoint, parameters);
-        return new LogEndpoint(uri, this, logger);
+        endpoint.setLogger(logger);
+        return endpoint;
     }
 
     /**
@@ -89,7 +92,6 @@ public class LogComponent extends DefaultComponent {
      * Sets a custom {@link ExchangeFormatter} to convert the Exchange to a String suitable
for logging.
      * <p />
      * If not specified, we default to {@link LogFormatter}.
-     * @param exchangeFormatter
      */
     public void setExchangeFormatter(ExchangeFormatter exchangeFormatter) {
         this.exchangeFormatter = exchangeFormatter;

http://git-wip-us.apache.org/repos/asf/camel/blob/d83f6eec/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java
index 74a826e..9cbfffd 100644
--- a/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java
@@ -27,7 +27,14 @@ import org.apache.camel.util.ServiceHelper;
  */
 public class LogEndpoint extends ProcessorEndpoint {
 
-    private Processor logger;
+    private volatile Processor logger;
+
+    private String level;
+    private String marker;
+    private Integer groupSize;
+    private Long groupInterval;
+    private Boolean groupActiveOnly;
+    private Long groupDelay;
 
     public LogEndpoint() {
     }
@@ -70,4 +77,52 @@ public class LogEndpoint extends ProcessorEndpoint {
     protected String createEndpointUri() {
         return "log:" + logger.toString();
     }
+
+    public String getLevel() {
+        return level;
+    }
+
+    public void setLevel(String level) {
+        this.level = level;
+    }
+
+    public String getMarker() {
+        return marker;
+    }
+
+    public void setMarker(String marker) {
+        this.marker = marker;
+    }
+
+    public Integer getGroupSize() {
+        return groupSize;
+    }
+
+    public void setGroupSize(Integer groupSize) {
+        this.groupSize = groupSize;
+    }
+
+    public Long getGroupInterval() {
+        return groupInterval;
+    }
+
+    public void setGroupInterval(Long groupInterval) {
+        this.groupInterval = groupInterval;
+    }
+
+    public Boolean getGroupActiveOnly() {
+        return groupActiveOnly;
+    }
+
+    public void setGroupActiveOnly(Boolean groupActiveOnly) {
+        this.groupActiveOnly = groupActiveOnly;
+    }
+
+    public Long getGroupDelay() {
+        return groupDelay;
+    }
+
+    public void setGroupDelay(Long groupDelay) {
+        this.groupDelay = groupDelay;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/d83f6eec/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
b/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
index be25920..59047ef 100644
--- a/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
@@ -16,8 +16,10 @@
  */
 package org.apache.camel.component.log;
 
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
+import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.impl.PropertyPlaceholderDelegateRegistry;
 import org.apache.camel.spi.ExchangeFormatter;
@@ -85,6 +87,47 @@ public class LogCustomFormatterTest extends ContextTestSupport {
     }
 
     @Test
+    public void testCustomFormatterInRegistryOptions() throws Exception {
+        context.stop();
+
+        exchangeFormatter = new TestExchangeFormatter();
+        JndiRegistry registry = getRegistryAsJndi();
+        registry.bind("logFormatter", exchangeFormatter);
+        assertEquals("", exchangeFormatter.getPrefix());
+
+        context.start();
+
+        String endpointUri = "log:" + LogCustomFormatterTest.class.getCanonicalName() + "?prefix=foo";
+        template.requestBody(endpointUri, "Hello World");
+        template.requestBody(endpointUri, "Hello World");
+
+        assertEquals(2, exchangeFormatter.getCounter());
+        assertEquals("foo", exchangeFormatter.getPrefix());
+    }
+
+    @Test
+    public void testCustomFormatterInRegistryUnknownOption() throws Exception {
+        context.stop();
+
+        exchangeFormatter = new TestExchangeFormatter();
+        JndiRegistry registry = getRegistryAsJndi();
+        registry.bind("logFormatter", exchangeFormatter);
+        assertEquals("", exchangeFormatter.getPrefix());
+
+        context.start();
+
+        // unknown parameter
+        try {
+            String endpointUri2 = "log:" + LogCustomFormatterTest.class.getCanonicalName()
+ "?prefix=foo&bar=no";
+            template.requestBody(endpointUri2, "Hello World");
+            fail("Should have thrown exception");
+        } catch (Exception e) {
+            ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class,
e.getCause());
+            assertTrue(cause.getMessage().endsWith("Unknown parameters=[{bar=no}]"));
+        }
+    }
+
+    @Test
     public void testFormatterNotPickedUpWithDifferentKey() throws Exception {
         context.stop();
         
@@ -118,11 +161,12 @@ public class LogCustomFormatterTest extends ContextTestSupport {
     public static class TestExchangeFormatter implements ExchangeFormatter {
         private int counter;
         private boolean addTen;
+        private String prefix = "";
         
         @Override
         public String format(Exchange exchange) {
             counter += addTen ? 10 : 1;
-            return exchange.toString();
+            return prefix + exchange.toString();
         }
         
         public int getCounter() {
@@ -136,7 +180,14 @@ public class LogCustomFormatterTest extends ContextTestSupport {
         public void setAddTen(boolean addTen) {
             this.addTen = addTen;
         }
-        
+
+        public String getPrefix() {
+            return prefix;
+        }
+
+        public void setPrefix(String prefix) {
+            this.prefix = prefix;
+        }
     }
     
 }


Mime
View raw message