-
Notifications
You must be signed in to change notification settings - Fork 33
/
MessageManagerMonitor.java
323 lines (260 loc) · 11.9 KB
/
MessageManagerMonitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright (c) 2000, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
/*
* @(#)MessageManagerMonitor.java 1.3 06/28/07
*/
package com.sun.messaging.jmq.jmsserver.management.mbeans;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.Vector;
import java.util.Hashtable;
import java.util.HashMap;
import java.nio.ByteBuffer;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanException;
import com.sun.messaging.jmq.io.*;
import com.sun.messaging.jms.management.server.*;
import com.sun.messaging.jmq.jmsserver.Globals;
import com.sun.messaging.jmq.jmsserver.core.Destination;
import com.sun.messaging.jmq.jmsserver.core.PacketReference;
import com.sun.messaging.jmq.jmsserver.util.BrokerException;
import com.sun.messaging.jmq.util.io.FilteringObjectInputStream;
public class MessageManagerMonitor extends MQMBeanReadOnly {
private static MBeanParameterInfo[] getMessageInfoSignature = {
new MBeanParameterInfo("destinationType", String.class.getName(), mbr.getString(mbr.I_DST_MGR_OP_PARAM_DEST_TYPE)),
new MBeanParameterInfo("destinationName", String.class.getName(), mbr.getString(mbr.I_DST_MGR_OP_PARAM_DEST_NAME)),
new MBeanParameterInfo("messageID", String.class.getName(), "Message ID"),
new MBeanParameterInfo("startMsgIndex", Long.class.getName(), "Start Message Index"),
new MBeanParameterInfo("maxNumMsgsRetrieved", Long.class.getName(), "Maximum Number of Messages Retrieved"),
new MBeanParameterInfo("getBody", Boolean.class.getName(), "Get Body") };
private static MBeanOperationInfo[] ops = { new MBeanOperationInfo("getMessageInfo", "Get information on messages from a destination",
getMessageInfoSignature, Vector.class.getName(), MBeanOperationInfo.INFO) };
public Vector getMessageInfo(String destinationType, String destinationName, String messageID, Long startMsgIndex, Long maxNumMsgsRetrieved,
Boolean getBody) throws MBeanException {
Vector msgInfo = new Vector();
try {
HashMap destNameType = new HashMap();
if ((destinationName == null) || (destinationType == null)) {
throw new BrokerException("Destination name and type not specified");
}
Destination[] ds = DL.getDestination(null, destinationName, (destinationType.equals(DestinationType.QUEUE)));
Destination d = ds[0]; // PART
if (d == null) {
throw new BrokerException(rb.getString(rb.X_DESTINATION_NOT_FOUND, destinationName));
}
if (getBody == null) {
getBody = Boolean.FALSE;
}
if (messageID != null) {
d.load();
SysMessageID sysMsgID = SysMessageID.get(messageID);
PacketReference pr = getPacketReference(sysMsgID);
if (pr != null) {
HashMap h = constructMessageInfo(sysMsgID, getBody.booleanValue(), destNameType);
msgInfo.add(h);
} else {
throw new BrokerException("Could not locate message " + messageID + " in destination " + destinationName);
}
} else {
SysMessageID sysMsgIDs[] = d.getSysMessageIDs(startMsgIndex, maxNumMsgsRetrieved);
for (int i = 0; i < sysMsgIDs.length; ++i) {
HashMap h = constructMessageInfo(sysMsgIDs[i], getBody.booleanValue(), destNameType);
msgInfo.add(h);
}
}
} catch (Exception e) {
handleOperationException("getMessageInfo", e);
}
return (msgInfo);
}
@Override
public String getMBeanName() {
return ("MessageManagerMonitor");
}
@Override
public String getMBeanDescription() {
return ("Monitoring MBean for Message Manager");
/*
* return (mbr.getString(mbr.I_MSG_MGR_MON_DESC));
*/
}
@Override
public MBeanAttributeInfo[] getMBeanAttributeInfo() {
return (null);
}
@Override
public MBeanOperationInfo[] getMBeanOperationInfo() {
return (ops);
}
@Override
public MBeanNotificationInfo[] getMBeanNotificationInfo() {
return (null);
}
private HashMap constructMessageInfo(SysMessageID sysMsgID, boolean getBody, HashMap destNameType) throws BrokerException {
HashMap h = new HashMap();
PacketReference pr = getPacketReference(sysMsgID);
Packet pkt = pr.getPacket();
HashMap msgHeaders = pr.getHeaders();
// Destination d = pr.getDestination();
String corrID = pkt.getCorrelationID(), errMsg;
String destType = DestinationType.QUEUE;
byte b[] = null;
h.put("CorrelationID", corrID);
if (corrID != null) {
try {
b = corrID.getBytes("UTF8");
} catch (Exception e) {
}
}
h.put("CorrelationIDAsBytes", b);
h.put("DeliveryMode",
(pkt.getPersistent()) ? Integer.valueOf(jakarta.jms.DeliveryMode.PERSISTENT) : Integer.valueOf(jakarta.jms.DeliveryMode.NON_PERSISTENT));
h.put("DestinationName", pkt.getDestination());
if (pkt.getIsQueue()) {
destType = DestinationType.QUEUE;
} else {
destType = DestinationType.TOPIC;
}
h.put("DestinationType", destType);
h.put("Expiration", Long.valueOf(pkt.getExpiration()));
h.put("MessageID", msgHeaders.get("JMSMessageID"));
h.put("Priority", Integer.valueOf(pkt.getPriority()));
h.put("Redelivered", Boolean.valueOf(pkt.getRedelivered()));
/*
* The ReplyTo information in the packet contains the destination name and class name (i.e. dest class name), which the
* broker cannot really use. We need to query/check if: - the destination exists - what it's type is
*/
String replyToDestName = pkt.getReplyTo();
if (replyToDestName != null) {
boolean destFound = false, isQueue = true;
if (destNameType != null) {
Boolean isQ = (Boolean) destNameType.get(replyToDestName);
if (isQ != null) {
isQueue = isQ.booleanValue();
destFound = true;
}
}
if (!destFound) {
try {
Destination topic, queue;
Destination[] ds = DL.findDestination(null, replyToDestName, true);
queue = ds[0]; // PART
ds = DL.findDestination(null, replyToDestName, false);
topic = ds[0];
if ((queue != null) && (topic != null)) {
errMsg = "Cannot determine type of ReplyTo destination." + " There is a topic and queue with the name: " + replyToDestName;
throw new BrokerException(errMsg);
} else if (queue != null) {
destFound = true;
isQueue = true;
} else if (topic != null) {
destFound = true;
isQueue = false;
}
if (destFound) {
/*
* Cache dest name/type so that we can look it up there next time.
*/
destNameType.put(replyToDestName, Boolean.valueOf(isQueue));
}
// else:
/*
* It is possible that this destination no longer exists. e.g. Temporary destination, whose connection has gone away.
* Not sure how to proceed at this point.
*/
} catch (Exception e) {
errMsg = "Caught exception while determining ReplyTo destination type";
throw new BrokerException(errMsg);
}
}
h.put("ReplyToDestinationName", replyToDestName);
if (destFound) {
if (isQueue) {
destType = DestinationType.QUEUE;
} else {
destType = DestinationType.TOPIC;
}
h.put("ReplyToDestinationType", destType);
}
}
h.put("Timestamp", Long.valueOf(pkt.getTimestamp()));
h.put("Type", pkt.getMessageType());
Hashtable msgProps;
try {
msgProps = pr.getProperties();
} catch (Exception e) {
msgProps = null;
}
h.put("MessageProperties", msgProps);
int packetType = pr.getPacket().getPacketType();
h.put("MessageBodyType", Integer.valueOf(packetType));
if (getBody) {
ByteBuffer bb = pr.getPacket().getMessageBodyByteBuffer();
byte[] msgBody = null;
if (bb.hasArray()) {
msgBody = bb.array();
}
switch (packetType) {
case PacketType.TEXT_MESSAGE:
try {
String textMsg = new String(msgBody, "UTF8");
h.put("MessageBody", textMsg);
} catch (Exception e) {
errMsg = "Caught exception while creating text message body";
throw new BrokerException(errMsg);
}
break;
case PacketType.BYTES_MESSAGE:
case PacketType.STREAM_MESSAGE:
h.put("MessageBody", msgBody);
break;
case PacketType.MAP_MESSAGE:
try {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(msgBody);
ObjectInputStream objectInputStream = new FilteringObjectInputStream(byteArrayInputStream);
HashMap mapMsg = (HashMap) objectInputStream.readObject();
h.put("MessageBody", mapMsg);
} catch (Exception e) {
errMsg = "Caught exception while creating map message body";
throw new BrokerException(errMsg);
}
break;
case PacketType.OBJECT_MESSAGE:
try {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(msgBody);
ObjectInputStream objectInputStream = new FilteringObjectInputStream(byteArrayInputStream);
Object objMsg = objectInputStream.readObject();
h.put("MessageBody", objMsg);
} catch (Exception e) {
errMsg = "Caught exception while creating object message body";
throw new BrokerException(errMsg);
}
break;
default:
errMsg = "Unsupported message type for GET_MESSAGES handler: " + packetType;
throw new BrokerException(errMsg);
}
}
return (h);
}
private PacketReference getPacketReference(SysMessageID sysMsgID) {
return Globals.getDestinationList().get(null, sysMsgID);
}
}