You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/01/09 12:24:06 UTC

[camel] branch master updated (9826d4d -> 3fad765)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from 9826d4d  Upgrade Libphonenumber to version 8.11.1
     new 488a0ad  Added method to get hostname safely
     new 3fad765  CAMEL-14380: camel-core - Add hostname function to simple language

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 core/camel-base/src/main/docs/simple-language.adoc |  2 ++
 .../simple/ast/SimpleFunctionExpression.java       |  2 ++
 .../apache/camel/language/simple/SimpleTest.java   |  8 +++++++
 .../org/apache/camel/util/InetAddressUtilTest.java |  5 ++++
 .../camel/support/builder/ExpressionBuilder.java   | 17 ++++++++++++++
 .../org/apache/camel/util/InetAddressUtil.java     | 27 ++++++++++++++++++++++
 6 files changed, 61 insertions(+)


[camel] 01/02: Added method to get hostname safely

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 488a0ad6eab95b029ca6da8c11622a3a2e3462b3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jan 9 13:07:13 2020 +0100

    Added method to get hostname safely
---
 .../org/apache/camel/util/InetAddressUtilTest.java |  5 ++++
 .../org/apache/camel/util/InetAddressUtil.java     | 27 ++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/core/camel-core/src/test/java/org/apache/camel/util/InetAddressUtilTest.java b/core/camel-core/src/test/java/org/apache/camel/util/InetAddressUtilTest.java
index e0e539c..d9c6fe7 100644
--- a/core/camel-core/src/test/java/org/apache/camel/util/InetAddressUtilTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/util/InetAddressUtilTest.java
@@ -32,4 +32,9 @@ public class InetAddressUtilTest extends Assert {
             // ignore if this test is run on a OS which cannot resolve hostname
         }
     }
+
+    @Test
+    public void testGetLocalHostNameSafe() {
+        InetAddressUtil.getLocalHostNameSafe();
+    }
 }
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/InetAddressUtil.java b/core/camel-util/src/main/java/org/apache/camel/util/InetAddressUtil.java
index bef17e7..a7b2c92 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/InetAddressUtil.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/InetAddressUtil.java
@@ -62,4 +62,31 @@ public final class InetAddressUtil {
         }
     }
 
+    /**
+     * When using the {@link java.net.InetAddress#getHostName()} method in an
+     * environment where neither a proper DNS lookup nor an <tt>/etc/hosts</tt>
+     * entry exists for a given host, the following exception will be thrown:
+     * <p/>
+     * <code>
+     * java.net.UnknownHostException: &lt;hostname&gt;: &lt;hostname&gt;
+     * at java.net.InetAddress.getLocalHost(InetAddress.java:1425)
+     * ...
+     * </code>
+     * <p/>
+     * Instead of just throwing an UnknownHostException and giving up, this
+     * method grabs a suitable hostname from the exception and prevents the
+     * exception from being thrown. If a suitable hostname cannot be acquired
+     * from the exception, then <tt>null</tt> is returned
+     *
+     * @return the hostname, or <tt>null</tt> if not possible to resolve
+     */
+    public static String getLocalHostNameSafe() {
+        try {
+            return getLocalHostName();
+        } catch (Throwable e) {
+            // ignore
+        }
+        return null;
+    }
+
 }


[camel] 02/02: CAMEL-14380: camel-core - Add hostname function to simple language

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 3fad765c1594020a050639b7ea1b2d4463dae3cd
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jan 9 13:12:08 2020 +0100

    CAMEL-14380: camel-core - Add hostname function to simple language
---
 core/camel-base/src/main/docs/simple-language.adoc      |  2 ++
 .../language/simple/ast/SimpleFunctionExpression.java   |  2 ++
 .../org/apache/camel/language/simple/SimpleTest.java    |  8 ++++++++
 .../apache/camel/support/builder/ExpressionBuilder.java | 17 +++++++++++++++++
 4 files changed, 29 insertions(+)

diff --git a/core/camel-base/src/main/docs/simple-language.adoc b/core/camel-base/src/main/docs/simple-language.adoc
index 793ec83..96e2fee 100644
--- a/core/camel-base/src/main/docs/simple-language.adoc
+++ b/core/camel-base/src/main/docs/simple-language.adoc
@@ -192,6 +192,8 @@ Exchange is being routed.
 |threadName |String |Returns the name of the current thread. Can be used for
 logging purpose.
 
+|hostname |String |Returns the local hostname (may be empty if not possible to resolve).
+
 |ref:xxx |Object |To lookup a bean from the Registry with
 the given id.
 
diff --git a/core/camel-base/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/core/camel-base/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index adabcdc..22de774 100644
--- a/core/camel-base/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/core/camel-base/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -390,6 +390,8 @@ public class SimpleFunctionExpression extends LiteralExpression {
             return ExpressionBuilder.exchangeExceptionStackTraceExpression();
         } else if (ObjectHelper.equal(expression, "threadName")) {
             return ExpressionBuilder.threadNameExpression();
+        } else if (ObjectHelper.equal(expression, "hostname")) {
+            return ExpressionBuilder.hostnameExpression();
         } else if (ObjectHelper.equal(expression, "camelId")) {
             return ExpressionBuilder.camelContextNameExpression();
         } else if (ObjectHelper.equal(expression, "routeId")) {
diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 766df56..311f1a8 100644
--- a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -40,6 +40,7 @@ import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.language.bean.RuntimeBeanExpressionException;
 import org.apache.camel.language.simple.types.SimpleIllegalSyntaxException;
 import org.apache.camel.spi.Language;
+import org.apache.camel.util.InetAddressUtil;
 import org.junit.Test;
 
 public class SimpleTest extends LanguageTestSupport {
@@ -236,6 +237,13 @@ public class SimpleTest extends LanguageTestSupport {
     }
 
     @Test
+    public void testSimpleHostname() throws Exception {
+        String name = InetAddressUtil.getLocalHostNameSafe();
+        assertExpression("${hostname}", name);
+        assertExpression("The host is ${hostname}", "The host is " + name);
+    }
+
+    @Test
     public void testSimpleStepId() throws Exception {
         assertExpression("${stepId}", null);
         exchange.setProperty(Exchange.STEP_ID, "foo");
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
index 48bdd26..9085cd5 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
@@ -47,6 +47,7 @@ import org.apache.camel.support.GroupIterator;
 import org.apache.camel.support.GroupTokenIterator;
 import org.apache.camel.support.LanguageSupport;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.InetAddressUtil;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.Scanner;
 import org.apache.camel.util.StringHelper;
@@ -735,6 +736,22 @@ public class ExpressionBuilder {
     }
 
     /**
+     * Returns the expression for the local hostname
+     */
+    public static Expression hostnameExpression() {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                return InetAddressUtil.getLocalHostNameSafe();
+            }
+
+            @Override
+            public String toString() {
+                return "hostname";
+            }
+        };
+    }
+
+    /**
      * Returns the expression for the current step id (if any)
      */
     public static Expression stepIdExpression() {