1 23 24 29 30 package com.sun.enterprise.admin.jmx.remote.server.servlet; 31 32 import java.io.*; 33 import java.util.logging.Logger ; 34 35 import java.net.*; 36 import javax.servlet.*; 37 import javax.servlet.http.*; 38 import javax.management.remote.message.Message; 39 import javax.management.remote.message.MBeanServerRequestMessage; 40 import javax.management.remote.message.MBeanServerResponseMessage; 41 42 43 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 44 import com.sun.enterprise.admin.jmx.remote.streams.*; 45 import com.sun.enterprise.admin.jmx.remote.server.MBeanServerRequestHandler; 46 47 48 65 public class RemoteJmxConnectorServlet extends HttpServlet { 66 67 private MBeanServerRequestHandler requestHandler; 68 69 private final String BINARY_DATA_TYPE = "application/octet-stream"; 70 71 private static final Logger logger = Logger.getLogger( 72 DefaultConfiguration.JMXCONNECTOR_LOGGER); 74 75 76 78 public void init(ServletConfig config) throws ServletException { 79 super.init(config); 80 81 requestHandler = new MBeanServerRequestHandler(config); 83 84 } 85 86 88 public void destroy() { 89 90 requestHandler.getNotificationManager().close(); 91 92 } 93 94 98 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 99 throws ServletException, IOException { 100 try { 101 102 String pathInfo = request.getPathInfo(); 103 if (pathInfo != null && pathInfo.trim().equals(DefaultConfiguration.NOTIF_MGR_PATHINFO)) { 104 requestHandler.getNotificationManager().getNotifications(request, response); 105 return; 106 } 107 108 final Message requestMessage = readRequestMessage(request); 109 final Message responseMessage = requestHandler.handle 110 ((MBeanServerRequestMessage)requestMessage); 111 112 drainInputStream(); JMXInbandStream.setIncomingStream(null); 114 MBeanServerResponseMessage tempMsg = 115 (MBeanServerResponseMessage) responseMessage; 116 StreamMBeanServerResponseMessage resmsg = 117 new StreamMBeanServerResponseMessage( 118 tempMsg.getMessageId(), 119 tempMsg.getWrappedResult(), 120 tempMsg.isException()); 121 InputStream in = JMXInbandStream.getOutgoingStream(); 122 if (in != null) { 123 resmsg.setStreamAvailable(true); 124 } 125 OutputStream out = response.getOutputStream(); 126 sendResponseMessage(response, resmsg, out); 127 if (in != null) 128 sendStream(in, out, getContentLength(resmsg)); 129 130 } 131 catch (ClassNotFoundException ce) { 132 final String message = "Detailed Message: Error in calling in Servlet:processRequest, actual exception is attached"; 133 throw new ServletException (message, ce); 134 } 135 } 136 137 138 private void drainInputStream() { 139 InputStream in = JMXInbandStream.getInputStream(); 140 if (in != null) { 141 byte[] bytes = new byte[8192]; 142 try { 143 while (in.read(bytes) != -1) 144 ; 145 } catch (IOException ignore) { 146 } 148 } 149 } 150 151 private void sendStream(InputStream in, OutputStream out, int padLen) 152 throws IOException { 153 OutputStream o = new JMXChunkedOutputStream(out); 154 byte[] bytes = new byte[8192]; 155 int len = 0; 156 while ( (len = in.read(bytes)) != -1) { 157 o.write(bytes, 0, len); 158 } 159 JMXInbandStream.setOutputStream(null, 0); 160 o.flush(); 161 ((JMXChunkedOutputStream)o).writeEOF(padLen); 162 } 163 164 165 169 protected void doGet(HttpServletRequest request, HttpServletResponse response) 170 throws ServletException, IOException { 171 processRequest(request, response); 172 } 173 174 178 protected void doPost(HttpServletRequest request, HttpServletResponse response) 179 throws ServletException, IOException { 180 processRequest(request, response); 181 } 182 183 private Message readRequestMessage(HttpServletRequest request) throws IOException, ClassNotFoundException { 184 185 JMXInbandStream.setOutputStream(null, 0); 186 InputStream in = request.getInputStream(); 187 final ObjectInputStream ois = new ObjectInputStream(in); 190 192 final MBeanServerRequestMessage m = 193 (MBeanServerRequestMessage) ois.readObject(); 194 195 StreamMBeanServerRequestMessage streamm = 196 (StreamMBeanServerRequestMessage) m; 197 if (streamm.isStreamAvailable()) { 198 JMXInbandStream.setIncomingStream( 199 new JMXChunkedInputStream(in)); 200 } 201 202 logger.fine("Method id is: " + m.getMethodId()); 203 return ( m ); 204 } 205 206 207 private void sendResponseMessage(HttpServletResponse response, Message message, OutputStream out) throws IOException { 209 210 configureResponse(response, message); 211 212 final ObjectOutputStream oos = new ObjectOutputStream( 215 new BufferedOutputStream(out)); 216 217 oos.writeObject(message); 218 oos.flush(); 219 } 221 222 private void configureResponse(HttpServletResponse response, Message message) { 223 response.setContentType(BINARY_DATA_TYPE); 224 225 int reslen = getContentLength(message); 226 if (JMXInbandStream.getOutgoingStream() != null) { 227 int len = (int) JMXInbandStream.getOutgoingStreamLength(); 228 int chunks = (int) ((len/8192) + 2); 229 reslen += len + (chunks * 4); 230 } 231 response.setContentLength(reslen ); 232 233 response.setHeader("Connection", "Keep-Alive"); 234 response.setStatus(HttpServletResponse.SC_OK); 235 } 236 240 private int getContentLength(Serializable serObject) { 241 int size = 0; 242 ObjectOutputStream oos = null; 243 244 try { 245 final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 246 oos = new ObjectOutputStream(baos); 247 oos.writeObject(serObject); 248 size = baos.size(); 249 } 250 catch (Exception e) { 251 e.printStackTrace(); 252 } 253 finally { 254 255 263 264 } 265 return size; 266 } 267 } 268 | Popular Tags |