KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > SafeRunner


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.runtime;
12
13 import org.eclipse.core.internal.runtime.*;
14 import org.eclipse.osgi.util.NLS;
15
16 /**
17  * Runs the given ISafeRunnable in a protected mode: exceptions
18  * thrown in the runnable are logged and passed to the runnable's
19  * exception handler. Such exceptions are not rethrown by this method.
20  * <p>
21  * This class can be used without OSGi running.
22  * </p>
23  * @since org.eclipse.equinox.common 3.2
24  */

25 public final class SafeRunner {
26
27     /**
28      * Runs the given runnable in a protected mode. Exceptions
29      * thrown in the runnable are logged and passed to the runnable's
30      * exception handler. Such exceptions are not rethrown by this method.
31      *
32      * @param code the runnable to run
33      */

34     public static void run(ISafeRunnable code) {
35         Assert.isNotNull(code);
36         try {
37             code.run();
38         } catch (Exception JavaDoc e) {
39             handleException(code, e);
40         } catch (LinkageError JavaDoc e) {
41             handleException(code, e);
42         }
43     }
44
45     private static void handleException(ISafeRunnable code, Throwable JavaDoc e) {
46         if (!(e instanceof OperationCanceledException)) {
47             // try to obtain the correct plug-in id for the bundle providing the safe runnable
48
Activator activator = Activator.getDefault();
49             String JavaDoc pluginId = null;
50             if (activator != null)
51                 pluginId = activator.getBundleId(code);
52             if (pluginId == null)
53                 pluginId = IRuntimeConstants.PI_COMMON;
54             String JavaDoc message = NLS.bind(CommonMessages.meta_pluginProblems, pluginId);
55             IStatus status;
56             if (e instanceof CoreException) {
57                 status = new MultiStatus(pluginId, IRuntimeConstants.PLUGIN_ERROR, message, e);
58                 ((MultiStatus) status).merge(((CoreException) e).getStatus());
59             } else {
60                 status = new Status(IStatus.ERROR, pluginId, IRuntimeConstants.PLUGIN_ERROR, message, e);
61             }
62             // Make sure user sees the exception: if the log is empty, log the exceptions on stderr
63
if (!RuntimeLog.isEmpty())
64                 RuntimeLog.log(status);
65             else
66                 e.printStackTrace();
67         }
68         code.handleException(e);
69     }
70 }
71
Popular Tags