KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > jobs > JobListeners


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.jobs;
12
13 import org.eclipse.core.internal.runtime.RuntimeLog;
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.core.runtime.jobs.*;
16 import org.eclipse.osgi.util.NLS;
17
18 /**
19  * Responsible for notifying all job listeners about job lifecycle events. Uses a
20  * specialized iterator to ensure the complex iteration logic is contained in one place.
21  */

22 class JobListeners {
23     interface IListenerDoit {
24         public void notify(IJobChangeListener listener, IJobChangeEvent event);
25     }
26
27     private final IListenerDoit aboutToRun = new IListenerDoit() {
28         public void notify(IJobChangeListener listener, IJobChangeEvent event) {
29             listener.aboutToRun(event);
30         }
31     };
32     private final IListenerDoit awake = new IListenerDoit() {
33         public void notify(IJobChangeListener listener, IJobChangeEvent event) {
34             listener.awake(event);
35         }
36     };
37     private final IListenerDoit done = new IListenerDoit() {
38         public void notify(IJobChangeListener listener, IJobChangeEvent event) {
39             listener.done(event);
40         }
41     };
42     private final IListenerDoit running = new IListenerDoit() {
43         public void notify(IJobChangeListener listener, IJobChangeEvent event) {
44             listener.running(event);
45         }
46     };
47     private final IListenerDoit scheduled = new IListenerDoit() {
48         public void notify(IJobChangeListener listener, IJobChangeEvent event) {
49             listener.scheduled(event);
50         }
51     };
52     private final IListenerDoit sleeping = new IListenerDoit() {
53         public void notify(IJobChangeListener listener, IJobChangeEvent event) {
54             listener.sleeping(event);
55         }
56     };
57     /**
58      * The global job listeners.
59      */

60     protected final ListenerList global = new ListenerList(ListenerList.IDENTITY);
61
62     /**
63      * TODO Could use an instance pool to re-use old event objects
64      */

65     static JobChangeEvent newEvent(Job job) {
66         JobChangeEvent instance = new JobChangeEvent();
67         instance.job = job;
68         return instance;
69     }
70
71     static JobChangeEvent newEvent(Job job, IStatus result) {
72         JobChangeEvent instance = new JobChangeEvent();
73         instance.job = job;
74         instance.result = result;
75         return instance;
76     }
77
78     static JobChangeEvent newEvent(Job job, long delay) {
79         JobChangeEvent instance = new JobChangeEvent();
80         instance.job = job;
81         instance.delay = delay;
82         return instance;
83     }
84
85     /**
86      * Process the given doit for all global listeners and all local listeners
87      * on the given job.
88      */

89     private void doNotify(final IListenerDoit doit, final IJobChangeEvent event) {
90         //notify all global listeners
91
Object JavaDoc[] listeners = global.getListeners();
92         int size = listeners.length;
93         for (int i = 0; i < size; i++) {
94             try {
95                 if (listeners[i] != null)
96                     doit.notify((IJobChangeListener) listeners[i], event);
97             } catch (Exception JavaDoc e) {
98                 handleException(listeners[i], e);
99             } catch (LinkageError JavaDoc e) {
100                 handleException(listeners[i], e);
101             }
102         }
103         //notify all local listeners
104
ListenerList list = ((InternalJob) event.getJob()).getListeners();
105         listeners = list == null ? null : list.getListeners();
106         if (listeners == null)
107             return;
108         size = listeners.length;
109         for (int i = 0; i < size; i++) {
110             try {
111                 if (listeners[i] != null)
112                     doit.notify((IJobChangeListener) listeners[i], event);
113             } catch (Exception JavaDoc e) {
114                 handleException(listeners[i], e);
115             } catch (LinkageError JavaDoc e) {
116                 handleException(listeners[i], e);
117             }
118         }
119     }
120
121     private void handleException(Object JavaDoc listener, Throwable JavaDoc e) {
122         //this code is roughly copied from InternalPlatform.run(ISafeRunnable),
123
//but in-lined here for performance reasons
124
if (e instanceof OperationCanceledException)
125             return;
126         String JavaDoc pluginId = JobOSGiUtils.getDefault().getBundleId(listener);
127         if (pluginId == null)
128             pluginId = JobManager.PI_JOBS;
129         String JavaDoc message = NLS.bind(JobMessages.meta_pluginProblems, pluginId);
130         RuntimeLog.log(new Status(IStatus.ERROR, pluginId, JobManager.PLUGIN_ERROR, message, e));
131     }
132
133     public void add(IJobChangeListener listener) {
134         global.add(listener);
135     }
136
137     public void remove(IJobChangeListener listener) {
138         global.remove(listener);
139     }
140
141     public void aboutToRun(Job job) {
142         doNotify(aboutToRun, newEvent(job));
143     }
144
145     public void awake(Job job) {
146         doNotify(awake, newEvent(job));
147     }
148
149     public void done(Job job, IStatus result, boolean reschedule) {
150         JobChangeEvent event = newEvent(job, result);
151         event.reschedule = reschedule;
152         doNotify(done, event);
153     }
154
155     public void running(Job job) {
156         doNotify(running, newEvent(job));
157     }
158
159     public void scheduled(Job job, long delay, boolean reschedule) {
160         JobChangeEvent event = newEvent(job, delay);
161         event.reschedule = reschedule;
162         doNotify(scheduled, event);
163     }
164
165     public void sleeping(Job job) {
166         doNotify(sleeping, newEvent(job));
167     }
168 }
169
Popular Tags