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.taobao.tddl.common.util.TDDLMBeanServer.java

private TDDLMBeanServer() {
    // MBServer/*from w  w w. jav  a 2 s.c  om*/
    String hostName = null;
    try {
        InetAddress addr = InetAddress.getLocalHost();

        hostName = addr.getHostName();
    } catch (IOException e) {
        log.error(LogPrefix + "Get HostName Error", e);
        hostName = "localhost";
    }
    String host = System.getProperty("hostName", hostName);
    try {
        boolean useJmx = Boolean.parseBoolean(System.getProperty("tddl.useJMX", "true"));
        if (useJmx) {
            mbs = ManagementFactory.getPlatformMBeanServer();
            int port = Integer.parseInt(System.getProperty("tddl.rmi.port", "6679"));
            String rmiName = System.getProperty("tddl.rmi.name", "tddlJmxServer");
            Registry reg = null;
            try {
                reg = LocateRegistry.getRegistry(port);
                reg.list();
            } catch (Exception e) {
                reg = null;
            }
            if (null == reg) {
                reg = LocateRegistry.createRegistry(port);
            }
            reg.list();
            String serverURL = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/" + rmiName;
            JMXServiceURL url = new JMXServiceURL(serverURL);
            final JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url,
                    null, mbs);
            connectorServer.start();
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    try {
                        System.err.println("JMXConnector stop");
                        connectorServer.stop();
                    } catch (IOException e) {
                        log.error(LogPrefix + e);
                    }
                }
            });
            log.warn(LogPrefix + "jmx url: " + serverURL);
        }
    } catch (Exception e) {
        log.error(LogPrefix + "create MBServer error", e);
    }
}

From source file:com.addthis.metrics.reporter.config.AbstractHostPortReporterConfig.java

public AbstractHostPortReporterConfig() {
    try {/*from   w w w  .java  2  s. com*/
        this.localhost = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        // not expected to happen with properly configured system
        log.error("Unable to get localhost", e);
    }
}

From source file:com.doculibre.constellio.filters.LocalRequestFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    Set<String> acceptedAndDynamicHosts = new HashSet<String>();
    acceptedAndDynamicHosts.addAll(Arrays.asList(ACCEPTED_HOSTS));

    // Solution adapted from http://www.exampledepot.com/egs/java.net/Local.html
    String hostName = InetAddress.getLocalHost().getHostName();
    InetAddress addrs[] = InetAddress.getAllByName(hostName);
    for (InetAddress addr : addrs) {
        String hostAddress = addr.getHostAddress();
        acceptedAndDynamicHosts.add(hostAddress);
    }//  w ww . ja  v a  2s.  c om

    boolean valid;
    if (isIgnoredRequest(request) || isFileRequest(request)) {
        valid = true;
    } else {
        valid = false;
        String remoteHost = request.getRemoteHost();
        for (String acceptedHost : acceptedAndDynamicHosts) {
            if (remoteHost.equals(acceptedHost)) {
                valid = true;
            }
        }
    }

    if (valid) {
        // Pass control on to the next filter
        chain.doFilter(request, response);
    } else {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        throw new ServletException("Cannot send request to a servlet from outside the Web application : "
                + httpRequest.getRequestURL());
    }
}

From source file:net.dfs.user.test.Store.java

public static String userIP() throws UnknownHostException {
    return InetAddress.getLocalHost().getHostAddress();
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static Key generateKey() throws Exception {

    String cle = CLE;//from ww w .j a  va 2 s  .c  om
    try {
        if (cle.isEmpty()) {
            InetAddress adrLocale;
            adrLocale = InetAddress.getLocalHost();
            cle = adrLocale.getHostName();
        }
        if (cle.length() > 16) {
            cle = cle.substring(0, 16);
        }
        if (cle.length() < 16) {
            int chartocomplete = 16 - cle.length();
            for (int i = 0; i < chartocomplete; i++) {
                cle = cle + i;
            }
        }
        logger.log(Level.INFO, "cle par defaut {0}", cle);
        keyValue = cle.getBytes();
    } catch (Exception e) {
        e.printStackTrace();

    }
    Key key = new SecretKeySpec(keyValue, ALGO);
    return key;
}

From source file:net.firejack.platform.service.deployment.broker.DeployBroker.java

@Override
protected ServiceResponse perform(ServiceRequest<NamedValues> request) throws Exception {
    Long packageId = (Long) request.getData().get("packageId");
    String name = (String) request.getData().get("name");
    String file = (String) request.getData().get("file");
    String host = InetAddress.getLocalHost().getHostName();

    if (debug) {//w  w  w.  j  a v  a2s.c o  m
        webapps = new File(System.getenv("CATALINA_HOME"), "webapps");
    }

    InputStream stream = OPFEngine.RegistryService.getPackageArchive(packageId, file);

    File app = new File(webapps, name + ".temp");
    FileOutputStream outputStream = FileUtils.openOutputStream(app);
    if (stream != null) {
        IOUtils.copy(stream, outputStream);
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(stream);

        File war = new File(app.getParent(), name);
        FileUtils.deleteQuietly(war);
        FileUtils.moveFile(app, war);
    }

    return new ServiceResponse("Deploy to server " + host + " successfully", true);
}

From source file:com.dianping.dpsf.jmx.DpsfRequestorMonitor.java

@MBeanMeta(order = 0)
public String getHostName() {
    try {//w  ww .ja  v  a  2s  . co  m
        return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        return "unknown";
    }
}

From source file:com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java

public InetAddress getLocalAddress() throws UnknownHostException {
    InetAddress result = InetAddress.getLocalHost();
    return result;
}

From source file:com.predic8.membrane.core.interceptor.CountInterceptor.java

private String getPage() throws UnknownHostException {
    StringWriter writer = new StringWriter();
    new Html(writer) {
        {//from  w  w  w .  j a  v a  2s  .com
            html();
            head();
            title().text(name).end();
            end();
            body();
            h1().text(name + "(" + InetAddress.getLocalHost().getHostAddress() + ")").end();
            p().text(
                    "This site is generated by a simple interceptor that counts how often this site was requested.")
                    .end();
            p().text("Request count: " + counter).end();
            endAll();
            done();
        }
    };
    return writer.toString();
}

From source file:com.yahoo.storm.yarn.StormMasterServerHandler.java

private void setStormHostConf() {
    try {//from   w  ww  .  j  av a  2 s  .c o  m
        String host_addr = InetAddress.getLocalHost().getHostAddress();
        LOG.info("Storm master host:" + host_addr);
        _storm_conf.put(Config.NIMBUS_HOST, host_addr);
    } catch (UnknownHostException ex) {
        LOG.warn("Failed to get IP address of local host");
    }
}