/*
 * Copyright 2012-2017 the original author or authors.
 *
 * 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.
 */

package org.springframework.boot.web.embedded.netty;

import java.net.BindException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.Loopback;
import reactor.ipc.netty.NettyContext;
import reactor.ipc.netty.http.server.HttpServer;

import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.server.WebServerException;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;

/**
 * {@link WebServer} that can be used to control a Reactor Netty web server. Usually this
 * class should be created using the {@link NettyReactiveWebServerFactory} and not
 * directly.
 *
 * @author Brian Clozel
 * @author Madhura Bhave
 * @since 2.0.0
 */
public class NettyWebServer implements WebServer, Loopback {

	private static final Log logger = LogFactory.getLog(NettyWebServer.class);

	private static CountDownLatch latch = new CountDownLatch(1);

	private final ReactorHttpHandlerAdapter handlerAdapter;

	private final HttpServer reactorServer;

	private AtomicReference<NettyContext> nettyContext = new AtomicReference<>();

	public NettyWebServer(HttpServer reactorServer,
			ReactorHttpHandlerAdapter handlerAdapter) {
		this.reactorServer = reactorServer;
		this.handlerAdapter = handlerAdapter;
	}

	@Override
	public Object connectedInput() {
		return this.reactorServer;
	}

	@Override
	public Object connectedOutput() {
		return this.reactorServer;
	}

	@Override
	public void start() throws WebServerException {
		if (this.nettyContext.get() == null) {
			try {
				this.nettyContext
						.set(this.reactorServer.newHandler(this.handlerAdapter).block());
			}
			catch (Exception ex) {
				if (findBindException(ex) != null) {
//					throw new PortInUseException();
				}
				throw new WebServerException("Unable to start Netty", ex);
			}
			NettyWebServer.logger.info("Netty started on port(s): " + getPort());
			startDaemonAwaitThread();
		}
	}

	private BindException findBindException(Exception ex) {
		Throwable candidate = ex;
		while (candidate != null) {
			if (candidate instanceof BindException) {
				return (BindException) candidate;
			}
			candidate = candidate.getCause();
		}
		return null;
	}

	private void startDaemonAwaitThread() {
		Thread awaitThread = new Thread("server") {

			@Override
			public void run() {
				try {
					NettyWebServer.latch.await();
				}
				catch (InterruptedException e) {
				}
			}

		};
		awaitThread.setContextClassLoader(getClass().getClassLoader());
		awaitThread.setDaemon(false);
		awaitThread.start();
	}

	@Override
	public void stop() throws WebServerException {
		NettyContext context = this.nettyContext.getAndSet(null);
		if (context != null) {
			context.dispose();
		}
		latch.countDown();
	}

	@Override
	public int getPort() {
		if (this.nettyContext.get() != null) {
			return this.nettyContext.get().address().getPort();
		}
		return 0;
	}

}

@@ -886,38 +886,8 @@
 ry;%0A
-import reactor.core.Loopback;%0A
 impo
@@ -921,16 +921,16 @@
 ontext;%0A
+
 import r
@@ -1478,18 +1478,8 @@
 rver
-, Loopback
  %7B%0A%0A
@@ -1564,23 +1564,16 @@
 private 
-static 
 CountDow
@@ -1588,32 +1588,8 @@
 atch
- = new CountDownLatch(1)
 ;%0A%0A%09
@@ -1949,165 +1949,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public Object connectedInput() %7B%0A%09%09return this.reactorServer;%0A%09%7D%0A%0A%09@Override%0A%09public Object connectedOutput() %7B%0A%09%09return this.reactorServer;%0A%09%7D%0A%0A
 %09@Ov
@@ -2046,16 +2046,55 @@
 null) %7B%0A
+%09%09%09this.latch = new CountDownLatch(1);%0A
 %09%09%09try %7B
@@ -2900,16 +2900,21 @@
 bServer.
+this.
 latch.aw
@@ -3275,18 +3275,23 @@
 ();%0A%09%09%7D%0A
-
 %09%09
+this.
 latch.co

Fix devtools support in NettyWebServer