ApplicationPidFileWriter Class — spring-boot Architecture
Architecture documentation for the ApplicationPidFileWriter class in ApplicationPidFileWriter.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot/src/main/java/org/springframework/boot/context/ApplicationPidFileWriter.java lines 65–258
public class ApplicationPidFileWriter implements ApplicationListener<SpringApplicationEvent>, Ordered {
private static final Log logger = LogFactory.getLog(ApplicationPidFileWriter.class);
private static final String DEFAULT_FILE_NAME = "application.pid";
private static final List<Property> FILE_PROPERTIES;
static {
List<Property> properties = new ArrayList<>();
properties.add(new SpringProperty("spring.pid.", "file"));
properties.add(new SpringProperty("spring.", "pidfile"));
properties.add(new SystemProperty("PIDFILE"));
FILE_PROPERTIES = Collections.unmodifiableList(properties);
}
private static final List<Property> FAIL_ON_WRITE_ERROR_PROPERTIES;
static {
List<Property> properties = new ArrayList<>();
properties.add(new SpringProperty("spring.pid.", "fail-on-write-error"));
properties.add(new SystemProperty("PID_FAIL_ON_WRITE_ERROR"));
FAIL_ON_WRITE_ERROR_PROPERTIES = Collections.unmodifiableList(properties);
}
private static final AtomicBoolean created = new AtomicBoolean();
private int order = Ordered.HIGHEST_PRECEDENCE + 13;
private final File file;
private Class<? extends SpringApplicationEvent> triggerEventType = ApplicationPreparedEvent.class;
/**
* Create a new {@link ApplicationPidFileWriter} instance using the filename
* 'application.pid'.
*/
public ApplicationPidFileWriter() {
this(new File(DEFAULT_FILE_NAME));
}
/**
* Create a new {@link ApplicationPidFileWriter} instance with a specified filename.
* @param filename the name of file containing pid
*/
public ApplicationPidFileWriter(String filename) {
this(new File(filename));
}
/**
* Create a new {@link ApplicationPidFileWriter} instance with a specified file.
* @param file the file containing pid
*/
public ApplicationPidFileWriter(File file) {
Assert.notNull(file, "'file' must not be null");
this.file = file;
}
/**
* Sets the type of application event that will trigger writing of the PID file.
* Defaults to {@link ApplicationPreparedEvent}. NOTE: If you use the
* {@link org.springframework.boot.context.event.ApplicationStartingEvent} to trigger
* the write, you will not be able to specify the PID filename in the Spring
* {@link Environment}.
* @param triggerEventType the trigger event type
*/
public void setTriggerEventType(Class<? extends SpringApplicationEvent> triggerEventType) {
Assert.notNull(triggerEventType, "'triggerEventType' must not be null");
this.triggerEventType = triggerEventType;
}
@Override
public void onApplicationEvent(SpringApplicationEvent event) {
if (this.triggerEventType.isInstance(event) && created.compareAndSet(false, true)) {
try {
writePidFile(event);
}
catch (Exception ex) {
String message = String.format("Cannot create pid file %s", this.file);
if (failOnWriteError(event)) {
throw new IllegalStateException(message, ex);
}
logger.warn(message, ex);
}
}
}
private void writePidFile(SpringApplicationEvent event) throws IOException {
File pidFile = this.file;
String override = getProperty(event, FILE_PROPERTIES);
if (override != null) {
pidFile = new File(override);
}
new ApplicationPid().write(pidFile);
pidFile.deleteOnExit();
}
private boolean failOnWriteError(SpringApplicationEvent event) {
String value = getProperty(event, FAIL_ON_WRITE_ERROR_PROPERTIES);
return Boolean.parseBoolean(value);
}
private @Nullable String getProperty(SpringApplicationEvent event, List<Property> candidates) {
for (Property candidate : candidates) {
String value = candidate.getValue(event);
if (value != null) {
return value;
}
}
return null;
}
public void setOrder(int order) {
this.order = order;
}
@Override
public int getOrder() {
return this.order;
}
/**
* Reset the created flag for testing purposes.
*/
protected static void reset() {
created.set(false);
}
/**
* Provides access to a property value.
*/
private interface Property {
@Nullable String getValue(SpringApplicationEvent event);
}
/**
* {@link Property} obtained from Spring's {@link Environment}.
*/
private static class SpringProperty implements Property {
private final String prefix;
private final String key;
SpringProperty(String prefix, String key) {
this.prefix = prefix;
this.key = key;
}
@Override
public @Nullable String getValue(SpringApplicationEvent event) {
Environment environment = getEnvironment(event);
if (environment == null) {
return null;
}
return environment.getProperty(this.prefix + this.key);
}
private @Nullable Environment getEnvironment(SpringApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent environmentPreparedEvent) {
return environmentPreparedEvent.getEnvironment();
}
if (event instanceof ApplicationPreparedEvent preparedEvent) {
return preparedEvent.getApplicationContext().getEnvironment();
}
if (event instanceof ApplicationReadyEvent readyEvent) {
return readyEvent.getApplicationContext().getEnvironment();
}
return null;
}
}
/**
* {@link Property} obtained from {@link SystemProperties}.
*/
private static class SystemProperty implements Property {
private final String[] properties;
SystemProperty(String name) {
this.properties = new String[] { name.toUpperCase(Locale.ENGLISH), name.toLowerCase(Locale.ENGLISH) };
}
@Override
public @Nullable String getValue(SpringApplicationEvent event) {
return SystemProperties.get(this.properties);
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free