aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/application/ContainerBuilder.java
blob: 6550d9b538672453b60ee1ee5ff2b3c6cb79301f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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.Collections;
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;

/**
 * <p>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}.</p>
 *
 * @author Simon Thoresen Hult
 */
public class ContainerBuilder {

    private final GuiceRepository guiceModules = new GuiceRepository();
    private final ServerRepository serverProviders = new ServerRepository(guiceModules);
    private final Map<String, BindingRepository<RequestHandler>> serverBindings = new HashMap<>();
    private final Map<String, BindingRepository<RequestHandler>> clientBindings = new HashMap<>();
    private Object appContext = null;

    public ContainerBuilder(Iterable<Module> 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> T getInstance(Key<T> key) {
        return guiceModules.getInstance(key);
    }

    public <T> T getInstance(Class<T> type) {
        return guiceModules.getInstance(type);
    }

    public ServerRepository serverProviders() {
        return serverProviders;
    }

    public BindingRepository<RequestHandler> serverBindings() {
        return serverBindings.get(BindingSet.DEFAULT);
    }

    public BindingRepository<RequestHandler> clientBindings() {
        return clientBindings.get(BindingSet.DEFAULT);
    }

    public BindingRepository<RequestHandler> serverBindings(String setName) {
        return serverBindings.computeIfAbsent(setName, __ -> new BindingRepository<>());
    }

    public BindingRepository<RequestHandler> clientBindings(String setName) {
        return clientBindings.computeIfAbsent(setName, __ -> new BindingRepository<>());
    }

    public Map<String, BindingSet<RequestHandler>> activateServerBindings() {
        return serverBindings.entrySet().stream().collect(toUnmodifiableMap(entry -> entry.getKey(),
                                                                            entry -> entry.getValue().activate()));
    }

    public Map<String, BindingSet<RequestHandler>> activateClientBindings() {
        return clientBindings.entrySet().stream().collect(toUnmodifiableMap(entry -> entry.getKey(),
                                                                            entry -> entry.getValue().activate()));
    }

    @SuppressWarnings({ "unchecked" })
    public static <T> Class<T> safeClassCast(Class<T> baseClass, Class<?> someClass) {
        if (!baseClass.isAssignableFrom(someClass)) {
            throw new IllegalArgumentException("Expected " + baseClass.getName() + ", got " +
                                               someClass.getName() + ".");
        }
        return (Class<T>)someClass;
    }

    public static List<String> safeStringSplit(Object obj, String delim) {
        if (!(obj instanceof String)) {
            return Collections.emptyList();
        }
        List<String> lst = new LinkedList<>();
        for (String str : ((String)obj).split(delim)) {
            str = str.trim();
            if (!str.isEmpty()) {
                lst.add(str);
            }
        }
        return lst;
    }

}