package com.onaro.util.jfc; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * Handles an exception thrown in the AWT event-dispatch thread. * By default exceptions are just logged. * AWT's java.awt.EventDispatchThread searches the system property * "sun.awt.exception.handler" when it catces an exception. If found it * will attempt to do the following: *
  • Load the class named by the value of that property, using the current * thread's context class loader, *
  • Instantiate that class using its zero-argument constructor, *
  • Find the resulting handler object's public void handle method, * which should take a single argument of type Throwable, and *
  • Invoke the handler's handle method, passing it the thrown * exception that was caught. * * If any of the first three steps fail then this the exception will simply be * dumped to the standard error. An exception thrown by the handler object's handle * will be caught, and will disable further usage of this handler. *

    * Note: This method is a temporary hack to work around the absence of a * real API that provides the ability to replace the event-dispatch thread. * The magic "sun.awt.exception.handler" property will be removed in a future * release. */ public class AWTExceptionHandler { static Logger logger = LogManager.getLogger(AWTExceptionHandler.class); /** * Handle an uncaught exception by simply sending it to the logger. * * @param t the uncaught exception */ public void handle (Throwable t) { logger.error("Uncaught exception in java.awt.EventDispatchThread - " + t.getMessage(), t); //$NON-NLS-1$ } /** * Install this handler by setting the system property. */ public static void install() { install(AWTExceptionHandler.class.getName()); } /** * Install this handler by setting the system property. * @param className the name of the handler class */ public static void install(String className) { logger.debug("Replacing current AWT exception handler (" + //$NON-NLS-1$ System.getProperty("sun.awt.exception.handler") + //$NON-NLS-1$ ") with " + className); //$NON-NLS-1$ System.setProperty("sun.awt.exception.handler", className); //$NON-NLS-1$ } }