1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: HttpRmiServlet.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   package com.mjh.switchrmi.transport.http;
27   
28   import com.mjh.switchrmi.*;
29   
30   import java.util.*;
31   
32   import javax.naming.*;
33   
34   import javax.servlet.ServletConfig;
35   import javax.servlet.ServletException;
36   import javax.servlet.http.Cookie;
37   import javax.servlet.http.HttpServlet;
38   import javax.servlet.http.HttpServletRequest;
39   import javax.servlet.http.HttpServletResponse;
40   import javax.servlet.http.HttpSession;
41   
42   import org.apache.log4j.Logger;
43   
44   public class HttpRmiServlet extends HttpServlet
45   {
46       private static final Logger log = 
47               Logger.getLogger(HttpRmiServlet.class.getName());
48       public final static String CONFIG_INIT_PARAM_NAME = 
49               "switchrmi.configuration";
50       private Context jndiContext;
51   
52       /**
53        * Creates a new HttpRmiServlet object.
54        */
55       public HttpRmiServlet()
56       {
57       }
58   
59       /**
60        * DOCUMENT ME!
61        * 
62        * @param config DOCUMENT ME!
63        * @throws ServletException DOCUMENT ME!
64        */
65       public void init(ServletConfig config)
66                 throws ServletException
67       {
68           boolean debug = log.isDebugEnabled();
69   
70           super.init(config);
71   
72           try
73           {
74               if (debug)
75               {
76                   log.debug("config = " + config);
77               }
78   
79               java.util.Enumeration e = config.getInitParameterNames();
80   
81               while (e.hasMoreElements())
82               {
83                   String name = (String) e.nextElement();
84   
85                   if (debug)
86                   {
87                       log.debug(name + "  = " + config.getInitParameter(name));
88                   }
89               }
90   
91               String resourceName = 
92                       config.getInitParameter(CONFIG_INIT_PARAM_NAME);
93               Hashtable env = new Hashtable();
94   
95               env.put(Context.PROVIDER_URL, resourceName);
96               env.put(Context.INITIAL_CONTEXT_FACTORY, 
97                       "com.mjh.switchrmi.jndi.JndiContextImplFactory");
98               jndiContext = new InitialContext(env);
99   
100              if (debug)
101              {
102                  log.debug("jndiContext = " + jndiContext);
103              }
104          }
105          catch (Exception ex)
106          {
107              if (debug)
108              {
109                  log.debug("Exception in RmiServlet(): ", ex);
110              }
111  
112              throw new ServletException(ex.toString());
113          }
114      }
115  
116      protected void doGet(HttpServletRequest request, 
117                           HttpServletResponse response)
118      {
119          doPost(request, response);
120      }
121  
122      protected void doPost(HttpServletRequest request, 
123                            HttpServletResponse response)
124      {
125          boolean debug = log.isDebugEnabled();
126          RmiHandler handler = new RmiHandler();
127          RmiContext context = null;
128  
129          try
130          {
131              context = new RmiContextImpl(true, 
132                                           request.getRequestURL().toString(), 
133                                           jndiContext);
134  
135              HttpServletTransport transport = 
136                      (HttpServletTransport) context.getTransport();
137  
138              transport.setServletRequest(request);
139              transport.setServletResponse(response);
140  
141              HttpSession session = request.getSession();
142              Cookie[] cookies = request.getCookies();
143  
144              handler.serviceInvoke(context, new HttpSessionScopeAccess(session));
145          }
146          catch (Exception ex)
147          {
148              try
149              {
150                  if (debug)
151                  {
152                      log.debug("Problem: ", ex);
153                  }
154  
155                  if (context != null)
156                  {
157                      handler.handleFault(ex, context);
158                  }
159              }
160              catch (Exception ex1)
161              {
162                  if (debug)
163                  {
164                      log.debug("Exception:", ex1);
165                  }
166              }
167          }
168          finally
169          {
170              handler.reset();
171              handler = null;
172          }
173      }
174  }