Skip to content

Commit 8757597

Browse files
committed
Make RMQConnectionFactory and RMQDestination compatible with RMQObjectFactory
RMQConnectionFactory#getReference and RMQDestination#getReference now include enough information to re-create similar instances. All the properties supported in RMQObjectFactory are now included in RMQConnectionFactory#getReference. Fixes #128 (cherry picked from commit 5e04bf1) Conflicts: src/test/java/com/rabbitmq/jms/admin/RMQConnectionFactoryTest.java
1 parent aac59b2 commit 8757597

File tree

7 files changed

+136
-10
lines changed

7 files changed

+136
-10
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
@@ -599,10 +599,22 @@ private static String uriVirtualHostEscape(String vHost) {
599599
*/
600600
@Override
601601
public Reference getReference() throws NamingException {
602-
Reference ref = new Reference(RMQConnectionFactory.class.getName());
602+
Reference ref = new Reference(RMQConnectionFactory.class.getName(), RMQObjectFactory.class.getName(), null);
603603
addStringRefProperty(ref, "uri", this.getUri());
604+
addStringRefProperty(ref, "host", this.getHost());
605+
addStringRefProperty(ref, "password", this.getPassword());
606+
addIntegerRefProperty(ref, "port", this.getPort());
604607
addIntegerRefProperty(ref, "queueBrowserReadMax", this.getQueueBrowserReadMax());
605608
addIntegerRefProperty(ref, "onMessageTimeoutMs", this.getOnMessageTimeoutMs());
609+
addIntegerRefProperty(ref, "channelsQos", this.getChannelsQos());
610+
addBooleanProperty(ref, "ssl", this.ssl);
611+
addLongRefProperty(ref, "terminationTimeout", this.getTerminationTimeout());
612+
addStringRefProperty(ref, "username", this.getUsername());
613+
addStringRefProperty(ref, "virtualHost", this.getVirtualHost());
614+
addBooleanProperty(ref, "cleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose",
615+
this.isCleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose());
616+
addBooleanProperty(ref, "declareReplyToDestination",
617+
this.declareReplyToDestination);
606618
return ref;
607619
}
608620

@@ -634,6 +646,35 @@ private static void addIntegerRefProperty(Reference ref,
634646
ref.add(ra);
635647
}
636648

649+
/**
650+
* Adds an long valued property to a Reference (as a RefAddr).
651+
* @param ref - the reference to contain the value
652+
* @param propertyName - the name of the property
653+
* @param value - the value to store with the property
654+
*/
655+
private static void addLongRefProperty(Reference ref,
656+
String propertyName,
657+
Long value) {
658+
if (value == null || propertyName == null) return;
659+
RefAddr ra = new StringRefAddr(propertyName, String.valueOf(value));
660+
ref.add(ra);
661+
}
662+
663+
/**
664+
* Adds a boolean valued property to a Reference (as a StringRefAddr) if the value is <code>true</code>
665+
* (default <code>false</code> on read assumed).
666+
* @param ref - the reference to contain the value
667+
* @param propertyName - the name of the property
668+
* @param value - the value to store with the property
669+
*/
670+
private static final void addBooleanProperty(Reference ref,
671+
String propertyName,
672+
boolean value) {
673+
if (propertyName==null) return;
674+
RefAddr ra = new StringRefAddr(propertyName, String.valueOf(value));
675+
ref.add(ra);
676+
}
677+
637678
/**
638679
* {@inheritDoc}
639680
*/

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/integration/tests/RabbitAPIConnectionFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import com.rabbitmq.jms.admin.RMQConnectionFactory;
99

10+
import java.security.NoSuchAlgorithmException;
11+
1012
/**
1113
* Connection factory for use in integration tests.
1214
*/
@@ -39,7 +41,13 @@ public Connection createConnection(String userName, String password) throws JMSE
3941
return super.createConnection(userName, password);
4042
}
4143
};
42-
rmqCF.setSsl(testssl);
44+
if (testssl) {
45+
try {
46+
rmqCF.useSslProtocol();
47+
} catch (NoSuchAlgorithmException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
4351
rmqCF.setQueueBrowserReadMax(qbrMax);
4452
return rmqCF;
4553
}

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import javax.naming.StringRefAddr;
1818
import java.io.ByteArrayOutputStream;
1919
import java.io.ObjectOutputStream;
20+
import java.lang.reflect.Field;
2021
import java.util.Enumeration;
2122
import java.util.Hashtable;
2223
import java.util.List;
@@ -36,8 +37,18 @@ public class RMQConnectionFactoryTest {
3637
static {
3738
RMQConnectionFactory defaultFact = new RMQConnectionFactory();
3839
defaultProps.setProperty("uri", defaultFact.getUri());
40+
defaultProps.setProperty("host", defaultFact.getHost());
41+
defaultProps.setProperty("password", defaultFact.getPassword());
42+
defaultProps.setProperty("port", "5672");
3943
defaultProps.setProperty("queueBrowserReadMax", "0");
4044
defaultProps.setProperty("onMessageTimeoutMs", "2000");
45+
defaultProps.setProperty("channelsQos", "-1");
46+
defaultProps.setProperty("ssl", "false");
47+
defaultProps.setProperty("terminationTimeout", "15000");
48+
defaultProps.setProperty("username", "guest");
49+
defaultProps.setProperty("virtualHost", "/");
50+
defaultProps.setProperty("cleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose", "false");
51+
defaultProps.setProperty("declareReplyToDestination", "true");
4152
}
4253

4354
private static Properties getProps(Reference ref) {
@@ -89,7 +100,7 @@ private static void removeRefProperty(Reference ref,
89100
public void testDefaultConnectionFactoryReference() throws Exception {
90101
RMQConnectionFactory connFactory = new RMQConnectionFactory();
91102
Reference ref = connFactory.getReference();
92-
103+
assertThat(getProps(ref)).hasSameSizeAs(defaultProps);
93104
assertEquals(defaultProps, getProps(ref), "Not the default properties");
94105
}
95106

@@ -102,7 +113,7 @@ public void testUpdatedConnectionFactoryReference() throws Exception {
102113
connFactory.setPort(42);
103114
connFactory.setQueueBrowserReadMax(52);
104115
connFactory.setOnMessageTimeoutMs(62);
105-
connFactory.setSsl(true);
116+
connFactory.useSslProtocol();
106117
connFactory.setTerminationTimeout(1234567890123456789L);
107118
connFactory.setUsername("fred");
108119
connFactory.setVirtualHost("bill");
@@ -123,10 +134,14 @@ public void testConnectionFactoryRegeneration() throws Exception {
123134
connFactory.setPassword("my-password");
124135
connFactory.setPort(42);
125136
connFactory.setQueueBrowserReadMax(52);
126-
connFactory.setSsl(true);
137+
connFactory.setOnMessageTimeoutMs(66);
138+
connFactory.setChannelsQos(250);
139+
connFactory.useSslProtocol();
127140
connFactory.setTerminationTimeout(1234567890123456789L);
128141
connFactory.setUsername("fred");
129142
connFactory.setVirtualHost("bill");
143+
connFactory.setCleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose(true);
144+
connFactory.setDeclareReplyToDestination(false);
130145

131146
Reference ref = connFactory.getReference();
132147

@@ -138,12 +153,19 @@ public void testConnectionFactoryRegeneration() throws Exception {
138153
assertEquals("my-password", newFactory.getPassword(), "Not the correct password");
139154
assertEquals(42, newFactory.getPort(), "Not the correct port");
140155
assertEquals(52, newFactory.getQueueBrowserReadMax(), "Not the correct queueBrowserReadMax");
156+
assertEquals(66, newFactory.getOnMessageTimeoutMs());
157+
assertEquals(250, newFactory.getChannelsQos());
141158
assertEquals(true, newFactory.isSsl(), "Not the correct ssl");
142159

143-
assertEquals(15000L, newFactory.getTerminationTimeout(), "Not the correct terminationTimeout");
160+
assertEquals(1234567890123456789L, newFactory.getTerminationTimeout(), "Not the correct terminationTimeout");
144161

145162
assertEquals("fred", newFactory.getUsername(), "Not the correct username");
146163
assertEquals("bill", newFactory.getVirtualHost(), "Not the correct virtualHost");
164+
assertTrue(newFactory.isCleanUpServerNamedQueuesForNonDurableTopicsOnSessionClose());
165+
166+
Field declareReplyToDestinationField = RMQConnectionFactory.class.getDeclaredField("declareReplyToDestination");
167+
declareReplyToDestinationField.setAccessible(true);
168+
assertFalse((Boolean) declareReplyToDestinationField.get(newFactory));
147169
}
148170

149171
@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)