camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] camel git commit: CAMEL-8780 Fixed the issue of setting exec command headers
Date Mon, 18 May 2015 05:52:19 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.14.x 1426de82c -> 35cbfbb14
  refs/heads/camel-2.15.x 1d2166e95 -> a696a468c


CAMEL-8780 Fixed the issue of setting exec command headers


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

Branch: refs/heads/camel-2.15.x
Commit: a696a468c16bdc24b4e9d942b752d227f86c52bb
Parents: 1d2166e
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Mon May 18 13:25:22 2015 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Mon May 18 13:51:50 2015 +0800

----------------------------------------------------------------------
 .../simple/SimpleParserExpressionTest.java      |  9 +++
 .../component/exec/impl/DefaultExecBinding.java | 26 ++++++++-
 .../exec/impl/DefaultExecCommandExecutor.java   |  1 -
 .../component/exec/DefaultExecBindingTest.java  | 60 ++++++++++++++++++++
 .../camel/component/snmp/SnmpConverters.java    | 38 ++++++++-----
 5 files changed, 115 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a696a468/camel-core/src/test/java/org/apache/camel/language/simple/SimpleParserExpressionTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleParserExpressionTest.java
b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleParserExpressionTest.java
index 4da2b9c..bbc2316 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleParserExpressionTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleParserExpressionTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.language.simple;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.camel.ExchangeTestSupport;
@@ -37,6 +38,14 @@ public class SimpleParserExpressionTest extends ExchangeTestSupport {
 
         assertEquals("'Hello'", exp.evaluate(exchange, String.class));
     }
