All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.jdisc.application.ContainerBuilder Maven / Gradle / Ivy

There is a newer version: 8.410.10
Show newest version
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;

import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.Module;
import com.yahoo.jdisc.Container;
import com.yahoo.jdisc.handler.RequestHandler;

import java.util.*;
import java.util.concurrent.ThreadFactory;

/**
 * 

This is the inactive, mutable {@link Container}. Because it requires references to the application internals, it * should always be injected by guice or created by calling {@link ContainerActivator#newContainerBuilder()}. Once the * builder has been configured, it is activated by calling {@link * ContainerActivator#activateContainer(ContainerBuilder)}. You may use the {@link #setAppContext(Object)} method to * attach an arbitrary object to a Container, which will be available in the corresponding {@link * DeactivatedContainer}.

* * @author Simon Thoresen Hult */ public class ContainerBuilder { private final GuiceRepository guiceModules = new GuiceRepository(); private final ServerRepository serverProviders = new ServerRepository(guiceModules); private final Map> serverBindings = new HashMap<>(); private final Map> clientBindings = new HashMap<>(); private Object appContext = null; public ContainerBuilder(Iterable guiceModules) { this.guiceModules.installAll(guiceModules); this.guiceModules.install(new AbstractModule() { @Override public void configure() { bind(ContainerBuilder.class).toInstance(ContainerBuilder.this); } }); this.serverBindings.put(BindingSet.DEFAULT, new BindingRepository()); this.clientBindings.put(BindingSet.DEFAULT, new BindingRepository()); } public void setAppContext(Object ctx) { appContext = ctx; } public Object appContext() { return appContext; } public GuiceRepository guiceModules() { return guiceModules; } public T getInstance(Key key) { return guiceModules.getInstance(key); } public T getInstance(Class type) { return guiceModules.getInstance(type); } public ServerRepository serverProviders() { return serverProviders; } public BindingRepository serverBindings() { return serverBindings.get(BindingSet.DEFAULT); } public BindingRepository serverBindings(String setName) { BindingRepository ret = serverBindings.get(setName); if (ret == null) { ret = new BindingRepository<>(); serverBindings.put(setName, ret); } return ret; } public Map> activateServerBindings() { Map> ret = new HashMap<>(); for (Map.Entry> entry : serverBindings.entrySet()) { ret.put(entry.getKey(), entry.getValue().activate()); } return ImmutableMap.copyOf(ret); } public BindingRepository clientBindings() { return clientBindings.get(BindingSet.DEFAULT); } public BindingRepository clientBindings(String setName) { BindingRepository ret = clientBindings.get(setName); if (ret == null) { ret = new BindingRepository<>(); clientBindings.put(setName, ret); } return ret; } public Map> activateClientBindings() { Map> ret = new HashMap<>(); for (Map.Entry> entry : clientBindings.entrySet()) { ret.put(entry.getKey(), entry.getValue().activate()); } return ImmutableMap.copyOf(ret); } @SuppressWarnings({ "unchecked" }) public static Class safeClassCast(Class baseClass, Class someClass) { if (!baseClass.isAssignableFrom(someClass)) { throw new IllegalArgumentException("Expected " + baseClass.getName() + ", got " + someClass.getName() + "."); } return (Class)someClass; } public static List safeStringSplit(Object obj, String delim) { if (!(obj instanceof String)) { return Collections.emptyList(); } List lst = new LinkedList<>(); for (String str : ((String)obj).split(delim)) { str = str.trim(); if (!str.isEmpty()) { lst.add(str); } } return lst; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy