|
24 | 24 | */ |
25 | 25 | public class RunnerAwareServerFactory implements ServerFactory { |
26 | 26 |
|
27 | | - private static final Logger LOGGER = LoggerFactory.getLogger(RunnerAwareServerFactory.class); |
28 | | - |
29 | | - private static final RunnerAwareServerFactory instance = new RunnerAwareServerFactory(); |
30 | | - |
31 | | - private ServerFactory underlying; |
32 | | - private GlobalConfig config; |
33 | | - private Dispatcher dispatcher; |
34 | | - private BenchRepo benchRepo; |
35 | | - private final Serializer serializer; |
36 | | - |
37 | | - private RunnerAwareServerFactory() { |
38 | | - this.serializer = new Serializer(); |
39 | | - } |
40 | | - |
41 | | - @Override |
42 | | - public Server build(Environment environment) { |
43 | | - ensureIsInitialized(); |
44 | | - |
45 | | - Server server = underlying.build(environment); |
46 | | - |
47 | | - HttpConnectorFactory connectorFactory = new HttpConnectorFactory(); |
48 | | - connectorFactory.setPort(config.getRunnerPort()); |
49 | | - Connector connector = connectorFactory |
50 | | - .build(server, environment.metrics(), "Runner", new ExecutorThreadPool(32)); |
51 | | - server.addConnector( |
52 | | - connector |
53 | | - ); |
54 | | - Map<Connector, Handler> handlerMap = new HashMap<>(); |
55 | | - |
56 | | - for (Connector serverConnector : server.getConnectors()) { |
57 | | - handlerMap.put(serverConnector, server.getHandler()); |
58 | | - } |
59 | | - |
60 | | - MutableServletContextHandler handler = new MutableServletContextHandler(); |
61 | | - handler.getServletContext().addServlet( |
62 | | - "Runner Websocket servlet", |
63 | | - new ServerMasterWebsocketServlet(dispatcher, serializer, config.getRunnerToken(), benchRepo) |
64 | | - ) |
65 | | - .addMapping("/runner-connector"); |
66 | | - handlerMap.put(connector, handler); |
67 | | - |
68 | | - server.setHandler(new RoutingHandler(handlerMap)); |
69 | | - |
70 | | - LOGGER.debug("Registered the websocket servlet"); |
71 | | - |
72 | | - return server; |
73 | | - } |
74 | | - |
75 | | - @Override |
76 | | - public void configure(Environment environment) { |
77 | | - underlying.configure(environment); |
78 | | - } |
79 | | - |
80 | | - |
81 | | - /** |
82 | | - * Sets the config to use. |
83 | | - * |
84 | | - * @param config the config to use |
85 | | - * @throws IllegalStateException if a config has already been set |
86 | | - */ |
87 | | - public void setConfig(GlobalConfig config) { |
88 | | - if (this.config != null) { |
89 | | - throw new IllegalStateException("I already have a config!"); |
90 | | - } |
91 | | - this.config = config; |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * Sets the server factory to delegate to. |
96 | | - * |
97 | | - * @param underlying the underlying server factory |
98 | | - * @throws IllegalStateException if a server factory has already been set |
99 | | - */ |
100 | | - public void setServerFactory(ServerFactory underlying) { |
101 | | - if (this.underlying != null) { |
102 | | - throw new IllegalStateException("I already have a server factory!"); |
103 | | - } |
104 | | - |
105 | | - this.underlying = underlying; |
106 | | - } |
107 | | - |
108 | | - /** |
109 | | - * Sets the dispatcher to use. |
110 | | - * |
111 | | - * @param dispatcher the dispatcher |
112 | | - * @throws IllegalStateException if a dispatcher has already been set |
113 | | - */ |
114 | | - public void setDispatcher(Dispatcher dispatcher) { |
115 | | - if (this.dispatcher != null) { |
116 | | - throw new IllegalStateException("I already have a dispatcher!"); |
117 | | - } |
118 | | - this.dispatcher = dispatcher; |
119 | | - } |
120 | | - |
121 | | - /** |
122 | | - * Sets the bench reo to use. |
123 | | - * |
124 | | - * @param benchRepo the bench repo |
125 | | - * @throws IllegalStateException if a bench repo has already been set |
126 | | - */ |
127 | | - public void setBenchRepo(BenchRepo benchRepo) { |
128 | | - if (this.benchRepo != null) { |
129 | | - throw new IllegalStateException("I already have a bench repo!"); |
130 | | - } |
131 | | - this.benchRepo = benchRepo; |
132 | | - } |
133 | | - |
134 | | - /** |
135 | | - * Returns whether the factory lacks an underlying server factory. |
136 | | - * |
137 | | - * @return true if no underlying server factory is available |
138 | | - */ |
139 | | - public boolean lacksFactory() { |
140 | | - return underlying == null; |
141 | | - } |
142 | | - |
143 | | - private void ensureIsInitialized() { |
144 | | - if (config == null) { |
145 | | - throw new IllegalStateException("Config not initialized"); |
146 | | - } |
147 | | - if (underlying == null) { |
148 | | - throw new IllegalStateException("Server factory not initialized"); |
149 | | - } |
150 | | - if (dispatcher == null) { |
151 | | - throw new IllegalStateException("Dispatcher not initialized"); |
152 | | - } |
153 | | - } |
154 | | - |
155 | | - |
156 | | - /** |
157 | | - * Returns the instance of the {@link RunnerAwareServerFactory}. |
158 | | - * |
159 | | - * @return the instance |
160 | | - */ |
161 | | - public static RunnerAwareServerFactory getInstance() { |
162 | | - return instance; |
163 | | - } |
| 27 | + private static final Logger LOGGER = LoggerFactory.getLogger(RunnerAwareServerFactory.class); |
| 28 | + |
| 29 | + private static final RunnerAwareServerFactory instance = new RunnerAwareServerFactory(); |
| 30 | + |
| 31 | + private ServerFactory underlying; |
| 32 | + private GlobalConfig config; |
| 33 | + private Dispatcher dispatcher; |
| 34 | + private BenchRepo benchRepo; |
| 35 | + private final Serializer serializer; |
| 36 | + |
| 37 | + private RunnerAwareServerFactory() { |
| 38 | + this.serializer = new Serializer(); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public Server build(Environment environment) { |
| 43 | + ensureIsInitialized(); |
| 44 | + |
| 45 | + Server server = underlying.build(environment); |
| 46 | + |
| 47 | + HttpConnectorFactory connectorFactory = new HttpConnectorFactory(); |
| 48 | + connectorFactory.setPort(config.getRunnerPort()); |
| 49 | + Connector connector = |
| 50 | + connectorFactory.build(server, environment.metrics(), "Runner", new ExecutorThreadPool(32)); |
| 51 | + server.addConnector(connector); |
| 52 | + Map<Connector, Handler> handlerMap = new HashMap<>(); |
| 53 | + |
| 54 | + for (Connector serverConnector : server.getConnectors()) { |
| 55 | + handlerMap.put(serverConnector, server.getHandler()); |
| 56 | + } |
| 57 | + |
| 58 | + MutableServletContextHandler handler = new MutableServletContextHandler(); |
| 59 | + handler |
| 60 | + .getServletContext() |
| 61 | + .addServlet( |
| 62 | + "Runner Websocket servlet", |
| 63 | + new ServerMasterWebsocketServlet( |
| 64 | + dispatcher, serializer, config.getRunnerToken(), benchRepo)) |
| 65 | + .addMapping("/runner-connector"); |
| 66 | + handlerMap.put(connector, handler); |
| 67 | + |
| 68 | + server.setHandler(new RoutingHandler(handlerMap)); |
| 69 | + |
| 70 | + LOGGER.debug("Registered the websocket servlet"); |
| 71 | + |
| 72 | + return server; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void configure(Environment environment) { |
| 77 | + underlying.configure(environment); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets the config to use. |
| 82 | + * |
| 83 | + * @param config the config to use |
| 84 | + * @throws IllegalStateException if a config has already been set |
| 85 | + */ |
| 86 | + public void setConfig(GlobalConfig config) { |
| 87 | + if (this.config != null) { |
| 88 | + throw new IllegalStateException("I already have a config!"); |
| 89 | + } |
| 90 | + this.config = config; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Sets the server factory to delegate to. |
| 95 | + * |
| 96 | + * @param underlying the underlying server factory |
| 97 | + * @throws IllegalStateException if a server factory has already been set |
| 98 | + */ |
| 99 | + public void setServerFactory(ServerFactory underlying) { |
| 100 | + if (this.underlying != null) { |
| 101 | + throw new IllegalStateException("I already have a server factory!"); |
| 102 | + } |
| 103 | + |
| 104 | + this.underlying = underlying; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Sets the dispatcher to use. |
| 109 | + * |
| 110 | + * @param dispatcher the dispatcher |
| 111 | + * @throws IllegalStateException if a dispatcher has already been set |
| 112 | + */ |
| 113 | + public void setDispatcher(Dispatcher dispatcher) { |
| 114 | + if (this.dispatcher != null) { |
| 115 | + throw new IllegalStateException("I already have a dispatcher!"); |
| 116 | + } |
| 117 | + this.dispatcher = dispatcher; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Sets the bench reo to use. |
| 122 | + * |
| 123 | + * @param benchRepo the bench repo |
| 124 | + * @throws IllegalStateException if a bench repo has already been set |
| 125 | + */ |
| 126 | + public void setBenchRepo(BenchRepo benchRepo) { |
| 127 | + if (this.benchRepo != null) { |
| 128 | + throw new IllegalStateException("I already have a bench repo!"); |
| 129 | + } |
| 130 | + this.benchRepo = benchRepo; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns whether the factory lacks an underlying server factory. |
| 135 | + * |
| 136 | + * @return true if no underlying server factory is available |
| 137 | + */ |
| 138 | + public boolean lacksFactory() { |
| 139 | + return underlying == null; |
| 140 | + } |
| 141 | + |
| 142 | + private void ensureIsInitialized() { |
| 143 | + if (config == null) { |
| 144 | + throw new IllegalStateException("Config not initialized"); |
| 145 | + } |
| 146 | + if (underlying == null) { |
| 147 | + throw new IllegalStateException("Server factory not initialized"); |
| 148 | + } |
| 149 | + if (dispatcher == null) { |
| 150 | + throw new IllegalStateException("Dispatcher not initialized"); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Returns the instance of the {@link RunnerAwareServerFactory}. |
| 156 | + * |
| 157 | + * @return the instance |
| 158 | + */ |
| 159 | + public static RunnerAwareServerFactory getInstance() { |
| 160 | + return instance; |
| 161 | + } |
164 | 162 | } |
0 commit comments