Strange but true, a Java program cannot detect the situation where it is still running but the laptop is going to sleep because the user closed the lid. Neither can a full-blown Java application server like GlassFish detect a power loss event reported by the UPS and safely inform its clients or sibling cluster nodes that it will be away soon.
This is ridiculous, as the operating system knows all these events very well and there are native APIs to inform applications about state changes for decades. I wonder why nobody every asked to such an API on the Java platform?
Anyways, if you share my impression that it would be beneficial for an application to detect such state changes, then please vote for my proposal on OpenJDK’s JIRA.