UndertowHttpServer.java 1.9 KB
Newer Older
1
/*
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.http.server.reactive.bootstrap;
18 19 20 21

import io.undertow.Undertow;
import io.undertow.server.HttpHandler;

22 23
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
R
Rossen Stoyanchev 已提交
24 25 26
import org.springframework.http.server.reactive.UndertowHttpHandlerAdapter;
import org.springframework.util.Assert;

27 28 29
/**
 * @author Marek Hawrylczak
 */
30
public class UndertowHttpServer extends HttpServerSupport implements HttpServer {
31

R
Polish  
Rossen Stoyanchev 已提交
32
	private Undertow server;
33

34
	private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
35

36 37
	private boolean running;

38 39
	public void setDataBufferFactory(DataBufferFactory dataBufferFactory) {
		this.dataBufferFactory = dataBufferFactory;
40
	}
R
Polish  
Rossen Stoyanchev 已提交
41

42 43 44
	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(getHttpHandler());
45 46
		HttpHandler handler =
				new UndertowHttpHandlerAdapter(getHttpHandler(), dataBufferFactory);
47
		this.server = Undertow.builder().addHttpListener(getPort(), getHost())
R
Polish  
Rossen Stoyanchev 已提交
48
				.setHandler(handler).build();
49 50 51 52
	}

	@Override
	public void start() {
R
Polish  
Rossen Stoyanchev 已提交
53 54 55
		if (!this.running) {
			this.server.start();
			this.running = true;
56 57 58 59 60 61
		}

	}

	@Override
	public void stop() {
R
Polish  
Rossen Stoyanchev 已提交
62 63 64
		if (this.running) {
			this.server.stop();
			this.running = false;
65 66 67 68 69
		}
	}

	@Override
	public boolean isRunning() {
R
Polish  
Rossen Stoyanchev 已提交
70
		return this.running;
71
	}
R
Polish  
Rossen Stoyanchev 已提交
72

73
}