/**
 * Copyright (c) 2009-2012 EBM WebSourcing, 2012-2013 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see <http://www.gnu.org/licenses/>
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.bc.sql;

import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jbi.JBIException;

import org.ow2.petals.bc.sql.service.SQLService;
import org.ow2.petals.bc.sql.su.SUManager;
import org.ow2.petals.component.framework.bc.DefaultBindingComponent;
import org.ow2.petals.component.framework.jbidescriptor.generated.Provides;
import org.ow2.petals.component.framework.su.AbstractServiceUnitManager;

/**
 * @author Adrien Louis - EBM WebSourcing
 */
public class SqlComponent extends DefaultBindingComponent {

    private DataSources dataSources;

    private SQLService sqlService;

    private ExecutorService threadPool;

    /**
     * initialize the component
     * 
     * @throws JBIException
     *             impossible to initialize the component
     */
    @Override
    protected final void doInit() throws javax.jbi.JBIException {
        this.dataSources = new DataSources(this.getLogger());
        this.sqlService = new SQLService(this.getLogger());
        this.threadPool = Executors.newCachedThreadPool();
    }

    @Override
    protected final void doShutdown() throws javax.jbi.JBIException {
        super.doShutdown();
        // stop active outStreamer
        this.getLogger().warning("shutdown outmessage streams if any");
        final List<Runnable> interruptedOutStreamerList = this.threadPool.shutdownNow();
        if (interruptedOutStreamerList.size() > 0) {
            this.getLogger().warning("some outmessage streams have been interrupted");
        }

        // close data sources
        this.getLogger().warning("close datasources if any");
        final Set<Provides> dataSourcesKeys = this.dataSources.getDataSourceKeys();
        for (final Provides key : dataSourcesKeys) {
            this.getLogger().warning(
                    "Close dataSource for " + key.getServiceName() + "-" + key.getEndpointName());
            this.dataSources.closeDataSource(this.dataSources.getDataSource(key));
        }
    }

    @Override
    protected AbstractServiceUnitManager createServiceUnitManager() {
        return new SUManager(this);
    }

    public SQLService getSQLService() {
        return this.sqlService;
    }

    public DataSources getDataSources() {
        return this.dataSources;
    }

    public ExecutorService getThreadPool() {
        return this.threadPool;
    }

}
