KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > wsmgmt > msg > MessageTraceFactory


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 package com.sun.enterprise.admin.wsmgmt.msg;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.regex.Matcher JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30 import java.security.Principal JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import javax.xml.soap.SOAPMessage JavaDoc;
33 import javax.xml.soap.SOAPBody JavaDoc;
34 import javax.xml.soap.SOAPFault JavaDoc;
35 import com.sun.enterprise.security.SecurityContext;
36 import com.sun.appserv.management.ext.wsmgmt.MessageTrace;
37 import com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl;
38 import com.sun.enterprise.admin.wsmgmt.filter.spi.FilterContext;
39 import com.sun.enterprise.webservice.monitoring.TransportInfo;
40 import com.sun.enterprise.webservice.monitoring.TransportType;
41
42 import javax.xml.soap.SOAPException JavaDoc;
43
44 import java.util.logging.Logger JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import com.sun.logging.LogDomains;
47 import com.sun.enterprise.util.i18n.StringManager;
48 import com.sun.enterprise.admin.wsmgmt.SOAPMessageContext;
49 import org.apache.coyote.tomcat5.CoyoteRequestFacade;
50 //import com.sun.xml.rpc.server.http.HttpServletRequest;
51

52 /**
53  * Factory class to that generates a message trace object from the SOAP
54  * invocation events.
55  */

