Package net.lr.db.datasource

Source Code of net.lr.db.datasource.Activator

package net.lr.db.datasource;

import java.util.Dictionary;
import java.util.Hashtable;

import javax.sql.DataSource;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class Activator implements BundleActivator {
   

    public final class DataSourceConfig implements ManagedService {
        private final BundleContext context;
        private ServiceRegistration serviceReg = null;

        public DataSourceConfig(BundleContext context) {
            this.context = context;
        }

        @SuppressWarnings("rawtypes")
        private String getString(String key, Dictionary properties) {
            Object value = properties.get(key);
            return (value == null || !(value instanceof String)) ? "" : (String)value;
           
        }

        @SuppressWarnings("rawtypes")
        @Override
        public void updated(Dictionary properties) throws ConfigurationException {
            if (serviceReg != null) {
                try {
                    serviceReg.unregister();
                } catch (Exception e) {
                }
                serviceReg = null;
            }
            MysqlDataSource ds = new MysqlDataSource();
            ds.setUrl(getString("url", properties));
            ds.setUser(getString("user", properties));
            ds.setPassword(getString("password", properties));

            Dictionary<String, String> regProperties = new Hashtable<String, String>();
            regProperties.put("osgi.jndi.service.name" , getString("name", properties));
            serviceReg = context.registerService(DataSource.class, ds, regProperties);
        }
    }

    @Override
    public void start(final BundleContext context) throws Exception {
        ManagedService managedService = new DataSourceConfig(context);
        Dictionary<String, String> properties = new Hashtable<String, String>();
        properties.put(Constants.SERVICE_PID, "mysqldatasource");
        context.registerService(ManagedService.class, managedService, properties);
    }

    @Override
    public void stop(BundleContext context) throws Exception {
    }

}
TOP

Related Classes of net.lr.db.datasource.Activator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
bject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-20639858-1', 'auto'); ga('send', 'pageview');