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

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.management.ManagementFactory;

import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

/**
 * An application process ID.
 *
 * @author Phillip Webb
 */
public class ApplicationPid {

	private final String pid;

	public ApplicationPid() {
		this.pid = getPid();
	}

	protected ApplicationPid(String pid) {
		this.pid = pid;
	}

	private String getPid() {
		try {
			String jvmName = ManagementFactory.getRuntimeMXBean().getName();
			return jvmName.split("@")[0];
		}
		catch (Throwable ex) {
			return null;
		}
	}

	@Override
	public String toString() {
		return (this.pid == null ? "???" : this.pid);
	}

	@Override
	public int hashCode() {
		return ObjectUtils.nullSafeHashCode(this.pid);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj != null && obj instanceof ApplicationPid) {
			return ObjectUtils.nullSafeEquals(this.pid, ((ApplicationPid) obj).pid);
		}
		return false;
	}

	/**
	 * Write the PID to the specified file.
	 * @param file the PID file
	 * @throws IllegalStateException if no PID is available.
	 * @throws IOException if the file cannot be written
	 */
	public void write(File file) throws IOException {
		Assert.state(this.pid != null, "No PID available");
		createParentFolder(file);
		try (FileWriter writer = new FileWriter(file)) {
			writer.append(this.pid);
		}
	}

	private void createParentFolder(File file) {
		File parent = file.getParentFile();
		if (parent != null) {
			parent.mkdirs();
		}
	}

}

@@ -671,16 +671,54 @@
 o.File;%0A
+import java.io.FileNotFoundException;%0A
 import j
@@ -736,16 +736,16 @@
 Writer;%0A
-
 import j
@@ -810,16 +810,118 @@
 Factory;
+%0Aimport java.nio.file.Files;%0Aimport java.nio.file.attribute.PosixFilePermission;%0Aimport java.util.Set;
 %0A%0Aimport
@@ -1096,16 +1096,191 @@
 nPid %7B%0A%0A
+%09private static final PosixFilePermission%5B%5D WRITE_PERMISSIONS = %7B%0A%09%09%09PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_WRITE,%0A%09%09%09PosixFilePermission.OTHERS_WRITE %7D;%0A%0A
 %09private
@@ -2352,16 +2352,40 @@
 (file);%0A
+%09%09assertCanWrite(file);%0A
 %09%09try (F
@@ -2571,16 +2571,16 @@
 null) %7B%0A
-
 %09%09%09paren
@@ -2599,10 +2599,630 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09private void assertCanWrite(File file) throws IOException %7B%0A%09%09if (!file.canWrite() %7C%7C !canWritePosixFile(file)) %7B%0A%09%09%09throw new FileNotFoundException(file.toString() + %22 (permission denied)%22);%0A%09%09%7D%0A%09%7D%0A%0A%09private boolean canWritePosixFile(File file) throws IOException %7B%0A%09%09try %7B%0A%09%09%09Set%3CPosixFilePermission%3E permissions = Files%0A%09%09%09%09%09.getPosixFilePermissions(file.toPath());%0A%09%09%09for (PosixFilePermission permission : WRITE_PERMISSIONS) %7B%0A%09%09%09%09if (permissions.contains(permission)) %7B%0A%09%09%09%09%09return true;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09return false;%0A%09%09%7D%0A%09%09catch (UnsupportedOperationException ex) %7B%0A%09%09%09// Assume that we can%0A%09%09%09return true;%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

Check POSIX file permissions before writing PID