1 17 18 package org.apache.avalon.cornerstone.blocks.connection; 19 20 import java.net.ServerSocket ; 21 import java.util.HashMap ; 22 import org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory; 23 import org.apache.avalon.cornerstone.services.connection.ConnectionManager; 24 import org.apache.avalon.cornerstone.services.threads.ThreadManager; 25 import org.apache.excalibur.thread.ThreadPool; 26 import org.apache.avalon.framework.activity.Disposable; 27 import org.apache.avalon.framework.logger.AbstractLogEnabled; 28 import org.apache.avalon.framework.service.ServiceException; 29 import org.apache.avalon.framework.service.ServiceManager; 30 import org.apache.avalon.framework.service.Serviceable; 31 32 39 public class DefaultConnectionManager 40 extends AbstractLogEnabled 41 implements ConnectionManager, Serviceable, Disposable 42 { 43 private final HashMap m_connections = new HashMap (); 44 private ThreadManager m_threadManager; 45 46 49 public void service( final ServiceManager serviceManager ) 50 throws ServiceException 51 { 52 m_threadManager = (ThreadManager)serviceManager.lookup( ThreadManager.ROLE ); 53 } 54 55 public void dispose() 56 { 57 if( getLogger().isDebugEnabled() ) 58 { 59 getLogger().debug( "disposal" ); 60 } 61 final String [] names = (String [])m_connections.keySet().toArray( new String [ 0 ] ); 62 for( int i = 0; i < names.length; i++ ) 63 { 64 try 65 { 66 disconnect( names[ i ] ); 67 } 68 catch( final Exception e ) 69 { 70 final String message = "Error disconnecting " + names[ i ]; 71 getLogger().warn( message, e ); 72 } 73 } 74 } 75 76 87 public synchronized void connect( String name, 88 ServerSocket socket, 89 ConnectionHandlerFactory handlerFactory, 90 ThreadPool threadPool ) 91 throws Exception 92 { 93 if( null != m_connections.get( name ) ) 94 { 95 final String message = "Connection already exists with name " + name; 96 throw new IllegalArgumentException ( message ); 97 } 98 99 if( 0 == socket.getSoTimeout() ) 101 { 102 socket.setSoTimeout( 500 ); 103 } 104 105 final Connection runner = 106 new Connection( socket, handlerFactory, threadPool ); 107 setupLogger( runner ); 108 m_connections.put( name, runner ); 109 threadPool.execute( runner ); 110 } 111 112 121 public void connect( String name, 122 ServerSocket socket, 123 ConnectionHandlerFactory handlerFactory ) 124 throws Exception 125 { 126 connect( name, socket, handlerFactory, m_threadManager.getDefaultThreadPool() ); 127 } 128 129 135 public void disconnect( final String name ) 136 throws Exception 137 { 138 disconnect( name, false ); 139 } 140 141 151 public synchronized void disconnect( final String name, final boolean tearDown ) 152 throws Exception 153 { 154 final Connection connection = (Connection)m_connections.remove( name ); 155 156 if( connection != null ) 157 { 158 connection.dispose(); 160 } 161 else 162 { 163 final String error = 164 "Invalid request for the disconnection of an unrecognized connection name: " 165 + name; 166 throw new IllegalArgumentException ( error ); 167 } 168 } 169 } 170 | Popular Tags |