aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/application/ContainerBuilder.java
blob: eb602737c8f74f8a6b72bfcc8abc38eaf0c0dc47 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright Yahoo. 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;

/**
 * <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> serverBindings(String setName) {
        BindingRepository<RequestHandler> ret = serverBindings.get(setName);
        if (ret == null) {
            ret = new BindingRepository<>();
            serverBindings.put(setName, ret);
        }
        return ret;
    }

    public Map<String, BindingSet<RequestHandler>> activateServerBindings() {
        Map<String, BindingSet<RequestHandler>> ret = new HashMap<>();
        for (Map.Entry<String, BindingRepository<RequestHandler>> entry : serverBindings.entrySet()) {
            ret.put(entry.getKey(), entry.getValue().activate());
        }
        return ImmutableMap.copyOf(ret);
    }

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

    public BindingRepository<RequestHandler> clientBindings(String setName) {
        BindingRepository<RequestHandler> ret = clientBindings.get(setName);
        if (ret == null) {
            ret = new BindingRepository<>();
            clientBindings.put(setName, ret);
        }
        return ret;
    }

    public Map<String, BindingSet<RequestHandler>> activateClientBindings() {
        Map<String, BindingSet<RequestHandler>> ret = new HashMap<>();
        for (Map.Entry<String, BindingRepository<RequestHandler>> entry : clientBindings.entrySet()) {
            ret.put(entry.getKey(), entry.getValue().activate());
        }
        return ImmutableMap.copyOf(ret);
    }

    @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;
    }

}