Example usage for java.net NetworkInterface getNetworkInterfaces

List of usage examples for java.net NetworkInterface getNetworkInterfaces

Introduction

In this page you can find the example usage for java.net NetworkInterface getNetworkInterfaces.

Prototype

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException 

Source Link

Document

Returns an Enumeration of all the interfaces on this machine.

Usage

From source file:org.exoplatform.outlook.jcr.ContentLink.java

/**
 * Instantiates a new content link.//  w  w  w  .  j  a va 2s. c o m
 *
 * @param jcrService the jcr service
 * @param sessionProviders the session providers
 * @param finder the finder
 * @param organization the organization
 * @param identityRegistry the identity registry
 * @param cacheService the cache service
 * @param params the params
 * @throws ConfigurationException the configuration exception
 */
public ContentLink(RepositoryService jcrService, SessionProviderService sessionProviders, NodeFinder finder,
        OrganizationService organization, IdentityRegistry identityRegistry, CacheService cacheService,
        InitParams params) throws ConfigurationException {
    this.jcrService = jcrService;
    this.sessionProviders = sessionProviders;
    this.finder = finder;
    this.organization = organization;
    this.identityRegistry = identityRegistry;

    this.activeLinks = cacheService.getCacheInstance(LINK_CACHE_NAME);

    if (params != null) {
        PropertiesParam param = params.getPropertiesParam("link-configuration");
        if (param != null) {
            config = Collections.unmodifiableMap(param.getProperties());
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Property parameters link-configuration not found, will use default settings.");
            }
            config = Collections.<String, String>emptyMap();
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Component configuration not found, will use default settings.");
        }
        config = Collections.<String, String>emptyMap();
    }

    StringBuilder restUrl = new StringBuilder();
    String exoBaseUrl = System.getProperty(EXO_BASE_URL);
    if (exoBaseUrl == null || exoBaseUrl.toUpperCase().toLowerCase().startsWith("http://localhost")) {
        // seems we have base URL not set explicitly for the server
        String schema = config.get(CONFIG_SCHEMA);
        if (schema == null || (schema = schema.trim()).length() == 0) {
            schema = "http";
        }

        String host = config.get(CONFIG_HOST);
        if (host == null || host.trim().length() == 0) {
            host = null;
            try {
                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                while (host == null && interfaces.hasMoreElements()) {
                    NetworkInterface nic = interfaces.nextElement();
                    Enumeration<InetAddress> addresses = nic.getInetAddresses();
                    while (host == null && addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        if (!address.isLoopbackAddress()) {
                            host = address.getHostName();
                        }
                    }
                }
            } catch (SocketException e) {
                // cannot get net interfaces
            }

            if (host == null) {
                try {
                    host = InetAddress.getLocalHost().getHostName();
                } catch (UnknownHostException e) {
                    host = "localhost:8080"; // assume development environment otherwise
                }
            }
        }
        restUrl.append(schema);
        restUrl.append("://");
        restUrl.append(host);
    } else {
        restUrl.append(exoBaseUrl);
    }

    restUrl.append('/');
    restUrl.append(PortalContainer.getCurrentPortalContainerName());
    restUrl.append('/');
    restUrl.append(PortalContainer.getCurrentRestContextName());
    this.restUrl = restUrl.toString();

    LOG.info("Default service URL for content links is " + this.restUrl);
}

From source file:org.alfresco.filesys.auth.PassthruServerFactory.java

/**
 * Set the broadcast mask to use for NetBIOS name lookups
 * //from www  .  jav  a 2 s .c o  m
 * @param bcastMask String
 * @exception AlfrescoRuntimeException
 */
public final void setBroadcastMask(String bcastMask) throws IOException {

    if (bcastMask == null || bcastMask.length() == 0) {

        // Clear the NetBIOS subnet mask

        NetBIOSSession.setDefaultSubnetMask(null);
        return;
    }

    // Find the network adapter with the matching broadcast mask

    try {
        Enumeration<NetworkInterface> netEnum = NetworkInterface.getNetworkInterfaces();
        NetworkInterface bcastIface = null;

        while (netEnum.hasMoreElements() && bcastIface == null) {

            NetworkInterface ni = netEnum.nextElement();
            for (InterfaceAddress iAddr : ni.getInterfaceAddresses()) {
                InetAddress broadcast = iAddr.getBroadcast();
                if (broadcast != null && broadcast.getHostAddress().equals(bcastMask))
                    bcastIface = ni;
            }
        }

        // DEBUG

        if (logger.isDebugEnabled()) {
            if (bcastIface != null)
                logger.debug("Broadcast mask " + bcastMask + " found on network interface "
                        + bcastIface.getDisplayName() + "/" + bcastIface.getName());
            else
                logger.debug("Failed to find network interface for broadcast mask " + bcastMask);
        }

        // Check if we found a valid network interface for the broadcast mask

        if (bcastIface == null)
            throw new AlfrescoRuntimeException(
                    "Network interface for broadcast mask " + bcastMask + " not found");

        // Set the NetBIOS broadcast mask

        NetBIOSSession.setDefaultSubnetMask(bcastMask);
    } catch (SocketException ex) {
    }
}

