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

import org.apache.catalina.Container;
import org.apache.catalina.core.StandardContext;

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

	@Override
	public void loadOnStartup(Container[] children) {
	}

	public void deferredLoadOnStartup() {
		super.loadOnStartup(findChildren());
	}

}

@@ -760,16 +760,60 @@
 Context;
+%0Aimport org.springframework.util.ClassUtils;
 %0A%0A/**%0A *
@@ -1128,44 +1128,700 @@
 %7B%0A%09%09
-super.loadOnStartup(findChildren());
+// Some older Servlet frameworks (e.g. Struts, BIRT) use the Thread context class%0A%09%09// loader to create servlet instances in this phase. If they do that and then try%0A%09%09// to initialize them later the class loader may have changed, so wrap the call to%0A%09%09// loadOnStartup in what we think its going to be the main webapp classloader at%0A%09%09// runtime.%0A%09%09ClassLoader classLoader = getLoader().getClassLoader();%0A%09%09ClassLoader existingLoader = null;%0A%09%09if (classLoader != null) %7B%0A%09%09%09existingLoader = ClassUtils.overrideThreadContextClassLoader(classLoader);%0A%09%09%7D%0A%09%09super.loadOnStartup(findChildren());%0A%09%09if (existingLoader != null) %7B%0A%09%09%09ClassUtils.overrideThreadContextClassLoader(existingLoader);%0A%09%09%7D
 %0A%09%7D%0A

Set Thread context class loader while Tomcat starts up