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:net.dfs.remote.main.ClientServicesStarter.java

/**
 * Client application will be started with the main() of the {@link ClientServicesStarter}
 * //  w w w. ja v a2 s .  c  om
 * @param args the parameter which is passed to the main()
 */
public static void startClient() {

    try {
        props.put("client.ip", InetAddress.getLocalHost().getHostAddress());

    } catch (IOException e) {
        e.printStackTrace();
    }

    ClientServicesStarter client = new ClientServicesStarter();
    RemoteNodeRegistration nodeRegister = (RemoteNodeRegistration) client.startProxy();

    try {
        if (nodeRegister != null) {
            String SERVER_IP = nodeRegister.registerNode(InetAddress.getLocalHost().getHostAddress());
            ClientServicesStarter.serverName(SERVER_IP);
            client.loadNode();
        } else {
            log.debug("Server at " + props.getProperty("server.ip") + " not found");
            System.exit(1);
        }

    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

}

From source file:com.fns.xlator.monitoring.StatsdSettings.java

public String getApplicationHostname() {
    String hostname = "localhost";
    try {/*from www.  j  av a  2s .  com*/
        hostname = InetAddress.getLocalHost().getHostName().replace('.', '_');
    } catch (UnknownHostException uhe) {
        // do nothing
    }
    final String shortName = applicationName.length() > 6 ? applicationName.substring(0, 6) : applicationName;
    return String.format("%s-%s", shortName, hostname);
}

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

/**
 * Looks up the IP number of the local host.
 * Note that Java does not guarantee that the result is
 * IPv4 or IPv6.//w  w w. j a  v  a  2  s.  co  m
 * @return the found IP; returns "UNKNOWNIP" if it could not
 * be found.
 */
public static String getLocalIP() {
    String result;
    try {
        result = InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        result = "UNKNOWNIP";
    }
    return result;
}

From source file:io.airlift.event.client.EventJsonSerializer.java

public EventJsonSerializer(EventTypeMetadata<T> eventTypeMetadata) {
    Preconditions.checkNotNull(eventTypeMetadata, "eventTypeMetadata is null");

    this.eventTypeMetadata = eventTypeMetadata;
    if (eventTypeMetadata.getHostField() == null) {
        try {/*from  w  ww .ja  v  a2  s  . com*/
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException("Unable to determine local host name");
        }
    } else {
        hostName = null;
    }
}

From source file:mase.stat.RunStatistics.java

public static String getComputerName() {
    String result = System.getProperty("user.name") + " @ ";
    try {/*from ww  w  .j a v  a 2 s.c  om*/
        result += InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException ex) {
        result += "Unknown";
    }
    return result;
}

From source file:com.netscape.cmstools.OCSPClient.java

public static Options createOptions() throws UnknownHostException {

    Options options = new Options();

    Option option = new Option("d", true, "Security database location (default: current directory)");
    option.setArgName("database");
    options.addOption(option);//from  www . j  a v  a  2 s  . co m

    option = new Option("h", true,
            "OCSP server hostname (default: " + InetAddress.getLocalHost().getCanonicalHostName() + ")");
    option.setArgName("hostname");
    options.addOption(option);

    option = new Option("p", true, "OCSP server port number (default: 8080)");
    option.setArgName("port");
    options.addOption(option);

    option = new Option("t", true, "OCSP service path (default: /ocsp/ee/ocsp)");
    option.setArgName("path");
    options.addOption(option);

    option = new Option("c", true, "CA certificate nickname (default: CA Signing Certificate)");
    option.setArgName("nickname");
    options.addOption(option);

    option = new Option("n", true, "Number of submissions (default: 1)");
    option.setArgName("times");
    options.addOption(option);

    option = new Option(null, "serial", true, "Serial number of certificate to be checked");
    option.setArgName("serial");
    options.addOption(option);

    option = new Option(null, "input", true, "Input file containing DER-encoded OCSP request");
    option.setArgName("input");
    options.addOption(option);

    option = new Option(null, "output", true, "Output file to store DER-encoded OCSP response");
    option.setArgName("output");
    options.addOption(option);

    options.addOption("v", "verbose", false, "Run in verbose mode.");
    options.addOption(null, "help", false, "Show help message.");

    return options;
}

From source file:net.nikore.gozer.marathon.MarathonHost.java

@Inject
public MarathonHost(RibbonUtils utils, ObjectMapper mapper, GozerMarathonConfig config) throws Exception {
    this.utils = utils;
    this.mapper = mapper;
    this.ip = InetAddress.getLocalHost().getHostAddress();
    this.marathonHost = config.getMarathonHost();
}

From source file:net.testdriven.psiprobe.controllers.DecoratorController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    try {//from ww  w  .  j  a v  a  2s  .c  o  m
        request.setAttribute("hostname", InetAddress.getLocalHost().getHostName());
    } catch (UnknownHostException e) {
        request.setAttribute("hostname", "unknown");
    }

    Properties version = (Properties) getApplicationContext().getBean("version");
    request.setAttribute("version", version.getProperty("probe.version"));

    Object uptimeStart = getServletContext().getAttribute(UptimeListener.START_TIME_KEY);
    if (uptimeStart != null && uptimeStart instanceof Long) {
        long l = (Long) uptimeStart;
        long uptime = System.currentTimeMillis() - l;
        long uptime_days = uptime / (1000 * 60 * 60 * 24);

        uptime = uptime % (1000 * 60 * 60 * 24);
        long uptime_hours = uptime / (1000 * 60 * 60);

        uptime = uptime % (1000 * 60 * 60);
        long uptime_mins = uptime / (1000 * 60);

        request.setAttribute("uptime_days", uptime_days);
        request.setAttribute("uptime_hours", uptime_hours);
        request.setAttribute("uptime_mins", uptime_mins);
    }

    //
    // Work out the language of the interface by matching resource files that we have
    // to the request locale.
    //
    List fileNames = getMessageFileNamesForLocale(request.getLocale());
    String lang = "en";
    for (Object fileName : fileNames) {
        String f = (String) fileName;
        if (getServletContext().getResource(f + ".properties") != null) {
            lang = f.substring(messagesBasename.length() + 1);
            break;
        }
    }

    request.setAttribute("lang", lang);

    return super.handleRequestInternal(request, response);
}

