Add parent directory creation
Also tidied a few other things up (like not needing SmartApplicationListener). Fixes gh-550
This commit is contained in:
parent
f6488c7f66
commit
41cdb7b48d
|
|
@ -16,30 +16,30 @@
|
|||
|
||||
package org.springframework.boot.actuate.system;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.boot.util.SystemUtils;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.event.SmartApplicationListener;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.boot.util.SystemUtils;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* An {@link org.springframework.context.ApplicationListener} that saves
|
||||
* application PID into file
|
||||
* An {@link org.springframework.context.ApplicationListener} that saves application PID
|
||||
* into file
|
||||
*
|
||||
* @since 1.0.2
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class ApplicationPidListener implements SmartApplicationListener {
|
||||
|
||||
private static Class<?>[] EVENT_TYPES = {ApplicationStartedEvent.class};
|
||||
public class ApplicationPidListener implements
|
||||
ApplicationListener<ApplicationStartedEvent>, Ordered {
|
||||
|
||||
private static final String DEFAULT_PID_FILE_NAME = "application.pid";
|
||||
|
||||
|
|
@ -47,27 +47,24 @@ public class ApplicationPidListener implements SmartApplicationListener {
|
|||
|
||||
private int order = Ordered.HIGHEST_PRECEDENCE + 13;
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
private static final Log logger = LogFactory.getLog(ApplicationPidListener.class);
|
||||
|
||||
private String pidFileName = DEFAULT_PID_FILE_NAME;
|
||||
|
||||
@Override
|
||||
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
|
||||
for (Class<?> type : EVENT_TYPES) {
|
||||
if (type.isAssignableFrom(eventType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
/**
|
||||
* Sets the pid file name. This file will contain current process id.
|
||||
*
|
||||
* @param pidFileName the name of file containing pid
|
||||
*/
|
||||
public ApplicationPidListener(String pidFileName) {
|
||||
this.pidFileName = pidFileName;
|
||||
}
|
||||
|
||||
public ApplicationPidListener() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsSourceType(Class<?> sourceType) {
|
||||
return SpringApplication.class.isAssignableFrom(sourceType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent applicationEvent) {
|
||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||
if (pidFileCreated.get()) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -75,48 +72,58 @@ public class ApplicationPidListener implements SmartApplicationListener {
|
|||
String applicationPid;
|
||||
try {
|
||||
applicationPid = SystemUtils.getApplicationPid();
|
||||
} catch (IllegalStateException ignore) {
|
||||
}
|
||||
catch (IllegalStateException ignore) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (pidFileCreated.compareAndSet(false, true)) {
|
||||
File file = new File(pidFileName);
|
||||
File file = new File(this.pidFileName);
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
File parent = file.getParentFile();
|
||||
if (parent != null) {
|
||||
parent.mkdirs();
|
||||
}
|
||||
fileOutputStream = new FileOutputStream(file);
|
||||
fileOutputStream.write(applicationPid.getBytes());
|
||||
} catch (FileNotFoundException e) {
|
||||
log.warn(String.format("Cannot create pid file %s !", pidFileName));
|
||||
} catch (IOException e) {
|
||||
log.warn(String.format("Cannot write to pid file %s!", pidFileName));
|
||||
} finally {
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
logger.warn(String
|
||||
.format("Cannot create pid file %s !", this.pidFileName));
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.warn(String.format("Cannot write to pid file %s!",
|
||||
this.pidFileName));
|
||||
}
|
||||
finally {
|
||||
if (fileOutputStream != null) {
|
||||
try {
|
||||
fileOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
log.warn(String.format("Cannot close pid file %s!", pidFileName));
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.warn(String.format("Cannot close pid file %s!",
|
||||
this.pidFileName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow pid file to be re-written
|
||||
*/
|
||||
public static void reset() {
|
||||
pidFileCreated.set(false);
|
||||
}
|
||||
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pid file name. This file will contains current process id.
|
||||
*
|
||||
* @param pidFileName the name of file containing pid
|
||||
*/
|
||||
public void setPidFileName(String pidFileName) {
|
||||
this.pidFileName = pidFileName;
|
||||
return this.order;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,35 +16,56 @@
|
|||
|
||||
package org.springframework.boot.actuate.system;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by jkubrynski@gmail.com / 2014-03-25
|
||||
* @author Jakub Kubrynski
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class ApplicationPidListenerTest {
|
||||
|
||||
private static final String[] NO_ARGS = {};
|
||||
|
||||
public static final String PID_FILE_NAME = "test.pid";
|
||||
@After
|
||||
public void init() {
|
||||
ApplicationPidListener.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreatePidFile() {
|
||||
//given
|
||||
ApplicationPidListener sut = new ApplicationPidListener();
|
||||
sut.setPidFileName(PID_FILE_NAME);
|
||||
// given
|
||||
String pidFileName = "test.pid";
|
||||
ApplicationPidListener sut = new ApplicationPidListener(pidFileName);
|
||||
|
||||
//when
|
||||
sut.onApplicationEvent(new ApplicationStartedEvent(
|
||||
new SpringApplication(), NO_ARGS));
|
||||
// when
|
||||
sut.onApplicationEvent(new ApplicationStartedEvent(new SpringApplication(),
|
||||
NO_ARGS));
|
||||
|
||||
//then
|
||||
File pidFile = new File(PID_FILE_NAME);
|
||||
// then
|
||||
File pidFile = new File(pidFileName);
|
||||
assertTrue(pidFile.exists());
|
||||
pidFile.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreatePidFileParentDirectory() {
|
||||
// given
|
||||
String pidFileName = "target/pid/test.pid";
|
||||
ApplicationPidListener sut = new ApplicationPidListener(pidFileName);
|
||||
|
||||
// when
|
||||
sut.onApplicationEvent(new ApplicationStartedEvent(new SpringApplication(),
|
||||
NO_ARGS));
|
||||
|
||||
// then
|
||||
File pidFile = new File(pidFileName);
|
||||
assertTrue(pidFile.exists());
|
||||
pidFile.delete();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue