List of usage examples for java.net NetworkInterface getDisplayName
public String getDisplayName()
From source file:org.graphwalker.Util.java
protected static InetAddress getInternetAddr(final String nic) { // Find the real network interface NetworkInterface iface = null; InetAddress ia = null;/*from ww w. j a v a2s . co m*/ boolean foundNIC = false; try { for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces .hasMoreElements() && foundNIC == false;) { iface = ifaces.nextElement(); Util.logger.debug("Interface: " + iface.getDisplayName()); for (Enumeration<InetAddress> ips = iface.getInetAddresses(); ips.hasMoreElements() && foundNIC == false;) { ia = ips.nextElement(); Util.logger.debug(ia.getCanonicalHostName() + " " + ia.getHostAddress()); if (!ia.isLoopbackAddress()) { Util.logger.debug(" Not a loopback address..."); if (!ia.getHostAddress().contains(":") && nic.equals(iface.getDisplayName())) { Util.logger.debug(" Host address does not contain ':'"); Util.logger.debug(" Interface: " + iface.getName() + " seems to be InternetInterface. I'll take it..."); foundNIC = true; } } } } } catch (SocketException e) { Util.logger.error(e.getMessage()); } finally { if (!foundNIC && nic != null) { Util.logger.error("Could not bind to network interface: " + nic); throw new RuntimeException("Could not bind to network interface: " + nic); } else if (!foundNIC) { Util.logger.error("Could not bind to any network interface"); throw new RuntimeException("Could not bind to any network interface: "); } } return ia; }
From source file:com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java
public List<String> getLocalHostAddresses(InetAddressAcceptor aAcceptor) throws SocketException { List<String> result = new ArrayList<String>(); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); if (aAcceptor.acceptNetworkInterface(networkInterface)) { if (fLog.isInfoEnabled()) { String name = networkInterface.getName(); String displayName = networkInterface.getDisplayName(); fLog.info(MessageFormat.format( "Discovered NetworkInterface - ACCEPTED. Name:[{0}]. Display Name:[{1}]", name, displayName));/* w w w . j a v a 2 s. co m*/ } collectInterfaceIPs(networkInterface, result, aAcceptor); } else if (fLog.isInfoEnabled()) { String name = networkInterface.getName(); String displayName = networkInterface.getDisplayName(); fLog.info(MessageFormat.format( "Discovered NetworkInterface - SKIPPED. Name:[{0}]. Display Name:[{1}]", name, displayName)); } } return result; }
From source file:com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java
public List<InetAddress> getLocalAddresses(InetAddressAcceptor aAcceptor) throws SocketException { List<InetAddress> result = new ArrayList<InetAddress>(); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); if (aAcceptor.acceptNetworkInterface(networkInterface)) { if (fLog.isInfoEnabled()) { String name = networkInterface.getName(); String displayName = networkInterface.getDisplayName(); fLog.info(MessageFormat.format( "Discovered NetworkInterface - ACCEPTED. Name:[{0}]. Display Name:[{1}]", name, displayName));//from ww w . ja va 2s. c om } collectInterfaceAddresses(networkInterface, result, aAcceptor); } else { if (fLog.isInfoEnabled()) { String name = networkInterface.getName(); String displayName = networkInterface.getDisplayName(); fLog.info(MessageFormat.format( "Discovered NetworkInterface - SKIPPED. Name:[{0}]. Display Name:[{1}]", name, displayName)); } } } return result; }
From source file:com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java
public List<NetworkInterface> getNetworkInterfaces(InetAddressAcceptor aAcceptor) throws SocketException { List<NetworkInterface> result = new ArrayList<NetworkInterface>(); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); if (aAcceptor.acceptNetworkInterface(networkInterface)) { if (fLog.isInfoEnabled()) { String name = networkInterface.getName(); String displayName = networkInterface.getDisplayName(); fLog.info(MessageFormat.format( "Discovered NetworkInterface - ACCEPTED. Name:[{0}]. Display Name:[{1}]", name, displayName));/*from w w w . j a v a2 s.c o m*/ } result.add(networkInterface); } else { if (fLog.isInfoEnabled()) { String name = networkInterface.getName(); String displayName = networkInterface.getDisplayName(); fLog.info(MessageFormat.format( "Discovered NetworkInterface - SKIPPED. Name:[{0}]. Display Name:[{1}]", name, displayName)); } } } return result; }
From source file:com.marcosdiez.server.php.service.Network.java
public Network() { names = new LinkedList<String>(); titles = new LinkedList<String>(); adresses = new LinkedList<String>(); try {/*from w w w . j a v a2 s .c o m*/ List<NetworkInterface> list = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface iface : list) { List<InetAddress> addresses = Collections.list(iface.getInetAddresses()); for (InetAddress address : addresses) { if (!address.isLoopbackAddress()) { String host = address.getHostAddress().toUpperCase(); if (InetAddressUtils.isIPv4Address(host)) { names.add(iface.getName()); titles.add(host + "(" + iface.getDisplayName() + ")"); adresses.add(host); } } } } for (NetworkInterface iface : list) { List<InetAddress> addresses = Collections.list(iface.getInetAddresses()); for (InetAddress address : addresses) { if (address.isLoopbackAddress()) { String host = address.getHostAddress().toUpperCase(); if (InetAddressUtils.isIPv4Address(host)) { names.add(iface.getName()); titles.add(host + "(" + iface.getDisplayName() + ")"); adresses.add(host); } } } } } catch (Exception ex) { } names.add("all"); adresses.add("0.0.0.0"); titles.add("0.0.0.0 (all)"); }
From source file:org.chromium.ChromeSystemNetwork.java
private void getNetworkInterfaces(final CordovaArgs args, final CallbackContext callbackContext) { cordova.getThreadPool().execute(new Runnable() { @Override//from w w w . j av a 2 s .com public void run() { try { JSONArray ret = new JSONArray(); ArrayList<NetworkInterface> interfaces = Collections .list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface iface : interfaces) { if (iface.isLoopback()) { continue; } for (InterfaceAddress interfaceAddress : iface.getInterfaceAddresses()) { InetAddress address = interfaceAddress.getAddress(); if (address == null) { continue; } JSONObject data = new JSONObject(); data.put("name", iface.getDisplayName()); // Strip address scope zones for IPv6 address. data.put("address", address.getHostAddress().replaceAll("%.*", "")); data.put("prefixLength", interfaceAddress.getNetworkPrefixLength()); ret.put(data); } } callbackContext.success(ret); } catch (Exception e) { Log.e(LOG_TAG, "Error occured while getting network interfaces", e); callbackContext.error("Could not get network interfaces"); } } }); }
From source file:com.safi.asterisk.handler.SafletEngine.java
public static String getMacAddress() { try {/*from w ww.j av a2 s . c o m*/ Enumeration<NetworkInterface> enm = NetworkInterface.getNetworkInterfaces(); while (enm.hasMoreElements()) { // for(int i=0;i<addresses.length;i++){ NetworkInterface ni = enm.nextElement(); try { byte[] mac = ni.getHardwareAddress(); String macTarget = getMacString(mac); if (StringUtils.isNotBlank(macTarget)) return macTarget.toUpperCase(); } catch (Exception ignore) { if (log.isDebugEnabled()) log.debug("Skipping " + ni.getDisplayName()); } } } catch (Exception ex) { } return ""; }
From source file:fr.inria.ucn.collectors.NetworkStateCollector.java
private JSONArray getIfconfig(Map<String, JSONObject> stats) throws JSONException { JSONArray ifaces = new JSONArray(); // make sure the stats is read networkStats();/*from w w w. j a va 2 s . c o m*/ Enumeration<NetworkInterface> en = null; try { en = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { Log.d(Constants.LOGTAG, "failed to list interfaces", e); } if (en != null) { while (en.hasMoreElements()) { NetworkInterface intf = en.nextElement(); JSONObject iface = new JSONObject(); iface.put("display_name", intf.getDisplayName()); iface.put("name", intf.getName()); iface.put("is_virtual", intf.isVirtual()); iface.put("stats", stats.get(intf.getName())); try { iface.put("mtu", intf.getMTU()); iface.put("is_loopback", intf.isLoopback()); iface.put("is_ptop", intf.isPointToPoint()); iface.put("is_up", intf.isUp()); } catch (SocketException e) { Log.d(Constants.LOGTAG, "failed to read interface data", e); } JSONArray ips = new JSONArray(); List<InterfaceAddress> ilist = intf.getInterfaceAddresses(); for (InterfaceAddress ia : ilist) { ips.put(ia.getAddress().getHostAddress()); } iface.put("addresses", ips); ifaces.put(iface); } } else { for (String name : stats.keySet()) { JSONObject iface = new JSONObject(); iface.put("name", name); iface.put("stats", stats.get(name)); ifaces.put(iface); } } return ifaces; }
From source file:org.sakuli.actions.environment.CipherUtil.java
/** * fetch the local network interfaceLog and reads out the MAC of the chosen encryption interface. * Must be called before the methods {@link #encrypt(String)} or {@link #decrypt(String)}. * * @throws SakuliCipherException for wrong interface names and MACs. *//* www . ja v a2s .co m*/ @PostConstruct public void scanNetworkInterfaces() throws SakuliCipherException { Enumeration<NetworkInterface> networkInterfaces; try { interfaceName = checkEthInterfaceName(); networkInterfaces = getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface anInterface = networkInterfaces.nextElement(); if (anInterface.getHardwareAddress() != null) { interfaceLog = interfaceLog + "\nNET-Interface " + anInterface.getIndex() + " - Name: " + anInterface.getName() + "\t MAC: " + formatMAC(anInterface.getHardwareAddress()) + "\t VirtualAdapter: " + anInterface.isVirtual() + "\t Loopback: " + anInterface.isLoopback() + "\t Desc.: " + anInterface.getDisplayName(); } if (anInterface.getName().equals(interfaceName)) { macOfEncryptionInterface = anInterface.getHardwareAddress(); } } if (macOfEncryptionInterface == null) { throw new SakuliCipherException( "Cannot resolve MAC address ... please check your config of the property: " + ActionProperties.ENCRYPTION_INTERFACE + "=" + interfaceName, interfaceLog); } } catch (Exception e) { throw new SakuliCipherException(e, interfaceLog); } }
From source file:net.pms.newgui.NetworkTab.java
public JComponent build() { FormLayout layout = new FormLayout("left:pref, 2dlu, p, 2dlu , p, 2dlu, p, 2dlu, pref:grow", "p, 0dlu, p, 0dlu, p, 3dlu, p, 3dlu, p, 3dlu,p, 3dlu, p, 15dlu, p, 3dlu,p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p,3dlu, p, 3dlu, p, 15dlu, p,3dlu, p, 3dlu, p, 15dlu, p, 3dlu, p"); PanelBuilder builder = new PanelBuilder(layout); builder.setBorder(Borders.DLU4_BORDER); builder.setOpaque(true);/*from ww w . j a v a 2 s . co m*/ CellConstraints cc = new CellConstraints(); smcheckBox = new JCheckBox(Messages.getString("NetworkTab.3")); smcheckBox.setContentAreaFilled(false); smcheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { PMS.getConfiguration().setMinimized((e.getStateChange() == ItemEvent.SELECTED)); } }); if (PMS.getConfiguration().isMinimized()) { smcheckBox.setSelected(true); } JComponent cmp = builder.addSeparator(Messages.getString("NetworkTab.5"), cc.xyw(1, 1, 9)); cmp = (JComponent) cmp.getComponent(0); cmp.setFont(cmp.getFont().deriveFont(Font.BOLD)); builder.addLabel(Messages.getString("NetworkTab.0"), cc.xy(1, 7)); final KeyedComboBoxModel kcbm = new KeyedComboBoxModel( new Object[] { "bg", "ca", "zhs", "zht", "cz", "da", "nl", "en", "fi", "fr", "de", "el", "is", "it", "ja", "no", "pl", "pt", "br", "ro", "ru", "sl", "es", "sv" }, new Object[] { "Bulgarian", "Catalan", "Chinese (Simplified)", "Chinese (Traditional)", "Czech", "Danish", "Dutch", "English", "Finnish", "French", "German", "Greek", "Icelandic", "Italian", "Japanese", "Norwegian", "Polish", "Portuguese", "Portuguese (Brazilian)", "Romanian", "Russian", "Slovenian", "Spanish", "Swedish" }); langs = new JComboBox(kcbm); langs.setEditable(false); //langs.setSelectedIndex(0); String defaultLang = null; if (configuration.getLanguage() != null && configuration.getLanguage().length() > 0) { defaultLang = configuration.getLanguage(); } else { defaultLang = Locale.getDefault().getLanguage(); } if (defaultLang == null) { defaultLang = "en"; } kcbm.setSelectedKey(defaultLang); if (langs.getSelectedIndex() == -1) { langs.setSelectedIndex(0); } langs.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { configuration.setLanguage((String) kcbm.getSelectedKey()); } } }); builder.add(langs, cc.xyw(3, 7, 7)); builder.add(smcheckBox, cc.xyw(1, 9, 9)); JButton service = new JButton(Messages.getString("NetworkTab.4")); service.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (PMS.get().installWin32Service()) { JOptionPane.showMessageDialog( (JFrame) (SwingUtilities.getWindowAncestor((Component) PMS.get().getFrame())), Messages.getString("NetworkTab.11") + Messages.getString("NetworkTab.12"), "Information", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog( (JFrame) (SwingUtilities.getWindowAncestor((Component) PMS.get().getFrame())), Messages.getString("NetworkTab.14"), "Error", JOptionPane.ERROR_MESSAGE); } } }); builder.add(service, cc.xy(1, 11)); if (System.getProperty(LooksFrame.START_SERVICE) != null || !Platform.isWindows()) { service.setEnabled(false); } host = new JTextField(PMS.getConfiguration().getServerHostname()); host.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { configuration.setHostname(host.getText()); PMS.get().getFrame().setReloadable(true); } }); // host.setEnabled( StringUtils.isBlank(configuration.getNetworkInterface())) ; port = new JTextField(configuration.getServerPort() != 5001 ? "" + configuration.getServerPort() : ""); port.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { try { String p = port.getText(); if (StringUtils.isEmpty(p)) { p = "5001"; } int ab = Integer.parseInt(p); configuration.setServerPort(ab); PMS.get().getFrame().setReloadable(true); } catch (NumberFormatException nfe) { } } }); cmp = builder.addSeparator(Messages.getString("NetworkTab.22"), cc.xyw(1, 21, 9)); cmp = (JComponent) cmp.getComponent(0); cmp.setFont(cmp.getFont().deriveFont(Font.BOLD)); ArrayList<String> names = new ArrayList<String>(); names.add(""); ArrayList<String> interfaces = new ArrayList<String>(); interfaces.add(""); Enumeration<NetworkInterface> enm; try { enm = NetworkInterface.getNetworkInterfaces(); while (enm.hasMoreElements()) { NetworkInterface ni = enm.nextElement(); // check for interface has at least one ip address. if (ni.getInetAddresses().hasMoreElements()) { names.add(ni.getName()); String displayName = ni.getDisplayName(); if (displayName == null) { displayName = ni.getName(); } interfaces.add(displayName.trim()); } } } catch (SocketException e1) { logger.error(null, e1); } final KeyedComboBoxModel networkInterfaces = new KeyedComboBoxModel(names.toArray(), interfaces.toArray()); networkinterfacesCBX = new JComboBox(networkInterfaces); networkInterfaces.setSelectedKey(configuration.getNetworkInterface()); networkinterfacesCBX.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { configuration.setNetworkInterface((String) networkInterfaces.getSelectedKey()); //host.setEnabled( StringUtils.isBlank(configuration.getNetworkInterface())) ; PMS.get().getFrame().setReloadable(true); } } }); ip_filter = new JTextField(PMS.getConfiguration().getIpFilter()); ip_filter.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { configuration.setIpFilter(ip_filter.getText()); PMS.get().getFrame().setReloadable(true); } }); builder.addLabel(Messages.getString("NetworkTab.20"), cc.xy(1, 23)); builder.add(networkinterfacesCBX, cc.xyw(3, 23, 7)); builder.addLabel(Messages.getString("NetworkTab.23"), cc.xy(1, 25)); builder.add(host, cc.xyw(3, 25, 7)); builder.addLabel(Messages.getString("NetworkTab.24"), cc.xy(1, 27)); builder.add(port, cc.xyw(3, 27, 7)); builder.addLabel(Messages.getString("NetworkTab.30"), cc.xy(1, 29)); builder.add(ip_filter, cc.xyw(3, 29, 7)); cmp = builder.addSeparator(Messages.getString("NetworkTab.31"), cc.xyw(1, 31, 9)); cmp = (JComponent) cmp.getComponent(0); cmp.setFont(cmp.getFont().deriveFont(Font.BOLD)); newHTTPEngine = new JCheckBox(Messages.getString("NetworkTab.32")); newHTTPEngine.setSelected(PMS.getConfiguration().isHTTPEngineV2()); newHTTPEngine.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { PMS.getConfiguration().setHTTPEngineV2((e.getStateChange() == ItemEvent.SELECTED)); } }); builder.add(newHTTPEngine, cc.xyw(1, 33, 9)); preventSleep = new JCheckBox(Messages.getString("NetworkTab.33")); preventSleep.setSelected(PMS.getConfiguration().isPreventsSleep()); preventSleep.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { PMS.getConfiguration().setPreventsSleep((e.getStateChange() == ItemEvent.SELECTED)); } }); builder.add(preventSleep, cc.xyw(1, 35, 9)); cmp = builder.addSeparator(Messages.getString("NetworkTab.34"), cc.xyw(1, 37, 9)); cmp = (JComponent) cmp.getComponent(0); cmp.setFont(cmp.getFont().deriveFont(Font.BOLD)); JPanel panel = builder.getPanel(); JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); return scrollPane; }