Example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

List of usage examples for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool.

Prototype

public GenericObjectPool(PoolableObjectFactory factory) 

Source Link

Document

Create a new GenericObjectPool using the specified factory.

Usage

From source file:org.mule.transport.ftp.FtpConnector.java

protected GenericObjectPool createPool(FtpConnectionFactory connectionFactory) {
    GenericObjectPool genericPool = new GenericObjectPool(connectionFactory);
    byte poolExhaustedAction = ThreadingProfile.DEFAULT_POOL_EXHAUST_ACTION;

    ThreadingProfile receiverThreadingProfile = this.getReceiverThreadingProfile();
    if (receiverThreadingProfile != null) {
        int threadingProfilePoolExhaustedAction = receiverThreadingProfile.getPoolExhaustedAction();
        if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_WAIT) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
        } else if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_ABORT) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        } else if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_RUN) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
        }/*from  www .  j a va  2s.c om*/
    }

    genericPool.setWhenExhaustedAction(poolExhaustedAction);
    genericPool.setTestOnBorrow(isValidateConnections());
    return genericPool;
}

From source file:org.mule.transport.ftps.FtpsConnector.java

protected GenericObjectPool createPool(FtpsConnectionFactory connectionFactory) {
    GenericObjectPool genericPool = new GenericObjectPool(connectionFactory);
    byte poolExhaustedAction = ThreadingProfile.DEFAULT_POOL_EXHAUST_ACTION;

    ThreadingProfile receiverThreadingProfile = this.getReceiverThreadingProfile();
    if (receiverThreadingProfile != null) {
        int threadingProfilePoolExhaustedAction = receiverThreadingProfile.getPoolExhaustedAction();
        if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_WAIT) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
        } else if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_ABORT) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        } else if (threadingProfilePoolExhaustedAction == ThreadingProfile.WHEN_EXHAUSTED_RUN) {
            poolExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
        }// ww w.  j  a va 2 s. co  m
    }

    genericPool.setWhenExhaustedAction(poolExhaustedAction);
    genericPool.setTestOnBorrow(isValidateConnections());
    return genericPool;
}

From source file:org.omadac.pool.impl.PoolingAdapter.java

public PoolingAdapter(Object prototype) {
    this.poolableServiceFactory = new PoolableServiceFactory(prototype);
    objectPool = new GenericObjectPool(poolableServiceFactory);
}

From source file:org.omadac.pool.impl.PoolingAdapter.java

public PoolingAdapter(BeanCreator beanCreator, ExecutionContext context) {
    this.poolableBeanCreator = new PoolableBeanCreator(beanCreator, context);
    objectPool = new GenericObjectPool(poolableBeanCreator);
}

From source file:org.openanzo.security.ldap.LdapConnectionManager.java

/**
 * //from   ww w  .  j a  va 2s.  c o m
 * @param configProperties
 * @throws AnzoException
 */
public LdapConnectionManager(Dictionary<? extends Object, ? extends Object> configProperties)
        throws AnzoException {
    factory = new LdapConnectionFactory(configProperties);
    pool = new GenericObjectPool(factory);
    pool.setTestOnBorrow(true);
}

From source file:org.openanzo.security.ldap.LdapConnectionManager.java

/**
 * @param userDN/*from w  w w .  ja  va2  s .com*/
 * @param userPassword
 * @param host
 * @param port
 * 
 */
public LdapConnectionManager(String userDN, String userPassword, String host, Integer port, boolean useSSL,
        String keystoreFile, String keystorePassword, String keystoreType, String truststoreFile,
        String truststorePassword, String truststoreType) {
    factory = new LdapConnectionFactory(userDN, userPassword, host, port, useSSL, keystoreFile,
            keystorePassword, keystoreType, truststoreFile, truststorePassword, truststoreType);
    pool = new GenericObjectPool(factory);
    pool.setTestOnBorrow(true);
}

From source file:org.openbravo.database.ConnectionProviderImpl.java

