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.441.21
Show newest version
// Copyright Vespa.ai. 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.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toUnmodifiableMap;

/**
 * 

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 clientBindings() { return clientBindings.get(BindingSet.DEFAULT); } public BindingRepository serverBindings(String setName) { return serverBindings.computeIfAbsent(setName, __ -> new BindingRepository<>()); } public BindingRepository clientBindings(String setName) { return clientBindings.computeIfAbsent(setName, __ -> new BindingRepository<>()); } public Map> activateServerBindings() { return serverBindings.entrySet().stream().collect(toUnmodifiableMap(entry -> entry.getKey(), entry -> entry.getValue().activate())); } public Map> activateClientBindings() { return clientBindings.entrySet().stream().collect(toUnmodifiableMap(entry -> entry.getKey(), entry -> entry.getValue().activate())); } @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 List.of(); } 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