/*
 * Copyright 2012-2018 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.tomcat;

import org.apache.catalina.Container;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Manager;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.session.ManagerBase;

import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

/**
 * Tomcat {@link StandardContext} used by {@link TomcatWebServer} to support deferred
 * initialization.
 *
 * @author Phillip Webb
 * @author Andy Wilkinson
 */
class TomcatEmbeddedContext extends StandardContext {

	private TomcatStarter starter;

	private final boolean overrideLoadOnStart;

	TomcatEmbeddedContext() {
		this.overrideLoadOnStart = ReflectionUtils
				.findMethod(StandardContext.class, "loadOnStartup", Container[].class)
				.getReturnType() == boolean.class;
	}

	@Override
	public boolean loadOnStartup(Container[] children) {
		if (this.overrideLoadOnStart) {
			return true;
		}
		return super.loadOnStartup(children);
	}

	@Override
	public void setManager(Manager manager) {
		if (manager instanceof ManagerBase) {
			((ManagerBase) manager).setSessionIdGenerator(new LazySessionIdGenerator());
		}
		super.setManager(manager);
	}

	public void deferredLoadOnStartup() throws LifecycleException {
		// Some older Servlet frameworks (e.g. Struts, BIRT) use the Thread context class
		// loader to create servlet instances in this phase. If they do that and then try
		// to initialize them later the class loader may have changed, so wrap the call to
		// loadOnStartup in what we think its going to be the main webapp classloader at
		// runtime.
		ClassLoader classLoader = getLoader().getClassLoader();
		ClassLoader existingLoader = null;
		if (classLoader != null) {
			existingLoader = ClassUtils.overrideThreadContextClassLoader(classLoader);
		}
		try {
			if (this.overrideLoadOnStart) {
				// Earlier versions of Tomcat used a version that returned void. If that
				// version is used our overridden loadOnStart method won't have been
				// called and the original will have already run.
				boolean started = super.loadOnStartup(findChildren());
				Assert.state(started,
						"Unable to start embedded tomcat context " + getName());
			}
		}
		finally {
			if (existingLoader != null) {
				ClassUtils.overrideThreadContextClassLoader(existingLoader);
			}
		}
	}

	public void setStarter(TomcatStarter starter) {
		this.starter = starter;
	}

	public TomcatStarter getStarter() {
		return this.starter;
	}

}

@@ -976,57 +976,8 @@
 ils;
-%0Aimport org.springframework.util.ReflectionUtils;
 %0A%0A/*
@@ -1233,399 +1233,139 @@
 ;%0A%0A%09
-private final boolean overrideLoadOnStart;%0A%0A%09TomcatEmbeddedContext() %7B%0A%09%09this.overrideLoadOnStart = ReflectionUtils%0A%09%09%09%09.findMethod(StandardContext.class, %22loadOnStartup%22, Container%5B%5D.class)%0A%09%09%09%09.getReturnType() == boolean.class;%0A%09%7D%0A%0A%09@Override%0A%09public boolean loadOnStartup(Container%5B%5D children) %7B%0A%09%09if (this.overrideLoadOnStart) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%09%09return super.loadOnStartup(children)
+@Override%0A%09public boolean loadOnStartup(Container%5B%5D children) %7B%0A%09%09// deferred until later (see deferredLoadOnStartup)%0A%09%09return true
 ;%0A%09%7D
@@ -2210,248 +2210,8 @@
 y %7B%0A
-%09%09%09if (this.overrideLoadOnStart) %7B%0A%09%09%09%09// Earlier versions of Tomcat used a version that returned void. If that%0A%09%09%09%09// version is used our overridden loadOnStart method won't have been%0A%09%09%09%09// called and the original will have already run.%0A%09
 %09%09%09b
@@ -2267,17 +2267,16 @@
 ());%0A%09%09%09
-%09
 Assert.s
@@ -2292,15 +2292,9 @@
 ted,
-%0A%09%09%09%09%09%09
+ 
 %22Una
@@ -2346,21 +2346,16 @@
 ame());%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%09%09fi

Remove TomcatEmbeddedContext.overrideLoadOnStart