public void addNewPool(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns,
        int maxConns, double maxConnTime, String dbSessionConfig, String rdbms, String name) throws Exception {
    log4j.debug("Loading underlying JDBC driver.");
    try {/*from   ww  w  . j  av  a2s.  co m*/
        Class.forName(dbDriver);
    } catch (ClassNotFoundException e) {
        throw new Exception(e);
    }
    log4j.debug("Done.");

    GenericObjectPool connectionPool = new GenericObjectPool(null);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    connectionPool.setMaxActive(maxConns);
    connectionPool.setTestOnBorrow(false);
    connectionPool.setTestOnReturn(false);
    connectionPool.setTestWhileIdle(false);

    KeyedObjectPoolFactory keyedObject = new StackKeyedObjectPoolFactory();
    ConnectionFactory connectionFactory = new OpenbravoDriverManagerConnectionFactory(dbServer, dbLogin,
            dbPassword, dbSessionConfig, rdbms);
    @SuppressWarnings("unused")
    // required by dbcp
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, keyedObject, null, false, true);

    Class.forName("org.apache.commons.dbcp.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    driver.registerPool(contextName + "_" + name, connectionPool);

    if (this.defaultPoolName == null || this.defaultPoolName.equals("")) {
        this.defaultPoolName = name;
        this.bbdd = dbServer;
        this.rdbms = rdbms;
    }
}

From source file:org.opencms.db.CmsDbPool.java

/**
 * Creates a JDBC DriverManager based DBCP connection pool.<p>
 * /*from   w w w  . j av  a 2  s.  c  o  m*/
 * @param config the configuration (opencms.properties)
 * @param key the key of the database pool in the configuration
 * @return String the URL to access the created DBCP pool
 * @throws Exception if the pool could not be initialized
 */
