blob: 4f4544fa8f86cf640850d05f41f832da7583332d (
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
|
// 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.Key;
import com.yahoo.jdisc.AbstractResource;
import com.yahoo.jdisc.Container;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.application.BindingMatch;
import com.yahoo.jdisc.application.BindingSet;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.NullContent;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.handler.ResponseHandler;
import java.util.Objects;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
class ContainerSnapshot extends AbstractResource implements Container {
private final TimeoutManagerImpl timeoutMgr;
private final ActiveContainer container;
private final ResourceReference containerReference;
private final BindingSet<RequestHandler> serverBindings;
private final BindingSet<RequestHandler> clientBindings;
ContainerSnapshot(ActiveContainer container, BindingSet<RequestHandler> serverBindings,
BindingSet<RequestHandler> clientBindings)
{
this.timeoutMgr = container.timeoutManager();
this.container = container;
this.serverBindings = serverBindings;
this.clientBindings = clientBindings;
this.containerReference = container.refer();
}
@Override
public <T> T getInstance(Key<T> key) {
return container.guiceInjector().getInstance(key);
}
@Override
public <T> T getInstance(Class<T> type) {
return container.guiceInjector().getInstance(type);
}
@Override
public RequestHandler resolveHandler(Request request) {
BindingMatch<RequestHandler> match = request.isServerRequest() ? serverBindings.match(request.getUri())
: clientBindings.match(request.getUri());
if (match == null) {
return null;
}
request.setBindingMatch(match);
RequestHandler ret = new NullContentRequestHandler(match.target());
if (request.getTimeoutManager() == null) {
ret = timeoutMgr.manageHandler(ret);
}
return ret;
}
@Override
protected void destroy() {
containerReference.close();
}
@Override
public long currentTimeMillis() {
return timeoutMgr.timer().currentTimeMillis();
}
private static class NullContentRequestHandler implements RequestHandler {
final RequestHandler delegate;
NullContentRequestHandler(RequestHandler delegate) {
Objects.requireNonNull(delegate, "delegate");
this.delegate = delegate;
}
@Override
public ContentChannel handleRequest(Request request, ResponseHandler responseHandler) {
ContentChannel content = delegate.handleRequest(request, responseHandler);
if (content == null) {
content = NullContent.INSTANCE;
}
return content;
}
@Override
public void handleTimeout(Request request, ResponseHandler responseHandler) {
delegate.handleTimeout(request, responseHandler);
}
@Override
public ResourceReference refer() {
return delegate.refer();
}
@Override
public void release() {
delegate.release();
}
@Override
public String toString() {
return delegate.toString();
}
}
}
|