Search in sources :

Example 1 with UserCredentialsConnectionFactoryAdapter

use of org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter in project camel by apache.

the class JmsEndpointConfigurationTest method testSetConnectionFactoryAndUsernameAndPassword.

@Test
public void testSetConnectionFactoryAndUsernameAndPassword() throws Exception {
    JmsEndpoint endpoint = resolveMandatoryEndpoint("jms:topic:Foo.Bar?connectionFactory=#myConnectionFactory&username=James&password=ABC", JmsEndpoint.class);
    ConnectionFactory cf = endpoint.getConfiguration().getConnectionFactory();
    assertNotNull("The connectionFactory should not be null", cf);
    assertTrue("The connectionFactory should be the instance of UserCredentialsConnectionFactoryAdapter", cf instanceof UserCredentialsConnectionFactoryAdapter);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) UserCredentialsConnectionFactoryAdapter(org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter) Test(org.junit.Test)

Example 2 with UserCredentialsConnectionFactoryAdapter

use of org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter in project camel by apache.

the class JmsEndpointConfigurationTest method testSetUsernameAndPassword.

@Test
public void testSetUsernameAndPassword() throws Exception {
    JmsEndpoint endpoint = resolveMandatoryEndpoint("jms:topic:Foo.Bar?username=James&password=ABC", JmsEndpoint.class);
    ConnectionFactory cf = endpoint.getConfiguration().getConnectionFactory();
    assertNotNull("The connectionFactory should not be null", cf);
    assertTrue("The connectionFactory should be the instance of UserCredentialsConnectionFactoryAdapter", cf instanceof UserCredentialsConnectionFactoryAdapter);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) UserCredentialsConnectionFactoryAdapter(org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter) Test(org.junit.Test)

Example 3 with UserCredentialsConnectionFactoryAdapter

use of org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter in project camel by apache.

the class JmsComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    boolean pubSubDomain = false;
    boolean tempDestination = false;
    if (ObjectHelper.isNotEmpty(remaining)) {
        if (remaining.startsWith(JmsConfiguration.QUEUE_PREFIX)) {
            pubSubDomain = false;
            remaining = removeStartingCharacters(remaining.substring(JmsConfiguration.QUEUE_PREFIX.length()), '/');
        } else if (remaining.startsWith(JmsConfiguration.TOPIC_PREFIX)) {
            pubSubDomain = true;
            remaining = removeStartingCharacters(remaining.substring(JmsConfiguration.TOPIC_PREFIX.length()), '/');
        } else if (remaining.startsWith(JmsConfiguration.TEMP_QUEUE_PREFIX)) {
            pubSubDomain = false;
            tempDestination = true;
            remaining = removeStartingCharacters(remaining.substring(JmsConfiguration.TEMP_QUEUE_PREFIX.length()), '/');
        } else if (remaining.startsWith(JmsConfiguration.TEMP_TOPIC_PREFIX)) {
            pubSubDomain = true;
            tempDestination = true;
            remaining = removeStartingCharacters(remaining.substring(JmsConfiguration.TEMP_TOPIC_PREFIX.length()), '/');
        }
    }
    final String subject = convertPathToActualDestination(remaining, parameters);
    // lets make sure we copy the configuration as each endpoint can
    // customize its own version
    JmsConfiguration newConfiguration = getConfiguration().copy();
    JmsEndpoint endpoint;
    if (pubSubDomain) {
        if (tempDestination) {
            endpoint = createTemporaryTopicEndpoint(uri, this, subject, newConfiguration);
        } else {
            endpoint = createTopicEndpoint(uri, this, subject, newConfiguration);
        }
    } else {
        QueueBrowseStrategy strategy = getQueueBrowseStrategy();
        if (tempDestination) {
            endpoint = createTemporaryQueueEndpoint(uri, this, subject, newConfiguration, strategy);
        } else {
            endpoint = createQueueEndpoint(uri, this, subject, newConfiguration, strategy);
        }
    }
    // resolve any custom connection factory first
    ConnectionFactory cf = resolveAndRemoveReferenceParameter(parameters, "connectionFactory", ConnectionFactory.class);
    if (cf != null) {
        endpoint.getConfiguration().setConnectionFactory(cf);
    }
    // if username or password provided then wrap the connection factory
    String cfUsername = getAndRemoveParameter(parameters, "username", String.class, getConfiguration().getUsername());
    String cfPassword = getAndRemoveParameter(parameters, "password", String.class, getConfiguration().getPassword());
    if (cfUsername != null && cfPassword != null) {
        cf = endpoint.getConfiguration().getConnectionFactory();
        ObjectHelper.notNull(cf, "ConnectionFactory");
        LOG.debug("Wrapping existing ConnectionFactory with UserCredentialsConnectionFactoryAdapter using username: {} and password: ******", cfUsername);
        UserCredentialsConnectionFactoryAdapter ucfa = new UserCredentialsConnectionFactoryAdapter();
        ucfa.setTargetConnectionFactory(cf);
        ucfa.setPassword(cfPassword);
        ucfa.setUsername(cfUsername);
        endpoint.getConfiguration().setConnectionFactory(ucfa);
    } else {
        // if only username or password was provided then fail
        if (cfUsername != null || cfPassword != null) {
            if (cfUsername == null) {
                throw new IllegalArgumentException("Username must also be provided when using username/password as credentials.");
            } else {
                throw new IllegalArgumentException("Password must also be provided when using username/password as credentials.");
            }
        }
    }
    // jms header strategy
    String strategyVal = getAndRemoveParameter(parameters, KEY_FORMAT_STRATEGY_PARAM, String.class);
    JmsKeyFormatStrategy strategy = resolveStandardJmsKeyFormatStrategy(strategyVal);
    if (strategy != null) {
        endpoint.setJmsKeyFormatStrategy(strategy);
    } else {
        // its not a standard, but a reference
        parameters.put(KEY_FORMAT_STRATEGY_PARAM, strategyVal);
        endpoint.setJmsKeyFormatStrategy(resolveAndRemoveReferenceParameter(parameters, KEY_FORMAT_STRATEGY_PARAM, JmsKeyFormatStrategy.class));
    }
    MessageListenerContainerFactory messageListenerContainerFactory = resolveAndRemoveReferenceParameter(parameters, "messageListenerContainerFactoryRef", MessageListenerContainerFactory.class);
    if (messageListenerContainerFactory == null) {
        messageListenerContainerFactory = resolveAndRemoveReferenceParameter(parameters, "messageListenerContainerFactory", MessageListenerContainerFactory.class);
    }
    if (messageListenerContainerFactory != null) {
        endpoint.setMessageListenerContainerFactory(messageListenerContainerFactory);
    }
    setProperties(endpoint.getConfiguration(), parameters);
    endpoint.setHeaderFilterStrategy(getHeaderFilterStrategy());
    return endpoint;
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) UserCredentialsConnectionFactoryAdapter(org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter)

Aggregations

ConnectionFactory (javax.jms.ConnectionFactory)3 UserCredentialsConnectionFactoryAdapter (org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter)3 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)2 Test (org.junit.Test)2