Search in sources :

Example 21 with HashMap

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

the class MetaInfConfiguration method scanForFragment.

/**
     * Scan for META-INF/web-fragment.xml file in the given jar.
     * 
     * @param context the context for the scan
     * @param jar the jar resource to scan for fragements in
     * @param cache the resource cache
     * @throws Exception if unable to scan for fragments
     */
public void scanForFragment(WebAppContext context, Resource jar, ConcurrentHashMap<Resource, Resource> cache) throws Exception {
    Resource webFrag = null;
    if (cache != null && cache.containsKey(jar)) {
        webFrag = cache.get(jar);
        if (webFrag == EmptyResource.INSTANCE) {
            if (LOG.isDebugEnabled())
                LOG.debug(jar + " cached as containing no META-INF/web-fragment.xml");
            return;
        } else if (LOG.isDebugEnabled())
            LOG.debug(jar + " META-INF/web-fragment.xml found in cache ");
    } else {
        //not using caches or not in the cache so check for the web-fragment.xml
        if (LOG.isDebugEnabled())
            LOG.debug(jar + " META-INF/web-fragment.xml checked");
        if (jar.isDirectory()) {
            //TODO   ????
            webFrag = jar.addPath("/META-INF/web-fragment.xml");
        } else {
            URI uri = jar.getURI();
            webFrag = Resource.newResource(uriJarPrefix(uri, "!/META-INF/web-fragment.xml"));
        }
        if (!webFrag.exists() || webFrag.isDirectory()) {
            webFrag.close();
            webFrag = EmptyResource.INSTANCE;
        }
        if (cache != null) {
            //web-fragment.xml doesn't exist: put token in cache to signal we've seen the jar               
            Resource old = cache.putIfAbsent(jar, webFrag);
            if (old != null)
                webFrag = old;
            else if (LOG.isDebugEnabled())
                LOG.debug(jar + " META-INF/web-fragment.xml cache updated");
        }
        if (webFrag == EmptyResource.INSTANCE)
            return;
    }
    Map<Resource, Resource> fragments = (Map<Resource, Resource>) context.getAttribute(METAINF_FRAGMENTS);
    if (fragments == null) {
        fragments = new HashMap<Resource, Resource>();
        context.setAttribute(METAINF_FRAGMENTS, fragments);
    }
    fragments.put(jar, webFrag);
    if (LOG.isDebugEnabled())
        LOG.debug(webFrag + " added to context");
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) EmptyResource(org.eclipse.jetty.util.resource.EmptyResource) URI(java.net.URI) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with HashMap

use of java.util.HashMap in project vert.x by eclipse.

the class HostnameResolutionTest method testMultipleSearchDomain.

@Test
public void testMultipleSearchDomain() throws Exception {
    Map<String, String> records = new HashMap<>();
    records.put("host1.foo.com", "127.0.0.1");
    records.put("host2.bar.com", "127.0.0.2");
    records.put("host3.bar.com", "127.0.0.3");
    records.put("host3.foo.com", "127.0.0.4");
    dnsServer.stop();
    dnsServer = FakeDNSServer.testResolveA(records);
    dnsServer.start();
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false).addSearchDomain("foo.com").addSearchDomain("bar.com")));
    // "host1" resolves via the "foo.com" search path
    CountDownLatch latch1 = new CountDownLatch(1);
    vertx.resolveAddress("host1", onSuccess(resolved -> {
        assertEquals("127.0.0.1", resolved.getHostAddress());
        latch1.countDown();
    }));
    awaitLatch(latch1);
    // "host2" resolves via the "bar.com" search path
    CountDownLatch latch2 = new CountDownLatch(1);
    vertx.resolveAddress("host2", onSuccess(resolved -> {
        assertEquals("127.0.0.2", resolved.getHostAddress());
        latch2.countDown();
    }));
    awaitLatch(latch2);
    // "host3" resolves via the the "foo.com" search path as it is the first one
    CountDownLatch latch3 = new CountDownLatch(1);
    vertx.resolveAddress("host3", onSuccess(resolved -> {
        assertEquals("127.0.0.4", resolved.getHostAddress());
        latch3.countDown();
    }));
    awaitLatch(latch3);
    // "host4" does not resolve
    vertx.resolveAddress("host4", onFailure(cause -> {
        assertTrue(cause instanceof UnknownHostException);
        testComplete();
    }));
    await();
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) VertxInternal(io.vertx.core.impl.VertxInternal) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 23 with HashMap

use of java.util.HashMap in project vert.x by eclipse.

the class HostnameResolutionTest method testSearchDomainWithNdots0.

@Test
public void testSearchDomainWithNdots0() throws Exception {
    Map<String, String> records = new HashMap<>();
    records.put("host1", "127.0.0.2");
    records.put("host1.foo.com", "127.0.0.3");
    dnsServer.stop();
    dnsServer = FakeDNSServer.testResolveA(records);
    dnsServer.start();
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false).addSearchDomain("foo.com").setNdots(0)));
    // "host1" resolves directly as ndots = 0
    CountDownLatch latch1 = new CountDownLatch(1);
    vertx.resolveAddress("host1", onSuccess(resolved -> {
        assertEquals("127.0.0.2", resolved.getHostAddress());
        latch1.countDown();
    }));
    awaitLatch(latch1);
    // "host1.foo.com" resolves to host1.foo.com
    CountDownLatch latch2 = new CountDownLatch(1);
    vertx.resolveAddress("host1.foo.com", onSuccess(resolved -> {
        assertEquals("127.0.0.3", resolved.getHostAddress());
        latch2.countDown();
    }));
    awaitLatch(latch2);
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) VertxInternal(io.vertx.core.impl.VertxInternal) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 24 with HashMap

