001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.utils;
038:
039: import java.lang.Thread.UncaughtExceptionHandler;
040: import org.netbeans.installer.utils.helper.ErrorLevel;
041: import org.netbeans.installer.utils.helper.FinishHandler;
042:
043: /**
044: *
045: * @author Kirill Sorokin
046: */
047: public final class ErrorManager {
048: /////////////////////////////////////////////////////////////////////////////////
049: // Static
050: private static UncaughtExceptionHandler exceptionHandler;
051: private static FinishHandler finishHandler;
052:
053: public static synchronized void notifyDebug(String message) {
054: notify(ErrorLevel.DEBUG, message);
055: }
056:
057: public static synchronized void notifyDebug(String message,
058: Throwable e) {
059: notify(ErrorLevel.DEBUG, message, e);
060: }
061:
062: public static synchronized void notify(String message) {
063: notify(ErrorLevel.MESSAGE, message);
064: }
065:
066: public static synchronized void notify(String message, Throwable e) {
067: notify(ErrorLevel.MESSAGE, message, e);
068: }
069:
070: public static synchronized void notifyWarning(String message) {
071: notify(ErrorLevel.WARNING, message);
072: }
073:
074: public static synchronized void notifyWarning(String message,
075: Throwable e) {
076: notify(ErrorLevel.WARNING, message, e);
077: }
078:
079: public static synchronized void notifyError(String message) {
080: notify(ErrorLevel.ERROR, message);
081: }
082:
083: public static synchronized void notifyError(String message,
084: Throwable e) {
085: notify(ErrorLevel.ERROR, message, e);
086: }
087:
088: public static synchronized void notifyCritical(String message) {
089: notify(ErrorLevel.CRITICAL, message);
090: }
091:
092: public static synchronized void notifyCritical(String message,
093: Throwable e) {
094: notify(ErrorLevel.CRITICAL, message, e);
095: }
096:
097: public static synchronized void notify(int level, String message) {
098: notify(level, message, null);
099: }
100:
101: public static synchronized void notify(int level,
102: Throwable exception) {
103: notify(level, null, exception);
104: }
105:
106: public static synchronized void notify(int level, String message,
107: Throwable exception) {
108: // parameters validation
109: assert (message != null) || (exception != null);
110:
111: String dialogText = StringUtils.EMPTY_STRING;
112:
113: if (message != null) {
114: LogManager.log(level, message);
115: dialogText = message;
116: } else {
117: dialogText = ResourceUtils.getString(ErrorManager.class,
118: ERROR_UNEXPECTED_ERROR_KEY);
119: }
120: if (exception != null) {
121: LogManager.log(level, exception);
122: dialogText += ResourceUtils.getString(ErrorManager.class,
123: ERROR_EXCEPTION_MESSAGE_KEY, exception.getClass()
124: .getName(), exception.getMessage());
125: }
126: if (LogManager.getLogFile() != null) {
127: dialogText += ResourceUtils.getString(ErrorManager.class,
128: ERROR_LOGFILE_INFO_KEY, LogManager.getLogFile()
129: .getAbsolutePath());
130: }
131: switch (level) {
132: case ErrorLevel.MESSAGE:
133: UiUtils.showMessageDialog(dialogText, ResourceUtils
134: .getString(ErrorManager.class, ERROR_MESSAGE_KEY),
135: UiUtils.MessageType.INFORMATION);
136: return;
137: case ErrorLevel.WARNING:
138: UiUtils.showMessageDialog(dialogText, ResourceUtils
139: .getString(ErrorManager.class, ERROR_WARNING_KEY),
140: UiUtils.MessageType.WARNING);
141: return;
142: case ErrorLevel.ERROR:
143: UiUtils.showMessageDialog(dialogText, ResourceUtils
144: .getString(ErrorManager.class, ERROR_ERROR_KEY),
145: UiUtils.MessageType.ERROR);
146: return;
147: case ErrorLevel.CRITICAL:
148: UiUtils.showMessageDialog(dialogText, ResourceUtils
149: .getString(ErrorManager.class, ERROR_CRITICAL_KEY),
150: UiUtils.MessageType.CRITICAL);
151: finishHandler.criticalExit();
152: return;
153: default:
154: return;
155: }
156: }
157:
158: public static UncaughtExceptionHandler getExceptionHandler() {
159: return exceptionHandler;
160: }
161:
162: public static void setExceptionHandler(
163: final UncaughtExceptionHandler exceptionHandler) {
164: ErrorManager.exceptionHandler = exceptionHandler;
165: }
166:
167: public static FinishHandler getFinishHandler() {
168: return finishHandler;
169: }
170:
171: public static void setFinishHandler(
172: final FinishHandler finishHandler) {
173: ErrorManager.finishHandler = finishHandler;
174: }
175:
176: /////////////////////////////////////////////////////////////////////////////////
177: // Instance
178: private ErrorManager() {
179: // does nothing
180: }
181:
182: /////////////////////////////////////////////////////////////////////////////////
183: // Inner Classes
184: public static class ExceptionHandler implements
185: UncaughtExceptionHandler {
186: public void uncaughtException(final Thread thread,
187: final Throwable exception) {
188: ErrorManager.notifyCritical(ResourceUtils.getString(
189: ErrorManager.class, ERROR_UNEXPECTED_EXCEPTION_KEY,
190: thread.getName()), exception);
191: }
192: }
193:
194: private static final String ERROR_UNEXPECTED_EXCEPTION_KEY = "EM.ununexpected.exception";//NOI18N
195: private static final String ERROR_CRITICAL_KEY = "EM.errortype.critical";//NOI18N
196: private static final String ERROR_WARNING_KEY = "EM.errortype.warning";//NOI18N
197: private static final String ERROR_ERROR_KEY = "EM.errortype.error";//NOI18N
198: private static final String ERROR_MESSAGE_KEY = "EM.errortype.message";//NOI18N
199: private static final String ERROR_UNEXPECTED_ERROR_KEY = "EM.unexpected.error";//NOI18N
200: private static final String ERROR_EXCEPTION_MESSAGE_KEY = "EM.exception.message";//NOI18N
201: private static final String ERROR_LOGFILE_INFO_KEY = "EM.exception.logfile.info";//NOI18N
202: }
|