FileDocCategorySizeDatePackage
RemoteJmxConnectorServlet.javaAPI DocGlassfish v2 API11360Fri May 04 22:36:32 BST 2007com.sun.enterprise.admin.jmx.remote.server.servlet

RemoteJmxConnectorServlet

public class RemoteJmxConnectorServlet extends HttpServlet
The crux of server side implementation of JSR 160 over HTTP. This is the single servlet that provides the entire support. The configuration is provided by the standard deployment descriptor in web.xml. The most notable support is that of the security-constraint. The algorithm that {@link #doGet} of this Servlet employs is pretty straightforward:
  • Read the object from the ObjectInputStream backed by its own InputStream
  • Carefully deserialize the Object depending upon the static classpath and other classes known. In other words, no dynamic class loading is used.
  • Take care of the versioning information
  • Invoke a handler to return a result of MBeanServerConnection invocation
  • Configure the HTTP Response Headers
  • Write the result to the ObjectOutputStream backed by its own OutputStream
author
Kedar Mhaswade
version
1.0

Fields Summary
private com.sun.enterprise.admin.jmx.remote.server.MBeanServerRequestHandler
requestHandler
private final String
BINARY_DATA_TYPE
private static final Logger
logger
Constructors Summary
Methods Summary
private voidconfigureResponse(javax.servlet.http.HttpServletResponse response, javax.management.remote.message.Message message)

        response.setContentType(BINARY_DATA_TYPE);
/* BEGIN -- S1WS_MOD */
        int reslen = getContentLength(message);
        if (JMXInbandStream.getOutgoingStream() != null) {
            int len = (int) JMXInbandStream.getOutgoingStreamLength();
            int chunks = (int) ((len/8192) + 2);
            reslen += len + (chunks * 4);
        }
        response.setContentLength(reslen /*getContentLength(message)*/);
/* END -- S1WS_MOD */
		response.setHeader("Connection", "Keep-Alive"); 
        response.setStatus(HttpServletResponse.SC_OK);
    
public voiddestroy()
Destroys the servlet.

/* BEGIN -- S1WS_MOD */
        requestHandler.getNotificationManager().close();
/* END -- S1WS_MOD */
    
protected voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Handles the HTTP GET method.

param
request servlet request
param
response servlet response

        processRequest(request, response);
    
protected voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Handles the HTTP POST method.

param
request servlet request
param
response servlet response

        processRequest(request, response);
    
private voiddrainInputStream()

        InputStream in = JMXInbandStream.getInputStream();
        if (in != null) {
            byte[] bytes = new byte[8192];
            try {
                while (in.read(bytes) != -1)
                    ;
            } catch (IOException ignore) {
                // XXX: Log it
            }
        }
    
private intgetContentLength(java.io.Serializable serObject)
Returns the size of given seialized object in bytes. The size is calculated from the underlying ByteArrayOutputStream backing an ObjectStream, onto which the Object is written.

        int size = 0;
        ObjectOutputStream oos = null;

        try {
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(serObject);
            size = baos.size();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
/* BEGIN -- S1WS_MOD */
/*
            try {
                if (oos != null) {
                    oos.close();
                }
            }
            catch (Exception e){}
*/
/* END -- S1WS_MOD */
        }
        return size;
    
public voidinit(javax.servlet.ServletConfig config)
Initializes the servlet.

/*, 
        DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );*/

/* END -- S1WS_MOD */
            
          
        super.init(config);
/* BEGIN -- S1WS_MOD */
//        requestHandler = new MBeanServerRequestHandler();
        requestHandler = new MBeanServerRequestHandler(config);
/* END -- S1WS_MOD */
    
protected voidprocessRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Processes requests for both HTTP GET and POST methods.

param
request servlet request
param
response servlet response

        try {
/* BEGIN -- S1WS_MOD */
            String pathInfo = request.getPathInfo();
            if (pathInfo != null && pathInfo.trim().equals(DefaultConfiguration.NOTIF_MGR_PATHINFO)) {
                requestHandler.getNotificationManager().getNotifications(request, response);
                return;
            }
/* END -- S1WS_MOD */
            final Message requestMessage = readRequestMessage(request);
            final Message responseMessage = requestHandler.handle
                ((MBeanServerRequestMessage)requestMessage);
/* BEGIN -- S1WS_MOD */
            drainInputStream(); // Since, in.close only reads byte by byte.
            JMXInbandStream.setIncomingStream(null);
            MBeanServerResponseMessage tempMsg =
                (MBeanServerResponseMessage) responseMessage;
            StreamMBeanServerResponseMessage resmsg =
                new StreamMBeanServerResponseMessage(
                                tempMsg.getMessageId(),
                                tempMsg.getWrappedResult(),
                                tempMsg.isException());
            InputStream in = JMXInbandStream.getOutgoingStream();
            if (in != null) {
                resmsg.setStreamAvailable(true);
            }
            OutputStream out = response.getOutputStream();
            sendResponseMessage(response, resmsg, out);
            if (in != null)
                sendStream(in, out, getContentLength(resmsg));
/* END -- S1WS_MOD */
        }
        catch (ClassNotFoundException ce) {
            final String message = "Detailed Message: Error in calling in Servlet:processRequest, actual exception is attached";
            throw new ServletException (message, ce);
        }
    
private javax.management.remote.message.MessagereadRequestMessage(javax.servlet.http.HttpServletRequest request)

/* BEGIN -- S1WS_MOD */
        JMXInbandStream.setOutputStream(null, 0);
        InputStream in = request.getInputStream();
//        final ObjectInputStream ois = new ObjectInputStream(
//			new BufferedInputStream(request.getInputStream()));
        final ObjectInputStream ois = new ObjectInputStream(in);
//			new BufferedInputStream(in));
/* END -- S1WS_MOD */
        final MBeanServerRequestMessage m = 
			(MBeanServerRequestMessage) ois.readObject();
/* BEGIN -- S1WS_MOD */
        StreamMBeanServerRequestMessage streamm =
                (StreamMBeanServerRequestMessage) m;
        if (streamm.isStreamAvailable()) {
            JMXInbandStream.setIncomingStream(
                new JMXChunkedInputStream(in));
        }
/* END -- S1WS_MOD */
		logger.fine("Method id is: " + m.getMethodId());
		return ( m );
    
private voidsendResponseMessage(javax.servlet.http.HttpServletResponse response, javax.management.remote.message.Message message, java.io.OutputStream out)

/* END -- S1WS_MOD */
        configureResponse(response, message);
/* BEGIN -- S1WS_MOD */
//        final ObjectOutputStream oos = new ObjectOutputStream(
//			new BufferedOutputStream(response.getOutputStream()));
        final ObjectOutputStream oos = new ObjectOutputStream(
			new BufferedOutputStream(out));
/* END -- S1WS_MOD */
        oos.writeObject(message);
		oos.flush();
		//oos.close();
    
private voidsendStream(java.io.InputStream in, java.io.OutputStream out, int padLen)

        OutputStream o = new JMXChunkedOutputStream(out);
        byte[] bytes = new byte[8192];
        int len = 0;
        while ( (len = in.read(bytes)) != -1) {
            o.write(bytes, 0, len);
        }
        JMXInbandStream.setOutputStream(null, 0);
        o.flush();
        ((JMXChunkedOutputStream)o).writeEOF(padLen);