use of java.util.HashMap in project elasticsearch by elastic.

the class BootstrapForTesting method getPluginPermissions.

/**
     * we don't know which codesources belong to which plugin, so just remove the permission from key codebases
     * like core, test-framework, etc. this way tests fail if accesscontroller blocks are missing.
     */
@SuppressForbidden(reason = "accesses fully qualified URLs to configure security")
static Map<String, Policy> getPluginPermissions() throws Exception {
    List<URL> pluginPolicies = Collections.list(BootstrapForTesting.class.getClassLoader().getResources(PluginInfo.ES_PLUGIN_POLICY));
    if (pluginPolicies.isEmpty()) {
        return Collections.emptyMap();
    }
    // compute classpath minus obvious places, all other jars will get the permission.
    Set<URL> codebases = new HashSet<>(Arrays.asList(parseClassPathWithSymlinks()));
    Set<URL> excluded = new HashSet<>(Arrays.asList(// es core
    Bootstrap.class.getProtectionDomain().getCodeSource().getLocation(), // es test framework
    BootstrapForTesting.class.getProtectionDomain().getCodeSource().getLocation(), // lucene test framework
    LuceneTestCase.class.getProtectionDomain().getCodeSource().getLocation(), // randomized runner
    RandomizedRunner.class.getProtectionDomain().getCodeSource().getLocation(), // junit library
    Assert.class.getProtectionDomain().getCodeSource().getLocation()));
    codebases.removeAll(excluded);
    // parse each policy file, with codebase substitution from the classpath
    final List<Policy> policies = new ArrayList<>();
    for (URL policyFile : pluginPolicies) {
        policies.add(Security.readPolicy(policyFile, codebases.toArray(new URL[codebases.size()])));
    }
    // consult each policy file for those codebases
    Map<String, Policy> map = new HashMap<>();
    for (URL url : codebases) {
        map.put(url.getFile(), new Policy() {

            @Override
            public boolean implies(ProtectionDomain domain, Permission permission) {
                // implements union
                for (Policy p : policies) {
                    if (p.implies(domain, permission)) {
                        return true;
                    }
                }
                return false;
            }
        });
    }
    return Collections.unmodifiableMap(map);
}
Also used : Policy(java.security.Policy) ProtectionDomain(java.security.ProtectionDomain) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(java.net.URL) FilePermission(java.io.FilePermission) SocketPermission(java.net.SocketPermission) Permission(java.security.Permission) HashSet(java.util.HashSet) SuppressForbidden(org.elasticsearch.common.SuppressForbidden)

Example 25 with HashMap

use of java.util.HashMap in project elasticsearch by elastic.

the class ModuleTestCase method assertMapInstanceBinding.

/**
     * Configures the module, and ensures a map exists between the "keyType" and "valueType",
     * and that all of the "expected" values are bound.
     */
@SuppressWarnings("unchecked")
public <K, V> void assertMapInstanceBinding(Module module, Class<K> keyType, Class<V> valueType, Map<K, V> expected) throws Exception {
    // this method is insane because java type erasure makes it incredibly difficult...
    Map<K, Key> keys = new HashMap<>();
    Map<Key, V> values = new HashMap<>();
    List<Element> elements = Elements.getElements(module);
    for (Element element : elements) {
        if (element instanceof InstanceBinding) {
            InstanceBinding binding = (InstanceBinding) element;
            if (binding.getKey().getRawType().equals(valueType)) {
                values.put(binding.getKey(), (V) binding.getInstance());
            } else if (binding.getInstance() instanceof Map.Entry) {
                Map.Entry entry = (Map.Entry) binding.getInstance();
                Object key = entry.getKey();
                Object providerValue = entry.getValue();
                if (key.getClass().equals(keyType) && providerValue instanceof ProviderLookup.ProviderImpl) {
                    ProviderLookup.ProviderImpl provider = (ProviderLookup.ProviderImpl) providerValue;
                    keys.put((K) key, provider.getKey());
                }
            }
        }
    }
    for (Map.Entry<K, V> entry : expected.entrySet()) {
        Key valueKey = keys.get(entry.getKey());
        assertNotNull("Could not find binding for key [" + entry.getKey() + "], found these keys:\n" + keys.keySet(), valueKey);
        V value = values.get(valueKey);
        assertNotNull("Could not find value for instance key [" + valueKey + "], found these bindings:\n" + elements);
        assertEquals(entry.getValue(), value);
    }
}
Also used : HashMap(java.util.HashMap) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) InstanceBinding(org.elasticsearch.common.inject.spi.InstanceBinding) ProviderLookup(org.elasticsearch.common.inject.spi.ProviderLookup) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)69230 Test (org.junit.Test)16584 ArrayList (java.util.ArrayList)16269 Map (java.util.Map)14814 List (java.util.List)8655 IOException (java.io.IOException)5791 HashSet (java.util.HashSet)5215 LinkedHashMap (java.util.LinkedHashMap)3834 File (java.io.File)3597 Set (java.util.Set)3468 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1946 Iterator (java.util.Iterator)1890 Date (java.util.Date)1815 Test (org.junit.jupiter.api.Test)1788 Test (org.testng.annotations.Test)1747 LinkedList (java.util.LinkedList)1641 URI (java.net.URI)1558 Collection (java.util.Collection)1173 Properties (java.util.Properties)1072 InputStream (java.io.InputStream)1067