Example usage for org.springframework.jms.connection UserCredentialsConnectionFactoryAdapter setUsername

List of usage examples for org.springframework.jms.connection UserCredentialsConnectionFactoryAdapter setUsername

Introduction

In this page you can find the example usage for org.springframework.jms.connection UserCredentialsConnectionFactoryAdapter setUsername.

Prototype

public void setUsername(String username) 

Source Link

Document

Set the username that this adapter should use for retrieving Connections.

Usage

From source file:net.lr.jmsbridge.ConnectionPool.java

public ConnectionFactory getConnectionFactory(UserNameAndPassword auth) {
    ConnectionFactory connectionFactory = connectionFactoryMap.get(auth);
    if (connectionFactory != null) {
        return connectionFactory;
    }/* w  w  w. ja  v  a 2 s  . co  m*/
    try {
        Context context = new InitialContext();
        Context envContext = (Context) context.lookup("java:comp/env");
        ConnectionFactory innerConnectionFactory = (ConnectionFactory) envContext
                .lookup("jms/ConnectionFactory");
        UserCredentialsConnectionFactoryAdapter credConnectionFactory = new UserCredentialsConnectionFactoryAdapter();
        credConnectionFactory.setTargetConnectionFactory(innerConnectionFactory);
        credConnectionFactory.setUsername(auth.getUserName());
        credConnectionFactory.setPassword(auth.getPassword());
        connectionFactory = new SingleConnectionFactory(innerConnectionFactory);
        connectionFactoryMap.put(auth, connectionFactory);
        return connectionFactory;
    } catch (NamingException e) {
        throw new RuntimeException("Could not find Connectionfactory in JNDI", e);
    }
}

From source file:com.jmstoolkit.queuebrowser.QueueBrowserView.java

private ConnectionFactory wrapConnectionFactory(String inJNDIName) throws NamingException {
    UserCredentialsConnectionFactoryAdapter uccfa = new UserCredentialsConnectionFactoryAdapter();
    uccfa.setUsername(appProperties.getProperty("jmstoolkit.username"));
    uccfa.setPassword(appProperties.getProperty("jmstoolkit.password"));
    uccfa.setTargetConnectionFactory((ConnectionFactory) jndiTemplate.lookup(inJNDIName));
    return uccfa;
}

From source file:com.headstrong.fusion.messaging.model.endpoint.binding.JmsBindingEndPointModeler.java

/**
 * Creates a {@link JmsComponent} using the parameters set.
 * //from  w  ww . j  a  v a2 s .  c o m
 * @param routeBuilder
 * @return {@link JmsComponent}
 * @throws ProcessModellingException
 */
@SuppressWarnings("unchecked")
private Component getJmsComponent(RouteBuilder routeBuilder) throws ProcessModellingException {
    JmsComponent jmsComponent = null;
    if (this.getProvider().equals(Provider.activemq.toString())) {
        jmsComponent = ActiveMQComponent.activeMQComponent();
        jmsComponent.setConnectionFactory(new PooledConnectionFactory(this.getBrokerUrl()));
        jmsComponent.setCamelContext(routeBuilder.getContext());
        jmsComponent.setAcknowledgementMode(Session.AUTO_ACKNOWLEDGE);
    } else if (this.getProvider().equals(Provider.ibmmq.toString())) {
        JmsConnectionFactory factory = null;
        try {
            JmsFactoryFactory jmsFactoryFactory;
            jmsFactoryFactory = JmsFactoryFactory.getInstance(JmsConstants.WMQ_PROVIDER);
            factory = jmsFactoryFactory.createConnectionFactory();
            factory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
            factory.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, this.getQueueManager());
            factory.setStringProperty(WMQConstants.WMQ_HOST_NAME, this.getBrokerUrl());
            factory.setIntProperty(WMQConstants.WMQ_PORT, this.getPort());
            if (this.getChannel() != null && !this.getChannel().isEmpty())
                factory.setStringProperty(WMQConstants.WMQ_CHANNEL, this.getChannel());
            factory.setIntProperty(WMQConstants.DELIVERY_MODE, this.getDeliveryMode());
        } catch (JMSException e) {
            logger.error("Error connecting to JMS provider.", e);
            throw new ProcessModellingException("Error connecting to JMS provider.", e);
        }
        UserCredentialsConnectionFactoryAdapter adapter = new UserCredentialsConnectionFactoryAdapter();
        adapter.setTargetConnectionFactory(factory);
        if (this.getUserName() != null) {
            adapter.setUsername(this.getUserName());
        }
        if (this.getPassword() != null) {
            adapter.setPassword(this.getPassword());
        }
        jmsComponent = JmsComponent.jmsComponent();
        jmsComponent.setConnectionFactory(adapter);
        jmsComponent.setCamelContext(routeBuilder.getContext());
        jmsComponent.setAcknowledgementMode(Session.AUTO_ACKNOWLEDGE);
    } else {
        Hashtable<String, String> prop = new Hashtable<String, String>();
        // Mandatory property.
        prop.put(Context.PROVIDER_URL, this.getLdapConfiguration().getProviderUrl());
        prop.put(Context.INITIAL_CONTEXT_FACTORY, this.getLdapConfiguration().getInitialContextFactory());

        // Only these optional properties supported now.
        if (this.getLdapConfiguration().getSecurityAuthentication() != null) {
            prop.put(Context.SECURITY_AUTHENTICATION, this.getLdapConfiguration().getSecurityAuthentication());
        }
        if (this.getLdapConfiguration().getSecutiryPrincipal() != null) {
            prop.put(Context.SECURITY_PRINCIPAL, this.getLdapConfiguration().getSecutiryPrincipal());
        }
        if (this.getLdapConfiguration().getSecutiryCredentials() != null) {
            prop.put(Context.SECURITY_CREDENTIALS, this.getLdapConfiguration().getSecutiryCredentials());
        }
        Context context;
        ConnectionFactory connectionFactory;
        try {
            // HACK required to avoid ClassNotFoundException while
            // retrieving the
            // InitialContext.
            ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
                context = new InitialContext(prop);
                connectionFactory = (ConnectionFactory) context
                        .lookup(this.getLdapConfiguration().getConnectionFactoryJndiName());
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
            // HACK -- Finished
        } catch (NamingException e) {
            logger.error("Error connecting to JMS provider.", e);
            throw new ProcessModellingException("Error connecting to JMS provider.", e);
        }
        UserCredentialsConnectionFactoryAdapter adapter = new UserCredentialsConnectionFactoryAdapter();
        adapter.setTargetConnectionFactory(connectionFactory);
        if (this.getUserName() != null) {
            adapter.setUsername(this.getUserName());
        }
        if (this.getPassword() != null) {
            adapter.setPassword(this.getPassword());
        }
        jmsComponent = JmsComponent.jmsComponent();
        jmsComponent.setConnectionFactory(adapter);
        jmsComponent.setCamelContext(routeBuilder.getContext());
        jmsComponent.setAcknowledgementMode(Session.AUTO_ACKNOWLEDGE);
    }

    if (jmsComponent != null) {
        jmsComponent.setConcurrentConsumers(getThreadCount());
    }
    return jmsComponent;
}