+    
+    public void testSimpleStringList() throws Exception {
+        SimpleExpressionParser parser = new SimpleExpressionParser("\"Hello\" \"World\"",
true);
+        Expression exp = parser.parseExpression();
+
+        assertEquals("\"Hello\" \"World\"", exp.evaluate(exchange, String.class));
+    }
+    
 
     public void testSimpleSingleQuoteWithFunction() throws Exception {
         exchange.getIn().setBody("World");

http://git-wip-us.apache.org/repos/asf/camel/blob/a696a468/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecBinding.java
b/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecBinding.java
index ac87b56..5e51a7f 100644
--- a/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecBinding.java
+++ b/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecBinding.java
@@ -29,7 +29,6 @@ import org.apache.camel.component.exec.ExecResult;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 import static org.apache.camel.component.exec.impl.ExecParseUtils.splitToWhiteSpaceSeparatedTokens;
 
 /**
@@ -55,8 +54,12 @@ public class DefaultExecBinding implements ExecBinding {
         boolean useStderrOnEmptyStdout = getAndRemoveHeader(exchange.getIn(), EXEC_USE_STDERR_ON_EMPTY_STDOUT,
endpoint.isUseStderrOnEmptyStdout(), Boolean.class);
         InputStream input = exchange.getIn().getBody(InputStream.class);
 
-        // try to convert args to list at fist
-        List<String> argsList = exchange.getContext().getTypeConverter().convertTo(List.class,
exchange, args);
+        // If the args is a list of strings already..
+        List<String> argsList = null;
+        if (isListOfStrings(args)) {
+            argsList = (List<String>) args;
+        }
+
         if (argsList == null) {
             // no we could not do that, then parse it as a string to a list
             String s = endpoint.getArgs();
@@ -72,6 +75,23 @@ public class DefaultExecBinding implements ExecBinding {
         return new ExecCommand(cmd, argsList, dir, timeout, input, outFile, useStderrOnEmptyStdout);
     }
 
+    private boolean isListOfStrings(Object o) {
+        if (o == null) {
+            return false;
+        }
+        if (!(o instanceof List)) {
+            return false;
+        }
+        @SuppressWarnings("rawtypes")
+        List argsList = (List)o;
+        for (Object s : argsList) {
+            if (s.getClass() != String.class) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     public void writeOutput(Exchange exchange, ExecResult result) {
         ObjectHelper.notNull(exchange, "exchange");
         ObjectHelper.notNull(result, "result");

http://git-wip-us.apache.org/repos/asf/camel/blob/a696a468/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecCommandExecutor.java
----------------------------------------------------------------------
diff --git a/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecCommandExecutor.java
b/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecCommandExecutor.java
index 4ca391d..9011231 100644
--- a/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecCommandExecutor.java
+++ b/components/camel-exec/src/main/java/org/apache/camel/component/exec/impl/DefaultExecCommandExecutor.java
@@ -23,7 +23,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 
-import org.apache.camel.Exchange;
 import org.apache.camel.component.exec.ExecCommand;
 import org.apache.camel.component.exec.ExecCommandExecutor;
 import org.apache.camel.component.exec.ExecDefaultExecutor;

http://git-wip-us.apache.org/repos/asf/camel/blob/a696a468/components/camel-exec/src/test/java/org/apache/camel/component/exec/DefaultExecBindingTest.java
----------------------------------------------------------------------
diff --git a/components/camel-exec/src/test/java/org/apache/camel/component/exec/DefaultExecBindingTest.java
b/components/camel-exec/src/test/java/org/apache/camel/component/exec/DefaultExecBindingTest.java
new file mode 100644
index 0000000..62fcc23
--- /dev/null
+++ b/components/camel-exec/src/test/java/org/apache/camel/component/exec/DefaultExecBindingTest.java
@@ -0,0 +1,60 @@
+/**
+ * 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.camel.component.exec;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.camel.Component;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.exec.impl.DefaultExecBinding;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DefaultExecBindingTest extends CamelTestSupport {
+   
+    @Test     
+    public void testReadInput() throws Exception {
+        ExecCommand command = readInput("exec:test", Collections.EMPTY_LIST);
+        Assert.assertEquals("Get a wrong args.", Collections.EMPTY_LIST, command.getArgs());
+        List<String> args = Arrays.asList("arg1", "arg2");
+        command = readInput("exec:test", args);
+        assertEquals("Get a wrong args.", args, command.getArgs());
+        
+        command = readInput("exec:test", "arg1 arg2");
+        assertEquals("Get a wrong args.", args, command.getArgs());
+        
+        command = readInput("exec:test?args=arg1 arg2", null);
+        assertEquals("Get a wrong args.", args, command.getArgs());
+    }
+    
+    private ExecCommand readInput(String execEndpointUri, Object args) throws Exception {
+        DefaultExecBinding binding = new DefaultExecBinding();
+        ExecEndpoint execEndpoint = createExecEndpoint(execEndpointUri);
+        Exchange exchange = execEndpoint.createExchange();
+        exchange.getIn().setHeader(ExecBinding.EXEC_COMMAND_ARGS, args);
+        return binding.readInput(exchange, execEndpoint);
+    }
+    
+    private ExecEndpoint createExecEndpoint(String uri) throws Exception {
+        Component component = context.getComponent("exec");
+        return (ExecEndpoint)component.createEndpoint(uri);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/a696a468/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
----------------------------------------------------------------------
diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
index 9bfd9de..990cbc8 100644
--- a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
+++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpConverters.java
@@ -46,26 +46,34 @@ public final class SnmpConverters {
     }
 
     @Converter
+    // Camel could use this method to convert the String into a List
     public static OIDList toOIDList(String s, Exchange exchange) {
-        OIDList list = new OIDList();
+        try {
+            OIDList list = new OIDList();
 
-        if (s != null && s.indexOf(",") != -1) {
-            // seems to be a comma separated oid list
-            StringTokenizer strTok = new StringTokenizer(s, ",");
-            while (strTok.hasMoreTokens()) {
-                String tok = strTok.nextToken();
-                if (tok != null && tok.trim().length() > 0) {
-                    list.add(new OID(tok.trim()));
-                } else {
-                    // empty token - skip
+            if (s != null && s.indexOf(",") != -1) {
+                // seems to be a comma separated oid list
+                StringTokenizer strTok = new StringTokenizer(s, ",");
+                while (strTok.hasMoreTokens()) {
+                    String tok = strTok.nextToken();
+                    if (tok != null && tok.trim().length() > 0) {
+                        list.add(new OID(tok.trim()));
+                    } else {
+                        // empty token - skip
+                    }
                 }
+            } else if (s != null) {
+                // maybe a single oid
+                list.add(new OID(s.trim()));
             }
-        } else if (s != null) {
-            // maybe a single oid
-            list.add(new OID(s.trim()));
-        }
 
-        return list;
+            return list;
+        } catch (Throwable e) {
+            // return null if we can't convert without an error 
+            // and it could let camel to choice the other converter to do the job
+            // new OID(...) will throw NumberFormatException if it's not a valid OID
+            return null;
+        }
     }
 
     private static void entryAppend(StringBuilder sb, String tag, String value) {


Mime
View raw message