Example usage for java.net InetAddress getLocalHost

List of usage examples for java.net InetAddress getLocalHost

Introduction

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

Prototype

public static InetAddress getLocalHost() throws UnknownHostException 

Source Link

Document

Returns the address of the local host.

Usage

From source file:com.jivesoftware.os.jive.utils.http.client.CustomSecureProtocolSocketFactoryTest.java

@Test
public void testCreateSocketLocalWithTimeout() throws Exception {
    HttpConnectionParams params = mock(HttpConnectionParams.class);
    Socket socket = mock(Socket.class);

    when(params.getConnectionTimeout()).thenReturn(788);
    when(params.getSoTimeout()).thenReturn(789);
    when(sslSocketFactory.createSocket()).thenReturn(socket);

    customSecureProtocolSocketFactory.createSocket("host", 444, InetAddress.getLocalHost(), 8765, params);

    verify(sslSocketFactory).createSocket();
    verify(socket).setSoTimeout(789);//  ww  w.j a  v  a 2 s .c om
    verify(socket).connect(new InetSocketAddress("host", 444), 788);
}

From source file:es.tid.keyserver.controllers.db.DataBaseTest.java

/**
 * Test class constructor./*  w  w w . j  a  va  2s  .  com*/
 * @author <a href="mailto:jgm1986@hotmail.com">Javier Martinez Gusano</a>
 * @since v0.4.3
 */
public DataBaseTest() {
    try {
        dbAddress = InetAddress.getLocalHost();
    } catch (UnknownHostException ex) {
        System.out.println("[ ERROR ] Can't load 'localhost' address.");
        Logger.getLogger(DataBaseTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    dbPort = 6379;
    dbPassword = "foobared";
    dbIndex = 0;
    // Connection DB test.
    JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), this.dbAddress.getHostAddress(), this.dbPort,
            Protocol.DEFAULT_TIMEOUT, this.dbPassword);
    try {
        // Redis connected.
        dataBaseObj = pool.getResource();
        dataBaseObj.ping();
        dataBaseObj.close();
        this.dbAvailable = true;
    } catch (Exception e) {
        this.dbAvailable = false;
        System.out.println("[ WARNING ] Redis Server is not available. JUnit tests will be skipped.");
    }

}

From source file:net.shopxx.controller.admin.VirAccountController.java

/**
 * /*from  w  w  w . jav a 2s  .com*/
 */
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(Long id, String amount, String content, ModelMap model,
        RedirectAttributes redirectAttributes, String recruitMap) {

    VirAccount virAccount = virAccountService.find(id);
    Admin admin = adminService.getCurrent();

    InetAddress addr;
    try {
        addr = InetAddress.getLocalHost();
        virAccount.setLastRechargeIp(addr.getHostAddress());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    BigDecimal rebateAmounts = virAccount.getRebateAmounts().add(new BigDecimal(amount));
    virAccount.setRebateAmounts(rebateAmounts);
    virAccount.setLastRechargeDate(new Date());
    virAccountService.update(virAccount);

    VirTradeLog virTradeLog = new VirTradeLog();
    virTradeLog.setVirAccount(virAccount);
    virTradeLog.setType(VirTradeLog.Type.rebate);
    virTradeLog.setOperator(admin.getName());
    if (content == null) {
        virTradeLog.setContent(admin.getName() + "? " + virAccount.getSerialNumber() + ""
                + new BigDecimal(amount));
    } else {
        virTradeLog.setContent(content);
    }
    virTradeLog.setRebateAmount(new BigDecimal(amount));
    virTradeLog.setRechargeAmount(new BigDecimal(0));
    virTradeLogService.save(virTradeLog);
    model.addAttribute("vitual", virAccount);
    addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);

    return "redirect:list.jhtml";
}

From source file:net.ravendb.client.RavenDBAwareTests.java

