KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > RuntimeLog


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Julian Chen - fix for bug #92572, jclRM
11  *******************************************************************************/

12 package org.eclipse.core.internal.runtime;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import org.eclipse.core.runtime.*;
17
18 /**
19  * NOT API!!! This log infrastructure was split from the InternalPlatform.
20  *
21  * @since org.eclipse.equinox.common 3.2
22  */

23 // XXX this must be removed and replaced with something more reasonable
24
public final class RuntimeLog {
25
26     private static ArrayList JavaDoc logListeners = new ArrayList JavaDoc(5);
27
28     /**
29      * Keep the messages until the first log listener is registered.
30      * Once first log listeners is registred, it is going to receive
31      * all status messages accumulated during the period when no log
32      * listener was available.
33      */

34     private static ArrayList JavaDoc queuedMessages = new ArrayList JavaDoc(5);
35
36     /**
37      * @see Platform#addLogListener(ILogListener)
38      */

39     public static void addLogListener(ILogListener listener) {
40         synchronized (logListeners) {
41             boolean firstListener = (logListeners.size() == 0);
42             // replace if already exists (Set behaviour but we use an array
43
// since we want to retain order)
44
logListeners.remove(listener);
45             logListeners.add(listener);
46             if (firstListener) {
47                 for (Iterator JavaDoc i = queuedMessages.iterator(); i.hasNext();) {
48                     try {
49                         IStatus recordedMessage = (IStatus) i.next();
50                         listener.logging(recordedMessage, IRuntimeConstants.PI_RUNTIME);
51                     } catch (Exception JavaDoc e) {
52                         handleException(e);
53                     } catch (LinkageError JavaDoc e) {
54                         handleException(e);
55                     }
56                 }
57                 queuedMessages.clear();
58             }
59         }
60     }
61
62     /**
63      * @see Platform#removeLogListener(ILogListener)
64      */

65     public static void removeLogListener(ILogListener listener) {
66         synchronized (logListeners) {
67             logListeners.remove(listener);
68         }
69     }
70
71     /**
72      * Checks if the given listener is present
73      */

74     public static boolean contains(ILogListener listener) {
75         synchronized (logListeners) {
76             return logListeners.contains(listener);
77         }
78     }
79
80     /**
81      * Notifies all listeners of the platform log.
82      */

83     public static void log(final IStatus status) {
84         // create array to avoid concurrent access
85
ILogListener[] listeners;
86         synchronized (logListeners) {
87             listeners = (ILogListener[]) logListeners.toArray(new ILogListener[logListeners.size()]);
88             if (listeners.length == 0) {
89                 queuedMessages.add(status);
90                 return;
91             }
92         }
93         for (int i = 0; i < listeners.length; i++) {
94             try {
95                 listeners[i].logging(status, IRuntimeConstants.PI_RUNTIME);
96             } catch (Exception JavaDoc e) {
97                 handleException(e);
98             } catch (LinkageError JavaDoc e) {
99                 handleException(e);
100             }
101         }
102     }
103
104     private static void handleException(Throwable JavaDoc e) {
105         if (!(e instanceof OperationCanceledException)) {
106             // Got a error while logging. Don't try to log again, just put it into stderr
107
e.printStackTrace();
108         }
109     }
110
111     /**
112      * Helps determine if any listeners are registered with the logging mechanism.
113      * @return true if no listeners are registered
114      */

115     public static boolean isEmpty() {
116         synchronized (logListeners) {
117             return (logListeners.size() == 0);
118         }
119     }
120
121 }
122
Popular Tags