comparison dwtx/core/internal/jobs/JobListeners.d @ 122:9d0585bcb7aa

Add core.jobs package
author Frank Benoit <benoit@tionex.de>
date Tue, 12 Aug 2008 02:34:21 +0200
parents
children
comparison
equal deleted inserted replaced
121:c0304616ea23 122:9d0585bcb7aa
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.core.internal.jobs.JobListeners;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.core.internal.runtime.RuntimeLog;
18 import dwtx.core.runtime.IStatus;
19 import dwtx.core.runtime.ListenerList;
20 import dwtx.core.runtime.OperationCanceledException;
21 import dwtx.core.runtime.Status;
22 import dwtx.core.runtime.jobs.IJobChangeEvent;
23 import dwtx.core.runtime.jobs.IJobChangeListener;
24 import dwtx.core.runtime.jobs.Job;
25 import dwtx.osgi.util.NLS;
26 import dwtx.core.internal.jobs.JobChangeEvent;
27 import dwtx.core.internal.jobs.InternalJob;
28 import dwtx.core.internal.jobs.JobOSGiUtils;
29 import dwtx.core.internal.jobs.JobManager;
30 import dwtx.core.internal.jobs.JobMessages;
31
32 /**
33 * Responsible for notifying all job listeners about job lifecycle events. Uses a
34 * specialized iterator to ensure the complex iteration logic is contained in one place.
35 */
36 class JobListeners {
37 interface IListenerDoit {
38 public void notify(IJobChangeListener listener, IJobChangeEvent event);
39 }
40
41 private const IListenerDoit aboutToRun_;
42 private const IListenerDoit awake_;
43 private const IListenerDoit done_;
44 private const IListenerDoit running_;
45 private const IListenerDoit scheduled_;
46 private const IListenerDoit sleeping_;
47 /**
48 * The global job listeners.
49 */
50 protected const ListenerList global;
51
52 this(){
53 aboutToRun_ = new class IListenerDoit {
54 public void notify(IJobChangeListener listener, IJobChangeEvent event) {
55 listener.aboutToRun(event);
56 }
57 };
58 awake_ = new class IListenerDoit {
59 public void notify(IJobChangeListener listener, IJobChangeEvent event) {
60 listener.awake(event);
61 }
62 };
63 done_ = new class IListenerDoit {
64 public void notify(IJobChangeListener listener, IJobChangeEvent event) {
65 listener.done(event);
66 }
67 };
68 running_ = new class IListenerDoit {
69 public void notify(IJobChangeListener listener, IJobChangeEvent event) {
70 listener.running(event);
71 }
72 };
73 scheduled_ = new class IListenerDoit {
74 public void notify(IJobChangeListener listener, IJobChangeEvent event) {
75 listener.scheduled(event);
76 }
77 };
78 sleeping_ = new class IListenerDoit {
79 public void notify(IJobChangeListener listener, IJobChangeEvent event) {
80 listener.sleeping(event);
81 }
82 };
83 global = new ListenerList(ListenerList.IDENTITY);
84 }
85
86 /**
87 * TODO Could use an instance pool to re-use old event objects
88 */
89 static JobChangeEvent newEvent(Job job) {
90 JobChangeEvent instance = new JobChangeEvent();
91 instance.job = job;
92 return instance;
93 }
94
95 static JobChangeEvent newEvent(Job job, IStatus result) {
96 JobChangeEvent instance = new JobChangeEvent();
97 instance.job = job;
98 instance.result = result;
99 return instance;
100 }
101
102 static JobChangeEvent newEvent(Job job, long delay) {
103 JobChangeEvent instance = new JobChangeEvent();
104 instance.job = job;
105 instance.delay = delay;
106 return instance;
107 }
108
109 /**
110 * Process the given doit for all global listeners and all local listeners
111 * on the given job.
112 */
113 private void doNotify(IListenerDoit doit, IJobChangeEvent event) {
114 //notify all global listeners
115 Object[] listeners = global.getListeners();
116 int size = listeners.length;
117 for (int i = 0; i < size; i++) {
118 try {
119 if (listeners[i] !is null)
120 doit.notify(cast(IJobChangeListener) listeners[i], event);
121 } catch (Exception e) {
122 handleException(listeners[i], e);
123 // } catch (LinkageError e) {
124 // handleException(listeners[i], e);
125 }
126 }
127 //notify all local listeners
128 ListenerList list = (cast(InternalJob) event.getJob()).getListeners();
129 listeners = list is null ? null : list.getListeners();
130 if (listeners is null)
131 return;
132 size = listeners.length;
133 for (int i = 0; i < size; i++) {
134 try {
135 if (listeners[i] !is null)
136 doit.notify(cast(IJobChangeListener) listeners[i], event);
137 } catch (Exception e) {
138 handleException(listeners[i], e);
139 // } catch (LinkageError e) {
140 // handleException(listeners[i], e);
141 }
142 }
143 }
144
145 private void handleException(Object listener, Exception e) {
146 //this code is roughly copied from InternalPlatform.run(ISafeRunnable),
147 //but in-lined here for performance reasons
148 if (cast(OperationCanceledException)e )
149 return;
150 String pluginId = JobOSGiUtils.getDefault().getBundleId(listener);
151 if (pluginId is null)
152 pluginId = JobManager.PI_JOBS;
153 String message = NLS.bind(JobMessages.meta_pluginProblems, pluginId);
154 RuntimeLog.log(new Status(IStatus.ERROR, pluginId, JobManager.PLUGIN_ERROR, message, e));
155 }
156
157 public void add(IJobChangeListener listener) {
158 global.add(cast(Object)listener);
159 }
160
161 public void remove(IJobChangeListener listener) {
162 global.remove(cast(Object)listener);
163 }
164
165 public void aboutToRun(Job job) {
166 doNotify(aboutToRun_, newEvent(job));
167 }
168
169 public void awake(Job job) {
170 doNotify(awake_, newEvent(job));
171 }
172
173 public void done(Job job, IStatus result, bool reschedule) {
174 JobChangeEvent event = newEvent(job, result);
175 event.reschedule = reschedule;
176 doNotify(done_, event);
177 }
178
179 public void running(Job job) {
180 doNotify(running_, newEvent(job));
181 }
182
183 public void scheduled(Job job, long delay, bool reschedule) {
184 JobChangeEvent event = newEvent(job, delay);
185 event.reschedule = reschedule;
186 doNotify(scheduled_, event);
187 }
188
189 public void sleeping(Job job) {
190 doNotify(sleeping_, newEvent(job));
191 }
192 }