1 17 18 package org.apache.avalon.cornerstone.services.connection; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.net.ServerSocket ; 23 24 import org.apache.avalon.cornerstone.services.sockets.ServerSocketFactory; 25 import org.apache.avalon.cornerstone.services.sockets.SocketManager; 26 import org.apache.avalon.cornerstone.services.threads.ThreadManager; 27 28 import org.apache.excalibur.thread.ThreadPool; 29 30 import org.apache.avalon.framework.activity.Disposable; 31 import org.apache.avalon.framework.activity.Initializable; 32 import org.apache.avalon.framework.component.ComponentException; 33 import org.apache.avalon.framework.component.WrapperComponentManager; 34 import org.apache.avalon.framework.configuration.Configurable; 35 import org.apache.avalon.framework.configuration.Configuration; 36 import org.apache.avalon.framework.configuration.ConfigurationException; 37 import org.apache.avalon.framework.container.ContainerUtil; 38 import org.apache.avalon.framework.context.Context; 39 import org.apache.avalon.framework.context.ContextException; 40 import org.apache.avalon.framework.context.Contextualizable; 41 import org.apache.avalon.framework.logger.AbstractLogEnabled; 42 import org.apache.avalon.framework.logger.Logger; 43 import org.apache.avalon.framework.service.ServiceException; 44 import org.apache.avalon.framework.service.ServiceManager; 45 import org.apache.avalon.framework.service.Serviceable; 46 47 52 public abstract class AbstractService 53 extends AbstractLogEnabled 54 implements Contextualizable, Serviceable, Configurable, Initializable, Disposable 55 { 56 protected ConnectionManager m_connectionManager; 57 protected SocketManager m_socketManager; 58 protected ConnectionHandlerFactory m_factory; 59 protected ThreadManager m_threadManager; 60 protected ThreadPool m_threadPool; 61 protected String m_serverSocketType; 62 protected int m_port; 63 protected InetAddress m_bindTo; protected ServerSocket m_serverSocket; 65 protected String m_connectionName; 66 67 public AbstractService() 68 { 69 m_factory = createFactory(); 70 m_serverSocketType = "plain"; 71 } 72 73 protected String getThreadPoolName() 74 { 75 return null; 76 } 77 78 protected abstract ConnectionHandlerFactory createFactory(); 79 80 public void enableLogging( final Logger logger ) 81 { 82 super.enableLogging( logger ); 83 ContainerUtil.enableLogging( m_factory, logger ); 84 } 85 86 public void contextualize( final Context context ) 87 throws ContextException 88 { 89 ContainerUtil.contextualize( m_factory, context ); 90 } 91 92 public void service( final ServiceManager serviceManager ) 93 throws ServiceException 94 { 95 m_connectionManager = (ConnectionManager)serviceManager.lookup( ConnectionManager.ROLE ); 96 m_socketManager = (SocketManager)serviceManager.lookup( SocketManager.ROLE ); 97 if( null != getThreadPoolName() ) 98 { 99 m_threadManager = 100 (ThreadManager)serviceManager.lookup( ThreadManager.ROLE ); 101 m_threadPool = m_threadManager.getThreadPool( getThreadPoolName() ); 102 } 103 ContainerUtil.service( m_factory, serviceManager ); 104 try 105 { 106 ContainerUtil.compose( m_factory, new WrapperComponentManager( serviceManager ) ); 107 } 108 catch( final ComponentException ce ) 109 { 110 throw new ServiceException( ConnectionHandlerFactory.class.getName(), ce.getMessage(), ce ); 111 } 112 } 113 114 public void configure( final Configuration configuration ) 115 throws ConfigurationException 116 { 117 ContainerUtil.configure( m_factory, configuration ); 118 } 119 120 public void initialize() 121 throws Exception 122 { 123 ContainerUtil.initialize( m_factory ); 124 125 if( null == m_connectionName ) 126 { 127 final StringBuffer sb = new StringBuffer (); 128 sb.append( m_serverSocketType ); 129 sb.append( ':' ); 130 sb.append( m_port ); 131 132 if( null != m_bindTo ) 133 { 134 sb.append( '/' ); 135 sb.append( m_bindTo ); 136 } 137 138 m_connectionName = sb.toString(); 139 } 140 141 final ServerSocketFactory factory = 142 m_socketManager.getServerSocketFactory( m_serverSocketType ); 143 144 if( null == m_bindTo ) 145 { 146 m_serverSocket = factory.createServerSocket( m_port ); 147 } 148 else 149 { 150 m_serverSocket = factory.createServerSocket( m_port, 5, m_bindTo ); 151 } 152 153 if( null == m_threadPool ) 154 { 155 m_connectionManager.connect( m_connectionName, m_serverSocket, 156 m_factory ); 157 } 158 else 159 { 160 m_connectionManager.connect( m_connectionName, m_serverSocket, 161 m_factory, m_threadPool ); 162 } 163 } 164 165 public void dispose() 166 { 167 try 168 { 169 m_connectionManager.disconnect( m_connectionName ); 170 } 171 catch( final Exception e ) 172 { 173 final String message = "Error disconnecting"; 174 getLogger().warn( message, e ); 175 } 176 177 try 178 { 179 m_serverSocket.close(); 180 } 181 catch( final IOException ioe ) 182 { 183 final String message = "Error closing server socket"; 184 getLogger().warn( message, ioe ); 185 } 186 } 187 } 188 | Popular Tags |