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.mgmtp.perfload.logging.DefaultResultLoggerTest.java

@BeforeTest
public void init() throws IOException {
    tl = new TestLogger();
    localhost = InetAddress.getLocalHost();
    logger = new DefaultResultLogger(tl, localhost, "client", "operation", "target", 1, 2, 3);
    requestId = UUID.randomUUID();
}

From source file:gobblin.util.EmailUtils.java

/**
 * A general method for sending emails./*from   w  ww .  j  a v a  2  s. c o m*/
 *
 * @param state a {@link State} object containing configuration properties
 * @param subject email subject
 * @param message email message
 * @throws EmailException if there is anything wrong sending the email
 */
public static void sendEmail(State state, String subject, String message) throws EmailException {
    Email email = new SimpleEmail();
    email.setHostName(state.getProp(ConfigurationKeys.EMAIL_HOST_KEY, ConfigurationKeys.DEFAULT_EMAIL_HOST));
    if (state.contains(ConfigurationKeys.EMAIL_SMTP_PORT_KEY)) {
        email.setSmtpPort(state.getPropAsInt(ConfigurationKeys.EMAIL_SMTP_PORT_KEY));
    }
    email.setFrom(state.getProp(ConfigurationKeys.EMAIL_FROM_KEY));
    if (state.contains(ConfigurationKeys.EMAIL_USER_KEY)
            && state.contains(ConfigurationKeys.EMAIL_PASSWORD_KEY)) {
        email.setAuthentication(state.getProp(ConfigurationKeys.EMAIL_USER_KEY), PasswordManager
                .getInstance(state).readPassword(state.getProp(ConfigurationKeys.EMAIL_PASSWORD_KEY)));
    }
    Iterable<String> tos = Splitter.on(',').trimResults().omitEmptyStrings()
            .split(state.getProp(ConfigurationKeys.EMAIL_TOS_KEY));
    for (String to : tos) {
        email.addTo(to);
    }

    String hostName;
    try {
        hostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException uhe) {
        LOGGER.error("Failed to get the host name", uhe);
        hostName = "unknown";
    }

    email.setSubject(subject);
    String fromHostLine = String.format("This email was sent from host: %s%n%n", hostName);
    email.setMsg(fromHostLine + message);
    email.send();
}

From source file:org.freewheelschedule.freewheel.controlserver.ControlThread.java

