Example usage for java.net InetAddress getLoopbackAddress

List of usage examples for java.net InetAddress getLoopbackAddress

Introduction

In this page you can find the example usage for java.net InetAddress getLoopbackAddress.

Prototype

public static InetAddress getLoopbackAddress() 

Source Link

Document

Returns the loopback address.

Usage

From source file:org.opennms.poller.remote.Main.java

public void initializePinger() {
    if (m_disableIcmp) {
        LOG.info("Disabling ICMP by user request.");
        System.setProperty("org.opennms.netmgt.icmp.pingerClass", "org.opennms.netmgt.icmp.NullPinger");
        PingerFactory.setInstance(new NullPinger());
        return;//from  ww  w . j  a v a 2  s. c  o  m
    }

    final String pingerClass = System.getProperty("org.opennms.netmgt.icmp.pingerClass");
    if (pingerClass == null) {
        LOG.info("System property org.opennms.netmgt.icmp.pingerClass is not set; using JnaPinger by default");
        System.setProperty("org.opennms.netmgt.icmp.pingerClass", "org.opennms.netmgt.icmp.jna.JnaPinger");
    }
    LOG.info("Pinger class: {}", System.getProperty("org.opennms.netmgt.icmp.pingerClass"));
    try {
        final Pinger pinger = PingerFactory.getInstance();
        pinger.ping(InetAddress.getLoopbackAddress());
    } catch (final Throwable t) {
        LOG.warn(
                "Unable to get pinger instance.  Setting pingerClass to NullPinger.  For details, see: http://www.opennms.org/wiki/ICMP_could_not_be_initialized");
        System.setProperty("org.opennms.netmgt.icmp.pingerClass", "org.opennms.netmgt.icmp.NullPinger");
        PingerFactory.setInstance(new NullPinger());
        if (m_gui) {
            final String message = "ICMP (ping) could not be initialized: " + t.getMessage()
                    + "\nDisabling ICMP and using the NullPinger instead."
                    + "\nFor details, see: http://www.opennms.org/wiki/ICMP_could_not_be_initialized";
            JOptionPane.showMessageDialog(null, message, "ICMP Not Available", JOptionPane.WARNING_MESSAGE);
        }
    }
}

From source file:de.bieniekconsulting.trafficmonitor.connector.snmp.ConnectorTestCase.java

/**
 * Test getConnection//from w  w w  . java 2  s  .c  om
 *
 * @exception Throwable Thrown if case of an error
 */
@Test
public void testGetConnection2() throws Throwable {
    assertThat(connectionFactory1).isNotNull();
    Snmp4JConnection connection1 = connectionFactory1.getConnection(InetAddress.getLoopbackAddress(), 161,
            "community");
    assertThat(connection1).isNotNull();
    connection1.close();
}

From source file:org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer.java

private static IpAddress getSourceRloc(InetAddress sourceRloc) {
    if (sourceRloc == null) {
        sourceRloc = InetAddress.getLoopbackAddress();
    }/*from   ww  w .  jav  a  2  s.  c  om*/

    if (sourceRloc instanceof Inet4Address) {
        return new IpAddress(new Ipv4Address(sourceRloc.getHostAddress()));
    } else {
        return new IpAddress(new Ipv6Address(sourceRloc.getHostAddress()));
    }
}

From source file:org.apache.htrace.impl.ZipkinSpanReceiver.java

/**
 * Set up the HTrace to Zipkin converter.
 *///  www.java2 s .  c o m
private void initConverter() {
    InetAddress tracedServiceHostname = null;
    // Try and get the hostname.  If it's not configured try and get the local hostname.
    try {
        //TODO (clehene) extract conf to constant
        //TODO (clehene) has this been deprecated?
        String host = conf.get("zipkin.traced-service-hostname", InetAddress.getLocalHost().getHostAddress());
        tracedServiceHostname = InetAddress.getByName(host);
    } catch (UnknownHostException e) {
        LOG.error("Couldn't get the localHost address", e);
    }
    short tracedServicePort = (short) conf.getInt("zipkin.traced-service-port", -1);
    byte[] address = tracedServiceHostname != null ? tracedServiceHostname.getAddress()
            : InetAddress.getLoopbackAddress().getAddress();
    int ipv4 = ByteBuffer.wrap(address).getInt();
    this.converter = new HTraceToZipkinConverter(ipv4, tracedServicePort);
}

