|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.codehaus.spice.netserve.connection.handlers; |
|
9 |
| |
|
10 |
| import java.net.Socket; |
|
11 |
| import java.util.Map; |
|
12 |
| import java.util.Hashtable; |
|
13 |
| import java.util.Collection; |
|
14 |
| import org.codehaus.spice.netserve.connection.RequestHandler; |
|
15 |
| import org.codehaus.spice.threadpool.ThreadPool; |
|
16 |
| import org.codehaus.spice.threadpool.ThreadControl; |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| public class ThreadPerRequestHandler |
|
25 |
| extends DelegatingRequestHandler |
|
26 |
| { |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| private final Map m_controlMap = new Hashtable(); |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| private final ThreadPool m_threadPool; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
15
| public ThreadPerRequestHandler( final RequestHandler handler,
|
|
44 |
| final ThreadPool threadPool ) |
|
45 |
| { |
|
46 |
15
| super( handler );
|
|
47 |
15
| if( null == threadPool )
|
|
48 |
| { |
|
49 |
3
| throw new NullPointerException( "threadPool" );
|
|
50 |
| } |
|
51 |
12
| m_threadPool = threadPool;
|
|
52 |
| } |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
12
| public void handleConnection( final Socket socket )
|
|
60 |
| { |
|
61 |
12
| final Runnable runnable = createRunnable( socket );
|
|
62 |
12
| final ThreadControl control = m_threadPool.execute( runnable );
|
|
63 |
12
| m_controlMap.put( socket, control );
|
|
64 |
| } |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
6
| protected void endConnection( Socket socket )
|
|
72 |
| { |
|
73 |
6
| m_controlMap.remove( socket );
|
|
74 |
6
| super.endConnection( socket );
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
9
| public void shutdown( final long timeout )
|
|
83 |
| { |
|
84 |
9
| markAsShutdown();
|
|
85 |
9
| final ThreadControl[] controls;
|
|
86 |
9
| synchronized( m_controlMap )
|
|
87 |
| { |
|
88 |
9
| final Collection collection = m_controlMap.values();
|
|
89 |
9
| controls = (ThreadControl[])collection.
|
|
90 |
| toArray( new ThreadControl[ collection.size() ] ); |
|
91 |
| } |
|
92 |
9
| for( int i = 0; i < controls.length; i++ )
|
|
93 |
| { |
|
94 |
9
| final ThreadControl control = controls[ i ];
|
|
95 |
9
| if( !control.isFinished() )
|
|
96 |
| { |
|
97 |
9
| control.interrupt();
|
|
98 |
| } |
|
99 |
| } |
|
100 |
9
| super.shutdown( timeout );
|
|
101 |
9
| for( int i = 0; i < controls.length; i++ )
|
|
102 |
| { |
|
103 |
9
| final ThreadControl control = controls[ i ];
|
|
104 |
9
| if( !control.isFinished() )
|
|
105 |
| { |
|
106 |
9
| try
|
|
107 |
| { |
|
108 |
9
| control.join( timeout );
|
|
109 |
| } |
|
110 |
| catch( final InterruptedException ie ) |
|
111 |
| { |
|
112 |
| |
|
113 |
| } |
|
114 |
| } |
|
115 |
| } |
|
116 |
| } |
|
117 |
| } |