-
Notifications
You must be signed in to change notification settings - Fork 215
/
ConnectionBasedJmsConnectionFactory.java
129 lines (105 loc) · 5.52 KB
/
ConnectionBasedJmsConnectionFactory.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
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.amqp;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.net.URI;
import java.util.Hashtable;
import java.util.Map;
import java.util.function.Supplier;
import javax.annotation.concurrent.NotThreadSafe;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.qpid.jms.JmsConnection;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.service.messaging.internal.ssl.SSLContextCreator;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.logs.ConnectionLogger;
import org.eclipse.ditto.connectivity.service.messaging.tunnel.SshTunnelState;
import org.slf4j.LoggerFactory;
import akka.actor.ActorSystem;
/**
* Factory for creating a {@link javax.jms.Connection} based on a {@link Connection}.
*/
@NotThreadSafe
public final class ConnectionBasedJmsConnectionFactory implements JmsConnectionFactory {
private static final org.slf4j.Logger LOGGER =
LoggerFactory.getLogger(ConnectionBasedJmsConnectionFactory.class);
private static final String SECURE_AMQP_SCHEME = "amqps";
private final Map<String, String> defaultConfig;
private final Supplier<SshTunnelState> sshTunnelConfigSupplier;
private final PlainCredentialsSupplier credentialsSupplier;
private ConnectionBasedJmsConnectionFactory(final Map<String, String> defaultConfig,
final Supplier<SshTunnelState> sshTunnelConfigSupplier,
final PlainCredentialsSupplier credentialsSupplier) {
this.defaultConfig = checkNotNull(defaultConfig, "defaultConfig");
this.sshTunnelConfigSupplier = checkNotNull(sshTunnelConfigSupplier, "sshTunnelConfigSupplier");
this.credentialsSupplier = credentialsSupplier;
}
/**
* Returns an instance of {@code ConnectionBasedJmsConnectionFactory}.
*
* @param defaultConfig the default AMQP config.
* @return the instance.
*/
public static ConnectionBasedJmsConnectionFactory getInstance(final Map<String, String> defaultConfig,
final Supplier<SshTunnelState> sshTunnelConfigSupplier,
final ActorSystem actorSystem,
final boolean doubleEncodingEnabled) {
final PlainCredentialsSupplier credentialsSupplier = SaslPlainCredentialsSupplier.of(actorSystem);
return new ConnectionBasedJmsConnectionFactory(defaultConfig, sshTunnelConfigSupplier, credentialsSupplier);
}
@Override
public JmsConnection createConnection(final Connection connection, final ExceptionListener exceptionListener,
final ConnectionLogger connectionLogger, final String clientId) throws JMSException, NamingException {
checkNotNull(connection, "Connection");
checkNotNull(exceptionListener, "Exception Listener");
final Context ctx = createContext(connection, clientId);
final org.apache.qpid.jms.JmsConnectionFactory cf =
(org.apache.qpid.jms.JmsConnectionFactory) ctx.lookup(connection.getId().toString());
if (isSecuredConnection(connection)) {
cf.setSslContext(SSLContextCreator.fromConnection(connection, null, connectionLogger)
.withoutClientCertificate());
}
@SuppressWarnings("squid:S2095") final JmsConnection jmsConnection = (JmsConnection) cf.createConnection();
jmsConnection.setExceptionListener(exceptionListener);
return jmsConnection;
}
private String buildAmqpConnectionUri(final Connection connection, final String clientId) {
return buildAmqpConnectionUri(connection, clientId, sshTunnelConfigSupplier, defaultConfig,
credentialsSupplier);
}
public static String buildAmqpConnectionUri(final Connection connection,
final String clientId,
final Supplier<SshTunnelState> sshTunnelConfigSupplier,
final Map<String, String> defaultConfig,
final PlainCredentialsSupplier plainCredentialsSupplier) {
final URI uri = sshTunnelConfigSupplier.get().getURI(connection);
final var amqpSpecificConfig = AmqpSpecificConfig.withDefault(clientId, connection, defaultConfig,
plainCredentialsSupplier);
final var connectionUri = amqpSpecificConfig.render(uri.toString());
LOGGER.debug("[{}] URI: {}", clientId, connectionUri);
return connectionUri;
}
private Context createContext(final Connection connection, final String clientId) throws NamingException {
final String connectionUri = buildAmqpConnectionUri(connection, clientId);
@SuppressWarnings("squid:S1149") final Hashtable<Object, Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");
env.put("connectionfactory." + connection.getId(), connectionUri);
return new InitialContext(env);
}
static boolean isSecuredConnection(final Connection connection) {
return SECURE_AMQP_SCHEME.equalsIgnoreCase(connection.getProtocol());
}
}