public static PoolingDriver createDriverManagerConnectionPool(CmsParameterConfiguration config, String key)
        throws Exception {

    // read the values of the pool configuration specified by the given key
    String jdbcDriver = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_DRIVER);
    String jdbcUrl = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL);
    String jdbcUrlParams = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL_PARAMS);
    int maxActive = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_ACTIVE, 10);
    int maxWait = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_WAIT, 2000);
    int maxIdle = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_IDLE, 5);
    int minEvictableIdleTime = config
            .getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MIN_EVICTABLE_IDLE_TIME, 1800000);
    int minIdle = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MIN_IDLE, 0);
    int numTestsPerEvictionRun = config
            .getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_NUM_TESTS_PER_EVICTION_RUN, 3);
    int timeBetweenEvictionRuns = config
            .getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TIME_BETWEEN_EVICTION_RUNS, 3600000);
    String testQuery = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_QUERY);
    String username = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_USERNAME);
    String password = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_PASSWORD);
    String poolUrl = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_POOL_URL);
    String whenExhaustedActionValue = config
            .get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_WHEN_EXHAUSTED_ACTION).trim();
    byte whenExhaustedAction = 0;
    boolean testOnBorrow = Boolean
            .valueOf(config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_ON_BORROW, "false").trim())
            .booleanValue();
    boolean testWhileIdle = Boolean
            .valueOf(
                    config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_WHILE_IDLE, "false").trim())
            .booleanValue();

    if ("block".equalsIgnoreCase(whenExhaustedActionValue)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
    } else if ("fail".equalsIgnoreCase(whenExhaustedActionValue)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    } else if ("grow".equalsIgnoreCase(whenExhaustedActionValue)) {
        whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
    } else {
        whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
    }

    if ("".equals(testQuery)) {
        testQuery = null;
    }

    if (username == null) {
        username = "";
    }

    if (password == null) {
        password = "";
    }

    // read the values of the statement pool configuration specified by the given key
    boolean poolingStmts = Boolean.valueOf(config
            .getString(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_POOLING, CmsStringUtil.TRUE).trim())
            .booleanValue();
    int maxActiveStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_ACTIVE, 25);
    int maxWaitStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_WAIT, 250);
    int maxIdleStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_IDLE, 15);
    String whenStmtsExhaustedActionValue = config
            .get(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_WHEN_EXHAUSTED_ACTION);
    byte whenStmtsExhaustedAction = GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW;
    if (whenStmtsExhaustedActionValue != null) {
        whenStmtsExhaustedActionValue = whenStmtsExhaustedActionValue.trim();
        whenStmtsExhaustedAction = ("block".equalsIgnoreCase(whenStmtsExhaustedActionValue))
                ? GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK
                : ("fail".equalsIgnoreCase(whenStmtsExhaustedActionValue))
                        ? GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL
                        : GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW;
    }

    int connectionAttempts = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_CONNECT_ATTEMTS, 10);
    int connetionsWait = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_CONNECT_WAITS, 5000);

    // create an instance of the JDBC driver
    Class.forName(jdbcDriver).newInstance();

    // initialize a keyed object pool to store connections
    GenericObjectPool connectionPool = new GenericObjectPool(null);

    /* Abandoned pool configuration:
     *  
     * In case the systems encounters "pool exhaustion" (runs out of connections),
     * comment the above line with "new GenericObjectPool(null)" and uncomment the 
     * 5 lines below. This will generate an "abandoned pool" configuration that logs 
     * abandoned connections to the System.out. Unfortunatly this code is deprecated,
     * so to avoid code warnings it's also disabled here. 
     * Tested with commons-pool v 1.2.
     */

    //        AbandonedConfig abandonedConfig = new AbandonedConfig();
    //        abandonedConfig.setLogAbandoned(true);
    //        abandonedConfig.setRemoveAbandoned(true);
    //        abandonedConfig.setRemoveAbandonedTimeout(5);
    //        GenericObjectPool connectionPool = new AbandonedObjectPool(null, abandonedConfig);
    // initialize an object pool to store connections
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(maxIdle);
    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxWait(maxWait);
    connectionPool.setWhenExhaustedAction(whenExhaustedAction);

    if (testQuery != null) {
        connectionPool.setTestOnBorrow(testOnBorrow);
        connectionPool.setTestWhileIdle(testWhileIdle);
        connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns);
        connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTime);
    }

    // initialize a connection factory to make the DriverManager taking connections from the pool
    if (jdbcUrlParams != null) {
        jdbcUrl += jdbcUrlParams;
    }

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, username, password);

    // Set up statement pool, if desired
    GenericKeyedObjectPoolFactory statementFactory = null;
    if (poolingStmts) {
        statementFactory = new GenericKeyedObjectPoolFactory(null, maxActiveStmts, whenStmtsExhaustedAction,
                maxWaitStmts, maxIdleStmts);
    }

    // initialize a factory to obtain pooled connections and prepared statements
    new PoolableConnectionFactory(connectionFactory, connectionPool, statementFactory, testQuery, false, true);

    // initialize a new pooling driver using the pool
    PoolingDriver driver = new PoolingDriver();
    driver.registerPool(poolUrl, connectionPool);

    Connection con = null;
    boolean connect = false;
    int connectionTests = 0;

    // try to connect once to the database to ensure it can be connected to at all
    // if the conection cannot be established, multiple attempts will be done to connect
    // just in cast the database was not fast enough to start before OpenCms was started

    do {
        try {
            // try to connect
            con = connectionFactory.createConnection();
            connect = true;
        } catch (Exception e) {
            // connection failed, increase attempts, sleept for some seconds and log a message
            connectionTests++;
            if (CmsLog.INIT.isInfoEnabled()) {
                CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_WAIT_FOR_DB_4, new Object[] {
                        poolUrl, jdbcUrl, new Integer(connectionTests), new Integer(connetionsWait) }));
            }
            Thread.sleep(connetionsWait);
        } finally {
            if (con != null) {
                con.close();
            }
        }
    } while (!connect && (connectionTests < connectionAttempts));

    if (CmsLog.INIT.isInfoEnabled()) {
        CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_JDBC_POOL_2, poolUrl, jdbcUrl));
    }
    return driver;
}

From source file:org.ow2.petals.activitibpmn.incoming.integration.AbstractOperation.java

/**
 * @param operationName/*w ww  .  j  av a 2 s  .c o m*/
 * @param faultActor
 * @param classesToBeBound
 *            Classes that can be (un)marshalled by this integration operation
 * @param log
 * @throws OperationInitializationException
 */
public AbstractOperation(final QName operationName, final URI faultActor, final Class<?>[] classesToBeBound,
        final Logger log) throws OperationInitializationException {
    this.log = log;
    this.operationName = operationName;
    this.faultActor = faultActor;

    try {
        final JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound);

        this.marshalerPool = new GenericObjectPool<Marshaller>(new MarshalerFactory(jaxbContext));
        this.unmarshalerPool = new GenericObjectPool<Unmarshaller>(new UnmarshalerFactory(jaxbContext));

    } catch (final JAXBException e) {
        throw new OperationInitializationException(this.operationName, e);
    }
}

From source file:org.paxle.parser.html.impl.HtmlParser.java

@Activate
protected void activate() {
    this.pool = new GenericObjectPool(this);
    ScriptScanner.STRICT = false;
}