From source file:org.apache.camel.component.jms.JmsComponent.java

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters)
        throws Exception {

    boolean pubSubDomain = false;
    boolean tempDestination = false;
    if (remaining.startsWith(JmsConfiguration.QUEUE_PREFIX)) {
        pubSubDomain = false;//from  w  ww .  ja v a 2  s  . c  om
        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 = new JmsTemporaryTopicEndpoint(uri, this, subject, newConfiguration);
        } else {
            endpoint = new JmsEndpoint(uri, this, subject, pubSubDomain, newConfiguration);
        }
    } else {
        QueueBrowseStrategy strategy = getQueueBrowseStrategy();
        if (tempDestination) {
            endpoint = new JmsTemporaryQueueEndpoint(uri, this, subject, newConfiguration, strategy);
        } else {
            endpoint = new JmsQueueEndpoint(uri, this, subject, newConfiguration, strategy);
        }
    }

    String selector = getAndRemoveParameter(parameters, "selector", String.class);
    if (selector != null) {
        endpoint.setSelector(selector);
    }
    String username = getAndRemoveParameter(parameters, "username", String.class);
    String password = getAndRemoveParameter(parameters, "password", String.class);
    if (username != null && password != null) {
        ConnectionFactory cf = endpoint.getConfiguration().getConnectionFactory();
        UserCredentialsConnectionFactoryAdapter ucfa = new UserCredentialsConnectionFactoryAdapter();
        ucfa.setTargetConnectionFactory(cf);
        ucfa.setPassword(password);
        ucfa.setUsername(username);
        endpoint.getConfiguration().setConnectionFactory(ucfa);
    } else {
        if (username != null || password != null) {
            // exclude the the saturation of username and password are all empty
            throw new IllegalArgumentException("The JmsComponent's username or password is null");
        }
    }

    // jms header strategy
    String strategyVal = getAndRemoveParameter(parameters, KEY_FORMAT_STRATEGY_PARAM, String.class);
    if ("default".equalsIgnoreCase(strategyVal)) {
        endpoint.setJmsKeyFormatStrategy(new DefaultJmsKeyFormatStrategy());
    } else if ("passthrough".equalsIgnoreCase(strategyVal)) {
        endpoint.setJmsKeyFormatStrategy(new PassThroughJmsKeyFormatStrategy());
    } else { // a reference
        parameters.put(KEY_FORMAT_STRATEGY_PARAM, strategyVal);
        endpoint.setJmsKeyFormatStrategy(resolveAndRemoveReferenceParameter(parameters,
                KEY_FORMAT_STRATEGY_PARAM, JmsKeyFormatStrategy.class));
    }

    setProperties(endpoint.getConfiguration(), parameters);
    endpoint.setHeaderFilterStrategy(getHeaderFilterStrategy());

    return endpoint;
}