KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > demos > helloworld > HelloWorldServerImpl


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.demos.helloworld;
88
89 import java.io.IOException JavaDoc;
90 import java.net.InetAddress JavaDoc;
91 import java.net.ServerSocket JavaDoc;
92 import java.net.UnknownHostException JavaDoc;
93 import org.apache.avalon.framework.activity.Disposable;
94 import org.apache.avalon.framework.activity.Initializable;
95 import org.apache.avalon.framework.configuration.Configurable;
96 import org.apache.avalon.framework.configuration.Configuration;
97 import org.apache.avalon.framework.configuration.ConfigurationException;
98 import org.apache.avalon.framework.context.Context;
99 import org.apache.avalon.framework.context.Contextualizable;
100 import org.apache.avalon.framework.logger.AbstractLogEnabled;
101 import org.apache.avalon.framework.service.ServiceException;
102 import org.apache.avalon.framework.service.ServiceManager;
103 import org.apache.avalon.framework.service.Serviceable;
104 import org.apache.avalon.phoenix.BlockContext;
105 import org.codehaus.spice.netserve.connection.RequestHandler;
106 import org.codehaus.spice.netserve.connection.SocketAcceptorManager;
107 import org.codehaus.spice.netserve.sockets.ServerSocketFactory;
108
109 /**
110  * @author Paul Hammant <Paul_Hammant at yahoo.com>
111  * @author Federico Barbieri <scoobie at pop.systemy.it>
112  * @version 1.0
113  * @dna.component
114  * @dna.service type="HelloWorldServer"
115  * @mx.component
116  */

117 public final class HelloWorldServerImpl
118     extends AbstractLogEnabled
119     implements HelloWorldServer, Contextualizable, Serviceable,
120       Configurable, Initializable, Disposable
121 {
122
123     private ServerSocketFactory m_socketManager;
124     private SocketAcceptorManager m_socketAcceptorManager;
125     private BlockContext m_context;
126     private String JavaDoc m_greeting = "Hello World!";
127     private InetAddress JavaDoc m_bindTo;
128     private int m_port;
129     private String JavaDoc m_connectionName = "HelloWorldListener";
130     private ServerSocket JavaDoc m_serverSocket;
131     private HelloWorldHandler m_helloWorldHandler;
132
133     /**
134      * Set the greeting message
135      *
136      * @mx.operation description="Set the greeting."
137      * @mx.parameter name="greeting" description="The new greeting message"
138      * @param greeting The new greeting message
139      */

140     public void setGreeting( final String JavaDoc greeting )
141     {
142         getLogger().debug( "Setting greeting to [" + greeting + "].");
143         m_greeting = greeting;
144     }
145
146     /**
147      * Fetch the greeting message
148      *
149      * @mx.attribute description="The current greeting"
150      * @return The current greeting message.
151      */

152     public String JavaDoc getGreeting()
153     {
154         return m_greeting;
155     }
156
157     /**
158      * Give the component its <code>Context</code>
159      *
160      * @param context The context
161      */

162     public void contextualize( final Context context )
163     {
164         m_context = (BlockContext)context;
165     }
166
167     /**
168      * Configure the component
169      *
170      * @param configuration The configuration
171      */

172     public void configure( final Configuration configuration )
173         throws ConfigurationException
174     {
175         m_port = configuration.getChild( "port" ).getValueAsInteger( 8000 );
176         try
177         {
178             final String JavaDoc bindAddress =
179               configuration.getChild( "bind" ).getValue();
180             m_bindTo = InetAddress.getByName( bindAddress );
181         }
182         catch( final UnknownHostException JavaDoc unhe )
183         {
184             throw new ConfigurationException(
185               "Malformed bind parameter", unhe );
186         }
187     }
188
189     /**
190      * Give the component its <code>ServiceManager</code>.
191      *
192      * @dna.dependency type="org.codehaus.spice.netserve.connection.SocketAcceptorManager"
193      * @dna.dependency type="org.codehaus.spice.netserve.sockets.ServerSocketFactory"
194      */

195     public void service( final ServiceManager serviceManager )
196         throws ServiceException
197     {
198         m_socketManager =
199           (ServerSocketFactory)serviceManager.lookup(
200             ServerSocketFactory.class.getName() );
201         m_socketAcceptorManager =
202           (SocketAcceptorManager)serviceManager.lookup(
203             SocketAcceptorManager.class.getName() );
204     }
205
206     /**
207      * Initialize the component
208      */

209     public void initialize()
210         throws Exception JavaDoc
211     {
212         getLogger().info( "Initializing..." );
213         m_serverSocket = m_socketManager.createServerSocket( m_port,
214                                                               5,
215                                                               m_bindTo );
216         m_helloWorldHandler = createHandler();
217         m_socketAcceptorManager.connect( m_connectionName,
218                                          m_serverSocket,
219                                          (RequestHandler)m_helloWorldHandler );
220         // This is only to help newbies...
221
System.out.println(
222           "HelloWorld server running with a greeting of '" + m_greeting
223           + "'. Point your browser to http://" + m_bindTo + ":" + m_port
224           + " to see its page" );
225     }
226
227     /**
228      * Dispose off the component
229      */

230     public void dispose()
231     {
232         getLogger().info( "Shutting down..." );
233         try
234         {
235             m_socketAcceptorManager.disconnect( m_connectionName );
236         }
237         catch( final Exception JavaDoc e )
238         {
239             getLogger().warn( "Error while disconnecting.", e );
240         }
241
242         try
243         {
244             m_serverSocket.close();
245         }
246         catch( final IOException JavaDoc ioe )
247         {
248             getLogger().warn( "Error while closing server socket.", ioe );
249         }
250     }
251
252     /**
253      * Create a request handler
254      *
255      * @return A new request handler
256      */

257     public HelloWorldHandler createHandler()
258         throws Exception JavaDoc
259     {
260         final HelloWorldHandler handler =
261           new HelloWorldHandler( this, m_context );
262         setupLogger( handler );
263         return handler;
264     }
265
266 }
267
Popular Tags