Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/292#discussion_r74410438
--- Diff:
core/src/main/java/org/apache/brooklyn/util/core/BrooklynNetworkUtils.java ---
@@ -19,24 +19,52 @@
package org.apache.brooklyn.util.core;
import java.net.InetAddress;
+import java.util.Collection;
import org.apache.brooklyn.core.location.geo.LocalhostExternalIpLoader;
import org.apache.brooklyn.core.server.BrooklynServiceAttributes;
import org.apache.brooklyn.util.JavaGroovyEquivalents;
import org.apache.brooklyn.util.core.flags.TypeCoercions;
import org.apache.brooklyn.util.net.Networking;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Range;
+import com.google.common.collect.RangeSet;
+import com.google.common.collect.TreeRangeSet;
+
public class BrooklynNetworkUtils {
- /** returns the externally-facing IP address from which this host
comes, or 127.0.0.1 if not resolvable */
+ /**
+ * returns the externally-facing IP address from which this host
comes, or 127.0.0.1 if not resolvable
+ */
public static String getLocalhostExternalIp() {
return LocalhostExternalIpLoader.getLocalhostIpQuicklyOrDefault();
}
- /** returns a IP address for localhost paying attention to a system
property to prevent lookup in some cases */
+ /**
+ * returns a IP address for localhost paying attention to a system
property to prevent lookup in some cases
+ */
public static InetAddress getLocalhostInetAddress() {
- return
TypeCoercions.coerce(JavaGroovyEquivalents.elvis(BrooklynServiceAttributes.LOCALHOST_IP_ADDRESS.getValue(),
+ return
TypeCoercions.coerce(JavaGroovyEquivalents.elvis(BrooklynServiceAttributes.LOCALHOST_IP_ADDRESS.getValue(),
Networking.getLocalHost()), InetAddress.class);
}
+ public static RangeSet<Integer> portRulesToRanges(Collection<String>
portRules) {
+ RangeSet<Integer> result = TreeRangeSet.create();
+ for (String portRule : portRules) {
+ if (portRule.contains("-")) {
+ String[] fromTo = portRule.split("-");
+ Preconditions.checkState(fromTo.length == 2);
+ result.add(closedRange(fromTo[0], fromTo[1]));
+ } else {
+ result.add(closedRange(portRule, portRule));
+ }
+ }
+ return result;
+ }
+
+ private static Range<Integer> closedRange(String from, String to) {
+ return Range.closed(Integer.parseInt(from), Integer.parseInt(to));
--- End diff --
Should we fail-fast with numbers that are out of range (i.e. negative or
greater than `Networking.MAX_PORT_NUMBER`)? Any use-case for a kind of port
that can exceed 65535 (I don't think there is)?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---