1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jmonit.support.jaxrpc;
17
18 import java.util.Iterator;
19
20 import javax.xml.namespace.QName;
21 import javax.xml.rpc.Call;
22 import javax.xml.rpc.handler.GenericHandler;
23 import javax.xml.rpc.handler.MessageContext;
24 import javax.xml.rpc.handler.soap.SOAPMessageContext;
25 import javax.xml.soap.AttachmentPart;
26 import javax.xml.soap.SOAPException;
27 import javax.xml.soap.SOAPMessage;
28
29 import org.jmonit.Monitoring;
30 import org.jmonit.Probe;
31 import org.jmonit.log.Log;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public class JaxRpcMonitoringHandler
76 extends GenericHandler
77 {
78 private static final String PROBE = Probe.class.getName();
79
80
81 private static Log log = Log.getLog( JaxRpcMonitoringHandler.class );
82
83 public boolean handleRequest( MessageContext context )
84 {
85 Probe probe = Monitoring.start( getAction( context ) );
86 context.setProperty( PROBE, probe );
87 return true;
88 };
89
90 public boolean handleResponse( MessageContext context )
91 {
92 stopMonitoring( context, true );
93 return true;
94 };
95
96 public boolean handleFault( MessageContext context )
97 {
98 stopMonitoring( context, false );
99 return true;
100 };
101
102
103
104
105
106
107
108
109
110
111 protected String getAction( MessageContext context )
112 {
113 String soapAction = (String) context.getProperty( Call.SOAPACTION_URI_PROPERTY );
114 if ( soapAction == null && context instanceof SOAPMessageContext )
115 {
116 SOAPMessageContext soapContext = (SOAPMessageContext) context;
117 SOAPMessage message = soapContext.getMessage();
118 try
119 {
120 soapAction = message.getSOAPBody().getFirstChild().getNodeName();
121 }
122 catch ( Exception e )
123 {
124
125 }
126 }
127
128 if ( soapAction == null )
129 {
130 log.warn( "Failed to extract the SOAP action" );
131 soapAction = "unknown";
132 }
133
134
135
136 int i = soapAction.indexOf( ':' );
137 if ( i >= 0 )
138 {
139 soapAction = soapAction.substring( i + 1 );
140 }
141
142 return soapAction;
143 }
144
145
146
147
148
149
150
151
152
153 protected void stopMonitoring( MessageContext context, boolean success )
154 {
155 Probe probe = (Probe) context.getProperty( PROBE );
156 if ( !success )
157 {
158 Monitoring.add( getAction( context ) + ".faults", probe.getElapsedTime() );
159 }
160 probe.stop( success );
161 context.removeProperty( PROBE );
162
163 if ( context instanceof SOAPMessageContext )
164 {
165 SOAPMessageContext soapContext = (SOAPMessageContext) context;
166 SOAPMessage message = soapContext.getMessage();
167 try
168 {
169 long size = 0;
170 for ( Iterator iterator = message.getAttachments(); iterator.hasNext(); )
171 {
172 AttachmentPart attachment = (AttachmentPart) iterator.next();
173 size += attachment.getSize();
174 }
175 size += 0;
176 }
177 catch ( SOAPException e )
178 {
179
180 }
181 }
182 }
183
184
185
186
187
188
189 public QName[] getHeaders()
190 {
191 return new QName[0];
192 }
193 }