56 class MessageTraceFactory {
57
58     /**
59      * Returns the singleton instance.
60      *
61      * @return singleton instance of this class
62      */

63     static MessageTraceFactory getInstance() {
64         if (_instance == null) {
65             _instance = new MessageTraceFactory();
66         }
67
68         return _instance;
69     }
70
71     /**
72      * Constructor.
73      */

74     private MessageTraceFactory() {
75         _messages = new HashMap JavaDoc();
76         _msgBodyPattern = Pattern.compile("<env:Body>.*</env:Body>");
77     }
78
79     /**
80      * Processes the SOAP request.
81      *
82      * @param ctx filter context
83      */

84     void processRequest(FilterContext ctx, String JavaDoc appId) {
85
86         // message id
87
String JavaDoc messageId = ctx.getMessageId();
88         com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace = new
89         com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl(messageId,
90         MessageTrace.CLASS_NAME);
91
92         // sets the time stamp
93
trace.setTimeStamp( System.currentTimeMillis() );
94
95         // set application id, FQ endpoint name
96
trace.setEndpointName( ctx.getFullyQualifiedName() );
97         trace.setApplicationID(appId);
98         trace.setCallFlowEnabled(ctx.isCallFlowEnabled());
99
100         // set request content, size and http header
101
setRequestInfo(ctx.getMessageContext(), trace);
102
103         // set transport type
104
setTransportType(ctx, trace);
105
106         // sets client host
107
setClientHost(ctx.getMessageContext(), trace);
108
109         _messages.put(messageId, trace);
110     }
111
112     /**
113      * Sets client host.
114      */

115     private void setClientHost(SOAPMessageContext smc,
116             com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) {
117
118         if (smc!= null) {
119             Iterator JavaDoc iter = smc.getPropertyNames();
120             if ( iter == null) {
121                 return;
122             }
123              CoyoteRequestFacade httpReq =
124                   (CoyoteRequestFacade) smc.getProperty(HTTP_REQUEST);
125              String JavaDoc clientHost = null;
126              if (httpReq != null) {
127                  clientHost = httpReq.getRemoteAddr();
128                   trace.setClientHost(clientHost);
129             }
130         }
131     }
132
133     /**
134      * Sets SOAP message content, request size and HTTP headers.
135      */

136     private void setRequestInfo(SOAPMessageContext ctx,
137             com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) {
138
139         ByteArrayOutputStream JavaDoc baos = null;
140         try {
141             // sets the SOAP message header
142
trace.setHTTPRequestHeaders(ctx.getHTTPRequestHeaders());
143             baos = getMsgAsBytes(ctx);
144
145             // sets the request size
146
trace.setRequestSize( baos.size() );
147             String JavaDoc soapMsg = baos.toString();
148             trace.setRequestContent(soapMsg);
149
150         } catch (Exception JavaDoc e) {
151             String JavaDoc msg =_stringMgr.getString(
152                 "MessageTraceFactory_NoMessageTrace", e.getMessage());
153             _logger.log(Level.INFO, msg);
154         }finally {
155             try {
156                 if (baos != null) {
157                     baos.close();
158                 }
159             } catch (Exception JavaDoc e) {
160                 _logger.log(Level.FINE, "Web Service message stream could not be closed : " + e.getMessage());
161             }
162         }
163     }
164
165     /**
166      * Sets the transport type.
167      */

168     private void setTransportType(FilterContext ctx,
169             com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) {
170
171         TransportInfo ti = ctx.getTransportInfo();
172         if (ti != null) {
173             TransportType type = ti.getTransportType();
174             trace.setTransportType( type.name() );
175         }
176     }
177
178     /**
179      * Processes the SOAP response.
180      *
181      * @param ctx filter context
182      */

183     void processResponse(FilterContext ctx) {
184
185         String JavaDoc messageId = ctx.getMessageId();
186
187         com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace =
188             (com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl)
189             _messages.get(messageId);
190
191         SOAPMessageContext mctx = ctx.getMessageContext();
192
193         // sets response content, size and message header
194
setResponseInfo(mctx, trace);
195
196         // sets caller principal
197
setCallerPrincipal(trace);
198
199         // sets fault code, fault string and fault actor
200
setFaultInfo(mctx, trace);
201
202     }
203
204     /**
205      * Post Processing of the SOAP response.
206      *
207      * @param ctx filter context
208      */

209     MessageTrace postProcessResponse(FilterContext ctx) {
210
211         String JavaDoc messageId = ctx.getMessageId();
212
213         com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace =
214             (com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl)
215             _messages.remove(messageId);
216
217         // sets response time
218
setResponseTime(trace, ctx.getExecutionTime());
219
220         return trace;
221     }
222
223     /**
224      * Sets faults information for a response.
225      */

226     private void setFaultInfo(SOAPMessageContext smc,
227             com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) {
228
229         if (smc != null) {
230             SOAPMessage JavaDoc sm = smc.getMessage();
231             try {
232                 if (sm != null) {
233                     SOAPBody JavaDoc sb = sm.getSOAPBody();
234                     if (sb != null) {
235                         SOAPFault JavaDoc fault = sb.getFault();
236
237                         if ( fault != null) {
238                             trace.setFaultCode( fault.getFaultCode() );
239                             trace.setFaultString( fault.getFaultString() );
240                             trace.setFaultActor( fault.getFaultString() );
241                         }
242                     }
243                 }
244             } catch (SOAPException JavaDoc se) {
245                 String JavaDoc msg = "Error while reading SOAP fault information";
246                 _logger.log(Level.FINE, msg, se);
247             }
248         }
249     }
250
251     /**
252      * Sets caller principal information.
253      */

254     private void setCallerPrincipal(
255             com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) {
256
257         SecurityContext sc = SecurityContext.getCurrent();
258         if (sc != null) {
259             Principal JavaDoc p = sc.getCallerPrincipal();
260             if (p != null) {
261                 trace.setPrincipalName( p.getName() );
262                 _logger.fine("[MTF] Caller Principal: " + p.getName());
263             }
264         }
265     }
266
267     /**
268      * Sets response time for this request.
269      */

270     private void setResponseTime(
271         com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace, long t) {
272
273         trace.setResponseTime(t);
274     }
275
276     /**
277      * Returns the SOAP message as an output stream.
278      */

279     private ByteArrayOutputStream JavaDoc getMsgAsBytes(SOAPMessageContext smc) {
280
281         ByteArrayOutputStream JavaDoc baos = null;
282
283         if (smc!= null) {
284             baos = new ByteArrayOutputStream JavaDoc();
285             try {
286                 smc.getMessage().writeTo(baos);
287             } catch (Exception JavaDoc e) {
288                 String JavaDoc msg = "Error while retrieving SOAP message";
289                 _logger.log(Level.FINE, msg, e);
290             }
291         }
292
293         return baos;
294     }
295
296     /**
297      * Sets the response content, size and message header
298      */

299     private void setResponseInfo(SOAPMessageContext ctx,
300             com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) {
301
302         ByteArrayOutputStream JavaDoc baos = null;
303         try {
304             baos = getMsgAsBytes(ctx);
305
306             if (baos == null) {
307                 return;
308             }
309
310             // sets the request size
311
trace.setResponseSize( baos.size() );
312             String JavaDoc soapMsg = baos.toString();
313             trace.setResponseContent(soapMsg);
314
315             // sets the SOAP message header
316
trace.setHTTPResponseHeaders(ctx.getHTTPResponseHeaders());
317         } catch (Exception JavaDoc e) {
318             String JavaDoc msg =_stringMgr.getString(
319                 "MessageTraceFactory_NoMessageTrace", e.getMessage());
320             _logger.log(Level.INFO, msg);
321         }finally {
322             try {
323                 if (baos != null) {
324                     baos.close();
325                 }
326             } catch (Exception JavaDoc e) {
327                 _logger.log(Level.FINE, "Web Service message stream could not be closed : " + e.getMessage());
328             }
329         }
330     }
331
332     // ---- PRIVATE - VARIABLES --------------------
333
private Map JavaDoc _messages = null;
334     private static MessageTraceFactory _instance = null;
335     private Pattern JavaDoc _msgBodyPattern = null;
336     private static final String JavaDoc HTTP_REQUEST =
337                 "com.sun.xml.rpc.server.http.HttpServletRequest";
338     private static final Logger JavaDoc _logger =
339         Logger.getLogger(LogDomains.ADMIN_LOGGER);
340     private static final StringManager _stringMgr =
341         StringManager.getManager(MessageTraceFactory.class);
342 }
343
Popular Tags