1 8 package org.codehaus.spice.netserve.connection.handlers; 9 10 import java.io.IOException ; 11 import java.net.Socket ; 12 import java.util.HashSet ; 13 import java.util.Set ; 14 import org.codehaus.spice.netserve.connection.RequestHandler; 15 16 22 public abstract class AbstractRequestHandler 23 implements RequestHandler 24 { 25 28 private final Set m_activeRequests = new HashSet (); 29 30 33 private boolean m_shutdown; 34 35 40 public void handleConnection( Socket socket ) 41 { 42 performRequest( socket ); 43 } 44 45 48 public void shutdown( final long timeout ) 49 { 50 markAsShutdown(); 51 final Thread [] threads; 52 synchronized( this ) 53 { 54 threads = (Thread [])m_activeRequests. 55 toArray( new Thread [ m_activeRequests.size() ] ); 56 } 57 for( int i = 0; i < threads.length; i++ ) 58 { 59 final Thread thread = threads[ i ]; 60 thread.interrupt(); 61 } 62 final long now = System.currentTimeMillis(); 63 final long then = now + timeout; 64 65 while( System.currentTimeMillis() < then || 0 == timeout ) 66 { 67 synchronized( this ) 68 { 69 if( 0 == m_activeRequests.size() ) 70 { 71 return; 72 } 73 try 74 { 75 wait( timeout ); 76 } 77 catch( final InterruptedException ie ) 78 { 79 } 81 } 82 } 83 } 84 85 88 protected void markAsShutdown() 89 { 90 m_shutdown = true; 91 } 92 93 98 protected boolean isShutdown() 99 { 100 return m_shutdown; 101 } 102 103 109 protected void performRequest( final Socket socket ) 110 { 111 synchronized( this ) 112 { 113 m_activeRequests.add( Thread.currentThread() ); 114 } 115 setupThreadName( socket ); 116 try 117 { 118 doPerformRequest( socket ); 119 } 120 catch( final Throwable t ) 121 { 122 errorHandlingConnection( socket, t ); 123 } 124 finally 125 { 126 endConnection( socket ); 127 synchronized( this ) 128 { 129 m_activeRequests.remove( Thread.currentThread() ); 130 notifyAll(); 131 } 132 } 133 } 134 135 141 protected abstract void doPerformRequest( Socket socket ) 142 throws Exception ; 143 144 149 protected void setupThreadName( final Socket socket ) 150 { 151 final String name = getThreadName( socket ); 152 Thread.currentThread().setName( name ); 153 } 154 155 160 protected void endConnection( final Socket socket ) 161 { 162 if( socket.isConnected() ) 163 { 164 try 165 { 166 socket.close(); 167 } 168 catch( final IOException ioe ) 169 { 170 errorClosingConnection( socket, ioe ); 171 } 172 } 173 } 174 175 181 protected Runnable createRunnable( final Socket socket ) 182 { 183 return new Runnable () 184 { 185 public void run() 186 { 187 performRequest( socket ); 188 } 189 }; 190 } 191 192 198 protected String getThreadName( final Socket socket ) 199 { 200 if( socket.isConnected() ) 201 { 202 return "RequestHandler for " + 203 socket.getInetAddress().getHostAddress() + ":" + 204 socket.getPort(); 205 } 206 else 207 { 208 return "RequestHandler for " + socket; 209 } 210 } 211 212 218 protected void errorHandlingConnection( final Socket socket, 219 final Throwable t ) 220 { 221 } 222 223 229 protected void errorClosingConnection( final Socket socket, 230 final Throwable t ) 231 { 232 errorHandlingConnection( socket, t ); 233 } 234 } 235 | Popular Tags |