KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tutorial > SimpleWebServerComponent


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package tutorial;
19
20 import java.net.ServerSocket JavaDoc;
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 /**
39  * A tutorial that demonstrates a simple web server component for merlin that
40  * uses the <code>SocketManager</code> and <code>ConnectionManager</code>
41  * components in the cornerstone and excalibur component packages.
42  *
43  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
44  *
45  * @avalon.component version="1.0" name="simple-web-server"
46  */

47 public class SimpleWebServerComponent
48     implements LogEnabled, Serviceable, Configurable, Executable, Disposable {
49   /**
50    * Internal reference to the logging channel supplied by the container.
51    */

52   private Logger m_logger;
53   /**
54    * Internal reference to the socket manager supplied by the container.
55    */

56   private SocketManager m_socketManager;
57   /**
58    * Internal reference to the connection manager supplied by the container.
59    */

60   private ConnectionManager m_connectionManager;
61   /**
62    * Internal reference to connection handler factory supplied by container
63    */

64   private ConnectionHandlerFactory m_connectionHandlerFactory;
65   /**
66    * Internal reference to the web server's listening port supplied
67    * by the conponent configuration. Defaults to 80.
68    */

69   private int m_port;
70   /**
71    * Alias for the name of our HTTP connection handler.
72    */

73   private final static String JavaDoc HTTP_LISTENER = "http-listener";
74
75   /**
76    * Supply of a logging channel by the container.
77    *
78    * @param logger the logging channel for this component
79    *
80    * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
81    */

82   public void enableLogging(Logger logger) {
83     m_logger = logger;
84     getLogger().info("logging");
85   }
86
87   /**
88    * Servicing of the component by the container during which service
89    * dependencies declared under the component can be resolved using the
90    * supplied service manager.
91    *
92    * @param manager the service manager
93        * @throws ServiceException if an failure occurs while servicing the component
94    *
95    * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
96    *
97    * @avalon.dependency type="org.apache.avalon.cornerstone.services.sockets.SocketManager:1.0" key="socket-manager"
98    * @avalon.dependency type="org.apache.avalon.cornerstone.services.connection.ConnectionManager:1.0" key="connection-manager"
99    * @avalon.dependency type="org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory:1.0" key="connection-handler-factory"
100    */

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   /**
109    * Configuration of the component by the container.
110    *
111    * TODO: Describe the configuration of the component.
112    *
113    * @param config the component configuration
114    * @throws ConfigurationException if a configuration error occurs
115    *
116    * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
117    */

118   public void configure(Configuration config) throws ConfigurationException {
119     getLogger().info("Configuring...");
120
121     // Add an HTTP socket listener?
122
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   /**
132    * Component execution trigger by the container following
133    * completion of the initialization stage.
134    *
135    * @see org.apache.avalon.framework.activity.Executable#execute()
136    */

137   public void execute() throws Exception JavaDoc {
138     // Use the Cornerstone SocketManager to give us a factory object for creating
139
// a plain ole server socket
140
ServerSocketFactory ssf = m_socketManager.getServerSocketFactory("plain");
141
142     // Use that factory to create a server socket
143
ServerSocket JavaDoc serverSocket = ssf.createServerSocket(m_port);
144
145     // attach the server socket to our connection manager
146
m_connectionManager.connect(HTTP_LISTENER, serverSocket, m_connectionHandlerFactory);
147
148     // we've started!
149
getLogger().info("Started HTTP listener socket on port {" + m_port + "}");
150   }
151
152   /**
153    * Component disposal trigger by the container during which
154    * the component will release consumed resources.
155    *
156    * @see org.apache.avalon.framework.activity.Disposable#dispose()
157    */

158   public void dispose() {
159     try {
160       // forcefully tear down all handlers...
161
m_connectionManager.disconnect(HTTP_LISTENER, true);
162     }
163     catch (Exception JavaDoc e) {
164       getLogger().error("Unexpected error while shutting down HTTP listener", e);
165     }
166     m_connectionManager = null;
167     m_socketManager = null;
168   }
169
170   /**
171    * Return the logging channel assigned to us by the container.
172    * @return the logging channel
173    */

174   private Logger getLogger() {
175     return m_logger;
176   }
177 }
178
Popular Tags