1 45 package org.openejb.webadmin.httpd; 46 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.io.OutputStream ; 50 import java.net.InetAddress ; 51 import java.net.Socket ; 52 import java.net.URL ; 53 import java.util.Properties ; 54 55 import javax.naming.Context ; 56 import javax.naming.InitialContext ; 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 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 jndiContext; 76 77 public void service(InputStream in, OutputStream out) throws ServiceException, IOException { 78 throw new UnsupportedOperationException ("service(in,out)"); 79 } 80 81 public void service(Socket socket) throws ServiceException, IOException { 82 85 InputStream in = socket.getInputStream(); 86 89 OutputStream out = socket.getOutputStream(); 90 91 92 try { 93 processRequest(socket, in, out); 94 } catch ( Throwable 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 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 getName() { 121 return "webadmin"; 122 } 123 124 public int getPort() { 125 return 0; 126 } 127 128 public String getIP() { 129 return ""; 130 } 131 132 133 137 public void init(Properties props) throws Exception { 138 139 141 Properties properties = new Properties (); 142 properties.put( 143 Context.INITIAL_CONTEXT_FACTORY, 144 "org.openejb.core.ivm.naming.InitContextFactory"); 145 jndiContext = new InitialContext (properties); 146 147 } 148 149 155 private void processRequest(Socket socket, InputStream in, OutputStream out) { 156 157 HttpRequestImpl req = new HttpRequestImpl(); 158 HttpResponseImpl res = new HttpResponseImpl(); 159 InetAddress client = socket.getInetAddress(); 160 161 162 try { 163 req.readMessage(in); 164 res.setRequest(req); 165 } catch (Throwable t) { 167 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 t2) { 177 } 180 return; 181 } 182 183 URL uri = null; 185 String 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 197 } catch (Throwable t) { 198 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 t2) { 212 } 215 return; 216 } 217 218 HttpObject httpObject = null; 219 220 try { 221 httpObject = getHttpObject(file); 222 } catch (Throwable t) { 224 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 try { 237 logger.error(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]"); 238 res.writeMessage(out); 239 } catch (Throwable t2) { 240 } 243 return; 244 } 245 246 try { 247 httpObject.onMessage(req, res); 248 } catch (Throwable t) { 249 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 t2) { 264 } 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 t) { 275 return; 278 } 279 } 280 281 286 private HttpObject getHttpObject(String beanName) throws IOException { 287 Object obj = null; 288 289 if (beanName.equals("/")) { 291 try { 292 obj = jndiContext.lookup("Webadmin/Home"); 293 } catch (javax.naming.NamingException ne) { 294 throw new IOException (ne.getMessage()); 295 } 296 } else { 297 try { 298 obj = jndiContext.lookup(beanName); 299 } catch (javax.naming.NameNotFoundException e) { 300 try { 301 obj = jndiContext.lookup("httpd/DefaultBean"); 302 } catch (javax.naming.NamingException ne) { 303 throw new IOException (ne.getMessage()); 304 } 305 } catch (javax.naming.NamingException e) { 306 throw new IOException (e.getMessage()); 307 } 308 } 309 310 HttpHome ejbHome = (HttpHome) obj; 311 HttpObject httpObject = null; 312 313 try { 314 httpObject = ejbHome.create(); 315 316 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 cre) { 322 throw new IOException (cre.getMessage()); 323 } 324 325 return httpObject; 326 } 327 } 328 | Popular Tags |