KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > webadmin > httpd > HttpServer


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: HttpServer.java 2476 2006-02-21 08:45:52Z dblevins $
44  */

45 package org.openejb.webadmin.httpd;
46
47 import java.io.IOException JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.io.OutputStream JavaDoc;
50 import java.net.InetAddress JavaDoc;
51 import java.net.Socket JavaDoc;
52 import java.net.URL JavaDoc;
53 import java.util.Properties JavaDoc;
54
55 import javax.naming.Context JavaDoc;
56 import javax.naming.InitialContext JavaDoc;
57
58 import org.openejb.server.ServerService;
59 import org.openejb.server.ServiceException;
60 import org.openejb.util.Logger;
61 import org.openejb.webadmin.HttpHome;
62 import org.openejb.webadmin.HttpObject;
63
64 /**
65  * This is the main class for the web administration. It takes care of the
66  * processing from the browser, sockets and threading.
67  *
68  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
69  * @author <a HREF="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
70  * @since 11/25/2001
71  */

72 public class HttpServer implements ServerService{
73
74     private static final Logger logger = Logger.getInstance( "OpenEJB.server", "org.openejb.server.util.resources" );
75     private InitialContext JavaDoc jndiContext;
76
77     public void service(InputStream JavaDoc in, OutputStream JavaDoc out) throws ServiceException, IOException JavaDoc {
78         throw new UnsupportedOperationException JavaDoc("service(in,out)");
79     }
80     
81     public void service(Socket JavaDoc socket) throws ServiceException, IOException JavaDoc {
82         /**
83          * The InputStream used to receive incoming messages from the client.
84          */

85         InputStream JavaDoc in = socket.getInputStream();
86         /**
87          * The OutputStream used to send outgoing response messages to the client.
88          */

89         OutputStream JavaDoc out = socket.getOutputStream();
90
91
92         try {
93             processRequest(socket, in, out);
94         } catch ( Throwable JavaDoc e ) {
95             logger.error( "Unexpected error", e );
96         } finally {
97             try {
98                 if ( out != null ) {
99                     out.flush();
100                     out.close();
101                 }
102                 if (in != null)
103                     in.close();
104                 if (socket != null)
105                     socket.close();
106             } catch ( Throwable JavaDoc t ){
107                 logger.error(
108                         "Encountered problem while closing connection with client: "
109                         + t.getMessage());
110             }
111         }
112     }
113
114     public void start() throws ServiceException {
115     }
116
117     public void stop() throws ServiceException {
118     }
119
120     public String JavaDoc getName() {
121         return "webadmin";
122     }
123
124     public int getPort() {
125         return 0;
126     }
127
128     public String JavaDoc getIP() {
129         return "";
130     }
131
132
133     /** Initalizes this instance and takes care of starting things up
134      * @param props a properties instance for system properties
135      * @throws Exception if an exeption is thrown
136      */

137     public void init(Properties JavaDoc props) throws Exception JavaDoc{
138
139         //props.putAll(System.getProperties());
140

141         Properties JavaDoc properties = new Properties JavaDoc();
142         properties.put(
143             Context.INITIAL_CONTEXT_FACTORY,
144             "org.openejb.core.ivm.naming.InitContextFactory");
145         jndiContext = new InitialContext JavaDoc(properties);
146
147     }
148
149     /**
150      * takes care of processing requests and creating the webadmin ejb's
151      *
152      * @param in the input stream from the browser
153      * @param out the output stream to the browser
154      */

155     private void processRequest(Socket JavaDoc socket, InputStream JavaDoc in, OutputStream JavaDoc out) {
156
157         HttpRequestImpl req = new HttpRequestImpl();
158         HttpResponseImpl res = new HttpResponseImpl();
159         InetAddress JavaDoc client = socket.getInetAddress();
160         
161
162         try {
163             req.readMessage(in);
164             res.setRequest(req);
165 // logger.info(client.getHostName()+": "+req.getRequestLine());
166
} catch (Throwable JavaDoc t) {
167             //TODO: log or something
168
//t.printStackTrace();
169
res =
170                 HttpResponseImpl.createError(
171                     "Could not read the request.\n" + t.getClass().getName() + ":\n" + t.getMessage(),
172                     t);
173             try {
174                 logger.error(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
175                 res.writeMessage(out);
176             } catch (Throwable JavaDoc t2) {
177                 //TODO: log or something
178
//t2.printStackTrace();
179
}
180             return;
181         }
182
183         //System.out.println("[] read");
184
URL JavaDoc uri = null;
185         String JavaDoc file = null;
186
187         try {
188             uri = req.getURI();
189             file = uri.getFile();
190             int querry = file.indexOf("?");
191             if (querry != -1) {
192                 file = file.substring(0, querry);
193             }
194
195             //System.out.println("[] file="+file);
196

197         } catch (Throwable JavaDoc t) {
198             //TODO: log or something
199
//t.printStackTrace();
200
res =
201                 HttpResponseImpl.createError(
202                     "Could not determine the module "
203                         + file
204                         + "\n"
205                         + t.getClass().getName()
206                         + ":\n"
207                         + t.getMessage());
208             try {
209                 logger.error(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
210                 res.writeMessage(out);
211             } catch (Throwable JavaDoc t2) {
212                 //TODO: log or something
213
//t2.printStackTrace();
214
}
215             return;
216         }
217
218         HttpObject httpObject = null;
219
220         try {
221             httpObject = getHttpObject(file);
222             //System.out.println("[] module="+httpObject);
223
} catch (Throwable JavaDoc t) {
224             //TODO: log or something
225
//t.printStackTrace();
226
res =
227                 HttpResponseImpl.createError(
228                     "Could not load the module "
229                         + file
230                         + "\n"
231                         + t.getClass().getName()
232                         + ":\n"
233                         + t.getMessage(),
234                     t);
235             //System.out.println("[] res="+res);
236
try {
237                 logger.error(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
238                 res.writeMessage(out);
239             } catch (Throwable JavaDoc t2) {
240                 //TODO: log or something
241
//t2.printStackTrace();
242
}
243             return;
244         }
245
246         try {
247             httpObject.onMessage(req, res);
248         } catch (Throwable JavaDoc t) {
249             //TODO: log or something
250
//t.printStackTrace();
251
res =
252                 HttpResponseImpl.createError(
253                     "Error occurred while executing the module "
254                         + file
255                         + "\n"
256                         + t.getClass().getName()
257                         + ":\n"
258                         + t.getMessage(),
259                     t);
260             try {
261                 logger.error(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
262                 res.writeMessage(out);
263             } catch (Throwable JavaDoc t2) {
264                 //TODO: log or something
265
//t2.printStackTrace();
266
}
267
268             return;
269         }
270
271         try {
272             logger.info(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
273             res.writeMessage(out);
274         } catch (Throwable JavaDoc t) {
275             //TODO: log or something
276
//t.printStackTrace();
277
return;
278         }
279     }
280
281     /** gets an ejb object reference for use in <code>processRequest</code>
282      * @param beanName the name of the ejb to look up
283      * @throws IOException if an exception is thrown
284      * @return an object reference of the ejb
285      */

286     private HttpObject getHttpObject(String JavaDoc beanName) throws IOException JavaDoc {
287         Object JavaDoc obj = null;
288
289         //check for no name, add something here later
290
if (beanName.equals("/")) {
291             try {
292                 obj = jndiContext.lookup("Webadmin/Home");
293             } catch (javax.naming.NamingException JavaDoc ne) {
294                 throw new IOException JavaDoc(ne.getMessage());
295             }
296         } else {
297             try {
298                 obj = jndiContext.lookup(beanName);
299             } catch (javax.naming.NameNotFoundException JavaDoc e) {
300                 try {
301                     obj = jndiContext.lookup("httpd/DefaultBean");
302                 } catch (javax.naming.NamingException JavaDoc ne) {
303                     throw new IOException JavaDoc(ne.getMessage());
304                 }
305             } catch (javax.naming.NamingException JavaDoc e) {
306                 throw new IOException JavaDoc(e.getMessage());
307             }
308         }
309
310         HttpHome ejbHome = (HttpHome) obj;
311         HttpObject httpObject = null;
312
313         try {
314             httpObject = ejbHome.create();
315
316             //
317
obj = org.openejb.util.proxy.ProxyManager.getInvocationHandler(httpObject);
318             org.openejb.core.ivm.BaseEjbProxyHandler handler = null;
319             handler = (org.openejb.core.ivm.BaseEjbProxyHandler) obj;
320             handler.setIntraVmCopyMode(false);
321         } catch (javax.ejb.CreateException JavaDoc cre) {
322             throw new IOException JavaDoc(cre.getMessage());
323         }
324
325         return httpObject;
326     }
327 }
328
Popular Tags