Example usage for java.util Collections list

List of usage examples for java.util Collections list

Introduction

In this page you can find the example usage for java.util Collections list.

Prototype

public static <T> ArrayList<T> list(Enumeration<T> e) 

Source Link

Document

Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

Usage

From source file:org.jgrades.lic.service.MacRule.java

public MacRule() {
    try {/*from w  w  w.  ja va  2 s.  co  m*/
        networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        LOGGER.trace("Network interfaces in system: {}", networkInterfaces);
    } catch (SocketException e) {
        LOGGER.error("Error during preparing network interfaces", e);
        throw new ViolationOfLicenceConditionException(e);
    }
}

From source file:camp.pixels.signage.util.NetworkInterfaces.java

/**
 * @param interfaceName eth0, wlan0 or NULL=use first interface 
 * @param useIPv4// ww w . j av a 2s .c  o m
 * @return address or empty string
 */
@SuppressLint("DefaultLocale")
public static String getIPAddress(String interfaceName, boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            if (!VALID_INTERFACES.contains(intf.getName()))
                continue;
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (SocketException ex) {
    }
    return "";
}

From source file:psiprobe.tools.logging.logback.LogbackLoggerAccessor.java

/**
 * Returns all appenders of this logger.
 * //ww w .  j av  a 2 s.  c  om
 * @return a list of {@link LogbackAppenderAccessor}s
 */
public List<LogbackAppenderAccessor> getAppenders() {
    List<LogbackAppenderAccessor> appenders = new ArrayList<>();
    try {
        for (Object appender : Collections.list(IteratorUtils.asEnumeration(
                (Iterator<Object>) MethodUtils.invokeMethod(getTarget(), "iteratorForAppenders", null)))) {
            List<Object> siftedAppenders = getSiftedAppenders(appender);
            if (siftedAppenders != null) {
                for (Object siftedAppender : siftedAppenders) {
                    wrapAndAddAppender(siftedAppender, appenders);
                }
            } else {
                wrapAndAddAppender(appender, appenders);
            }
        }
    } catch (Exception e) {
        logger.error("{}#getAppenders() failed", getTarget().getClass().getName(), e);
    }
    return appenders;
}

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.namelessrom.devicecontrol.net.NetworkInfo.java

public static String getIpAddress(final boolean useIPv4) {
    List<NetworkInterface> interfaces;
    try {//from  ww  w.  j a  va  2 s .  c o  m
        interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
    } catch (Exception e) {
        interfaces = null;
    }
    if (interfaces == null)
        return "0.0.0.0";
    for (NetworkInterface intf : interfaces) {
        final List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
        for (final InetAddress addr : addrs) {
            if (!addr.isLoopbackAddress()) {
                final String sAddr = addr.getHostAddress().toUpperCase();
                boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                if (useIPv4) {
                    if (isIPv4) {
                        return sAddr;
                    }
                } else {
                    if (!isIPv4) {
                        final int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                        return ((delim < 0) ? sAddr : sAddr.substring(0, delim));
                    }
                }
            }
        }
    }
    return "0.0.0.0";
}

From source file:psiprobe.tools.logging.slf4jlogback.TomcatSlf4jLogbackLoggerAccessor.java

/**
 * Returns all appenders of this logger.
 * // w w w  . j  ava  2s.  com
 * @return a list of {@link TomcatSlf4jLogbackAppenderAccessor}s
 */
public List<TomcatSlf4jLogbackAppenderAccessor> getAppenders() {
    List<TomcatSlf4jLogbackAppenderAccessor> appenders = new ArrayList<>();

    try {
        for (Object appender : Collections.list(IteratorUtils.asEnumeration(
                (Iterator<Object>) MethodUtils.invokeMethod(getTarget(), "iteratorForAppenders", null)))) {
            List<Object> siftedAppenders = getSiftedAppenders(appender);
            if (siftedAppenders != null) {
                for (Object siftedAppender : siftedAppenders) {
                    wrapAndAddAppender(siftedAppender, appenders);
                }
            } else {
                wrapAndAddAppender(appender, appenders);
            }
        }
    } catch (Exception e) {
        logger.error("{}#getAppenders() failed", getTarget().getClass().getName(), e);
    }
    return appenders;
}

From source file:org.peercast.pecaport.NetworkDeviceManager.java

/**
 * ?(eth0, eth1..)??// w ww  . j  av a 2s .co  m
 */
