001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.osgi.cf;
018
019import java.util.Dictionary;
020import java.util.Hashtable;
021
022import javax.jms.ConnectionFactory;
023
024import org.apache.activemq.ActiveMQConnectionFactory;
025import org.apache.activemq.jms.pool.PooledConnectionFactory;
026import org.osgi.framework.BundleContext;
027import org.osgi.framework.ServiceRegistration;
028import org.osgi.service.component.ComponentContext;
029import org.osgi.service.component.annotations.Activate;
030import org.osgi.service.component.annotations.Component;
031import org.osgi.service.component.annotations.ConfigurationPolicy;
032import org.osgi.service.component.annotations.Deactivate;
033
034@Component //
035( //
036    configurationPid = "org.apache.activemq", //
037    immediate = true, //
038    configurationPolicy = ConfigurationPolicy.REQUIRE //
039)
040public class ConnectionFactoryProvider {
041
042    private static final String OSGI_JNDI_SERVICE_NAME = "osgi.jndi.service.name";
043    private ServiceRegistration<ConnectionFactory> reg;
044
045    @Activate
046    public void create(ComponentContext compContext) {
047        BundleContext context = compContext.getBundleContext();
048        Dictionary<String, Object> config = compContext.getProperties();
049        String brokerURL = getString(config, "url", "tcp://localhost:61616");
050        String jndiName = getString(config, OSGI_JNDI_SERVICE_NAME, "jms/local");
051        String userName = getString(config, "userName", null);
052        String password = getString(config, "password", null);
053        long expiryTimeout = new Long(getString(config, "expiryTimeout", "0"));
054        int idleTimeout = new Integer(getString(config, "idleTimeout", "30000"));
055        int maxConnections = new Integer(getString(config, "maxConnections", "8"));
056        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerURL);
057        if (userName != null) {
058            cf.setUserName(userName);
059            cf.setPassword(password);
060        }
061        PooledConnectionFactory pcf = new PooledConnectionFactory();
062        pcf.setConnectionFactory(cf);
063        pcf.setExpiryTimeout(expiryTimeout);
064        pcf.setIdleTimeout(idleTimeout);
065        pcf.setMaxConnections(maxConnections);
066        Dictionary<String, String> props = new Hashtable<String, String>();
067        props.put(OSGI_JNDI_SERVICE_NAME, jndiName);
068        reg = context.registerService(ConnectionFactory.class, pcf, props);
069    }
070    
071    @Deactivate
072    public void deactivate() {
073        reg.unregister();
074    }
075
076    private String getString(Dictionary<String, Object> config, String key, String defaultValue) {
077        Object value = config.get(key);
078        return value != null ? value.toString() : defaultValue;
079    }
080}