2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.remoting.transport.servlet.web;
25
import org.jboss.logging.Logger;
26
import org.jboss.remoting.InvokerLocator;
27
import org.jboss.remoting.InvokerRegistry;
28
import org.jboss.remoting.ServerInvoker;
29
import org.jboss.remoting.transport.servlet.ServletServerInvokerMBean;
30
import org.jboss.remoting.util.SecurityUtility;
31
import javax.management.MBeanServer;
32
import javax.management.MBeanServerFactory;
33
import javax.management.MBeanServerInvocationHandler;
34
import javax.management.MalformedObjectNameException;
35
import javax.management.ObjectName;
36
import javax.servlet.ServletConfig;
37
import javax.servlet.ServletException;
38
import javax.servlet.ServletInputStream;
39
import javax.servlet.ServletOutputStream;
40
import javax.servlet.http.HttpServlet;
41
import javax.servlet.http.HttpServletRequest;
42
import javax.servlet.http.HttpServletResponse;
43
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
45
import java.lang.reflect.InvocationTargetException;
46
import java.lang.reflect.Method;
47
import java.net.MalformedURLException;
48
import java.security.AccessController;
49
import java.security.PrivilegedAction;
50
import java.security.PrivilegedActionException;
51
import java.security.PrivilegedExceptionAction;
52
import java.util.ArrayList;
53
import java.util.Iterator;
56
* The servlet that receives the inital http request for the ServletServerInvoker.
58
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
60
public class ServerInvokerServlet extends HttpServlet
62
private static Logger log = Logger.getLogger(ServerInvokerServlet.class);
63
private ServletServerInvokerMBean servletInvoker;
64
private static final long serialVersionUID = 8796224225710165263L;
67
* Initializes the servlet.
69
public void init(ServletConfig config) throws ServletException
73
// first see if the invoker is specified by its URL; if not, then see if the invoker was specified by name
74
servletInvoker = getInvokerFromInvokerUrl(config);
76
if (servletInvoker == null)
78
servletInvoker = getInvokerFromInvokerName(config);
80
if (servletInvoker == null)
82
throw new ServletException("Could not find init parameter for 'locatorUrl' or 'locatorName' - one of which must be supplied for ServerInvokerServlet to function.");
86
log.debug("Got ServletServerInvoker from InvokerName: " + config.getInitParameter("invokerName"));
91
log.debug("Got ServletServerInvoker from InvokerLocator: " + config.getInitParameter("locatorUrl"));
96
* Destroys the servlet.
104
* Read a MarshalledInvocation and dispatch it to the target JMX object
105
* invoke(Invocation) object.
107
* @param request servlet request
108
* @param response servlet response
110
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
111
throws ServletException, IOException
113
boolean trace = log.isTraceEnabled();
116
log.trace("processRequest, ContentLength: " + request.getContentLength());
117
log.trace("processRequest, ContentType: " + request.getContentType());
120
int bufferSize = 1024;
121
byte[] byteBuffer = new byte[bufferSize];
122
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
125
int contentLength = request.getContentLength();
126
ServletInputStream inputStream = request.getInputStream();
127
int amtRead = inputStream.read(byteBuffer);
131
byteOutputStream.write(byteBuffer, pointer, amtRead);
133
if (amtRead < bufferSize && byteOutputStream.size() >= contentLength)
135
//done reading, so process
138
amtRead = inputStream.read(byteBuffer);
140
byteOutputStream.flush();
141
byte[] totalByteArray = byteOutputStream.toByteArray();
142
byte[] out = processRequest(servletInvoker, request, totalByteArray, response);
143
ServletOutputStream outStream = response.getOutputStream();
144
outStream.write(out);
147
//response.setContentLength(out.length);
151
* Handles the HTTP <code>GET</code> method.
153
* @param request servlet request
154
* @param response servlet response
156
protected void doGet(HttpServletRequest request, HttpServletResponse response)
157
throws ServletException, IOException
159
processRequest(request, response);
163
* Handles the HTTP <code>POST</code> method.
165
* @param request servlet request
166
* @param response servlet response
168
protected void doPost(HttpServletRequest request, HttpServletResponse response)
169
throws ServletException, IOException
171
processRequest(request, response);
175
* Returns a short description of the servlet.
177
public String getServletInfo()
179
return "Servlet front to JBossRemoting servlet server invoker.";
183
* Returns the servlet server invoker but only if it was specified via the
184
* "invokerUrl" init parameter.
186
* @param config the servlet configuration
187
* @return the servlet server invoker as specified by the "invokerUrl", or
188
* <code>null</code> if "invokerUrl" init parameter was not specified
189
* @throws ServletException
191
protected ServletServerInvokerMBean getInvokerFromInvokerUrl(ServletConfig config)
192
throws ServletException
194
String locatorUrl = config.getInitParameter("locatorUrl");
195
if (locatorUrl == null)
201
InvokerLocator validatedLocator = new InvokerLocator(locatorUrl);
202
locatorUrl = InvokerLocator.validateLocator(validatedLocator).getLocatorURI();
204
catch (MalformedURLException e)
206
log.warn("malformed URL: " + locatorUrl);
210
ServerInvoker[] serverInvokers = InvokerRegistry.getServerInvokers();
211
if (serverInvokers != null && serverInvokers.length > 0)
213
for (int x = 0; x < serverInvokers.length; x++)
215
ServerInvoker svrInvoker = serverInvokers[x];
216
InvokerLocator locator = svrInvoker.getLocator();
217
if (locatorUrl.equalsIgnoreCase(locator.getLocatorURI()))
219
return (ServletServerInvokerMBean) svrInvoker;
223
throw new ServletException("Can not find servlet server invoker with same locator as specified (" + locatorUrl + ")");
226
throw new ServletException("Can not find any server invokers registered. " +
227
"Could be that servlet server invoker not registered or " +
228
"has been created using different classloader.");
232
* Returns the servlet server invoker but only if it was specified via the
233
* "invokerName" init parameter.
235
* @param config the servlet configuration
236
* @return the servlet server invoker as specified by the "invokerName", or
237
* <code>null</code> if "invokerName" init parameter was not specified
238
* @throws ServletException
240
protected ServletServerInvokerMBean getInvokerFromInvokerName(ServletConfig config)
241
throws ServletException
243
ObjectName localInvokerName = null;
245
String name = config.getInitParameter("invokerName");
253
localInvokerName = new ObjectName(name);
254
log.debug("localInvokerName=" + localInvokerName);
256
catch (MalformedObjectNameException e)
258
throw new ServletException("Failed to build invokerName", e);
261
// Lookup the MBeanServer
262
String mbeanServerId = config.getInitParameter("mbeanServer");
263
MBeanServer mbeanServer = getMBeanServer(mbeanServerId);
264
if (mbeanServer == null)
266
throw new ServletException("Failed to locate the MBeanServer");
269
return (ServletServerInvokerMBean)
270
MBeanServerInvocationHandler.newProxyInstance(mbeanServer,
272
ServletServerInvokerMBean.class,
277
* Returns the MBeanServer where the server invoker should be found. This should only be
278
* used if the "invokerName" init parameter is specified (where that invoker name is the
279
* object name registered in the returned MBeanServer).
281
* @param mbeanServerId indicates which MBeanServer to use
283
* @return MBeanServer where the invoker is supposed to be registered
285
protected MBeanServer getMBeanServer(String mbeanServerId)
287
if (mbeanServerId == null)
289
mbeanServerId = "jboss";
292
if (mbeanServerId.equals("*platform*"))
296
MBeanServer s = getPlatformMBeanServer();
297
log.debug("Using platform MBeanServer");
302
mbeanServerId = "jboss";
306
Iterator i = findMBeanServer(null).iterator();
309
MBeanServer server = (MBeanServer) i.next();
311
if (server.getDefaultDomain() == null)
315
if (server.getDefaultDomain().equals(mbeanServerId))
317
log.debug("Using MBeanServer with defaultDomain: " + mbeanServerId);
325
static private ArrayList findMBeanServer(final String agentId)
327
if (SecurityUtility.skipAccessControl())
329
return MBeanServerFactory.findMBeanServer(agentId);
332
return (ArrayList)AccessController.doPrivileged( new PrivilegedAction()
336
return MBeanServerFactory.findMBeanServer(agentId);
341
static private MBeanServer getPlatformMBeanServer()
342
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
344
if (SecurityUtility.skipAccessControl())
349
c = Class.forName("java.lang.management.ManagementFactory");
353
System.out.println("Unable to access java.lang.management.ManagementFactory: must be using jdk 1.4");
356
Method m = c.getMethod("getPlatformMBeanServer", new Class[] {});
357
MBeanServer s = (MBeanServer) m.invoke(null, new Object[] {});
363
return (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
366
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
371
c = Class.forName("java.lang.management.ManagementFactory");
375
System.out.println("Unable to access java.lang.management.ManagementFactory: must be using jdk 1.4");
378
Method m = c.getMethod("getPlatformMBeanServer", new Class[] {});
379
MBeanServer s = (MBeanServer) m.invoke(null, new Object[] {});
384
catch (PrivilegedActionException e)
386
Throwable cause = e.getCause();
387
if (cause instanceof NoSuchMethodException)
388
throw (NoSuchMethodException) cause;
389
else if (cause instanceof IllegalAccessException)
390
throw (IllegalAccessException) cause;
392
throw (InvocationTargetException) cause;
396
static private byte[] processRequest(final ServletServerInvokerMBean invoker,
397
final HttpServletRequest request,
398
final byte[] byteArray,
399
final HttpServletResponse response)
400
throws ServletException, IOException
402
if (SecurityUtility.skipAccessControl())
404
return invoker.processRequest(request, byteArray, response);
409
return (byte[]) AccessController.doPrivileged( new PrivilegedExceptionAction()
411
public Object run() throws ServletException, IOException
413
return invoker.processRequest(request, byteArray, response);
417
catch (PrivilegedActionException e)
419
Throwable cause = e.getCause();
420
if (cause instanceof ServletException)
421
throw (ServletException) cause;
423
throw (IOException) e.getCause();
b'\\ No newline at end of file'