blob: cfd5f753c4f12246ebe3ad2331fc5f40aff8bad7 (
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.standalone;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Module;
import com.google.inject.util.Modules;
import com.yahoo.jdisc.application.ContainerActivator;
import com.yahoo.jdisc.application.ContainerBuilder;
import com.yahoo.jdisc.application.DeactivatedContainer;
import com.yahoo.jdisc.application.OsgiFramework;
import com.yahoo.jdisc.http.ConnectorConfig;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.container.Container;
import com.yahoo.vespa.model.container.http.ConnectorFactory;
import com.yahoo.vespa.model.container.http.Http;
import com.yahoo.vespa.model.container.http.JettyHttpServer;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Einar M R Rosenvinge
*/
public class StandaloneContainerActivator implements BundleActivator {
@Override
public void start(BundleContext bundleContext) throws Exception {
for (ConnectorConfig config: getConnectorConfigs(getContainer())) {
ServerSocketChannel socketChannel = bindChannel(config);
registerChannels(bundleContext, config.listenPort(), socketChannel);
}
}
static void registerChannels(BundleContext bundleContext, int listenPort, ServerSocketChannel boundChannel) {
Hashtable<String, Integer> properties = new Hashtable<>();
properties.put("port", listenPort);
bundleContext.registerService(ServerSocketChannel.class, boundChannel, properties);
}
static ServerSocketChannel bindChannel(ConnectorConfig channelInfo) throws IOException {
ServerSocketChannel serverChannel = ServerSocketChannel.open();
InetSocketAddress bindAddress = new InetSocketAddress(channelInfo.listenPort());
serverChannel.socket().bind(bindAddress, channelInfo.acceptQueueSize());
return serverChannel;
}
@Override
public void stop(BundleContext bundleContext) throws Exception { }
static Container getContainer(Module... modules) {
Module activatorModule = new ActivatorModule();
List<Module> allModules = new ArrayList<>();
allModules.addAll(Arrays.asList(modules));
allModules.add(activatorModule);
StandaloneContainerApplication app = new StandaloneContainerApplication(Guice.createInjector(Modules.combine(allModules)));
return app.container();
}
static List<ConnectorConfig> getConnectorConfigs(Container container) {
return getConnectorConfigs(container.getHttpServer());
}
private static List<ConnectorConfig> getConnectorConfigs(JettyHttpServer jettyHttpServer) {
if (jettyHttpServer == null)
return Collections.emptyList();
return jettyHttpServer.getConnectorFactories().stream().
map(StandaloneContainerActivator::getConnectorConfig).
collect(Collectors.toList());
}
private static ConnectorConfig getConnectorConfig(ConnectorFactory connectorFactory) {
return VespaModel.getConfig(ConnectorConfig.class, connectorFactory);
}
private static class ActivatorModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(OsgiFramework.class).toInstance(new DummyOsgiFramework());
binder.bind(ContainerActivator.class).toInstance(new DummyActivatorForStandaloneContainerApp());
}
}
private static class DummyActivatorForStandaloneContainerApp implements ContainerActivator {
@Override
public ContainerBuilder newContainerBuilder() {
return new ContainerBuilder(new ArrayList<Module>());
}
@Override
public DeactivatedContainer activateContainer(ContainerBuilder builder) {
return new DeactivatedContainer() {
@Override
public Object appContext() {
return new Object();
}
@Override
public void notifyTermination(Runnable task) {
}
};
}
}
public static class DummyOsgiFramework implements OsgiFramework {
@Override
public List<Bundle> installBundle(String bundleLocation) {
throw new UnsupportedOperationException();
}
@Override
public void startBundles(List<Bundle> bundles, boolean privileged) {
throw new UnsupportedOperationException();
}
@Override
public void refreshPackages() {
throw new UnsupportedOperationException();
}
@Override
public BundleContext bundleContext() {
return null;
}
@Override
public List<Bundle> bundles() {
return Collections.emptyList();
}
@Override
public List<Bundle> getBundles(Bundle requestingBundle) {
return Collections.emptyList();
}
@Override
public void allowDuplicateBundles(Collection<Bundle> bundles) { }
@Override
public void start() {
}
@Override
public void stop() {
}
}
}
|