/*
 * Copyright 2012-2016 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.autoconfigure;

import javax.validation.Validation;

import org.apache.catalina.mbeans.MBeanFactory;

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.logging.LoggingApplicationListener;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;

/**
 * {@link ApplicationListener} to trigger early initialization in a background thread of
 * time consuming tasks.
 *
 * @author Phillip Webb
 * @author Andy Wilkinson
 * @since 1.3.0
 */
@Order(LoggingApplicationListener.DEFAULT_ORDER + 1)
public class BackgroundPreinitializer implements ApplicationListener<ApplicationEvent> {

	private volatile Thread initializationThread;

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof ApplicationStartedEvent) {
			performInitialization();
		}
		else if (event instanceof ContextRefreshedEvent) {
			awaitInitialization();
		}
	}

	private void performInitialization() {
		try {
			this.initializationThread = new Thread(new Runnable() {

				@Override
				public void run() {
					runSafely(new MessageConverterInitializer());
					runSafely(new MBeanFactoryInitializer());
					runSafely(new ValidationInitializer());
				}

				public void runSafely(Runnable runnable) {
					try {
						runnable.run();
					}
					catch (Throwable ex) {
						// Ignore
					}
				}

			}, "background-preinit");
			this.initializationThread.start();
		}
		catch (Exception ex) {
			// This will fail on GAE where creating threads is prohibited. We can safely
			// continue but startup will be slightly slower as the initialization will now
			// happen on the main thread.
		}
	}

	private void awaitInitialization() {
		try {
			this.initializationThread.join();
		}
		catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
		finally {
			this.initializationThread = null;
		}
	}

	/**
	 * Early initializer for Spring MessageConverters.
	 */
	private static class MessageConverterInitializer implements Runnable {

		@Override
		public void run() {
			new AllEncompassingFormHttpMessageConverter();
		}

	}

	/**
	 * Early initializer to load Tomcat MBean XML.
	 */
	private static class MBeanFactoryInitializer implements Runnable {

		@Override
		public void run() {
			new MBeanFactory();
		}

	}

	/**
	 * Early initializer for javax.validation.
	 */
	private static class ValidationInitializer implements Runnable {

		@Override
		public void run() {
			Validation.byDefaultProvider().configure();
		}

	}
}

@@ -1875,36 +1875,24 @@
 ry %7B%0A%09%09%09
-this.initializationT
+Thread t
 hread = 
@@ -2282,16 +2282,35 @@
 init%22);%0A
+%09%09%09thread.start();%0A
 %09%09%09this.
@@ -2325,32 +2325,33 @@
 zationThread
-.start()
+ = thread
 ;%0A%09%09%7D%0A%09%09catc
@@ -2612,25 +2612,32 @@
 n() %7B%0A%09%09
-try %7B%0A%09%09%09
+Thread thread = 
 this.ini
@@ -2653,16 +2653,61 @@
 onThread
+;%0A%09%09if (thread != null) %7B%0A%09%09%09try %7B%0A%09%09%09%09thread
 .join();
@@ -2705,26 +2705,28 @@
 d.join();%0A%09%09
+%09
 %7D%0A
+%09
 %09%09catch (Int
@@ -2748,24 +2748,25 @@
 on ex) %7B%0A%09%09%09
+%09
 Thread.curre
@@ -2791,18 +2791,20 @@
 pt();%0A%09%09
+%09
 %7D%0A
+%09
 %09%09finall
@@ -2806,24 +2806,25 @@
 inally %7B%0A%09%09%09
+%09
 this.initial
@@ -2845,16 +2845,21 @@
 = null;%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Handle multiple ContextRefreshedEvents in BackgroundPreinitializer