From source file:com.zq.fin.seckill.web.BaseController.java

 /**
 * ??ip/*from w w  w  .j a v a  2  s .  c  o m*/
 * @param request
 * @return
 */
public static String getIpAddr(HttpServletRequest request){
      
   String ipAddress = request.getHeader("x-forwarded-for");
   if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("Proxy-Client-IP");
   }
   if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("WL-Proxy-Client-IP");
   }
   if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getRemoteAddr();
      if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
         //????IP
         InetAddress inet=null;
         try {
            inet = InetAddress.getLocalHost();
         } catch (UnknownHostException e) {
            e.printStackTrace();
         }
         ipAddress= inet.getHostAddress();
      }
   }
   //?IPIP,IP','
   if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
      if(ipAddress.indexOf(",")>0){
         ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
      }
   }
      
   return ipAddress; 
}

From source file:com.googlecode.psiprobe.controllers.DecoratorController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    try {// w  w w. j a v  a  2s  . c o  m
        request.setAttribute("hostname", InetAddress.getLocalHost().getHostName());
    } catch (UnknownHostException e) {
        request.setAttribute("hostname", "unknown");
    }

    Properties version = (Properties) getApplicationContext().getBean("version");
    request.setAttribute("version", version.getProperty("probe.version"));

    Object uptimeStart = getServletContext().getAttribute(UptimeListener.START_TIME_KEY);
    if (uptimeStart != null && uptimeStart instanceof Long) {
        long l = ((Long) uptimeStart).longValue();
        long uptime = System.currentTimeMillis() - l;
        long uptime_days = uptime / (1000 * 60 * 60 * 24);

        uptime = uptime % (1000 * 60 * 60 * 24);
        long uptime_hours = uptime / (1000 * 60 * 60);

        uptime = uptime % (1000 * 60 * 60);
        long uptime_mins = uptime / (1000 * 60);

        request.setAttribute("uptime_days", new Long(uptime_days));
        request.setAttribute("uptime_hours", new Long(uptime_hours));
        request.setAttribute("uptime_mins", new Long(uptime_mins));
    }

    //
    // Work out the language of the interface by matching resource files that we have
    // to the request locale.
    //
    List fileNames = getMessageFileNamesForLocale(request.getLocale());
    String lang = "en";
    for (Iterator it = fileNames.iterator(); it.hasNext();) {
        String f = (String) it.next();
        if (getServletContext().getResource(f + ".properties") != null) {
            lang = f.substring(messagesBasename.length() + 1);
            break;
        }
    }

    request.setAttribute("lang", lang);

    return super.handleRequestInternal(request, response);
}