KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

11 package org.eclipse.core.internal.runtime;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15 import org.eclipse.core.runtime.*;
16 import org.osgi.framework.Bundle;
17
18 /**
19  *
20  */

21 public class Log implements ILog {
22     Bundle bundle;
23     Set JavaDoc logListeners = new HashSet JavaDoc(5);
24
25     public Log(Bundle plugin) {
26         this.bundle = plugin;
27     }
28
29     /**
30      * Adds the given log listener to this log. Subsequently the log listener will
31      * receive notification of all log events passing through this log.
32      *
33      * @see Platform#addLogListener(ILogListener)
34      */

35     public void addLogListener(ILogListener listener) {
36         synchronized (logListeners) {
37             logListeners.add(listener);
38         }
39     }
40
41     /**
42      * Returns the plug-in with which this log is associated.
43      */

44     public Bundle getBundle() {
45         return bundle;
46     }
47
48     /**
49      * Logs the given status. The status is distributed to the log listeners
50      * installed on this log and then to the log listeners installed on the platform.
51      *
52      * @see Plugin#getLog()
53      */

54     public void log(final IStatus status) {
55         // Log to the platform log first in case a listener throws an error.
56
InternalPlatform.getDefault().log(status);
57         // create array to avoid concurrent access
58
ILogListener[] listeners;
59         synchronized (logListeners) {
60             listeners = (ILogListener[]) logListeners.toArray(new ILogListener[logListeners.size()]);
61         }
62         for (int i = 0; i < listeners.length; i++) {
63             final ILogListener listener = listeners[i];
64             ISafeRunnable code = new ISafeRunnable() {
65                 public void run() throws Exception JavaDoc {
66                     listener.logging(status, bundle.getSymbolicName());
67                 }
68
69                 public void handleException(Throwable JavaDoc e) {
70                     //Ignore
71
}
72             };
73             SafeRunner.run(code);
74         }
75     }
76
77     /**
78      * Removes the given log listener to this log. Subsequently the log listener will
79      * no longer receive notification of log events passing through this log.
80      *
81      * @see Platform#removeLogListener(ILogListener)
82      */

83     public void removeLogListener(ILogListener listener) {
84         synchronized (logListeners) {
85             logListeners.remove(listener);
86         }
87     }
88 }
89
Popular Tags