All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.bc.ftp.FtpSuManager Maven / Gradle / Ivy

There is a newer version: 3.9.0
Show newest version
/**
 * Copyright (c) 2007-2012 EBM WebSourcing, 2012-2016 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.ftp;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.ow2.petals.bc.ftp.connection.FTPConnectionInfo;
import org.ow2.petals.bc.ftp.connection.FTPConnectionInfoBuilder;
import org.ow2.petals.bc.ftp.connection.WrappedFTPClient;
import org.ow2.petals.bc.ftp.connection.WrappedFTPClientFactory;
import org.ow2.petals.component.framework.AbstractComponent;
import org.ow2.petals.component.framework.api.configuration.SuConfigurationParameters;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.ow2.petals.component.framework.jbidescriptor.generated.Provides;
import org.ow2.petals.component.framework.su.AbstractServiceUnitManager;
import org.ow2.petals.component.framework.su.ServiceUnitDataHandler;

/**
 * @author Mathieu CARROLLE - EBM WebSourcing
 */
public class FtpSuManager extends AbstractServiceUnitManager {

    public FtpSuManager(final AbstractComponent component) {
        super(component);
    }

    @Override
    protected void doDeploy(final ServiceUnitDataHandler suDH) throws PEtALSCDKException {
        final Provides provides = suDH.getDescriptor().getServices().getProvides().get(0);
        final SuConfigurationParameters extensions = suDH.getConfigurationExtensions(provides);
        FTPConnectionInfo connectionConfiguration;
        try {
            connectionConfiguration = FTPConnectionInfoBuilder.buildFTPConnectionInfo(extensions,
                    provides.getRetrypolicy());
        } catch (MissingElementException e) {
            throw new PEtALSCDKException(e);
        }
        final WrappedFTPClientFactory factory = new WrappedFTPClientFactory(
                connectionConfiguration, this.logger);
        final GenericObjectPool pool = new GenericObjectPool(factory);
        pool.setConfig(this.configurePool(connectionConfiguration, pool));
        ((FTPComponent) getComponent()).getMapOfPool().put(provides, pool);
    }

    @Override
    protected void doUndeploy(final ServiceUnitDataHandler suDH) throws PEtALSCDKException {
        final Provides provides = suDH.getDescriptor().getServices().getProvides().get(0);
        final ObjectPool pool = ((FTPComponent) getComponent()).getMapOfPool().get(provides);

        final PEtALSCDKException ex = new PEtALSCDKException("Error during undeploy");

        try {
            this.logger.fine("Closing pool's resource");
            pool.clear();
            pool.close();
        } catch (Exception e) {
            ex.addSuppressed(e);
        }

        ex.throwIfNeeded();
    }

    /**
     * Configure the pool
     * 
     * @param connectionConfiguration
     * @param pool
     * @return
     */
    private Config configurePool(final FTPConnectionInfo connectionConfiguration,
            final GenericObjectPool pool) {
        Config conf = new Config();
        int maxConnection = connectionConfiguration.getMaxConnection();
        if (maxConnection <= 0) {
            conf.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
        } else {
            this.logger.config("Number of maximum connection for the pool: " + maxConnection);
            // On borrow action, we block until an object is free
            conf.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
            // Wait is unlimited;
            conf.maxWait = -1;
            conf.maxActive = maxConnection;
            conf.maxIdle = maxConnection;
        }
        // Minimun time before an object become eligible to eviction
        conf.minEvictableIdleTimeMillis = connectionConfiguration.getMaxIdleTime();
        conf.timeBetweenEvictionRunsMillis = connectionConfiguration.getMaxIdleTime() / 2;
        conf.testOnBorrow = true;
        return conf;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy