1 17 18 package tutorial; 19 20 import java.net.ServerSocket ; 21 22 import org.apache.avalon.cornerstone.services.connection. 23 ConnectionHandlerFactory; 24 import org.apache.avalon.cornerstone.services.connection.ConnectionManager; 25 import org.apache.avalon.cornerstone.services.sockets.ServerSocketFactory; 26 import org.apache.avalon.cornerstone.services.sockets.SocketManager; 27 import org.apache.avalon.framework.activity.Disposable; 28 import org.apache.avalon.framework.activity.Executable; 29 import org.apache.avalon.framework.configuration.Configurable; 30 import org.apache.avalon.framework.configuration.Configuration; 31 import org.apache.avalon.framework.configuration.ConfigurationException; 32 import org.apache.avalon.framework.logger.LogEnabled; 33 import org.apache.avalon.framework.logger.Logger; 34 import org.apache.avalon.framework.service.ServiceException; 35 import org.apache.avalon.framework.service.ServiceManager; 36 import org.apache.avalon.framework.service.Serviceable; 37 38 47 public class SimpleWebServerComponent 48 implements LogEnabled, Serviceable, Configurable, Executable, Disposable { 49 52 private Logger m_logger; 53 56 private SocketManager m_socketManager; 57 60 private ConnectionManager m_connectionManager; 61 64 private ConnectionHandlerFactory m_connectionHandlerFactory; 65 69 private int m_port; 70 73 private final static String HTTP_LISTENER = "http-listener"; 74 75 82 public void enableLogging(Logger logger) { 83 m_logger = logger; 84 getLogger().info("logging"); 85 } 86 87 101 public void service(ServiceManager manager) throws ServiceException { 102 m_socketManager = (SocketManager) manager.lookup("socket-manager"); 103 m_connectionManager = (ConnectionManager) manager.lookup( 104 "connection-manager"); 105 m_connectionHandlerFactory = (ConnectionHandlerFactory) manager.lookup("connection-handler-factory"); 106 } 107 108 118 public void configure(Configuration config) throws ConfigurationException { 119 getLogger().info("Configuring..."); 120 121 Configuration httpConfig = config.getChild("http-listener", true); 123 if (httpConfig == null) { 124 throw new ConfigurationException("port attribute not found!"); 125 } 126 else { 127 m_port = httpConfig.getAttributeAsInteger("port", 80); 128 } 129 } 130 131 137 public void execute() throws Exception { 138 ServerSocketFactory ssf = m_socketManager.getServerSocketFactory("plain"); 141 142 ServerSocket serverSocket = ssf.createServerSocket(m_port); 144 145 m_connectionManager.connect(HTTP_LISTENER, serverSocket, m_connectionHandlerFactory); 147 148 getLogger().info("Started HTTP listener socket on port {" + m_port + "}"); 150 } 151 152 158 public void dispose() { 159 try { 160 m_connectionManager.disconnect(HTTP_LISTENER, true); 162 } 163 catch (Exception e) { 164 getLogger().error("Unexpected error while shutting down HTTP listener", e); 165 } 166 m_connectionManager = null; 167 m_socketManager = null; 168 } 169 170 174 private Logger getLogger() { 175 return m_logger; 176 } 177 } 178 | Popular Tags |