/*
 * Copyright (c) 2011-2015 Pivotal Software Inc, All Rights Reserved.
 *
 * 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.reactive.web.http.reactor;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.reactive.web.http.HttpServer;
import org.springframework.reactive.web.http.HttpServerSupport;
import org.springframework.util.Assert;
import reactor.bus.selector.Selectors;
import reactor.io.buffer.Buffer;
import reactor.io.net.NetStreams;


/**
 * @author Stephane Maldini
 */
public class ReactorHttpServer extends HttpServerSupport implements InitializingBean, HttpServer {

	private RequestHandlerAdapter reactorHandler;

	private reactor.io.net.http.HttpServer<Buffer, Buffer> reactorServer;

	private boolean running;


	@Override
	public boolean isRunning() {
		return this.running;
	}


	@Override
	public void afterPropertiesSet() throws Exception {

		Assert.notNull(getHttpHandler());
		this.reactorHandler = new RequestHandlerAdapter(getHttpHandler());

		this.reactorServer = (getPort() != -1 ?
				NetStreams.httpServer(getPort()) : NetStreams.httpServer());
	}


	@Override
	public void start() {
		if (!this.running) {
			try {
				this.reactorServer.route(Selectors.matchAll(), this.reactorHandler).start().await();
				this.running = true;
			}
			catch (InterruptedException ex) {
				throw new IllegalStateException(ex);
			}
		}
	}

	@Override
	public void stop() {
		if (this.running) {
			try {
				this.reactorServer.shutdown().await();
				this.running = false;
			}
			catch (InterruptedException ex) {
				throw new IllegalStateException(ex);
			}
		}
	}

}

@@ -630,16 +630,17 @@
 se.%0A */%0A
+%0A
 package 
@@ -687,16 +687,123 @@
 actor;%0A%0A
+import reactor.bus.selector.Selectors;%0Aimport reactor.io.buffer.Buffer;%0Aimport reactor.io.net.NetStreams;%0A%0A
 import o
@@ -1018,115 +1018,8 @@
 rt;%0A
-import reactor.bus.selector.Selectors;%0Aimport reactor.io.buffer.Buffer;%0Aimport reactor.io.net.NetStreams;%0A%0A
 %0A/**
@@ -1107,17 +1107,19 @@
 rSupport
- 
+%0A%09%09
 implemen
@@ -1300,17 +1300,16 @@
 nning;%0A%0A
-%0A
 %09@Overri
@@ -1364,25 +1364,24 @@
 unning;%0A%09%7D%0A%0A
-%0A
 %09@Override%0A%09
@@ -1580,21 +1580,17 @@
  != -1 ?
-%0A%09%09%09%09
+ 
 NetStrea
@@ -1615,17 +1615,21 @@
 ort()) :
- 
+%0A%09%09%09%09
 NetStrea
@@ -1650,17 +1650,16 @@
 ));%0A%09%7D%0A%0A
-%0A
 %09@Overri
@@ -1787,16 +1787,23 @@
 Handler)
+%0A%09%09%09%09%09%09
 .start()
@@ -1984,34 +1984,24 @@
 .running) %7B%0A
-%09%09%09try %7B%0A%09
 %09%09%09this.reac
@@ -2020,27 +2020,18 @@
 utdown()
-.await()
 ;%0A
-%09
 %09%09%09this.
@@ -2051,96 +2051,8 @@
 se;%0A
-%09%09%09%7D%0A%09%09%09catch (InterruptedException ex) %7B%0A%09%09%09%09throw new IllegalStateException(ex);%0A%09%09%09%7D%0A
 %09%09%7D%0A

Fix ReactorHttpServer with latest changes on shutdown behavior