1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
55 public HttpRmiServlet()
56 {
57 }
58
59
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 }