KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > internal > adaptor > EclipseErrorHandler


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
12 package org.eclipse.core.runtime.internal.adaptor;
13
14 import java.io.IOException JavaDoc;
15 import java.net.URLConnection JavaDoc;
16 import java.util.Properties JavaDoc;
17 import org.eclipse.osgi.baseadaptor.*;
18 import org.eclipse.osgi.baseadaptor.hooks.AdaptorHook;
19 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
20 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
21 import org.eclipse.osgi.framework.log.FrameworkLog;
22 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.BundleException;
25
26 public class EclipseErrorHandler implements AdaptorHook, HookConfigurator {
27     // System property used to prevent VM exit when unexpected errors occur
28
private static final String JavaDoc PROP_EXITONERROR = "eclipse.exitOnError"; //$NON-NLS-1$
29
private BaseAdaptor adaptor;
30
31     public void frameworkStart(BundleContext context) throws BundleException {
32         // do nothing
33
}
34
35     public void frameworkStop(BundleContext context) throws BundleException {
36         // do nothing
37
}
38
39     public void frameworkStopping(BundleContext context) {
40         // do nothing
41
}
42
43     public void addProperties(Properties JavaDoc properties) {
44         // do nothing
45
}
46
47     public URLConnection JavaDoc mapLocationToURLConnection(String JavaDoc location) throws IOException JavaDoc {
48         // do nothing
49
return null;
50     }
51
52     private boolean isFatalException(Throwable JavaDoc error) {
53         if (error instanceof VirtualMachineError JavaDoc) {
54             return true;
55         }
56         if (error instanceof ThreadDeath JavaDoc) {
57             return true;
58         }
59         return false;
60     }
61
62     public void handleRuntimeError(Throwable JavaDoc error) {
63         // this is the important method to handle errors
64
boolean exitOnError = false;
65         try {
66             // check the prop each time this happens (should NEVER happen!)
67
exitOnError = Boolean.valueOf(FrameworkProperties.getProperty(EclipseErrorHandler.PROP_EXITONERROR, "true")).booleanValue(); //$NON-NLS-1$
68
String JavaDoc message = EclipseAdaptorMsg.ECLIPSE_ADAPTOR_RUNTIME_ERROR;
69             if (exitOnError && isFatalException(error))
70                 message += ' ' + EclipseAdaptorMsg.ECLIPSE_ADAPTOR_EXITING;
71             FrameworkLogEntry logEntry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, message, 0, error, null);
72             adaptor.getFrameworkLog().log(logEntry);
73         } catch (Throwable JavaDoc t) {
74             // we may be in a currupted state and must be able to handle any
75
// errors (ie OutOfMemoryError)
76
// that may occur when handling the first error; this is REALLY the
77
// last resort.
78
try {
79                 error.printStackTrace();
80                 t.printStackTrace();
81             } catch (Throwable JavaDoc t1) {
82                 // if we fail that then we are beyond help.
83
}
84         } finally {
85             // do the exit outside the try block just incase another runtime
86
// error was thrown while logging
87
if (exitOnError && isFatalException(error))
88                 System.exit(13);
89         }
90     }
91
92     public boolean matchDNChain(String JavaDoc pattern, String JavaDoc[] dnChain) {
93         // do nothing
94
return false;
95     }
96
97     public void addHooks(HookRegistry hookRegistry) {
98         hookRegistry.addAdaptorHook(this);
99     }
100
101     public FrameworkLog createFrameworkLog() {
102         // do nothing
103
return null;
104     }
105
106     public void initialize(BaseAdaptor adaptor) {
107         this.adaptor = adaptor;
108     }
109 }
110
Popular Tags