Search in sources :

Example 1 with HashSet

use of java.util.HashSet in project jetty.project by eclipse.

the class BalancerServlet method getBalancerNames.

private Set<String> getBalancerNames() throws ServletException {
    Set<String> names = new HashSet<>();
    for (String initParameterName : Collections.list(getServletConfig().getInitParameterNames())) {
        if (!initParameterName.startsWith(BALANCER_MEMBER_PREFIX))
            continue;
        int endOfNameIndex = initParameterName.lastIndexOf(".");
        if (endOfNameIndex <= BALANCER_MEMBER_PREFIX.length())
            throw new UnavailableException(initParameterName + " parameter does not provide a balancer member name");
        names.add(initParameterName.substring(BALANCER_MEMBER_PREFIX.length(), endOfNameIndex));
    }
    return names;
}
Also used : UnavailableException(javax.servlet.UnavailableException) HashSet(java.util.HashSet)

Example 2 with HashSet

use of java.util.HashSet in project jetty.project by eclipse.

the class JarResourceTest method testJarFile.

@Test
public void testJarFile() throws Exception {
    String s = "jar:" + testResURI + "TestData/test.zip!/subdir/";
    Resource r = Resource.newResource(s);
    Set<String> entries = new HashSet<>(Arrays.asList(r.list()));
    assertEquals(3, entries.size());
    assertTrue(entries.contains("alphabet"));
    assertTrue(entries.contains("numbers"));
    assertTrue(entries.contains("subsubdir/"));
    File extract = File.createTempFile("extract", null);
    if (extract.exists())
        extract.delete();
    extract.mkdir();
    extract.deleteOnExit();
    r.copyTo(extract);
    Resource e = Resource.newResource(extract.getAbsolutePath());
    entries = new HashSet<>(Arrays.asList(e.list()));
    assertEquals(3, entries.size());
    assertTrue(entries.contains("alphabet"));
    assertTrue(entries.contains("numbers"));
    assertTrue(entries.contains("subsubdir/"));
    IO.delete(extract);
    s = "jar:" + testResURI + "TestData/test.zip!/subdir/subsubdir/";
    r = Resource.newResource(s);
    entries = new HashSet<>(Arrays.asList(r.list()));
    assertEquals(2, entries.size());
    assertTrue(entries.contains("alphabet"));
    assertTrue(entries.contains("numbers"));
    extract = File.createTempFile("extract", null);
    if (extract.exists())
        extract.delete();
    extract.mkdir();
    extract.deleteOnExit();
    r.copyTo(extract);
    e = Resource.newResource(extract.getAbsolutePath());
    entries = new HashSet<>(Arrays.asList(e.list()));
    assertEquals(2, entries.size());
    assertTrue(entries.contains("alphabet"));
    assertTrue(entries.contains("numbers"));
    IO.delete(extract);
}
Also used : File(java.io.File) ZipFile(java.util.zip.ZipFile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with HashSet

use of java.util.HashSet in project jetty.project by eclipse.

the class MetaInfConfiguration method scanForResources.

/**
     * Scan for META-INF/resources dir in the given jar.
     * 
     * @param context the context for the scan
     * @param target the target resource to scan for
     * @param cache the resource cache
     * @throws Exception if unable to scan for resources
     */
public void scanForResources(WebAppContext context, Resource target, ConcurrentHashMap<Resource, Resource> cache) throws Exception {
    Resource resourcesDir = null;
    if (cache != null && cache.containsKey(target)) {
        resourcesDir = cache.get(target);
        if (resourcesDir == EmptyResource.INSTANCE) {
            if (LOG.isDebugEnabled())
                LOG.debug(target + " cached as containing no META-INF/resources");
            return;
        } else if (LOG.isDebugEnabled())
            LOG.debug(target + " META-INF/resources found in cache ");
    } else {
        //not using caches or not in the cache so check for the resources dir
        if (LOG.isDebugEnabled())
            LOG.debug(target + " META-INF/resources checked");
        if (target.isDirectory()) {
            //TODO think  how to handle an unpacked jar file (eg for osgi)
            resourcesDir = target.addPath("/META-INF/resources");
        } else {
            //Resource represents a packed jar
            URI uri = target.getURI();
            resourcesDir = Resource.newResource(uriJarPrefix(uri, "!/META-INF/resources"));
        }
        if (!resourcesDir.exists() || !resourcesDir.isDirectory()) {
            resourcesDir.close();
            resourcesDir = EmptyResource.INSTANCE;
        }
        if (cache != null) {
            Resource old = cache.putIfAbsent(target, resourcesDir);
            if (old != null)
                resourcesDir = old;
            else if (LOG.isDebugEnabled())
                LOG.debug(target + " META-INF/resources cache updated");
        }
        if (resourcesDir == EmptyResource.INSTANCE) {
            return;
        }
    }
    //add it to the meta inf resources for this context
    Set<Resource> dirs = (Set<Resource>) context.getAttribute(METAINF_RESOURCES);
    if (dirs == null) {
        dirs = new HashSet<Resource>();
        context.setAttribute(METAINF_RESOURCES, dirs);
    }
    if (LOG.isDebugEnabled())
        LOG.debug(resourcesDir + " added to context");
    dirs.add(resourcesDir);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Resource(org.eclipse.jetty.util.resource.Resource) EmptyResource(org.eclipse.jetty.util.resource.EmptyResource) URI(java.net.URI)

Example 4 with HashSet

use of java.util.HashSet in project jetty.project by eclipse.

the class MetaInfConfiguration method getTlds.

/**
     * Find all .tld files in all subdirs of the given dir.
     * 
     * @param dir the directory to scan
     * @return the list of tlds found
     * @throws IOException if unable to scan the directory
     */
public Collection<URL> getTlds(File dir) throws IOException {
    if (dir == null || !dir.isDirectory())
        return Collections.emptySet();
    HashSet<URL> tlds = new HashSet<URL>();
    File[] files = dir.listFiles();
    if (files != null) {
        for (File f : files) {
            if (f.isDirectory())
                tlds.addAll(getTlds(f));
            else {
                String name = f.getCanonicalPath();
                if (name.contains("META-INF") && name.endsWith(".tld"))
                    tlds.add(f.toURI().toURL());
            }
        }
    }
    return tlds;
}
Also used : JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) HashSet(java.util.HashSet)

Example 5 with HashSet

use of java.util.HashSet in project jetty.project by eclipse.

the class MetaInfConfiguration method getTlds.

/**
     * Find all .tld files in the given jar.
     * 
     * @param uri the uri to jar file
     * @return the collection of tlds as url references  
     * @throws IOException if unable to scan the jar file
     */
public Collection<URL> getTlds(URI uri) throws IOException {
    HashSet<URL> tlds = new HashSet<URL>();
    String jarUri = uriJarPrefix(uri, "!/");
    URL url = new URL(jarUri);
    JarURLConnection jarConn = (JarURLConnection) url.openConnection();
    jarConn.setUseCaches(Resource.getDefaultUseCaches());
    JarFile jarFile = jarConn.getJarFile();
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry e = entries.nextElement();
        String name = e.getName();
        if (name.startsWith("META-INF") && name.endsWith(".tld")) {
            tlds.add(new URL(jarUri + name));
        }
    }
    if (!Resource.getDefaultUseCaches())
        jarFile.close();
    return tlds;
}
Also used : JarURLConnection(java.net.JarURLConnection) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)31093 ArrayList (java.util.ArrayList)6530 Test (org.junit.Test)5432 Set (java.util.Set)5384 HashMap (java.util.HashMap)5259 Map (java.util.Map)3115 List (java.util.List)2832 IOException (java.io.IOException)2238 File (java.io.File)1664 Iterator (java.util.Iterator)1455 LinkedHashSet (java.util.LinkedHashSet)1326 Collection (java.util.Collection)888 URI (java.net.URI)809 Test (org.testng.annotations.Test)784 LinkedList (java.util.LinkedList)724 Collectors (java.util.stream.Collectors)671 LinkedHashMap (java.util.LinkedHashMap)636 Date (java.util.Date)577 Test (org.junit.jupiter.api.Test)576 TreeSet (java.util.TreeSet)563