@Override
public void run() {
    String hostname;//  w w w .jav a2  s.  com
    try {
        hostname = (InetAddress.getLocalHost()).getHostName();
    } catch (UnknownHostException e1) {
        log.error("Unable to determine hostname", e1);
        return;
    }

    do {
        try {
            try {
                Trigger firedTrigger = triggerQueue.getNextTrigger(); // Get next fired trigger from queue and run the job.
                Job jobToRun = null;
                if (firedTrigger != null) {
                    jobToRun = firedTrigger.getJob();
                    if (jobToRun != null) {
                        runJob(hostname, jobToRun);
                    }
                    triggerQueue.remove(firedTrigger);
                }
            } catch (UnknownHostException e) {
                log.error("Unable to open socket to RemoteWorker", e);
            } catch (SocketException e) {
                log.error("RemoteWorker is currently unavailable.", e);
            } catch (IOException e) {
                log.error("Unable to communicate with RemoteWorker", e);
            } finally {
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            log.error("Control thread interrupted waiting for jobs", e);
        }
    } while (continueWaiting);
}

From source file:com.taobao.common.store.util.UniqId.java

private UniqId() {
    try {/*from  w  ww . j a  va2s.c  om*/
        final InetAddress addr = InetAddress.getLocalHost();

        hostAddr = addr.getHostAddress();
    } catch (final IOException e) {
        log.error("[UniqID] Get HostAddr Error", e);
        hostAddr = String.valueOf(System.currentTimeMillis());
    }

    if (null == hostAddr || hostAddr.trim().length() == 0 || "127.0.0.1".equals(hostAddr)) {
        hostAddr = String.valueOf(System.currentTimeMillis());
    }

    if (log.isDebugEnabled()) {
        log.debug("[UniqID]hostAddr is:" + hostAddr);
    }

    try {
        mHasher = MessageDigest.getInstance("MD5");
    } catch (final NoSuchAlgorithmException nex) {
        mHasher = null;
        log.error("[UniqID]new MD5 Hasher error", nex);
    }
}

From source file:com.flipkart.flux.controller.FSMVisualizationController.java

@RequestMapping(value = { "/fsmview" }, method = RequestMethod.GET)
public String fsmview(ModelMap modelMap, HttpServletRequest request) throws UnknownHostException {
    int fluxApiPort = configInjector.getInstance(Key.get(Integer.class, Names.named("Api.service.port")));
    String fluxApiUrl = "http://" + InetAddress.getLocalHost().getHostAddress() + ":" + fluxApiPort;
    modelMap.addAttribute("flux_api_url", fluxApiUrl);
    return FSM_VIEW;
}

From source file:dk.netarkivet.common.utils.SystemUtils.java

/**
 * Get the first hostname available for this machine, or "localhost" if none
 * are available./*from w w  w .  j a  v a2  s .  c  om*/
 *
 * @return A hostname, as returned by
 * InetAddress.getLocalHost().getCanonicalHostName()()
 */
public static String getLocalHostName() {
    String hostname = LOCALHOST;
    try {
        InetAddress localhost = InetAddress.getLocalHost();
        String localhostName = localhost.getCanonicalHostName();
        String localhostIp = localhost.getHostAddress();
        if (log.isTraceEnabled()) {
            log.trace("[getLocalHostName] Resolved: " + localhostName + " (" + localhostIp + ")");
        }
        return localhostName;
    } catch (UnknownHostException e) {
        // If no interfaces, use default;
        log.warn("Unable to resolve localhostname. Returning the default '" + LOCALHOST + "'");
    }
    return hostname;
}

From source file:com.hightail.metrics.rest.NewRelicHTTPv1Reporter.java

/**
 * Creates a new {@link com.codahale.metrics.ScheduledReporter} instance.
 *
 * @param registry     the {@link com.codahale.metrics.MetricRegistry} containing the definedMetrics this
 *                     reporter will report
 * @param metricNamePrefix       the reporter's name
 * @param filter       the filter for which definedMetrics to report
 * @param rateUnit//from   w  ww  .jav  a 2s .co m
 * @param durationUnit
 */
protected NewRelicHTTPv1Reporter(NewRelic newRelic, MetricRegistry registry, String metricNamePrefix,
        MetricFilter filter, TimeUnit rateUnit, TimeUnit durationUnit) {
    super(registry, "new-relic-http-reporter", filter, rateUnit, durationUnit);
    this.newRelic = newRelic;
    this.metricNamePrefix = metricNamePrefix;

    try {
        hostname = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException uhex) {
        logger.warn("This can be ignored: Agent hostId Error: ", uhex);
        hostname = NewRelicConstants.DEFAULT_AGENT_HOST;
    }

    logger.info("NewRelicHTTPv1Reporter initialized..");
}

From source file:com.beginner.core.utils.ProjectUtil.java

/**
 * ?IP(???)/*from   ww  w  . j ava2 s  . c  o m*/
 */
public static String getIp() {
    InetAddress addr = null;
    String ip;
    try {
        addr = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        logger.error("??IP??", e);
    }
    ip = addr.getHostAddress().toString();
    return ip;
}

From source file:com.codemacro.jcm.JCMConfig.java

public String getServerAddr() throws UnknownHostException {
    InetAddress addr = serverConfig.getAddress();
    if (addr == null) {
        addr = InetAddress.getLocalHost();
    }//from   w  w w  .  java2s . c  o  m
    return addr.getHostAddress();
}

From source file:com.marosj.backend.BackendHttpServlet.java

private String getIp() {
    String hostname = null;//from   ww w.  j  a  v a 2s  .c o m
    try {
        hostname = InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        hostname = "unknown";
    }
    return hostname;
}