diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainer.java |
Publish
Diffstat (limited to 'jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainer.java')
-rw-r--r-- | jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainer.java | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainer.java b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainer.java new file mode 100644 index 00000000000..a296bd1e327 --- /dev/null +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainer.java @@ -0,0 +1,137 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jdisc.core; + +import com.google.inject.AbstractModule; +import com.google.inject.Injector; +import com.yahoo.jdisc.AbstractResource; +import com.yahoo.jdisc.SharedResource; +import com.yahoo.jdisc.application.BindingSet; +import com.yahoo.jdisc.application.BindingSetSelector; +import com.yahoo.jdisc.application.ContainerBuilder; +import com.yahoo.jdisc.application.ResourcePool; +import com.yahoo.jdisc.application.UriPattern; +import com.yahoo.jdisc.handler.RequestHandler; +import com.yahoo.jdisc.service.BindingSetNotFoundException; +import com.yahoo.jdisc.service.CurrentContainer; +import com.yahoo.jdisc.service.NoBindingSetSelectedException; +import com.yahoo.jdisc.service.ServerProvider; + +import java.net.URI; +import java.util.Map; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class ActiveContainer extends AbstractResource implements CurrentContainer { + + private final ContainerTermination termination; + private final Injector guiceInjector; + private final Iterable<ServerProvider> serverProviders; + private final ResourcePool resourceReferences = new ResourcePool(); + private final Map<String, BindingSet<RequestHandler>> serverBindings; + private final Map<String, BindingSet<RequestHandler>> clientBindings; + private final BindingSetSelector bindingSetSelector; + private final TimeoutManagerImpl timeoutMgr; + + public ActiveContainer(ContainerBuilder builder) { + serverProviders = builder.serverProviders().activate(); + for (SharedResource resource : serverProviders) { + resourceReferences.retain(resource); + } + serverBindings = builder.activateServerBindings(); + for (BindingSet<RequestHandler> set : serverBindings.values()) { + for (Map.Entry<UriPattern, RequestHandler> entry : set) { + resourceReferences.retain(entry.getValue()); + } + } + clientBindings = builder.activateClientBindings(); + for (BindingSet<RequestHandler> set : clientBindings.values()) { + for (Map.Entry<UriPattern, RequestHandler> entry : set) { + resourceReferences.retain(entry.getValue()); + } + } + bindingSetSelector = builder.getInstance(BindingSetSelector.class); + timeoutMgr = builder.getInstance(TimeoutManagerImpl.class); + timeoutMgr.start(); + builder.guiceModules().install(new AbstractModule() { + + @Override + protected void configure() { + bind(TimeoutManagerImpl.class).toInstance(timeoutMgr); + } + }); + guiceInjector = builder.guiceModules().activate(); + termination = new ContainerTermination(builder.appContext()); + } + + @Override + protected void destroy() { + resourceReferences.release(); + timeoutMgr.shutdown(); + termination.run(); + } + + @Override + protected void finalize() throws Throwable { + try { + if (retainCount() > 0) { + destroy(); + } + } finally { + super.finalize(); + } + } + + /** + * Make this instance retain a reference to the resource until it is destroyed. + */ + void retainReference(SharedResource resource) { + resourceReferences.retain(resource); + } + + public ContainerTermination shutdown() { + return termination; + } + + public Injector guiceInjector() { + return guiceInjector; + } + + public Iterable<ServerProvider> serverProviders() { + return serverProviders; + } + + public Map<String, BindingSet<RequestHandler>> serverBindings() { + return serverBindings; + } + + public BindingSet<RequestHandler> serverBindings(String setName) { + return serverBindings.get(setName); + } + + public Map<String, BindingSet<RequestHandler>> clientBindings() { + return clientBindings; + } + + public BindingSet<RequestHandler> clientBindings(String setName) { + return clientBindings.get(setName); + } + + TimeoutManagerImpl timeoutManager() { + return timeoutMgr; + } + + @Override + public ContainerSnapshot newReference(URI uri) { + String name = bindingSetSelector.select(uri); + if (name == null) { + throw new NoBindingSetSelectedException(uri); + } + BindingSet<RequestHandler> serverBindings = serverBindings(name); + BindingSet<RequestHandler> clientBindings = clientBindings(name); + if (serverBindings == null || clientBindings == null) { + throw new BindingSetNotFoundException(name); + } + return new ContainerSnapshot(this, serverBindings, clientBindings); + } +} |