From source file:org.pepstock.jem.node.affinity.SystemInfo.java

/**
 * Returns the list of inet addresses scanning the network interfaces of machine
 * @return the list of inet addresses scanning the network interfaces of machine
 *//* w  w w  . j  av  a 2 s  . co m*/
private List<InetAddress> loadInetAddress() {
    List<InetAddress> list = new ArrayList<InetAddress>();
    try {
        // scans all network interfaces
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            // scans all ip addresses
            Enumeration<InetAddress> addresses = ni.getInetAddresses();
            while (addresses.hasMoreElements()) {
                // adds Ip address to the collection
                InetAddress addr = addresses.nextElement();
                list.add(addr);
            }
        }
    } catch (Exception e) {
        // debug
        LogAppl.getInstance().debug(e.getMessage(), e);
    }
    return list;
}

From source file:org.apache.sshd.common.util.net.SshdSocketAddress.java

/**
 * @return a {@link List} of local network addresses which are not multicast
 * or localhost sorted according to {@link #BY_HOST_ADDRESS}
 *///from w  w  w.  j  a  v  a2  s  .c  o m
public static List<InetAddress> getExternalNetwork4Addresses() {
    List<InetAddress> addresses = new ArrayList<>();
    try {
        for (Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); (nets != null)
                && nets.hasMoreElements();) {
            NetworkInterface netint = nets.nextElement();
            /* TODO - uncomment when 1.5 compatibility no longer required
            if (!netint.isUp()) {
            continue;    // ignore non-running interfaces
            }
            */

            for (Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); (inetAddresses != null)
                    && inetAddresses.hasMoreElements();) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (isValidHostAddress(inetAddress)) {
                    addresses.add(inetAddress);
                }
            }
        }
    } catch (SocketException e) {
        // swallow
    }

    if (GenericUtils.size(addresses) > 1) {
        Collections.sort(addresses, BY_HOST_ADDRESS);
    }

    return addresses;
}

From source file:com.dmsl.anyplace.utils.NetworkUtils.java

public static String getLocalIP(boolean useIPv4) {
    String ip = "No Local IP assigned";
    try {/* ww w. ja v a 2s. c  o m*/

        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : interfaces) {
            List<InetAddress> addresses = Collections.list(ni.getInetAddresses());
            for (InetAddress ia : addresses) {
                if (ia != null && !ia.isLoopbackAddress()) {
                    String sAddr = ia.getHostAddress().toUpperCase(Locale.ENGLISH);
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4) {
                            ip = sAddr;
                        }
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%');
                            ip = delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        ip = "Unknown Error, Report it!";
    }
    return ip;
}

From source file:de.madvertise.android.sdk.MadvertiseUtil.java

/**
 * Fetch the address of the enabled interface
 * /*w  w  w  .j  av a  2s  .c o m*/
 * @return ip address as string
 */
public static String getLocalIpAddress(MadvertiseViewCallbackListener listener) {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    if (inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
    } catch (SocketException e) {
        if (listener != null) {
            listener.onError(e);
        }
        e.printStackTrace();
    }

    if (listener != null) {
        listener.onError(new IllegalArgumentException("Couldn't obtain the local ip address"));
    }
    return "";
}

From source file:org.fusesource.mop.commands.AbstractContainerBase.java

protected String getHostName() throws Exception {
    String hostname = "localhost";
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface inet = interfaces.nextElement();
        Enumeration<InetAddress> addresses = inet.getInetAddresses();
        if (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (address.isLoopbackAddress()) {
                InetAddress localAddress = InetAddress.getByName(address.getHostName());
                hostname = localAddress.getHostName();
                break;
            }/*from   w w w .j a  va 2 s .  c  om*/
        }
    }
    return hostname;
}

From source file:org.wso2.javaagent.JDBCAgentPublisher.java

public static InetAddress getLocalAddress() throws SocketException, UnknownHostException {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface iface = interfaces.nextElement();
        Enumeration<InetAddress> addresses = iface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress addr = addresses.nextElement();
            if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                return addr;
            }/*from   ww w.  ja  v a2 s .  c om*/
        }
    }
    return InetAddress.getLocalHost();
}

From source file:com.bluetooth.activities.WiFiControl.java

/**
 * This method gets an IPv4 address for the user to enter in the browser.
 * /*from  ww w  .  j  a  v  a  2s  .  c om*/
 * @return The IP
 */
private String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}