001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.util;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018:
019: import java.util.Date;
020: import java.util.LinkedHashSet;
021: import java.util.LinkedList;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: /**
026: * User: Martin
027: * Date: 24.02.2006
028: * Time: 09:36:43
029: */
030: public class UncaughtExcpetionsModel {
031: @NotNull
032: @NonNls
033: private static final Logger LOG = Logger
034: .getLogger(UncaughtExcpetionsModel.class.getName());
035:
036: private static final int MAXIMUM_SIZE = 100;
037:
038: @NotNull
039: private static final UncaughtExcpetionsModel instance = new UncaughtExcpetionsModel();
040:
041: @NotNull
042: public static UncaughtExcpetionsModel getInstance() {
043: return instance;
044: }
045:
046: @NotNull
047: private LinkedHashSet<UncaughtExceptionModelListener> uncaughtExceptionModelListeners;
048: @NotNull
049: private LinkedList<ThrowableInfo> throwables;
050:
051: public UncaughtExcpetionsModel() {
052: uncaughtExceptionModelListeners = new LinkedHashSet<UncaughtExceptionModelListener>();
053: throwables = new LinkedList<ThrowableInfo>();
054: }
055:
056: public void addException(@NotNull
057: Throwable throwable) {
058: if (LOG.isLoggable(Level.SEVERE))
059: LOG.log(Level.SEVERE,
060: "UncaughtExcpetionsModel.addException ", throwable);
061:
062: throwables.add(new ThrowableInfo(System.currentTimeMillis(),
063: throwable));
064: if (throwables.size() > MAXIMUM_SIZE) {
065: throwables.removeFirst();
066: }
067:
068: LinkedHashSet<UncaughtExceptionModelListener> uncaughtExceptionModelListeners = new LinkedHashSet<UncaughtExceptionModelListener>(
069: this .uncaughtExceptionModelListeners);
070: for (UncaughtExceptionModelListener uncaughtExceptionModelListener : uncaughtExceptionModelListeners) {
071: uncaughtExceptionModelListener.exceptionCaught(throwable);
072: }
073: }
074:
075: @NotNull
076: public LinkedList<ThrowableInfo> getThrowables() {
077: return throwables;
078: }
079:
080: public void addUncaughtExceptionModelListener(@NotNull
081: UncaughtExceptionModelListener uncaughtExceptionModelListener) {
082: uncaughtExceptionModelListeners
083: .add(uncaughtExceptionModelListener);
084: }
085:
086: public void removeUncaughtExceptionModelListener(@NotNull
087: UncaughtExceptionModelListener uncaughtExceptionModelListener) {
088: uncaughtExceptionModelListeners
089: .remove(uncaughtExceptionModelListener);
090: }
091:
092: public void clearExceptions() {
093: throwables.clear();
094:
095: LinkedHashSet<UncaughtExceptionModelListener> uncaughtExceptionModelListeners = new LinkedHashSet<UncaughtExceptionModelListener>(
096: this .uncaughtExceptionModelListeners);
097: for (UncaughtExceptionModelListener uncaughtExceptionModelListener : uncaughtExceptionModelListeners) {
098: uncaughtExceptionModelListener.exceptionsCleared();
099: }
100: }
101:
102: public void exceptionsViewed() {
103: LinkedHashSet<UncaughtExceptionModelListener> uncaughtExceptionModelListeners = new LinkedHashSet<UncaughtExceptionModelListener>(
104: this .uncaughtExceptionModelListeners);
105: for (UncaughtExceptionModelListener uncaughtExceptionModelListener : uncaughtExceptionModelListeners) {
106: uncaughtExceptionModelListener.exceptionsViewed();
107: }
108: }
109:
110: public static class ThrowableInfo {
111: private boolean submitted;
112: private long millis;
113: @NotNull
114: private Throwable throwable;
115:
116: public ThrowableInfo(long millis, @NotNull
117: Throwable throwable) {
118: //noinspection ConstantConditions
119: if (throwable == null) {
120: throw new IllegalArgumentException(
121: "throwable must not be null");
122: }
123:
124: submitted = false;
125: this .millis = millis;
126: this .throwable = throwable;
127: }
128:
129: public long getMillis() {
130: return millis;
131: }
132:
133: @NotNull
134: public Throwable getThrowable() {
135: return throwable;
136: }
137:
138: public boolean isSubmitted() {
139: return submitted;
140: }
141:
142: public void setSubmitted(boolean submitted) {
143: this .submitted = submitted;
144: }
145:
146: @NotNull
147: public String toString() {
148: return (submitted ? "" : "* ") + new Date(millis) + " "
149: + throwable;
150: }
151: }
152: }
|