public static String getHostName() {
    try {/*from w w w . java 2 s  .c  o  m*/
        return InetAddress.getLocalHost().getHostName();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.dhr.security.LicenseValidation.java

String getMachineMacAddress() throws UnknownHostException, SocketException {
    InetAddress ip = InetAddress.getLocalHost();
    NetworkInterface network = NetworkInterface.getByInetAddress(ip);

    byte[] mac = network.getHardwareAddress();
    StringBuilder currentMachineMacAddress = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
        currentMachineMacAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
    }//from w  ww. ja  va2 s  . c  om
    System.out.println(currentMachineMacAddress.toString());
    return currentMachineMacAddress.toString();
}

From source file:com.axibase.tsd.collector.AtsdUtil.java

public static String resolveHostname() {
    try {// ww w  .j ava2 s  .co m
        return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        AtsdUtil.logInfo("Could not resolve hostname. " + e.getMessage());
        return DEFAULT_ENTITY;
    }
}

From source file:com.kappaware.logtrawler.Utils.java

public static String getHostname() {
    try {//from  ww w. j ava2s.  c  om
        return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        log.error("Unable to find our hostname!");
        return "UNKNOW";
    }
}

From source file:org.opennms.ovapi.OVsnmpSessionTest.java

public void setUp() throws Exception {
    Resource snmpData = new ClassPathResource("snmpTestData1.properties");

    m_host = InetAddress.getLocalHost().getHostAddress();

    m_agent = MockSnmpAgent.createAgentAndRun(snmpData, m_host + "/9161");
}

From source file:com.bigdata.dastor.utils.FBUtilities.java

public static InetAddress getLocalAddress() {
    if (localInetAddress_ == null)
        try {/*from www  .j a  va  2s  .c  o m*/
            localInetAddress_ = DatabaseDescriptor.getListenAddress() == null ? InetAddress.getLocalHost()
                    : DatabaseDescriptor.getListenAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    return localInetAddress_;
}

From source file:org.dawnsci.commandserver.core.producer.AliveConsumer.java

protected void startNotifications() throws Exception {

    if (uri == null)
        throw new NullPointerException("Please set the URI before starting notifications!");
    this.cbean = new ConsumerBean();
    cbean.setStatus(ConsumerStatus.STARTING);
    cbean.setName(getName());/*from   w w w .  ja v a 2  s .c  om*/
    cbean.setConsumerId(consumerId);
    cbean.setVersion(consumerVersion);
    cbean.setStartTime(System.currentTimeMillis());
    try {
        cbean.setHostName(InetAddress.getLocalHost().getHostName());
    } catch (UnknownHostException e) {
        // Not fatal but would be nice...
        e.printStackTrace();
    }

    System.out.println("Running events on topic " + Constants.ALIVE_TOPIC + " to notify of '" + getName()
            + "' service being available.");

    final Thread aliveThread = new Thread(new Runnable() {
        public void run() {

            try {
                ConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri);
                aliveConnection = connectionFactory.createConnection();
                aliveConnection.start();

                final Session session = aliveConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                final Topic topic = session.createTopic(Constants.ALIVE_TOPIC);
                final MessageProducer producer = session.createProducer(topic);

                final ObjectMapper mapper = new ObjectMapper();

                // Here we are sending the message out to the topic
                while (isActive()) {
                    try {
                        Thread.sleep(Constants.NOTIFICATION_FREQUENCY);

                        TextMessage temp = session.createTextMessage(mapper.writeValueAsString(cbean));
                        producer.send(temp, DeliveryMode.NON_PERSISTENT, 1, 5000);

                        if (ConsumerStatus.STARTING.equals(cbean.getStatus())) {
                            cbean.setStatus(ConsumerStatus.RUNNING);
                        }

                    } catch (InterruptedException ne) {
                        break;
                    } catch (Exception neOther) {
                        neOther.printStackTrace();
                    }
                }
            } catch (Exception ne) {
                ne.printStackTrace();
                setActive(false);
            }
        }
    });
    aliveThread.setName("Alive Notification Topic");
    aliveThread.setDaemon(true);
    aliveThread.setPriority(Thread.MIN_PRIORITY);
    aliveThread.start();
}