From source file:org.cryptomator.ui.util.SingleInstanceManager.java

/**
 * Checks if there is a valid port at/*from   w ww  .  ja va2s .co  m*/
 * {@link Preferences#userNodeForPackage(Class)} for {@link Main} under the
 * given applicationKey, tries to connect to the port at the loopback
 * address and checks if the port identifies with the applicationKey.
 * 
 * @param applicationKey
 *            key used to load the port and check the identity of the
 *            connection.
 * @return
 */
public static Optional<RemoteInstance> getRemoteInstance(String applicationKey) {
    Optional<Integer> port = getSavedPort(applicationKey);

    if (!port.isPresent()) {
        return Optional.empty();
    }

    SocketChannel channel = null;
    boolean close = true;
    try {
        channel = SocketChannel.open();
        channel.configureBlocking(false);
        LOG.info("connecting to instance {}", port.get());
        channel.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port.get()));

        SocketChannel fChannel = channel;
        if (!TimeoutTask.attempt(t -> fChannel.finishConnect(), 1000, 10)) {
            return Optional.empty();
        }

        LOG.info("connected to instance {}", port.get());

        final byte[] bytes = applicationKey.getBytes();
        ByteBuffer buf = ByteBuffer.allocate(bytes.length);
        tryFill(channel, buf, 1000);
        if (buf.hasRemaining()) {
            return Optional.empty();
        }

        buf.flip();

        for (int i = 0; i < bytes.length; i++) {
            if (buf.get() != bytes[i]) {
                return Optional.empty();
            }
        }

        close = false;
        return Optional.of(new RemoteInstance(channel));
    } catch (Exception e) {
        return Optional.empty();
    } finally {
        if (close) {
            IOUtils.closeQuietly(channel);
        }
    }
}

From source file:org.apache.hadoop.net.TestDNS.java

private String getLoopbackInterface() throws SocketException {
    return NetworkInterface.getByInetAddress(InetAddress.getLoopbackAddress()).getName();
}

From source file:org.cryptomator.ui.util.SingleInstanceManager.java

/**
 * Creates a server socket on a free port and saves the port in
 * {@link Preferences#userNodeForPackage(Class)} for {@link Main} under the
 * given applicationKey./*from w w  w  .j  a v  a2s.c o m*/
 * 
 * @param applicationKey
 *            key used to save the port and identify upon connection.
 * @param exec
 *            the task which is submitted is interruptable.
 * @return
 * @throws IOException
 */
public static LocalInstance startLocalInstance(String applicationKey, ExecutorService exec) throws IOException {
    final ServerSocketChannel channel = ServerSocketChannel.open();
    channel.configureBlocking(false);
    channel.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));

    final int port = ((InetSocketAddress) channel.getLocalAddress()).getPort();
    Preferences.userNodeForPackage(Main.class).putInt(applicationKey, port);
    LOG.info("InstanceManager bound to port {}", port);

    Selector selector = Selector.open();
    channel.register(selector, SelectionKey.OP_ACCEPT);

    LocalInstance instance = new LocalInstance(applicationKey, channel, selector);

    exec.submit(() -> {
        try {
            instance.port = ((InetSocketAddress) channel.getLocalAddress()).getPort();
        } catch (IOException e) {

        }
        instance.selectionLoop();
    });

    return instance;
}

From source file:com.alliander.osgp.acceptancetests.devicemanagement.RemoveDeviceSteps.java

private void createDevice(final String deviceIdentification, final boolean activated) {
    this.device = new DeviceBuilder().withDeviceIdentification(deviceIdentification)
            .withNetworkAddress(activated ? InetAddress.getLoopbackAddress() : null).isActivated(activated)
            .build();//  w  w w  .j a va  2 s .co  m
}

From source file:kina.config.GenericCassandraKinaConfig.java

/**
 * {@inheritDoc}/*from   w  w w. j  a  va 2 s .co m*/
 */
@Override
public CassandraKinaConfig<T> initialize() {
    if (isInitialized) {
        return this;
    }

    if (StringUtils.isEmpty(host)) {
        try {
            host = InetAddress.getLocalHost().getCanonicalHostName();
        } catch (UnknownHostException e) {
            LOG.warn("Cannot resolve local host canonical name, using \"localhost\"");
            host = InetAddress.getLoopbackAddress().getCanonicalHostName();
        }
    }

    validate();

    columnDefinitions();
    isInitialized = Boolean.TRUE;

    return this;
}