You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2018/02/13 01:55:02 UTC
[text] [TEXT-117] Add a local host string lookup:
LocalHostStringLookup
Repository: commons-text
Updated Branches:
refs/heads/master 2f9ba5529 -> 7b783fd1f
[TEXT-117] Add a local host string lookup: LocalHostStringLookup
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/7b783fd1
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/7b783fd1
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/7b783fd1
Branch: refs/heads/master
Commit: 7b783fd1fe04347769ec7c1402992941c9076bea
Parents: 2f9ba55
Author: Gary Gregory <ga...@gmail.com>
Authored: Mon Feb 12 18:54:59 2018 -0700
Committer: Gary Gregory <ga...@gmail.com>
Committed: Mon Feb 12 18:54:59 2018 -0700
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
.../text/lookup/InterpolatorStringLookup.java | 1 +
.../text/lookup/LocalHostStringLookup.java | 81 ++++++++++++++++++++
.../text/lookup/StringLookupFactory.java | 22 +++++-
...titutorWithInterpolatorStringLookupTest.java | 24 ++++++
.../text/lookup/LocalHostStringLookupTest.java | 45 +++++++++++
6 files changed, 172 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/7b783fd1/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4f1b8a6..114de08 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -52,6 +52,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="TEXT-114" type="add" dev="ggregory">Add a StrSubstitutor replacement based on interfaces: StringSubstitutor</action>
<action issue="TEXT-115" type="add" dev="ggregory">Add a StrBuilder replacement based on the StringMatcher interface: TextStringBuilder</action>
<action issue="TEXT-116" type="add" dev="ggregory">Add a StrTokenizer replacement based on the StringMatcher interface: StringTokenizer</action>
+ <action issue="TEXT-117" type="add" dev="ggregory">Add a local host string lookup: LocalHostStringLookup</action>
</release>
<release version="1.2" date="2017-12-12" description="Release 1.2">
http://git-wip-us.apache.org/repos/asf/commons-text/blob/7b783fd1/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
index 0a7b1b2..798c234 100644
--- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
@@ -82,6 +82,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
stringLookupMap.put("env", EnvironmentVariableStringLookup.INSTANCE);
stringLookupMap.put("java", JavaPlatformStringLookup.INSTANCE);
stringLookupMap.put("date", DateStringLookup.INSTANCE);
+ stringLookupMap.put("localhost", LocalHostStringLookup.INSTANCE);
}
/**
http://git-wip-us.apache.org/repos/asf/commons-text/blob/7b783fd1/src/main/java/org/apache/commons/text/lookup/LocalHostStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/LocalHostStringLookup.java b/src/main/java/org/apache/commons/text/lookup/LocalHostStringLookup.java
new file mode 100644
index 0000000..e2970e7
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/lookup/LocalHostStringLookup.java
@@ -0,0 +1,81 @@
+/*
+ * 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.commons.text.lookup;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ * Looks up keys related to the local host: host name, canonical host name, host address.
+ * <p>
+ * The lookup keys are:
+ * </p>
+ * <ul>
+ * <li><b>name</b>: for the local host name, for example {@code EXAMPLE}.</li>
+ * <li><b>canonical-name</b>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li>
+ * <li><b>address</b>: for the local host address, for example {@code 192.168.56.1}.</li>
+ * </ul>
+ *
+ * @since 1.3
+ */
+final class LocalHostStringLookup extends AbstractStringLookup {
+
+ /**
+ * Defines the singleton for this class.
+ */
+ static final LocalHostStringLookup INSTANCE = new LocalHostStringLookup();
+
+ /**
+ * No need to build instances for now.
+ */
+ private LocalHostStringLookup() {
+ // empty
+ }
+
+ /**
+ * Looks up the value of the Java platform key.
+ *
+ * @param key
+ * the key to be looked up, may be null
+ * @return The value of the environment variable.
+ */
+ @Override
+ public String lookup(final String key) {
+ switch (key) {
+ case "name":
+ try {
+ return InetAddress.getLocalHost().getHostName();
+ } catch (final UnknownHostException e) {
+ return null;
+ }
+ case "canonical-name":
+ try {
+ return InetAddress.getLocalHost().getCanonicalHostName();
+ } catch (final UnknownHostException e) {
+ return null;
+ }
+ case "address":
+ try {
+ return InetAddress.getLocalHost().getHostAddress();
+ } catch (final UnknownHostException e) {
+ return null;
+ }
+ default:
+ throw new IllegalArgumentException(key);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/7b783fd1/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
index ce63ba8..ac4d4c9 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -49,7 +49,8 @@ public final class StringLookupFactory {
}
/**
- * Returns the EnvironmentVariableStringLookup singleton instance.
+ * Returns the EnvironmentVariableStringLookup singleton instance where the lookup key is an environment variable
+ * name.
*
* @return the EnvironmentVariableStringLookup singleton instance.
*/
@@ -67,6 +68,7 @@ public final class StringLookupFactory {
* <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <li>"date" for the {@link DateStringLookup}.</li>
+ * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
* </ul>
*
* @return a new InterpolatorStringLookup.
@@ -85,6 +87,7 @@ public final class StringLookupFactory {
* <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <li>"date" for the {@link DateStringLookup}.</li>
+ * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
* </ul>
*
* @param <V>
@@ -107,6 +110,7 @@ public final class StringLookupFactory {
* <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <li>"date" for the {@link DateStringLookup}.</li>
+ * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
* </ul>
*
* @param defaultStringLookup
@@ -127,6 +131,20 @@ public final class StringLookupFactory {
}
/**
+ * Returns the LocalHostStringLookup singleton instance where the lookup key is one of:
+ * <ul>
+ * <li><b>name</b>: for the local host name, for example {@code EXAMPLE}.</li>
+ * <li><b>canonical-name</b>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li>
+ * <li><b>address</b>: for the local host address, for example {@code 192.168.56.1}.</li>
+ * </ul>
+ *
+ * @return the DateStringLookup singleton instance.
+ */
+ public StringLookup localHostStringLookup() {
+ return LocalHostStringLookup.INSTANCE;
+ }
+
+ /**
* Returns a new map-based lookup where the request for a lookup is answered with the value for that key.
*
* @param <V>
@@ -164,7 +182,7 @@ public final class StringLookupFactory {
}
/**
- * Returns the SystemPropertyStringLookup singleton instance where the key is a system property name.
+ * Returns the SystemPropertyStringLookup singleton instance where the lookup key is a system property name.
*
* @return the SystemPropertyStringLookup singleton instance.
*/
http://git-wip-us.apache.org/repos/asf/commons-text/blob/7b783fd1/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
index e8bca78..bfac9c8 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
@@ -17,6 +17,8 @@
package org.apache.commons.text;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
@@ -27,6 +29,28 @@ import org.junit.Test;
public class StringSubstitutorWithInterpolatorStringLookupTest {
@Test
+ public void testLocalHostLookup_Address() throws UnknownHostException {
+ final StringSubstitutor strSubst = new StringSubstitutor(
+ StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ Assert.assertEquals(InetAddress.getLocalHost().getHostAddress(), strSubst.replace("${localhost:address}"));
+ }
+
+ @Test
+ public void testLocalHostLookup_CanonicalName() throws UnknownHostException {
+ final StringSubstitutor strSubst = new StringSubstitutor(
+ StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ Assert.assertEquals(InetAddress.getLocalHost().getCanonicalHostName(),
+ strSubst.replace("${localhost:canonical-name}"));
+ }
+
+ @Test
+ public void testLocalHostLookup_Name() throws UnknownHostException {
+ final StringSubstitutor strSubst = new StringSubstitutor(
+ StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ Assert.assertEquals(InetAddress.getLocalHost().getHostName(), strSubst.replace("${localhost:name}"));
+ }
+
+ @Test
public void testMapAndSystemProperty() {
final String key = "key";
final String value = "value";
http://git-wip-us.apache.org/repos/asf/commons-text/blob/7b783fd1/src/test/java/org/apache/commons/text/lookup/LocalHostStringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/lookup/LocalHostStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/LocalHostStringLookupTest.java
new file mode 100644
index 0000000..ffcaf1c
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/lookup/LocalHostStringLookupTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.commons.text.lookup;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class LocalHostStringLookupTest {
+
+ @Test
+ public void testAddress() throws UnknownHostException {
+ Assert.assertEquals(LocalHostStringLookup.INSTANCE.lookup("address"),
+ InetAddress.getLocalHost().getHostAddress());
+ }
+
+ @Test
+ public void testCanonicalName() throws UnknownHostException {
+ Assert.assertEquals(LocalHostStringLookup.INSTANCE.lookup("canonical-name"),
+ InetAddress.getLocalHost().getCanonicalHostName());
+ }
+
+ @Test
+ public void testName() throws UnknownHostException {
+ Assert.assertEquals(LocalHostStringLookup.INSTANCE.lookup("name"), InetAddress.getLocalHost().getHostName());
+ }
+
+}