1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.core.lang.ri.executor;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import javax.swing.event.EventListenerList;
26 import org.jdtaus.core.container.ContainerFactory;
27 import org.jdtaus.core.lang.ExceptionEvent;
28 import org.jdtaus.core.lang.ExceptionListener;
29 import org.jdtaus.core.lang.spi.Executor;
30
31
32
33
34
35
36
37
38
39 public class Jdk14Executor implements Executor
40 {
41
42
43
44
45
46
47 public Jdk14Executor()
48 {
49 super();
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 private java.lang.String getDefaultThreadGroupName()
66 {
67 return (java.lang.String) ContainerFactory.getContainer().
68 getProperty( this, "defaultThreadGroupName" );
69
70 }
71
72
73
74
75
76
77
78 private final EventListenerList listeners = new EventListenerList();
79
80 public void addExceptionListener( final ExceptionListener listener )
81 {
82 if ( listener == null )
83 {
84 throw new NullPointerException( "listener" );
85 }
86
87 synchronized ( this.listeners )
88 {
89 this.listeners.add( ExceptionListener.class, listener );
90 }
91 }
92
93 public void removeExceptionListener( final ExceptionListener listener )
94 {
95 if ( listener == null )
96 {
97 throw new NullPointerException( "listener" );
98 }
99
100 synchronized ( this.listeners )
101 {
102 this.listeners.remove( ExceptionListener.class, listener );
103 }
104 }
105
106 public ExceptionListener[] getExceptionListeners()
107 {
108 synchronized ( this.listeners )
109 {
110 return (ExceptionListener[]) this.listeners.getListeners(
111 ExceptionListener.class );
112
113 }
114 }
115
116
117
118
119
120 private final List handledExceptions = new ArrayList( 255 );
121
122 public ExceptionEvent[] getExceptionEvents()
123 {
124 synchronized ( this.handledExceptions )
125 {
126 return (ExceptionEvent[]) this.handledExceptions.toArray(
127 new ExceptionEvent[ this.handledExceptions.size() ] );
128
129 }
130 }
131
132 public void handle( final Throwable t )
133 {
134 if ( t == null )
135 {
136 throw new NullPointerException( "t" );
137 }
138
139 final ExceptionEvent evt =
140 new ExceptionEvent( this, Thread.currentThread(), t );
141
142 this.fireOnException( evt );
143 }
144
145
146
147
148 public void executeAsynchronously( final Runnable runnable )
149 {
150 if ( runnable == null )
151 {
152 throw new NullPointerException( "runnable" );
153 }
154
155 new Thread( this.getThreadGroup(), runnable ).start();
156 }
157
158
159
160
161
162 private String threadGroupName;
163
164
165
166
167
168 private ThreadGroup threadGroup;
169
170
171
172
173
174
175
176 public Jdk14Executor( final String threadGroupName )
177 {
178 if ( threadGroupName != null )
179 {
180 this.threadGroupName = threadGroupName;
181 }
182 }
183
184
185
186
187 private final class ThreadGroup extends java.lang.ThreadGroup
188 {
189
190
191
192
193
194
195 ThreadGroup( final String name )
196 {
197 super( name );
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211 public void uncaughtException( final Thread t, final Throwable e )
212 {
213 if ( e instanceof ThreadDeath )
214 {
215 super.uncaughtException( t, e );
216 }
217 else
218 {
219 fireOnException(
220 new ExceptionEvent( Jdk14Executor.this, t, e ) );
221
222 }
223 }
224
225 }
226
227
228
229
230
231
232 private String getThreadGroupName()
233 {
234 if ( this.threadGroupName == null )
235 {
236 this.threadGroupName = this.getDefaultThreadGroupName();
237 }
238
239 return this.threadGroupName;
240 }
241
242
243
244
245
246
247
248
249 private ThreadGroup getThreadGroup()
250 {
251 if ( this.threadGroup == null )
252 {
253 this.threadGroup =
254 new Jdk14Executor.ThreadGroup( this.getThreadGroupName() );
255
256 }
257
258 return this.threadGroup;
259 }
260
261
262
263
264
265
266
267
268
269 private void fireOnException( final ExceptionEvent e )
270 {
271 if ( e == null )
272 {
273 throw new NullPointerException( "e" );
274 }
275
276 synchronized ( this.handledExceptions )
277 {
278 this.handledExceptions.add( e );
279 }
280
281 synchronized ( this.listeners )
282 {
283 final Object[] list = this.listeners.getListenerList();
284 for ( int i = list.length - 2; i >= 0; i -= 2 )
285 {
286 if ( list[i] == ExceptionListener.class )
287 {
288 ( (ExceptionListener) list[i + 1] ).onException( e );
289 }
290 }
291 }
292
293 final ExceptionListener[] exceptionListener =
294 this.getExceptionListener();
295
296 for ( int i = exceptionListener.length - 1; i >= 0; i-- )
297 {
298 exceptionListener[i].onException( e );
299 }
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313 private ExceptionListener[] getExceptionListener()
314 {
315 return (ExceptionListener[]) ContainerFactory.getContainer().
316 getDependency( this, "ExceptionListener" );
317
318 }
319
320
321
322
323 }