Skip to content

Commit e7ed4f2

Browse files
author
Simon MacMullen
committed
Merge bug21647 (allow consumers to be informed of queue death)
2 parents 5e8c767 + 8dd0cf0 commit e7ed4f2

File tree

8 files changed

+60
-34
lines changed

8 files changed

+60
-34
lines changed

build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<path id="test.javac.classpath">
2020
<path refid="javac.classpath"/>
21-
<!-- <pathelement path="${junit.home}/junit.jar"/> -->
21+
<!-- <pathelement path="${junit.home}/junit.jar"/> -->
2222
</path>
2323

2424
<path id="test.classpath">

src/com/rabbitmq/client/Connection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.rabbitmq.client;
1818

1919
import java.io.IOException;
20+
import java.net.InetAddress;
2021
import java.util.Map;
2122

2223
/**
@@ -46,7 +47,7 @@ public interface Connection extends ShutdownNotifier { // rename to AMQPConnecti
4647
* Retrieve the host.
4748
* @return the hostname of the peer we're connected to.
4849
*/
49-
String getHost();
50+
InetAddress getAddress();
5051

5152
/**
5253
* Retrieve the port number.

src/com/rabbitmq/client/DefaultSaslConfig.java

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,54 @@
2020
import javax.security.sasl.Sasl;
2121
import javax.security.sasl.SaslClient;
2222
import javax.security.sasl.SaslException;
23-
import java.util.Map;
23+
import java.util.Arrays;
24+
import java.util.HashSet;
25+
import java.util.List;
26+
import java.util.Set;
2427

2528
/**
2629
* Default implementation of SaslConfig that uses the standard Java
2730
* algorithm for selecting a sasl client.
2831
* @see com.rabbitmq.client.ConnectionFactory
2932
*/
3033
public class DefaultSaslConfig implements SaslConfig {
31-
private ConnectionFactory factory;
32-
private String authorizationId;
33-
private Map<String,?> mechanismProperties;
34-
private CallbackHandler callbackHandler;
34+
public static final String[] DEFAULT_PREFERRED_MECHANISMS = new String[]{"PLAIN"};
3535

36-
public DefaultSaslConfig(ConnectionFactory factory) {
37-
this.factory = factory;
38-
callbackHandler = new UsernamePasswordCallbackHandler(factory);
39-
}
36+
private final ConnectionFactory factory;
37+
private final List<String> mechanisms;
38+
private final CallbackHandler callbackHandler;
4039

41-
public void setAuthorizationId(String authorizationId) {
42-
this.authorizationId = authorizationId;
40+
/**
41+
* Create a DefaultSaslConfig which only wants to use PLAIN.
42+
*
43+
* @param factory - the ConnectionFactory to use to obtain username, password and host
44+
*/
45+
public DefaultSaslConfig(ConnectionFactory factory) {
46+
this(factory, DEFAULT_PREFERRED_MECHANISMS);
4347
}
4448

45-
public void setMechanismProperties(Map<String, ?> mechanismProperties) {
46-
this.mechanismProperties = mechanismProperties;
49+
/**
50+
* Create a DefaultSaslConfig with a list of mechanisms to use.
51+
*
52+
* @param factory - the ConnectionFactory to use to obtain username, password and host
53+
* @param mechanisms - a list of SASL mechanisms to use (in descending order of preference)
54+
*/
55+
public DefaultSaslConfig(ConnectionFactory factory, String[] mechanisms) {
56+
this.factory = factory;
57+
callbackHandler = new UsernamePasswordCallbackHandler(factory);
58+
this.mechanisms = Arrays.asList(mechanisms);
4759
}
4860

49-
public void setCallbackHandler(CallbackHandler callbackHandler) {
50-
this.callbackHandler = callbackHandler;
51-
}
61+
public SaslClient getSaslClient(String[] serverMechanisms) throws SaslException {
62+
Set<String> server = new HashSet<String>(Arrays.asList(serverMechanisms));
5263

53-
public SaslClient getSaslClient(String[] mechanisms) throws SaslException {
54-
return Sasl.createSaslClient(mechanisms, authorizationId, "AMQP",
55-
factory.getHost(), mechanismProperties, callbackHandler);
64+
for (String mechanism: mechanisms) {
65+
if (server.contains(mechanism)) {
66+
SaslClient saslClient = Sasl.createSaslClient(new String[]{mechanism},
67+
null, "AMQP", factory.getHost(), null, callbackHandler);
68+
if (saslClient != null) return saslClient;
69+
}
70+
}
71+
return null;
5672
}
5773
}

src/com/rabbitmq/client/impl/AMQConnection.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.EOFException;
2121
import java.io.IOException;
22+
import java.net.InetAddress;
2223
import java.net.SocketException;
2324
import java.util.HashMap;
2425
import java.util.Map;
@@ -152,8 +153,8 @@ public void ensureIsOpen()
152153
public Map<String, Object> _serverProperties;
153154

154155
/** {@inheritDoc} */
155-
public String getHost() {
156-
return _frameHandler.getHost();
156+
public InetAddress getAddress() {
157+
return _frameHandler.getAddress();
157158
}
158159

159160
/** {@inheritDoc} */
@@ -242,7 +243,7 @@ public void start()
242243

243244
// start the main loop going
244245
Thread ml = new MainLoop();
245-
ml.setName("AMQP Connection " + getHost() + ":" + getPort());
246+
ml.setName("AMQP Connection " + getHostAddress() + ":" + getPort());
246247
ml.start();
247248

248249
AMQP.Connection.Start connStart = null;
@@ -560,7 +561,7 @@ public void handleConnectionClose(Command closeCommand) {
560561
_brokerInitiatedShutdown = true;
561562
Thread scw = new SocketCloseWait(sse);
562563
scw.setName("AMQP Connection Closing Monitor " +
563-
getHost() + ":" + getPort());
564+
getHostAddress() + ":" + getPort());
564565
scw.start();
565566
}
566567

@@ -727,6 +728,10 @@ public void close(int closeCode,
727728
}
728729

729730
@Override public String toString() {
730-
return "amqp://" + _factory.getUsername() + "@" + getHost() + ":" + getPort() + _virtualHost;
731+
return "amqp://" + _factory.getUsername() + "@" + getHostAddress() + ":" + getPort() + _virtualHost;
732+
}
733+
734+
private String getHostAddress() {
735+
return getAddress() == null ? null : getAddress().getHostAddress();
731736
}
732737
}

src/com/rabbitmq/client/impl/FrameHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.rabbitmq.client.impl;
1818

1919
import java.io.IOException;
20+
import java.net.InetAddress;
2021
import java.net.SocketException;
2122
import java.net.SocketTimeoutException;
2223

@@ -25,8 +26,8 @@
2526
*/
2627

2728
public interface FrameHandler {
28-
/** Retrieve hostname of peer. */
29-
public String getHost();
29+
/** Retrieve address of peer. */
30+
public InetAddress getAddress();
3031

3132
/** Retrieve port number of peer. */
3233
public int getPort();

src/com/rabbitmq/client/impl/SocketFrameHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.DataInputStream;
2222
import java.io.DataOutputStream;
2323
import java.io.IOException;
24+
import java.net.InetAddress;
2425
import java.net.Socket;
2526
import java.net.SocketException;
2627

@@ -55,8 +56,8 @@ public SocketFrameHandler(Socket socket) throws IOException {
5556
_outputStream = new DataOutputStream(new BufferedOutputStream(_socket.getOutputStream()));
5657
}
5758

58-
public String getHost() {
59-
return _socket.getInetAddress().getHostName();
59+
public InetAddress getAddress() {
60+
return _socket.getInetAddress();
6061
}
6162

6263
public int getPort() {

test/src/com/rabbitmq/client/test/AMQConnectionTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.rabbitmq.client.test;
1818

1919
import java.io.IOException;
20+
import java.net.InetAddress;
2021
import java.net.SocketException;
2122
import java.net.SocketTimeoutException;
2223
import java.util.ArrayList;
@@ -159,8 +160,8 @@ public int getTimeout() throws SocketException {
159160
return 0;
160161
}
161162

162-
public String getHost() {
163-
return "MockFrameHandler";
163+
public InetAddress getAddress() {
164+
return null;
164165
}
165166

166167
public int getPort() {

test/src/com/rabbitmq/client/test/BrokenFramesTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.rabbitmq.client.test;
1919

2020
import java.io.IOException;
21+
import java.net.InetAddress;
2122
import java.net.SocketException;
2223
import java.util.ArrayList;
2324
import java.util.Iterator;
@@ -145,8 +146,8 @@ public int getTimeout() throws SocketException {
145146
return 0;
146147
}
147148

148-
public String getHost() {
149-
return "MyFrameHandler";
149+
public InetAddress getAddress() {
150+
return null;
150151
}
151152

152153
public int getPort() {

0 commit comments

Comments
 (0)