/*
 * Copyright 2012-2014 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.context.embedded.jetty;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

/**
 * {@link EmbeddedServletContainer} that can be used to control an embedded Jetty server.
 * Usually this class should be created using the
 * {@link JettyEmbeddedServletContainerFactory} and not directly.
 *
 * @author Phillip Webb
 * @author Dave Syer
 * @see JettyEmbeddedServletContainerFactory
 */
public class JettyEmbeddedServletContainer implements EmbeddedServletContainer {

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

	private final Server server;

	private final boolean autoStart;

	/**
	 * Create a new {@link JettyEmbeddedServletContainer} instance.
	 * @param server the underlying Jetty server
	 */
	public JettyEmbeddedServletContainer(Server server) {
		this(server, true);
	}

	/**
	 * Create a new {@link JettyEmbeddedServletContainer} instance.
	 * @param server the underlying Jetty server
	 */
	public JettyEmbeddedServletContainer(Server server, boolean autoStart) {
		this.autoStart = autoStart;
		Assert.notNull(server, "Jetty Server must not be null");
		this.server = server;
		initialize();
	}

	private synchronized void initialize() {
		try {
			this.server.start();
			// Start the server so the ServletContext is available, but stop the
			// connectors to prevent requests from being handled before the Spring context
			// is ready:
			Connector[] connectors = this.server.getConnectors();
			for (Connector connector : connectors) {
				connector.stop();
			}
		}
		catch (Exception ex) {
			try {
				// Ensure process isn't left running
				this.server.stop();
			}
			catch (Exception e) {
			}
			throw new EmbeddedServletContainerException(
					"Unable to start embedded Jetty servlet container", ex);
		}
	}

	@Override
	public void start() throws EmbeddedServletContainerException {
		if (!this.autoStart) {
			return;
		}
		try {
			this.server.start();
			for (Handler handler : this.server.getHandlers()) {
				handleDeferredInitialize(handler);
			}
			Connector[] connectors = this.server.getConnectors();
			for (Connector connector : connectors) {
				connector.start();
				this.logger.info("Jetty started on port: " + getLocalPort(connector));
			}
		}
		catch (Exception ex) {
			throw new EmbeddedServletContainerException(
					"Unable to start embedded Jetty servlet container", ex);
		}
	}

	private void handleDeferredInitialize(Handler handler) throws Exception {
		if (handler instanceof JettyEmbeddedWebAppContext) {
			((JettyEmbeddedWebAppContext) handler).deferredInitialize();
		}
		else if (handler instanceof HandlerWrapper) {
			handleDeferredInitialize(((HandlerWrapper) handler).getHandler());
		}
	}

	private Integer getLocalPort(Connector connector) {
		try {
			// Jetty 9 internals are different, but the method name is the same
			return (Integer) ReflectionUtils.invokeMethod(
					ReflectionUtils.findMethod(connector.getClass(), "getLocalPort"),
					connector);
		}
		catch (Exception ex) {
			this.logger.info("could not determine port ( " + ex.getMessage() + ")");
			return 0;
		}
	}

	@Override
	public synchronized void stop() {
		try {
			this.server.stop();
		}
		catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
		catch (Exception ex) {
			throw new EmbeddedServletContainerException(
					"Unable to stop embedded Jetty servlet container", ex);
		}
	}

	@Override
	public int getPort() {
		Connector[] connectors = this.server.getConnectors();
		for (Connector connector : connectors) {
			// Probably only one...
			return getLocalPort(connector);
		}
		return 0;
	}

	/**
	 * Returns access to the underlying Jetty Server.
	 */
	public Server getServer() {
		return this.server;
	}

}

@@ -1722,16 +1722,50 @@
 Start;%0A%0A
+%09private Connector%5B%5D connectors;%0A%0A
 %09/**%0A%09 *
@@ -2343,106 +2343,30 @@
 %0A%09%09%09
-this.server.start();%0A%09%09%09// Start the server so the ServletContext is available, but stop the%0A%09%09%09//
+// Cache and clear the
  con
@@ -2397,13 +2397,8 @@
 sts 
-from 
 bein
@@ -2417,27 +2417,38 @@
 fore
- the Spring
+%0A%09%09%09// the application
  context
 %0A%09%09%09
@@ -2443,22 +2443,16 @@
  context
-%0A%09%09%09//
  is read
@@ -2456,25 +2456,17 @@
 eady
-:
 %0A%09%09%09
-Connector%5B%5D 
+this.
 conn
@@ -2510,75 +2510,128 @@
 %0A%09%09%09
-for (Connector connector : connectors) %7B%0A%09%09%09%09connector.stop();%0A%09%09%09%7D
+this.server.setConnectors(null);%0A%0A%09%09%09// Start the server so that the ServletContext is available%0A%09%09%09this.server.start();
 %0A%09%09%7D
@@ -2954,24 +2954,71 @@
 Exception %7B%0A
+%09%09this.server.setConnectors(this.connectors);%0A%0A
 %09%09if (!this.

Rework Jetty startup so connectors are only started once