KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > remote > server > servlet > RemoteJmxConnectorServlet


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /* CVS information
25  * $Header: /cvs/glassfish/jmx-remote/rjmx-impl/src/java/com/sun/enterprise/admin/jmx/remote/server/servlet/RemoteJmxConnectorServlet.java,v 1.4 2005/12/25 04:26:43 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 04:26:43 $
28  */

29
30 package com.sun.enterprise.admin.jmx.remote.server.servlet;
31
32 import java.io.*;
33 import java.util.logging.Logger JavaDoc;
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 /* BEGIN -- S1WS_MOD */
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 /* END -- S1WS_MOD */
47
48 /** The crux of server side implementation of JSR 160 over HTTP. This is the
49  * single servlet that provides the entire support. The configuration is
50  * provided by the standard deployment descriptor in web.xml. The most notable
51  * support is that of the security-constraint. The algorithm that
52  * {@link #doGet} of this Servlet employs is pretty straightforward:
53  * <ul>
54  * <li> Read the object from the ObjectInputStream backed by its own InputStream </li>
55  * <li> Carefully deserialize the Object depending upon the static classpath and other classes known. In other words, no dynamic class loading is used.</li>
56  * <li> Take care of the versioning information </li>
57  * <li> Invoke a handler to return a result of MBeanServerConnection invocation </li>
58  * <li> Configure the HTTP Response Headers </li>
59  * <li> Write the result to the ObjectOutputStream backed by its own OutputStream </li>
60  * </ul>
61  *
62  * @author Kedar Mhaswade
63  * @version 1.0
64  */

65 public class RemoteJmxConnectorServlet extends HttpServlet {
66     
67     private MBeanServerRequestHandler requestHandler;
68     
69     private final String JavaDoc BINARY_DATA_TYPE = "application/octet-stream";
70 /* BEGIN -- S1WS_MOD */
71     private static final Logger JavaDoc logger = Logger.getLogger(
72         DefaultConfiguration.JMXCONNECTOR_LOGGER);/*,
73         DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );*/

74
75 /* END -- S1WS_MOD */
76     /** Initializes the servlet.
77      */

78     public void init(ServletConfig config) throws ServletException {
79         super.init(config);
80 /* BEGIN -- S1WS_MOD */
81 // requestHandler = new MBeanServerRequestHandler();
82
requestHandler = new MBeanServerRequestHandler(config);
83 /* END -- S1WS_MOD */
84     }
85     
86     /** Destroys the servlet.
87      */

88     public void destroy() {
89 /* BEGIN -- S1WS_MOD */
90         requestHandler.getNotificationManager().close();
91 /* END -- S1WS_MOD */
92     }
93     
94     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
95      * @param request servlet request
96      * @param response servlet response
97      */

98     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
99     throws ServletException, IOException {
100         try {
101 /* BEGIN -- S1WS_MOD */
102             String JavaDoc pathInfo = request.getPathInfo();
103             if (pathInfo != null && pathInfo.trim().equals(DefaultConfiguration.NOTIF_MGR_PATHINFO)) {
104                 requestHandler.getNotificationManager().getNotifications(request, response);
105                 return;
106             }
107 /* END -- S1WS_MOD */
108             final Message requestMessage = readRequestMessage(request);
109             final Message responseMessage = requestHandler.handle
110                 ((MBeanServerRequestMessage)requestMessage);
111 /* BEGIN -- S1WS_MOD */
112             drainInputStream(); // Since, in.close only reads byte by byte.
113
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 /* END -- S1WS_MOD */
130         }
131         catch (ClassNotFoundException JavaDoc ce) {
132             final String JavaDoc message = "Detailed Message: Error in calling in Servlet:processRequest, actual exception is attached";
133             throw new ServletException (message, ce);
134         }
135     }
136
137 /* BEGIN -- S1WS_MOD */
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                 // XXX: Log it
147
}
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 /* END -- S1WS_MOD */
164     
165     /** Handles the HTTP <code>GET</code> method.
166      * @param request servlet request
167      * @param response servlet response
168      */

169     protected void doGet(HttpServletRequest request, HttpServletResponse response)
170     throws ServletException, IOException {
171         processRequest(request, response);
172     }
173     
174     /** Handles the HTTP <code>POST</code> method.
175      * @param request servlet request
176      * @param response servlet response
177      */

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 JavaDoc {
184 /* BEGIN -- S1WS_MOD */
185         JMXInbandStream.setOutputStream(null, 0);
186         InputStream in = request.getInputStream();
187 // final ObjectInputStream ois = new ObjectInputStream(
188
// new BufferedInputStream(request.getInputStream()));
189
final ObjectInputStream ois = new ObjectInputStream(in);
190 // new BufferedInputStream(in));
191
/* END -- S1WS_MOD */
192         final MBeanServerRequestMessage m =
193             (MBeanServerRequestMessage) ois.readObject();
194 /* BEGIN -- S1WS_MOD */
195         StreamMBeanServerRequestMessage streamm =
196                 (StreamMBeanServerRequestMessage) m;
197         if (streamm.isStreamAvailable()) {
198             JMXInbandStream.setIncomingStream(
199                 new JMXChunkedInputStream(in));
200         }
201 /* END -- S1WS_MOD */
202         logger.fine("Method id is: " + m.getMethodId());
203         return ( m );
204     }
205     
206 /* BEGIN -- S1WS_MOD */
207 // private void sendResponseMessage(HttpServletResponse response, Message message) throws IOException {
208
private void sendResponseMessage(HttpServletResponse response, Message message, OutputStream out) throws IOException {
209 /* END -- S1WS_MOD */
210         configureResponse(response, message);
211 /* BEGIN -- S1WS_MOD */
212 // final ObjectOutputStream oos = new ObjectOutputStream(
213
// new BufferedOutputStream(response.getOutputStream()));
214
final ObjectOutputStream oos = new ObjectOutputStream(
215             new BufferedOutputStream(out));
216 /* END -- S1WS_MOD */
217         oos.writeObject(message);
218         oos.flush();
219         //oos.close();
220
}
221     
222     private void configureResponse(HttpServletResponse response, Message message) {
223         response.setContentType(BINARY_DATA_TYPE);
224 /* BEGIN -- S1WS_MOD */
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 /*getContentLength(message)*/);
232 /* END -- S1WS_MOD */
233         response.setHeader("Connection", "Keep-Alive");
234         response.setStatus(HttpServletResponse.SC_OK);
235     }
236     /** Returns the size of given seialized object in bytes.
237         The size is calculated from the underlying ByteArrayOutputStream
238         backing an ObjectStream, onto which the Object is written.
239     */

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 JavaDoc e) {
251             e.printStackTrace();
252         }
253         finally {
254 /* BEGIN -- S1WS_MOD */
255 /*
256             try {
257                 if (oos != null) {
258                     oos.close();
259                 }
260             }
261             catch (Exception e){}
262 */

263 /* END -- S1WS_MOD */
264         }
265         return size;
266     }
267 }
268
Popular Tags