1 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 24 public class ThreadPerRequestHandler 25 extends DelegatingRequestHandler 26 { 27 30 private final Map m_controlMap = new Hashtable (); 31 32 35 private final ThreadPool m_threadPool; 36 37 43 public ThreadPerRequestHandler( final RequestHandler handler, 44 final ThreadPool threadPool ) 45 { 46 super( handler ); 47 if( null == threadPool ) 48 { 49 throw new NullPointerException ( "threadPool" ); 50 } 51 m_threadPool = threadPool; 52 } 53 54 59 public void handleConnection( final Socket socket ) 60 { 61 final Runnable runnable = createRunnable( socket ); 62 final ThreadControl control = m_threadPool.execute( runnable ); 63 m_controlMap.put( socket, control ); 64 } 65 66 71 protected void endConnection( Socket socket ) 72 { 73 m_controlMap.remove( socket ); 74 super.endConnection( socket ); 75 } 76 77 82 public void shutdown( final long timeout ) 83 { 84 markAsShutdown(); 85 final ThreadControl[] controls; 86 synchronized( m_controlMap ) 87 { 88 final Collection collection = m_controlMap.values(); 89 controls = (ThreadControl[])collection. 90 toArray( new ThreadControl[ collection.size() ] ); 91 } 92 for( int i = 0; i < controls.length; i++ ) 93 { 94 final ThreadControl control = controls[ i ]; 95 if( !control.isFinished() ) 96 { 97 control.interrupt(); 98 } 99 } 100 super.shutdown( timeout ); 101 for( int i = 0; i < controls.length; i++ ) 102 { 103 final ThreadControl control = controls[ i ]; 104 if( !control.isFinished() ) 105 { 106 try 107 { 108 control.join( timeout ); 109 } 110 catch( final InterruptedException ie ) 111 { 112 } 114 } 115 } 116 } 117 } 118 | Popular Tags |