Home » openejb-3.1.2-src » org.apache » openejb » webadmin » httpd » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.webadmin.httpd;
   18   
   19   import java.io.IOException;
   20   import java.io.InputStream;
   21   import java.io.OutputStream;
   22   import java.net.InetAddress;
   23   import java.net.Socket;
   24   import java.net.URL;
   25   import java.util.Properties;
   26   
   27   import javax.naming.Context;
   28   import javax.naming.InitialContext;
   29   
   30   import org.apache.openejb.server.ServerService;
   31   import org.apache.openejb.server.ServiceException;
   32   import org.apache.openejb.util.Logger;
   33   import org.apache.openejb.util.LogCategory;
   34   import org.apache.openejb.webadmin.HttpHome;
   35   import org.apache.openejb.webadmin.HttpObject;
   36   
   37   /** 
   38    * This is the main class for the web administration.  It takes care of the
   39    * processing from the browser, sockets and threading.
   40    * 
   41    * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
   42    * @author <a href="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
   43    * @since 11/25/2001
   44    */
   45   public class HttpServer implements ServerService{
   46   
   47       private static final Logger logger = Logger.getInstance( LogCategory.OPENEJB_SERVER, HttpServer.class );
   48       private InitialContext jndiContext;
   49   
   50       public void service(InputStream in, OutputStream out) throws ServiceException, IOException {
   51           throw new UnsupportedOperationException("service(in,out)");
   52       }
   53       
   54       public void service(Socket socket) throws ServiceException, IOException {
   55           /**
   56            * The InputStream used to receive incoming messages from the client.
   57            */
   58           InputStream in = socket.getInputStream();
   59           /**
   60            * The OutputStream used to send outgoing response messages to the client.
   61            */
   62           OutputStream out = socket.getOutputStream();
   63   
   64   
   65           try {
   66               processRequest(socket, in, out);
   67           } catch ( Throwable e ) {
   68               logger.error( "Unexpected error", e );
   69           } finally {
   70               try {
   71                   if ( out != null ) {
   72                       out.flush();
   73                       out.close();
   74                   }
   75                   if (in != null)
   76                       in.close();
   77                   if (socket != null)
   78                       socket.close();
   79               } catch ( Throwable t ){
   80                   logger.error(
   81                           "Encountered problem while closing connection with client: "
   82                           + t.getMessage());
   83               }
   84           }
   85       }
   86   
   87       public void start() throws ServiceException {
   88       }
   89   
   90       public void stop() throws ServiceException {
   91       }
   92   
   93       public String getName() {
   94           return "webadmin";
   95       }
   96   
   97       public int getPort() {
   98           return 0;
   99       }
  100   
  101       public String getIP() {
  102           return "";
  103       }
  104   
  105   
  106       /** Initalizes this instance and takes care of starting things up
  107        * @param props a properties instance for system properties
  108        * @throws Exception if an exeption is thrown
  109        */
  110       public void init(Properties props) throws Exception{
  111   
  112           //props.putAll(System.getProperties());
  113   
  114           Properties properties = new Properties();
  115           properties.put(
  116               Context.INITIAL_CONTEXT_FACTORY,
  117               "org.apache.openejb.core.ivm.naming.InitContextFactory");
  118           jndiContext = new InitialContext(properties);
  119   
  120       }
  121   
  122       /** 
  123        * takes care of processing requests and creating the webadmin ejb's
  124        * 
  125        * @param in the input stream from the browser
  126        * @param out the output stream to the browser
  127        */
  128       private void processRequest(Socket socket, InputStream in, OutputStream out) {
  129   
  130           HttpRequestImpl req = new HttpRequestImpl();
  131           HttpResponseImpl res = new HttpResponseImpl();
  132           InetAddress client = socket.getInetAddress();
  133           
  134   
  135           try {
  136               req.readMessage(in);
  137               res.setRequest(req);
  138   //            logger.info(client.getHostName()+": "+req.getRequestLine());
  139           } catch (Throwable t) {
  140               //TODO: log or something
  141               //t.printStackTrace();
  142               res =
  143                   HttpResponseImpl.createError(
  144                       "Could not read the request.\n" + t.getClass().getName() + ":\n" + t.getMessage(),
  145                       t);
  146               try {
  147                   logger.error(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
  148                   res.writeMessage(out);
  149               } catch (Throwable t2) {
  150                   //TODO: log or something
  151                   //t2.printStackTrace();
  152               }
  153               return;
  154           }
  155   
  156           //System.out.println("[] read");
  157           URL uri = null;
  158           String file = null;
  159   
  160           try {
  161               uri = req.getURI();
  162               file = uri.getFile();
  163               int querry = file.indexOf("?");
  164               if (querry != -1) {
  165                   file = file.substring(0, querry);
  166               }
  167   
  168               //System.out.println("[] file="+file);
  169   
  170           } catch (Throwable t) {
  171               //TODO: log or something
  172               //t.printStackTrace();
  173               res =
  174                   HttpResponseImpl.createError(
  175                       "Could not determine the module "
  176                           + file
  177                           + "\n"
  178                           + t.getClass().getName()
  179                           + ":\n"
  180                           + t.getMessage());
  181               try {
  182                   logger.error(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
  183                   res.writeMessage(out);
  184               } catch (Throwable t2) {
  185                   //TODO: log or something
  186                   //t2.printStackTrace();
  187               }
  188               return;
  189           }
  190   
  191           HttpObject httpObject = null;
  192   
  193           try {
  194               httpObject = getHttpObject(file);
  195               //System.out.println("[] module="+httpObject);
  196           } catch (Throwable t) {
  197               //TODO: log or something
  198               //t.printStackTrace();
  199               res =
  200                   HttpResponseImpl.createError(
  201                       "Could not load the module "
  202                           + file
  203                           + "\n"
  204                           + t.getClass().getName()
  205                           + ":\n"
  206                           + t.getMessage(),
  207                       t);
  208               //System.out.println("[] res="+res);
  209               try {
  210                   logger.error(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
  211                   res.writeMessage(out);
  212               } catch (Throwable t2) {
  213                   //TODO: log or something
  214                   //t2.printStackTrace();
  215               }
  216               return;
  217           }
  218   
  219           try {
  220               httpObject.onMessage(req, res);
  221           } catch (Throwable t) {
  222               //TODO: log or something
  223               //t.printStackTrace();
  224               res =
  225                   HttpResponseImpl.createError(
  226                       "Error occurred while executing the module "
  227                           + file
  228                           + "\n"
  229                           + t.getClass().getName()
  230                           + ":\n"
  231                           + t.getMessage(),
  232                       t);
  233               try {
  234                   logger.error(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
  235                   res.writeMessage(out);
  236               } catch (Throwable t2) {
  237                   //TODO: log or something
  238                   //t2.printStackTrace();
  239               }
  240   
  241               return;
  242           }
  243   
  244           try {
  245               logger.info(client.getHostName()+": "+res.getCode()+" "+req.getRequestLine()+ " ["+ res.getResponseString()+"]");
  246               res.writeMessage(out);
  247           } catch (Throwable t) {
  248               //TODO: log or something
  249               //t.printStackTrace();
  250               return;
  251           }
  252       }
  253   
  254       /** gets an ejb object reference for use in <code>processRequest</code>
  255        * @param beanName the name of the ejb to look up
  256        * @throws IOException if an exception is thrown
  257        * @return an object reference of the ejb
  258        */
  259       private HttpObject getHttpObject(String beanName) throws IOException {
  260           Object obj = null;
  261   
  262           //check for no name, add something here later
  263           if (beanName.equals("/")) {
  264               try {
  265                   obj = jndiContext.lookup("Webadmin/Home");
  266               } catch (javax.naming.NamingException ne) {
  267                   throw new IOException(ne.getMessage());
  268               }
  269           } else {
  270               try {
  271                   obj = jndiContext.lookup(beanName);
  272               } catch (javax.naming.NameNotFoundException e) {
  273                   try {
  274                       obj = jndiContext.lookup("httpd/DefaultBean");
  275                   } catch (javax.naming.NamingException ne) {
  276                       throw new IOException(ne.getMessage());
  277                   }
  278               } catch (javax.naming.NamingException e) {
  279                   throw new IOException(e.getMessage());
  280               }
  281           }
  282   
  283           HttpHome ejbHome = (HttpHome) obj;
  284           HttpObject httpObject = null;
  285   
  286           try {
  287               httpObject = ejbHome.create();
  288   
  289               // 
  290               obj = org.apache.openejb.util.proxy.ProxyManager.getInvocationHandler(httpObject);
  291               org.apache.openejb.core.ivm.BaseEjbProxyHandler handler = null;
  292               handler = (org.apache.openejb.core.ivm.BaseEjbProxyHandler) obj;
  293               handler.setIntraVmCopyMode(false);
  294           } catch (javax.ejb.CreateException cre) {
  295               throw new IOException(cre.getMessage());
  296           }
  297   
  298           return httpObject;
  299       }
  300   }

Home » openejb-3.1.2-src » org.apache » openejb » webadmin » httpd » [javadoc | source]