/
ActiveMQDestination.java
410 lines (334 loc) · 13.1 KB
/
ActiveMQDestination.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.jms.client;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.JMSRuntimeException;
import java.io.Serializable;
import java.util.Properties;
import java.util.UUID;
import org.apache.activemq.artemis.api.core.Pair;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.jndi.JNDIStorable;
/**
* ActiveMQ Artemis implementation of a JMS Destination.
*/
public class ActiveMQDestination extends JNDIStorable implements Destination, Serializable {
// Constants -----------------------------------------------------
// Static --------------------------------------------------------
private static final long serialVersionUID = 5027962425462382883L;
public static final String QUEUE_QUALIFIED_PREFIX = "queue://";
public static final String TOPIC_QUALIFIED_PREFIX = "topic://";
public static final String TEMP_QUEUE_QUALIFED_PREFIX = "temp-queue://";
public static final String TEMP_TOPIC_QUALIFED_PREFIX = "temp-topic://";
private static final char SEPARATOR = '.';
private static String escape(final String input) {
if (input == null) {
return "";
}
return input.replace("\\", "\\\\").replace(".", "\\.");
}
/**
* Static helper method for working with destinations.
*/
public static ActiveMQDestination createDestination(String name, TYPE defaultType) {
if (name.startsWith(QUEUE_QUALIFIED_PREFIX)) {
return new ActiveMQQueue(name.substring(QUEUE_QUALIFIED_PREFIX.length()));
} else if (name.startsWith(TOPIC_QUALIFIED_PREFIX)) {
return new ActiveMQTopic(name.substring(TOPIC_QUALIFIED_PREFIX.length()));
} else if (name.startsWith(TEMP_QUEUE_QUALIFED_PREFIX)) {
return new ActiveMQQueue(name.substring(TEMP_QUEUE_QUALIFED_PREFIX.length()), true);
} else if (name.startsWith(TEMP_TOPIC_QUALIFED_PREFIX)) {
return new ActiveMQTopic(name.substring(TEMP_TOPIC_QUALIFED_PREFIX.length()), true);
}
switch (defaultType) {
case QUEUE:
return new ActiveMQQueue(name);
case TOPIC:
return new ActiveMQTopic(name);
case TEMP_QUEUE:
return new ActiveMQQueue(name, true);
case TEMP_TOPIC:
return new ActiveMQTopic(name, true);
case DESTINATION:
return new ActiveMQDestination(name, TYPE.DESTINATION, null);
default:
throw new IllegalArgumentException("Invalid default destination type: " + defaultType);
}
}
public static Destination fromPrefixedName(final String name) {
if (name.startsWith(ActiveMQDestination.QUEUE_QUALIFIED_PREFIX)) {
String address = name.substring(ActiveMQDestination.QUEUE_QUALIFIED_PREFIX.length());
return createQueue(address);
} else if (name.startsWith(ActiveMQDestination.TOPIC_QUALIFIED_PREFIX)) {
String address = name.substring(ActiveMQDestination.TOPIC_QUALIFIED_PREFIX.length());
return createTopic(address);
} else if (name.startsWith(ActiveMQDestination.TEMP_QUEUE_QUALIFED_PREFIX)) {
String address = name.substring(ActiveMQDestination.TEMP_QUEUE_QUALIFED_PREFIX.length());
return new ActiveMQTemporaryQueue(address, null);
} else if (name.startsWith(ActiveMQDestination.TEMP_TOPIC_QUALIFED_PREFIX)) {
String address = name.substring(ActiveMQDestination.TEMP_TOPIC_QUALIFED_PREFIX.length());
return new ActiveMQTemporaryTopic(address, null);
} else {
return new ActiveMQDestination(name, TYPE.DESTINATION, null);
}
}
public static String createQueueNameForSubscription(final boolean isDurable,
final String clientID,
final String subscriptionName) {
if (clientID != null) {
if (isDurable) {
return ActiveMQDestination.escape(clientID) + SEPARATOR +
ActiveMQDestination.escape(subscriptionName);
} else {
return "nonDurable" + SEPARATOR +
ActiveMQDestination.escape(clientID) + SEPARATOR +
ActiveMQDestination.escape(subscriptionName);
}
} else {
if (isDurable) {
return ActiveMQDestination.escape(subscriptionName);
} else {
return "nonDurable" + SEPARATOR +
ActiveMQDestination.escape(subscriptionName);
}
}
}
public static String createQueueNameForSharedSubscription(final boolean isDurable,
final String clientID,
final String subscriptionName) {
if (clientID != null) {
return (isDurable ? "Durable" : "nonDurable") + SEPARATOR +
ActiveMQDestination.escape(clientID) + SEPARATOR +
ActiveMQDestination.escape(subscriptionName);
} else {
return (isDurable ? "Durable" : "nonDurable") + SEPARATOR +
ActiveMQDestination.escape(subscriptionName);
}
}
public static Pair<String, String> decomposeQueueNameForDurableSubscription(final String queueName) {
StringBuffer[] parts = new StringBuffer[2];
int currentPart = 0;
parts[0] = new StringBuffer();
parts[1] = new StringBuffer();
int pos = 0;
while (pos < queueName.length()) {
char ch = queueName.charAt(pos);
pos++;
if (ch == SEPARATOR) {
currentPart++;
if (currentPart >= parts.length) {
throw new JMSRuntimeException("Invalid message queue name: " + queueName);
}
continue;
}
if (ch == '\\') {
if (pos >= queueName.length()) {
throw new JMSRuntimeException("Invalid message queue name: " + queueName);
}
ch = queueName.charAt(pos);
pos++;
}
parts[currentPart].append(ch);
}
if (currentPart != 1) {
/* JMS 2.0 introduced the ability to create "shared" subscriptions which do not require a clientID.
* In this case the subscription name will be the same as the queue name, but the above algorithm will put that
* in the wrong position in the array so we need to move it.
*/
parts[1] = parts[0];
parts[0] = new StringBuffer();
}
Pair<String, String> pair = new Pair<>(parts[0].toString(), parts[1].toString());
return pair;
}
public static SimpleString createQueueAddressFromName(final String name) {
return new SimpleString(QUEUE_QUALIFIED_PREFIX + name);
}
public static SimpleString createTopicAddressFromName(final String name) {
return new SimpleString(TOPIC_QUALIFIED_PREFIX + name);
}
public static ActiveMQQueue createQueue(final String address) {
return new ActiveMQQueue(address);
}
public static ActiveMQTopic createTopic(final String address) {
return new ActiveMQTopic(address);
}
public static ActiveMQTemporaryQueue createTemporaryQueue(final String address, final ActiveMQSession session) {
return new ActiveMQTemporaryQueue(address, session);
}
public static ActiveMQTemporaryQueue createTemporaryQueue(final String address) {
return createTemporaryQueue(address, null);
}
public static ActiveMQTemporaryQueue createTemporaryQueue(final ActiveMQSession session) {
String address = UUID.randomUUID().toString();
return createTemporaryQueue(address, session);
}
public static ActiveMQTemporaryTopic createTemporaryTopic(final ActiveMQSession session) {
String address = UUID.randomUUID().toString();
return createTemporaryTopic(address, session);
}
public static ActiveMQTemporaryTopic createTemporaryTopic(String address, final ActiveMQSession session) {
return new ActiveMQTemporaryTopic(address, session);
}
public static ActiveMQTemporaryTopic createTemporaryTopic(String address) {
return createTemporaryTopic(address, null);
}
// Attributes ----------------------------------------------------
/**
* The core address
*/
private SimpleString simpleAddress;
private final TYPE type;
private final transient ActiveMQSession session;
// Constructors --------------------------------------------------
protected ActiveMQDestination(final String address,
final TYPE type,
final ActiveMQSession session) {
this.simpleAddress = SimpleString.toSimpleString(address);
this.type = type;
this.session = session;
}
protected ActiveMQDestination(final SimpleString address,
final TYPE type,
final ActiveMQSession session) {
this.simpleAddress = address;
this.type = type;
this.session = session;
}
public void setAddress(String address) {
setSimpleAddress(SimpleString.toSimpleString(address));
}
public void setSimpleAddress(SimpleString address) {
if (address == null) {
throw new IllegalArgumentException("address cannot be null");
}
this.simpleAddress = address;
}
public void delete() throws JMSException {
if (session != null) {
if (session.getCoreSession().isClosed()) {
// Temporary queues will be deleted when the connection is closed.. nothing to be done then!
return;
}
if (isQueue()) {
session.deleteTemporaryQueue(this);
} else {
session.deleteTemporaryTopic(this);
}
}
}
public boolean isQueue() {
return TYPE.isQueue(type);
}
// Public --------------------------------------------------------
public String getAddress() {
return simpleAddress.toString();
}
public SimpleString getSimpleAddress() {
return simpleAddress;
}
public String getName() {
return simpleAddress.toString();
}
public boolean isTemporary() {
return TYPE.isTemporary(type);
}
public TYPE getType() {
return type;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ActiveMQDestination)) {
return false;
}
ActiveMQDestination that = (ActiveMQDestination) o;
return simpleAddress.equals(that.simpleAddress);
}
@Override
public int hashCode() {
return simpleAddress.hashCode();
}
@Override
protected void buildFromProperties(Properties props) {
setAddress(props.getProperty("address"));
}
@Override
protected void populateProperties(Properties props) {
props.put("address", getAddress());
}
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
public enum TYPE {
QUEUE,
TOPIC,
TEMP_QUEUE,
TEMP_TOPIC,
DESTINATION; // unknown
public byte getType() {
switch (this) {
case QUEUE:
return 0;
case TOPIC:
return 1;
case TEMP_QUEUE:
return 2;
case TEMP_TOPIC:
return 3;
case DESTINATION:
return 4;
default:
return -1;
}
}
public static TYPE getType(byte type) {
switch (type) {
case 0:
return QUEUE;
case 1:
return TOPIC;
case 2:
return TEMP_QUEUE;
case 3:
return TEMP_TOPIC;
case 4:
return DESTINATION;
default:
return null;
}
}
public static boolean isQueue(TYPE type) {
boolean result = false;
if (type != null && (type.equals(QUEUE) || type.equals(TEMP_QUEUE))) {
result = true;
}
return result;
}
public static boolean isTemporary(TYPE type) {
boolean result = false;
if (type != null && (type.equals(TEMP_TOPIC) || type.equals(TEMP_QUEUE))) {
result = true;
}
return result;
}
}
}