View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.util;
21  
22  /***
23   * The root thread group. Used to handle all uncaught exceptions that
24   * occur at any child thread of the group.
25   * 
26   * <p>If you want to catch all uncaught exceptions in your application, 
27   * use the following code snippet:
28   * 
29   * <p><pre>
30   *   public static void main(final String[] argv) {
31   * 	 	 RootThreadGroup tg = new RootThreadGroup("RootThreadGroup",
32   *                                               new ErrorHandler());
33   *		 Thread mainRunner = new Thread(tg, "XNapMain") {
34   *		 	 public void run() {
35   *				  runMain(argv);
36   *			 }
37   *		 };
38   *		 mainRunner.start();
39   *	 }
40   *	
41   *   public static void runMain(final String[] argv) {
42   * 	 	// run the actual main code
43   *   }
44   * </pre>
45   */
46  public class RootThreadGroup extends ThreadGroup {
47  
48  	private UncaughtExceptionListener listener;
49  	
50  	public RootThreadGroup(String name, UncaughtExceptionListener listener,
51  						   boolean installAsAWTExceptionHandler) 
52  	{
53  		super(name);
54  		
55  		this.listener = listener;
56  		
57  		if (installAsAWTExceptionHandler) {
58  			installAsAWTExceptionHandler();
59  		}
60  	}
61  
62  	public RootThreadGroup(String name, UncaughtExceptionListener listener)
63  	{
64  		this(name, listener, true);
65  	}
66  
67  	/***
68  	 * Invoked by the AWTEventQueue in case of an unhandled exception.
69  	 * @param e the uncaught exception
70  	 * @see #installAsAWTExceptionHandler()
71  	 */
72  	public void handle(Throwable e)
73  	{
74  		listener.uncaughtException(Thread.currentThread(), e);
75  	}
76  
77  	/***
78  	 * Installs this class as the awt exception handler.
79  	 */
80  	public void installAsAWTExceptionHandler()
81  	{
82  		System.setProperty("sun.awt.exception.handler", 
83  		   				   "org.xnap.commons.util.AWTExceptionHandler");
84  	}
85  
86  	/***
87  	 * @see java.lang.ThreadGroup#uncaughtException(java.lang.Thread,
88  	 * java.lang.Throwable)
89  	 */
90  	public void uncaughtException(Thread t, Throwable e) 
91  	{
92  		listener.uncaughtException(t, e);
93  	}
94  
95  }