public List<NetworkInterfaceInfo.Ethernet> getEthernetInterface() {
    List<NetworkInterfaceInfo.Ethernet> infos = new ArrayList<>();
    try {

        for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
            if (!ni.isLoopback() && !ni.isVirtual() && ni.getName().matches("^eth\\d+$") && //
                    ni.getInetAddresses().hasMoreElements())
                infos.add(new NetworkInterfaceInfo.Ethernet(ni));
        }
    } catch (SocketException e) {
        Log.w(TAG, "getEthernetInterface()", e);
    }
    return infos;
}

From source file:psiprobe.jsp.AddQueryParamTag.java

@Override
public int doStartTag() throws JspException {
    StringBuilder query = new StringBuilder();
    query.append(param).append("=").append(value);
    for (String name : Collections.list(pageContext.getRequest().getParameterNames())) {
        if (!param.equals(name)) {
            query.append("&").append(name).append("=")
                    .append(ServletRequestUtils.getStringParameter(pageContext.getRequest(), name, ""));
        }/*from  ww  w  .ja v a 2  s  . c  o m*/
    }
    try {
        pageContext.getOut().print(query);
    } catch (IOException e) {
        logger.debug("Exception printing query string to JspWriter", e);
        throw new JspException(e);
    }
    return EVAL_BODY_INCLUDE;
}

From source file:io.restassured.module.mockmvc.http.AttributeController.java

@RequestMapping(value = "/attribute", method = GET, produces = APPLICATION_JSON_VALUE)
public @ResponseBody String attribute(HttpServletRequest request) {
    Collection<String> attributes = new ArrayList<String>();
    for (String attributeName : Collections.list(request.getAttributeNames())) {
        attributes.add("\"" + attributeName + "\": \"" + request.getAttribute(attributeName) + "\"");
    }//www. jav a 2 s. c o m

    return "{" + StringUtils.join(attributes, ", ") + "}";
}

From source file:net.dataforte.commons.resources.ClassUtils.java

/**
 * Returns all resources beneath a folder. Supports filesystem, JARs and JBoss VFS
 * // ww w.j  a v  a2  s .  co m
 * @param folder
 * @return
 * @throws IOException
 */
public static URL[] getResources(String folder) throws IOException {
    List<URL> urls = new ArrayList<URL>();
    ArrayList<File> directories = new ArrayList<File>();
    try {
        ClassLoader cld = Thread.currentThread().getContextClassLoader();
        if (cld == null) {
            throw new IOException("Can't get class loader.");
        }
        // Ask for all resources for the path
        Enumeration<URL> resources = cld.getResources(folder);
        while (resources.hasMoreElements()) {
            URL res = resources.nextElement();
            String resProtocol = res.getProtocol();
            if (resProtocol.equalsIgnoreCase("jar")) {
                JarURLConnection conn = (JarURLConnection) res.openConnection();
                JarFile jar = conn.getJarFile();
                for (JarEntry e : Collections.list(jar.entries())) {
                    if (e.getName().startsWith(folder) && !e.getName().endsWith("/")) {
                        urls.add(new URL(
                                joinUrl(res.toString(), "/" + e.getName().substring(folder.length() + 1)))); // FIXME: fully qualified name
                    }
                }
            } else if (resProtocol.equalsIgnoreCase("vfszip") || resProtocol.equalsIgnoreCase("vfs")) { // JBoss 5+
                try {
                    Object content = res.getContent();
                    Method getChildren = content.getClass().getMethod("getChildren");
                    List<?> files = (List<?>) getChildren.invoke(res.getContent());
                    Method toUrl = null;
                    for (Object o : files) {
                        if (toUrl == null) {
                            toUrl = o.getClass().getMethod("toURL");
                        }
                        urls.add((URL) toUrl.invoke(o));
                    }
                } catch (Exception e) {
                    throw new IOException("Error while scanning " + res.toString(), e);
                }
            } else if (resProtocol.equalsIgnoreCase("file")) {
                directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8")));
            } else {
                throw new IOException("Unknown protocol for resource: " + res.toString());
            }
        }
    } catch (NullPointerException x) {
        throw new IOException(folder + " does not appear to be a valid folder (Null pointer exception)");
    } catch (UnsupportedEncodingException encex) {
        throw new IOException(folder + " does not appear to be a valid folder (Unsupported encoding)");
    }

    // For every directory identified capture all the .class files
    for (File directory : directories) {
        if (directory.exists()) {
            // Get the list of the files contained in the package
            String[] files = directory.list();
            for (String file : files) {
                urls.add(new URL("file:///" + joinPath(directory.getAbsolutePath(), file)));
            }
        } else {
            throw new IOException(
                    folder + " (" + directory.getPath() + ") does not appear to be a valid folder");
        }
    }
    URL[] urlsA = new URL[urls.size()];
    urls.toArray(urlsA);
    return urlsA;
}