/*
 * Copyright 2012-2017 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.gradle.plugin;

import io.spring.gradle.dependencymanagement.DependencyManagementPlugin;
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

/**
 * {@link Action} that is performed in response to the {@link DependencyManagementPlugin}
 * being applied.
 *
 * @author Andy Wilkinson
 */
final class DependencyManagementPluginAction implements PluginApplicationAction {

	private static final String SPRING_BOOT_VERSION = DependencyManagementPluginAction.class
			.getPackage().getImplementationVersion();

	private static final String SPRING_BOOT_BOM = "org.springframework.boot:spring-boot-dependencies:"
			+ SPRING_BOOT_VERSION;

	@Override
	public void execute(Project project) {
		project.getExtensions().findByType(DependencyManagementExtension.class)
				.imports((importsHandler) -> importsHandler.mavenBom(SPRING_BOOT_BOM));
	}

	@Override
	public Class<? extends Plugin<Project>> getPluginClass() {
		return DependencyManagementPlugin.class;
	}

}

@@ -664,16 +664,215 @@
 lugin;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0Aimport java.net.JarURLConnection;%0Aimport java.net.URL;%0Aimport java.net.URLConnection;%0Aimport java.util.jar.Attributes;%0Aimport java.util.jar.JarFile;%0A%0A
 import i
@@ -932,24 +932,24 @@
 mentPlugin;%0A
-
 import io.sp
@@ -1391,81 +1391,27 @@
 N = 
-DependencyManagementPluginAction.class%0A%09%09%09.getPackage().getImplementation
+determineSpringBoot
 Vers
@@ -1821,16 +1821,16 @@
 ass() %7B%0A
-
 %09%09return
@@ -1868,10 +1868,980 @@
 ss;%0A%09%7D%0A%0A
+%09private static String determineSpringBootVersion() %7B%0A%09%09String implementationVersion = DependencyManagementPluginAction.class.getPackage()%0A%09%09%09%09.getImplementationVersion();%0A%09%09if (implementationVersion != null) %7B%0A%09%09%09return implementationVersion;%0A%09%09%7D%0A%09%09URL codeSourceLocation = DependencyManagementPluginAction.class%0A%09%09%09%09.getProtectionDomain().getCodeSource().getLocation();%0A%09%09try %7B%0A%09%09%09URLConnection connection = codeSourceLocation.openConnection();%0A%09%09%09if (connection instanceof JarURLConnection) %7B%0A%09%09%09%09return getImplementationVersion(%0A%09%09%09%09%09%09((JarURLConnection) connection).getJarFile());%0A%09%09%09%7D%0A%09%09%09try (JarFile jarFile = new JarFile(new File(codeSourceLocation.toURI()))) %7B%0A%09%09%09%09return getImplementationVersion(jarFile);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09catch (Exception ex) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%7D%0A%0A%09private static String getImplementationVersion(JarFile jarFile) throws IOException %7B%0A%09%09return jarFile.getManifest().getMainAttributes()%0A%09%09%09%09.getValue(Attributes.Name.IMPLEMENTATION_VERSION);%0A%09%7D%0A%0A
 %7D%0A

Determine Spring Boot version for bom import correctly when using Java 9