Example usage for org.apache.commons.pool2 PooledObject getObject

List of usage examples for org.apache.commons.pool2 PooledObject getObject

Introduction

In this page you can find the example usage for org.apache.commons.pool2 PooledObject getObject.

Prototype

T getObject();

Source Link

Document

Obtain the underlying object that is wrapped by this instance of PooledObject .

Usage

From source file:net.sf.jasperreports.phantomjs.ProcessFactory.java

@Override
public void activateObject(PooledObject<PhantomJSProcess> pooledObject) throws Exception {
    super.activateObject(pooledObject);

    PhantomJSProcess process = pooledObject.getObject();
    if (process.hasEnded()) {
        if (log.isDebugEnabled()) {
            log.debug(process.getId() + " has ended");
        }/* w w w . j a  v  a 2  s  .c  om*/

        throw new JRRuntimeException("Process " + process.getId() + " has ended");
    }

    long borrowedCount = ((DefaultPooledObject<PhantomJSProcess>) pooledObject).getBorrowedCount();
    if (borrowedCount >= expirationCount) {
        if (log.isDebugEnabled()) {
            log.debug(process.getId() + " borrow count " + borrowedCount + " exceeded expiration count "
                    + expirationCount);
        }

        throw new JRRuntimeException("Process " + process.getId() + " borrow count exceeded");
    }

    long now = System.currentTimeMillis();
    if (now >= pooledObject.getCreateTime() + expirationTime) {
        if (log.isDebugEnabled()) {
            log.debug(process.getId() + " expiration time " + expirationTime + " from "
                    + pooledObject.getCreateTime() + " exceeded");
        }

        throw new JRRuntimeException("Process " + process.getId() + " expiration time exceeded");
    }
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.apns.StubAPNSConnectionKeyedPooledObjectFactory.java

@Override
public void destroyObject(APNSConnectionPoolImpl.APNSConnectionKey key, PooledObject<APNSConnection> connection)
        throws Exception {
    long activeFor = connection.getActiveTimeMillis();
    long idleFor = connection.getIdleTimeMillis();
    int hashCode = connection.getObject().hashCode();
    super.destroyObject(key, connection);
    LOGGER.info(String.format("Destroyed APNS Connection for key:%s, active:%d idle:%d hashcode:%d", key,
            activeFor / 1000L, idleFor / 1000L, hashCode));
}

From source file:com.google.devtools.build.lib.worker.WorkerFactory.java

/**
 * When a worker process is discarded, destroy its process, too.
 *///w w w.  ja v a 2 s .c o  m
@Override
public void destroyObject(WorkerKey key, PooledObject<Worker> p) throws Exception {
    if (workerOptions.workerVerbose) {
        reporter.handle(Event.info(
                String.format("Destroying %s worker (id %d)", key.getMnemonic(), p.getObject().getWorkerId())));
    }
    p.getObject().destroy();
}

From source file:net.identio.server.service.authentication.ldap.LdapConnectionFactory.java

@Override
public void destroyObject(PooledObject<InitialLdapContext> p) {

    LOG.debug("Begin destruction of an LDAP connection to: {}", ldapAuthMethod.getName());

    try {// ww  w .  java 2s.  c  om
        p.getObject().close();

        LOG.debug("Destroyed LDAP connection to: {}", ldapAuthMethod.getName());

    } catch (NamingException e) {
        LOG.error("Error when closing connection to LDAP server {}", ldapAuthMethod.getName());
    }
}

From source file:com.lambdaworks.redis.RedisConnectionPool.java

private PooledObjectFactory<T> createFactory(final RedisConnectionProvider<T> redisConnectionProvider) {
    return new BasePooledObjectFactory<T>() {

        @SuppressWarnings("unchecked")
        @Override/*  ww  w.j a  v a  2 s.  c o m*/
        public T create() throws Exception {

            T connection = redisConnectionProvider.createConnection();
            PooledConnectionInvocationHandler<T> h = new PooledConnectionInvocationHandler<>(connection,
                    RedisConnectionPool.this);

            Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(),
                    new Class<?>[] { redisConnectionProvider.getComponentType() }, h);

            return (T) proxy;
        }

        @Override
        public PooledObject<T> wrap(T obj) {
            return new DefaultPooledObject<>(obj);
        }

        @Override
        public boolean validateObject(PooledObject<T> p) {
            return Connections.isOpen(p.getObject());
        }

        @Override
        @SuppressWarnings("unchecked")
        public void destroyObject(PooledObject<T> p) throws Exception {

            T object = p.getObject();
            if (Proxy.isProxyClass(object.getClass())) {
                PooledConnectionInvocationHandler<T> invocationHandler = (PooledConnectionInvocationHandler<T>) Proxy
                        .getInvocationHandler(object);

                object = invocationHandler.getConnection();
            }

            Connections.close(object);
        }
    };
}

From source file:com.heliosapm.streams.collector.ds.pool.impls.JMXClientPoolBuilder.java

/**
 * {@inheritDoc}//from  ww  w . java2 s  .  c  om
 * @see com.heliosapm.streams.collector.ds.pool.PooledObjectFactoryBuilder#validateObject(org.apache.commons.pool2.PooledObject)
 */
@Override
public boolean validateObject(final PooledObject<MBeanServerConnection> p) {
    final MBeanServerConnection wcon = p.getObject();
    if (wcon == null)
        return false;
    try {
        final String[] domains = wcon.getDomains();
        return domains != null && domains.length > 0;
    } catch (Exception x) {
        return false;
    }
}

From source file:com.heliosapm.streams.collector.ds.pool.impls.JMXClientPoolBuilder.java

/**
 * {@inheritDoc}//from   www . j a  v a  2  s  . com
 * @see com.heliosapm.streams.collector.ds.pool.PooledObjectFactoryBuilder#destroyObject(org.apache.commons.pool2.PooledObject)
 */
@Override
public void destroyObject(final PooledObject<MBeanServerConnection> p) {
    final WrappedJMXClient wcon = (WrappedJMXClient) p.getObject();
    if (wcon != null) {
        try {
            wcon.realClose();
        } catch (Exception x) {
            /* No Op */}
    }
}

From source file:net.identio.server.service.authentication.ldap.LdapConnectionFactory.java

@Override
public boolean validateObject(PooledObject<InitialLdapContext> p) {

    LOG.debug("Validating connection to LDAP directory {}", ldapAuthMethod.getName());

    SearchControls controls = new SearchControls();
    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    controls.setCountLimit(1);/*from  ww w  .j  a v  a2s.  c o m*/
    controls.setTimeLimit(500);

    try {
        p.getObject().search("", ldapAuthMethod.getPoolConfig().getTestRequestFilter(), controls);
    } catch (NamingException e) {
        LOG.error("Validation of connection to LDAP directory {} failed", ldapAuthMethod.getName());
        return false;
    }

    return true;
}

From source file:JDBCPool.dbcp.demo.sourcecode.PoolableConnectionFactory.java

@Override
public void destroyObject(PooledObject<PoolableConnection> p) throws Exception {
    p.getObject().reallyClose();
}

From source file:JDBCPool.dbcp.demo.sourcecode.PoolableConnectionFactory.java

@Override
public boolean validateObject(PooledObject<PoolableConnection> p) {
    try {/*  ww  w. j  a  va2  s . c o  m*/
        validateLifetime(p);

        validateConnection(p.getObject());
        return true;
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug(Utils.getMessage("poolableConnectionFactory.validateObject.fail"), e);
        }
        return false;
    }
}