java.net.Inet4Address Scala Examples
The following examples show how to use java.net.Inet4Address.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: DockerUtils.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.util import java.net.{Inet4Address, InetAddress, NetworkInterface} import scala.collection.JavaConverters._ import scala.sys.process._ import scala.util.Try private[spark] object DockerUtils { def getDockerIp(): String = { def findFromDockerMachine(): Option[String] = { sys.env.get("DOCKER_MACHINE_NAME").flatMap { name => Try(Seq("/bin/bash", "-c", s"docker-machine ip $name 2>/dev/null").!!.trim).toOption } } sys.env.get("DOCKER_IP") .orElse(findFromDockerMachine()) .orElse(Try(Seq("/bin/bash", "-c", "boot2docker ip 2>/dev/null").!!.trim).toOption) .getOrElse { // This block of code is based on Utils.findLocalInetAddress(), but is modified to blacklist // certain interfaces. val address = InetAddress.getLocalHost // Address resolves to something like 127.0.1.1, which happens on Debian; try to find // a better address using the local network interfaces // getNetworkInterfaces returns ifs in reverse order compared to ifconfig output order // on unix-like system. On windows, it returns in index order. // It's more proper to pick ip address following system output order. val blackListedIFs = Seq( "vboxnet0", // Mac "docker0" // Linux ) val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toSeq.filter { i => !blackListedIFs.contains(i.getName) } val reOrderedNetworkIFs = activeNetworkIFs.reverse for (ni <- reOrderedNetworkIFs) { val addresses = ni.getInetAddresses.asScala .filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress).toSeq if (addresses.nonEmpty) { val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head) // because of Inet6Address.toHostName may add interface at the end if it knows about it val strippedAddress = InetAddress.getByAddress(addr.getAddress) return strippedAddress.getHostAddress } } address.getHostAddress } } }
Example 2
Source File: Dns.scala From perf_tester with Apache License 2.0 | 5 votes |
package akka.io import java.net.{ Inet4Address, Inet6Address, InetAddress, UnknownHostException } import akka.actor._ import akka.routing.ConsistentHashingRouter.ConsistentHashable import com.typesafe.config.Config import scala.collection.{ breakOut, immutable } abstract class Dns { def cached(name: String): Option[Dns.Resolved] = None def resolve(name: String)(system: ActorSystem, sender: ActorRef): Option[Dns.Resolved] = { val ret = cached(name) if (ret.isEmpty) IO(Dns)(system).tell(Dns.Resolve(name), sender) ret } } object Dns extends ExtensionId[DnsExt] with ExtensionIdProvider { sealed trait Command case class Resolve(name: String) extends Command with ConsistentHashable { override def consistentHashKey = name } case class Resolved(name: String, ipv4: immutable.Seq[Inet4Address], ipv6: immutable.Seq[Inet6Address]) extends Command { val addrOption: Option[InetAddress] = IpVersionSelector.getInetAddress(ipv4.headOption, ipv6.headOption) @throws[UnknownHostException] def addr: InetAddress = addrOption match { case Some(ipAddress) ⇒ ipAddress case None ⇒ throw new UnknownHostException(name) } } object Resolved { def apply(name: String, addresses: Iterable[InetAddress]): Resolved = { val ipv4: immutable.Seq[Inet4Address] = addresses.collect({ case a: Inet4Address ⇒ a })(breakOut) val ipv6: immutable.Seq[Inet6Address] = addresses.collect({ case a: Inet6Address ⇒ a })(breakOut) Resolved(name, ipv4, ipv6) } } def cached(name: String)(system: ActorSystem): Option[Resolved] = { Dns(system).cache.cached(name) } def resolve(name: String)(system: ActorSystem, sender: ActorRef): Option[Resolved] = { Dns(system).cache.resolve(name)(system, sender) } override def lookup() = Dns override def createExtension(system: ExtendedActorSystem): DnsExt = new DnsExt(system) override def get(system: ActorSystem): DnsExt = super.get(system) } class DnsExt(system: ExtendedActorSystem) extends IO.Extension { val Settings = new Settings(system.settings.config.getConfig("akka.io.dns")) class Settings private[DnsExt] (_config: Config) { import _config._ val Dispatcher: String = getString("dispatcher") val Resolver: String = getString("resolver") val ResolverConfig: Config = getConfig(Resolver) val ProviderObjectName: String = ResolverConfig.getString("provider-object") } val provider: DnsProvider = system.dynamicAccess.getClassFor[DnsProvider](Settings.ProviderObjectName).get.newInstance() val cache: Dns = provider.cache val manager: ActorRef = { system.systemActorOf( props = Props(provider.managerClass, this).withDeploy(Deploy.local).withDispatcher(Settings.Dispatcher), name = "IO-DNS") } def getResolver: ActorRef = manager } object IpVersionSelector { def getInetAddress(ipv4: Option[Inet4Address], ipv6: Option[Inet6Address]): Option[InetAddress] = System.getProperty("java.net.preferIPv6Addresses") match { case "true" ⇒ ipv6 orElse ipv4 case _ ⇒ ipv4 orElse ipv6 } }
Example 3
Source File: DockerUtils.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.util import java.net.{Inet4Address, InetAddress, NetworkInterface} import scala.collection.JavaConverters._ import scala.sys.process._ import scala.util.Try private[spark] object DockerUtils { def getDockerIp(): String = { def findFromDockerMachine(): Option[String] = { sys.env.get("DOCKER_MACHINE_NAME").flatMap { name => Try(Seq("/bin/bash", "-c", s"docker-machine ip $name 2>/dev/null").!!.trim).toOption } } sys.env.get("DOCKER_IP") .orElse(findFromDockerMachine()) .orElse(Try(Seq("/bin/bash", "-c", "boot2docker ip 2>/dev/null").!!.trim).toOption) .getOrElse { // This block of code is based on Utils.findLocalInetAddress(), but is modified to blacklist // certain interfaces. val address = InetAddress.getLocalHost // Address resolves to something like 127.0.1.1, which happens on Debian; try to find // a better address using the local network interfaces // getNetworkInterfaces returns ifs in reverse order compared to ifconfig output order // on unix-like system. On windows, it returns in index order. // It's more proper to pick ip address following system output order. val blackListedIFs = Seq( "vboxnet0", // Mac "docker0" // Linux ) val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toSeq.filter { i => !blackListedIFs.contains(i.getName) } val reOrderedNetworkIFs = activeNetworkIFs.reverse for (ni <- reOrderedNetworkIFs) { val addresses = ni.getInetAddresses.asScala .filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress).toSeq if (addresses.nonEmpty) { val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head) // because of Inet6Address.toHostName may add interface at the end if it knows about it val strippedAddress = InetAddress.getByAddress(addr.getAddress) return strippedAddress.getHostAddress } } address.getHostAddress } } }
Example 4
Source File: DockerUtils.scala From multi-tenancy-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.util import java.net.{Inet4Address, InetAddress, NetworkInterface} import scala.collection.JavaConverters._ import scala.sys.process._ import scala.util.Try private[spark] object DockerUtils { def getDockerIp(): String = { def findFromDockerMachine(): Option[String] = { sys.env.get("DOCKER_MACHINE_NAME").flatMap { name => Try(Seq("/bin/bash", "-c", s"docker-machine ip $name 2>/dev/null").!!.trim).toOption } } sys.env.get("DOCKER_IP") .orElse(findFromDockerMachine()) .orElse(Try(Seq("/bin/bash", "-c", "boot2docker ip 2>/dev/null").!!.trim).toOption) .getOrElse { // This block of code is based on Utils.findLocalInetAddress(), but is modified to blacklist // certain interfaces. val address = InetAddress.getLocalHost // Address resolves to something like 127.0.1.1, which happens on Debian; try to find // a better address using the local network interfaces // getNetworkInterfaces returns ifs in reverse order compared to ifconfig output order // on unix-like system. On windows, it returns in index order. // It's more proper to pick ip address following system output order. val blackListedIFs = Seq( "vboxnet0", // Mac "docker0" // Linux ) val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toSeq.filter { i => !blackListedIFs.contains(i.getName) } val reOrderedNetworkIFs = activeNetworkIFs.reverse for (ni <- reOrderedNetworkIFs) { val addresses = ni.getInetAddresses.asScala .filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress).toSeq if (addresses.nonEmpty) { val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head) // because of Inet6Address.toHostName may add interface at the end if it knows about it val strippedAddress = InetAddress.getByAddress(addr.getAddress) return strippedAddress.getHostAddress } } address.getHostAddress } } }
Example 5
Source File: ExamplesTest.scala From json-schema-codegen with Apache License 2.0 | 5 votes |
import java.net.{Inet6Address, InetAddress, Inet4Address, URI} import java.util.Date import argonaut.Argonaut._ import argonaut._ import org.scalatest.{FlatSpec, Matchers} import product.vox.shop._ class ExamplesTest extends FlatSpec with Matchers { "AdditionalPropertiesJson" should "encode and decode" in { import additional.Codecs._ test(additional.Properties("bvalue", Some(Map("p1" -> additional.PropertiesAdditional(1))))) } "AdditionalPropertiesOnlyJson" should "encode and decode" in { import additional.properties.Codecs._ test(additional.properties.Only(Some(Map("p1" -> additional.properties.OnlyAdditional(1))))) } "EnumStrings" should "encode and decode" in { import Codecs._ test(Strings.a) test(Strings.b) } "EnumIntegers" should "encode and decode" in { import Codecs._ test(Integers.v10) test(Integers.v20) } "Formats" should "encode and decode" in { import Codecs._ test(Formats( new URI("http://uri/address"), InetAddress.getByName("127.0.0.1").asInstanceOf[Inet4Address], InetAddress.getByName("FE80:0000:0000:0000:0202:B3FF:FE1E:8329").asInstanceOf[Inet6Address], new Date() )) } "Product" should "decode from string and encode to string" in { import product.vox.shop.Codecs._ val js = """{"name":"Recharge Cards (5 PIN)","prices":[{"cost":0.0187,"currency":"USD","moq":200000}],"eid":"iso-card-5-pin","description":"<p>ISO card, 5 PINs, printed 4 colour front and back</p>\n<p>Every card option shown below meets Tier 1 operator quality standards, at a competitive pricing including freight to your country that’s always openly visible, with streamlined fulfillment and support included, creating what we believe is the best overall value at the lowest total cost of ownership in the industry.</p>\n<p>Material: Cardboard 300 GSM, UV varnish both sides</p>\n<p>Scratch panel: Silver/Black Ink with black overprint</p> \n<p>Individually plastic wrapped in chain of 50 cards</p>\n<p>Small boxes of 500 cards, Master Carton of 5000 cards</p>\n<p>Alternate names: Scratch cards, RCV, top-up cards</p>\n","properties":[{"name":"Overscratch Protection","options":[{"name":"No protection"},{"name":"Protective measures against over scratching","prices":[{"cost":0.0253,"currency":"USD","moq":200000},{"cost":0.021,"currency":"USD","moq":500000},{"cost":0.02,"currency":"USD","moq":1000000},{"cost":0.0188,"currency":"USD","moq":5000000,"leadtime":21},{"cost":0.0173,"currency":"USD","moq":10000000},{"cost":0.0171,"currency":"USD","moq":50000000,"leadtime":28}]}]},{"name":"Payment terms","options":[{"name":"Payment on shipment readiness"},{"name":"Net 30 (subject to approval)"}]},{"name":"Order Timing","options":[{"name":"Ship order when ready"},{"name":"Pre-order for shipment in 3 months"}]}],"client":"112","sample":{"price":{"cost":250,"currency":"USD"}},"category":"recharge_cards","leadtime":14,"imageUrl":["https://d2w2n7dk76p3lq.cloudfront.net/product_image/recharge_cards/iso-5pin.png"],"types":[{"name":"Recharge Cards (5 PIN)","prices":[{"cost":0.0187,"currency":"USD","moq":200000},{"cost":0.0175,"currency":"USD","moq":500000},{"cost":0.0162,"currency":"USD","moq":1000000},{"cost":0.0153,"currency":"USD","moq":5000000,"leadtime":21},{"cost":0.0138,"currency":"USD","moq":10000000,"leadtime":28},{"cost":0.0137,"currency":"USD","moq":50000000,"leadtime":28}]}],"presentation":1000}""" val po = js.decodeValidation[Product] println(po) po.isSuccess shouldBe true test(po.toOption.get) } def test[T: CodecJson](value: T) = { val json = value.asJson println(json) json.jdecode[T] shouldBe DecodeResult.ok(value) } }
Example 6
Source File: NetUtils.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.utils import java.net.{Inet4Address, InetAddress, NetworkInterface} import scala.collection.JavaConverters._ object NetUtils { def findLocalInetAddress(): InetAddress = { val address = InetAddress.getLocalHost if (address.isLoopbackAddress) { val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toList.reverse for (ni <- activeNetworkIFs) { val addresses = ni.getInetAddresses.asScala.toList .filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress) if (addresses.nonEmpty) { val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head) val strippedAddress = InetAddress.getByAddress(addr.getAddress) return strippedAddress } } } address } }
Example 7
Source File: DockerUtils.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
package org.apache.spark.util import java.net.{Inet4Address, InetAddress, NetworkInterface} import scala.collection.JavaConverters._ import scala.sys.process._ import scala.util.Try private[spark] object DockerUtils { def getDockerIp(): String = { def findFromDockerMachine(): Option[String] = { sys.env.get("DOCKER_MACHINE_NAME").flatMap { name => Try(Seq("/bin/bash", "-c", s"docker-machine ip $name 2>/dev/null").!!.trim).toOption } } sys.env.get("DOCKER_IP") .orElse(findFromDockerMachine()) .orElse(Try(Seq("/bin/bash", "-c", "boot2docker ip 2>/dev/null").!!.trim).toOption) .getOrElse { // This block of code is based on Utils.findLocalInetAddress(), but is modified to blacklist // certain interfaces. val address = InetAddress.getLocalHost // Address resolves to something like 127.0.1.1, which happens on Debian; try to find // a better address using the local network interfaces // getNetworkInterfaces returns ifs in reverse order compared to ifconfig output order // on unix-like system. On windows, it returns in index order. // It's more proper to pick ip address following system output order. val blackListedIFs = Seq( "vboxnet0", // Mac "docker0" // Linux ) val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toSeq.filter { i => !blackListedIFs.contains(i.getName) } val reOrderedNetworkIFs = activeNetworkIFs.reverse for (ni <- reOrderedNetworkIFs) { val addresses = ni.getInetAddresses.asScala .filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress).toSeq if (addresses.nonEmpty) { val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head) // because of Inet6Address.toHostName may add interface at the end if it knows about it val strippedAddress = InetAddress.getByAddress(addr.getAddress) return strippedAddress.getHostAddress } } address.getHostAddress } } }
Example 8
Source File: DockerUtils.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.util import java.net.{Inet4Address, NetworkInterface, InetAddress} import scala.collection.JavaConverters._ import scala.sys.process._ import scala.util.Try private[spark] object DockerUtils { def getDockerIp(): String = { def findFromDockerMachine(): Option[String] = { sys.env.get("DOCKER_MACHINE_NAME").flatMap { name => Try(Seq("/bin/bash", "-c", s"docker-machine ip $name 2>/dev/null").!!.trim).toOption } } sys.env.get("DOCKER_IP") .orElse(findFromDockerMachine()) .orElse(Try(Seq("/bin/bash", "-c", "boot2docker ip 2>/dev/null").!!.trim).toOption) .getOrElse { // This block of code is based on Utils.findLocalInetAddress(), but is modified to blacklist // certain interfaces. val address = InetAddress.getLocalHost // Address resolves to something like 127.0.1.1, which happens on Debian; try to find // a better address using the local network interfaces // getNetworkInterfaces returns ifs in reverse order compared to ifconfig output order // on unix-like system. On windows, it returns in index order. // It's more proper to pick ip address following system output order. val blackListedIFs = Seq( "vboxnet0", // Mac "docker0" // Linux ) val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toSeq.filter { i => !blackListedIFs.contains(i.getName) } val reOrderedNetworkIFs = activeNetworkIFs.reverse for (ni <- reOrderedNetworkIFs) { val addresses = ni.getInetAddresses.asScala .filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress).toSeq if (addresses.nonEmpty) { val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head) // because of Inet6Address.toHostName may add interface at the end if it knows about it val strippedAddress = InetAddress.getByAddress(addr.getAddress) return strippedAddress.getHostAddress } } address.getHostAddress } } }
Example 9
Source File: BindAddress.scala From zipkin-mesos-framework with Apache License 2.0 | 5 votes |
package net.elodina.mesos.zipkin.utils import java.net.{Inet4Address, InetAddress, NetworkInterface} import java.util import scala.collection.JavaConversions._ class BindAddress(s: String) { private var _source: String = null private var _value: String = null def source: String = _source def value: String = _value parse() def parse() = { val idx = s.indexOf(":") if (idx != -1) { _source = s.substring(0, idx) _value = s.substring(idx + 1) } else { _value = s } if (source != null && source != "if") throw new IllegalArgumentException(s) } def resolve(): String = { _source match { case null => resolveAddress(_value) case "if" => resolveInterfaceAddress(_value) case _ => throw new IllegalStateException("Failed to resolve " + s) } } def resolveAddress(addressOrMask: String): String = { if (!addressOrMask.endsWith("*")) return addressOrMask val prefix = addressOrMask.substring(0, addressOrMask.length - 1) for (ni <- NetworkInterface.getNetworkInterfaces) { val address = ni.getInetAddresses.find(_.getHostAddress.startsWith(prefix)).orNull if (address != null) return address.getHostAddress } throw new IllegalStateException("Failed to resolve " + s) } def resolveInterfaceAddress(name: String): String = { val ni = NetworkInterface.getNetworkInterfaces.find(_.getName == name).orNull if (ni == null) throw new IllegalStateException("Failed to resolve " + s) val addresses: util.Enumeration[InetAddress] = ni.getInetAddresses val address = addresses.find(_.isInstanceOf[Inet4Address]).orNull if (address != null) return address.getHostAddress throw new IllegalStateException("Failed to resolve " + s) } override def hashCode(): Int = 31 * _source.hashCode + _value.hashCode override def equals(o: scala.Any): Boolean = { if (o == null || o.getClass != this.getClass) return false val address = o.asInstanceOf[BindAddress] _source == address._source && _value == address._value } override def toString: String = s }