Skip to content

Commit f7f922e

Browse files
Merge pull request #130 from rabbitmq/rabbitmq-jms-client-128-rmq-object-factory-compatibility
Make RMQConnectionFactory and RMQDestination compatible with RMQObjectFactory
2 parents 025230a + 5e04bf1 commit f7f922e

File tree

6 files changed

+125
-7
lines changed

6 files changed

+125
-7
lines changed

src/main/java/com/rabbitmq/jms/admin/RMQConnectionFactory.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,22 @@ private static String uriVirtualHostEscape(String vHost) {
556556
*/
557557
@Override
558558
public Reference getReference() throws NamingException {
559-
Reference ref = new Reference(RMQConnectionFactory.class.getName());
559+
Reference ref = new Reference(RMQConnectionFactory.class.getName(), RMQObjectFactory.class.getName(), null);
560560
addStringRefProperty(ref, "uri", this.getUri());
561+
addStringRefProperty(ref, "host", this.getHost());
562+
addStringRefProperty(ref, "password", this.getPassword());
563+
addIntegerRefProperty(ref, "port", this.getPort());
561564
addIntegerRefProperty(ref, "queueBrowserReadMax", this.getQueueBrowserReadMax());
562565
addIntegerRefProperty(ref, "onMessageTimeoutMs", this.getOnMessageTimeoutMs());
566+
addIntegerRefProperty(ref, "channelsQos", this.getChannelsQos());
567+
addBooleanProperty(ref, "ssl", this.ssl);
568+
addLongRefProperty(ref, "terminationTimeout", this.getTerminationTimeout());
569+
addStringRefProperty(ref, "username", this.getUsername());
570+
addStringRefProperty(ref, "virtualHost", this.getVirtualHost());
571+
addBooleanProperty(ref, "cleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose",
572+
this.isCleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose());
573+
addBooleanProperty(ref, "declareReplyToDestination",
574+
this.declareReplyToDestination);
563575
return ref;
564576
}
565577

@@ -591,6 +603,35 @@ private static void addIntegerRefProperty(Reference ref,
591603
ref.add(ra);
592604
}
593605

606+
/**
607+
* Adds an long valued property to a Reference (as a RefAddr).
608+
* @param ref - the reference to contain the value
609+
* @param propertyName - the name of the property
610+
* @param value - the value to store with the property
611+
*/
612+
private static void addLongRefProperty(Reference ref,
613+
String propertyName,
614+
Long value) {
615+
if (value == null || propertyName == null) return;
616+
RefAddr ra = new StringRefAddr(propertyName, String.valueOf(value));
617+
ref.add(ra);
618+
}
619+
620+
/**
621+
* Adds a boolean valued property to a Reference (as a StringRefAddr) if the value is <code>true</code>
622+
* (default <code>false</code> on read assumed).
623+
* @param ref - the reference to contain the value
624+
* @param propertyName - the name of the property
625+
* @param value - the value to store with the property
626+
*/
627+
private static final void addBooleanProperty(Reference ref,
628+
String propertyName,
629+
boolean value) {
630+
if (propertyName==null) return;
631+
RefAddr ra = new StringRefAddr(propertyName, String.valueOf(value));
632+
ref.add(ra);
633+
}
634+
594635
/**
595636
* {@inheritDoc}
596637
*/

src/main/java/com/rabbitmq/jms/admin/RMQDestination.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public String getQueueName() throws JMSException {
260260

261261
@Override
262262
public Reference getReference() throws NamingException {
263-
Reference ref = new Reference(this.getClass().getCanonicalName());
263+
Reference ref = new Reference(this.getClass().getCanonicalName(), RMQObjectFactory.class.getName(), null);
264264
addStringProperty(ref, "destinationName", this.destinationName);
265265
addBooleanProperty(ref, "amqp", this.amqp);
266266
addBooleanProperty(ref, "isQueue", this.isQueue);

src/main/java/com/rabbitmq/jms/admin/RMQObjectFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,25 @@ public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable<?,
143143
* javax.jms.ConnectionFactory
144144
* javax.jms.QueueConnectionFactory
145145
* javax.jms.TopicConnectionFactory
146+
* com.rabbitmq.jms.admin.RMQConnectionFactory
146147
* javax.jms.Topic
147148
* javax.jms.Queue
149+
* com.rabbitmq.jms.admin.RMQDestination
148150
*
149151
*/
150152
boolean topic = false;
151153
boolean connectionFactory = false;
152154
if ( javax.jms.QueueConnectionFactory.class.getName().equals(className)
153155
|| javax.jms.TopicConnectionFactory.class.getName().equals(className)
154156
|| javax.jms.ConnectionFactory.class.getName().equals(className)
157+
|| RMQConnectionFactory.class.getName().equals(className)
155158
) {
156159
connectionFactory = true;
157160
} else if (javax.jms.Topic.class.getName().equals(className)) {
158161
topic = true;
159162
} else if (javax.jms.Queue.class.getName().equals(className)) {
163+
} else if (RMQDestination.class.getName().equals(className)) {
164+
topic = !getBooleanProperty(ref, environment, "isQueue", true, false);
160165
} else {
161166
throw new NamingException(String.format("Unknown class [%s]", className));
162167
}

src/test/java/com/rabbitmq/jms/admin/RMQConnectionFactoryTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import javax.naming.StringRefAddr;
1616
import java.io.ByteArrayOutputStream;
1717
import java.io.ObjectOutputStream;
18+
import java.lang.reflect.Field;
1819
import java.util.Enumeration;
1920
import java.util.Hashtable;
2021
import java.util.List;
@@ -34,8 +35,18 @@ public class RMQConnectionFactoryTest {
3435
static {
3536
RMQConnectionFactory defaultFact = new RMQConnectionFactory();
3637
defaultProps.setProperty("uri", defaultFact.getUri());
38+
defaultProps.setProperty("host", defaultFact.getHost());
39+
defaultProps.setProperty("password", defaultFact.getPassword());
40+
defaultProps.setProperty("port", "5672");
3741
defaultProps.setProperty("queueBrowserReadMax", "0");
3842
defaultProps.setProperty("onMessageTimeoutMs", "2000");
43+
defaultProps.setProperty("channelsQos", "-1");
44+
defaultProps.setProperty("ssl", "false");
45+
defaultProps.setProperty("terminationTimeout", "15000");
46+
defaultProps.setProperty("username", "guest");
47+
defaultProps.setProperty("virtualHost", "/");
48+
defaultProps.setProperty("cleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose", "false");
49+
defaultProps.setProperty("declareReplyToDestination", "true");
3950
}
4051

4152
private static Properties getProps(Reference ref) {
@@ -87,7 +98,7 @@ private static void removeRefProperty(Reference ref,
8798
public void testDefaultConnectionFactoryReference() throws Exception {
8899
RMQConnectionFactory connFactory = new RMQConnectionFactory();
89100
Reference ref = connFactory.getReference();
90-
101+
assertThat(getProps(ref)).hasSameSizeAs(defaultProps);
91102
assertEquals(defaultProps, getProps(ref), "Not the default properties");
92103
}
93104

@@ -121,10 +132,14 @@ public void testConnectionFactoryRegeneration() throws Exception {
121132
connFactory.setPassword("my-password");
122133
connFactory.setPort(42);
123134
connFactory.setQueueBrowserReadMax(52);
135+
connFactory.setOnMessageTimeoutMs(66);
136+
connFactory.setChannelsQos(250);
124137
connFactory.useSslProtocol();
125138
connFactory.setTerminationTimeout(1234567890123456789L);
126139
connFactory.setUsername("fred");
127140
connFactory.setVirtualHost("bill");
141+
connFactory.setCleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose(true);
142+
connFactory.setDeclareReplyToDestination(false);
128143

129144
Reference ref = connFactory.getReference();
130145

@@ -136,12 +151,19 @@ public void testConnectionFactoryRegeneration() throws Exception {
136151
assertEquals("my-password", newFactory.getPassword(), "Not the correct password");
137152
assertEquals(42, newFactory.getPort(), "Not the correct port");
138153
assertEquals(52, newFactory.getQueueBrowserReadMax(), "Not the correct queueBrowserReadMax");
154+
assertEquals(66, newFactory.getOnMessageTimeoutMs());
155+
assertEquals(250, newFactory.getChannelsQos());
139156
assertEquals(true, newFactory.isSsl(), "Not the correct ssl");
140157

141-
assertEquals(15000L, newFactory.getTerminationTimeout(), "Not the correct terminationTimeout");
158+
assertEquals(1234567890123456789L, newFactory.getTerminationTimeout(), "Not the correct terminationTimeout");
142159

143160
assertEquals("fred", newFactory.getUsername(), "Not the correct username");
144161
assertEquals("bill", newFactory.getVirtualHost(), "Not the correct virtualHost");
162+
assertTrue(newFactory.isCleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose());
163+
164+
Field declareReplyToDestinationField = RMQConnectionFactory.class.getDeclaredField("declareReplyToDestination");
165+
declareReplyToDestinationField.setAccessible(true);
166+
assertFalse((Boolean) declareReplyToDestinationField.get(newFactory));
145167
}
146168

147169
@Test
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Copyright (c) 2020 VMware, Inc. or its affiliates. All rights reserved. */
2+
package com.rabbitmq.jms.admin;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
import javax.naming.Reference;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
public class RMQDestinationTest {
11+
12+
RMQObjectFactory rmqObjectFactory = new RMQObjectFactory();
13+
14+
@Test
15+
void queueRegeneration() throws Exception {
16+
RMQDestination queue = new RMQDestination("queue", true, false);
17+
Reference reference = queue.getReference();
18+
RMQDestination newQueue = (RMQDestination) rmqObjectFactory.getObjectInstance(reference, null, null, null);
19+
assertThat(newQueue.isQueue()).isTrue();
20+
assertThat(newQueue.getDestinationName()).isEqualTo("queue");
21+
assertThat(newQueue.isAmqp()).isFalse();
22+
}
23+
24+
@Test
25+
void topicRegeneration() throws Exception {
26+
RMQDestination topic = new RMQDestination("topic", false, false);
27+
Reference reference = topic.getReference();
28+
RMQDestination newTopic = (RMQDestination) rmqObjectFactory.getObjectInstance(reference, null, null, null);
29+
assertThat(newTopic.isQueue()).isFalse();
30+
assertThat(newTopic.getDestinationName()).isEqualTo("topic");
31+
assertThat(newTopic.isAmqp()).isFalse();
32+
}
33+
34+
@Test
35+
void amqpDestinationRegeneration() throws Exception {
36+
RMQDestination destination = new RMQDestination(
37+
"destination", "exchange", "routing-key", "queue"
38+
);
39+
Reference reference = destination.getReference();
40+
RMQDestination newReference = (RMQDestination) rmqObjectFactory.getObjectInstance(reference, null, null, null);
41+
assertThat(newReference.isQueue()).isTrue();
42+
assertThat(newReference.getDestinationName()).isEqualTo("destination");
43+
assertThat(newReference.isAmqp()).isTrue();
44+
assertThat(newReference.getAmqpExchangeName()).isEqualTo("exchange");
45+
assertThat(newReference.getAmqpRoutingKey()).isEqualTo("routing-key");
46+
assertThat(newReference.getAmqpQueueName()).isEqualTo("queue");
47+
}
48+
49+
}

src/test/java/com/rabbitmq/jms/admin/RMQObjectFactoryTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Copyright (c) 2018-2020 VMware, Inc. or its affiliates. All rights reserved. */
12
package com.rabbitmq.jms.admin;
23

34
import org.junit.jupiter.api.Test;
@@ -21,7 +22,7 @@ public class RMQObjectFactoryTest {
2122
private RMQObjectFactory rmqObjectFactory = new RMQObjectFactory();
2223

2324
@Test
24-
public void getObjectInstanceShouldCreateARMQConnectionFactoryViaReference() throws Exception {
25+
public void getObjectInstanceShouldCreateAMQPConnectionFactoryViaReference() throws Exception {
2526

2627
Reference ref = new Reference(ConnectionFactory.class.getName());
2728

@@ -42,7 +43,7 @@ public void getObjectInstanceShouldCreateARMQConnectionFactoryViaReference() thr
4243

4344

4445
@Test
45-
public void getObjectInstanceShouldCreateARMQConnectionFactoryViaEnvironment() throws Exception {
46+
public void getObjectInstanceShouldCreateAMQPConnectionFactoryViaEnvironment() throws Exception {
4647

4748
Hashtable<?, ?> environment = new Hashtable<Object, Object>() {{
4849
put("className", "javax.jms.ConnectionFactory");
@@ -69,7 +70,7 @@ public void getObjectInstanceShouldCreateARMQConnectionFactoryViaEnvironment() t
6970
}
7071

7172
@Test
72-
public void getObjectInstanceShouldCreateARMQDestinationQUEUEViaEnvironment() throws Exception {
73+
public void getObjectInstanceShouldCreateAMQPDestinationQUEUEViaEnvironment() throws Exception {
7374

7475
Hashtable<?, ?> environment = new Hashtable<Object, Object>() {{
7576
put("className", "javax.jms.Queue");

0 commit comments

Comments
 (0)