Java Code Examples for org.apache.commons.pool.impl.GenericObjectPool#borrowObject()

The following examples show how to use org.apache.commons.pool.impl.GenericObjectPool#borrowObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DirectCodecFactory.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
/**
 * Borrow an object from a pool.
 *
 * @param pool - the pull to borrow from, must not be null
 * @return - an object from the pool
 */
@SuppressWarnings("unchecked")
public <T> T borrow(GenericObjectPool pool) {
  try {
    return (T) pool.borrowObject();
  } catch (Exception e) {
    throw new ParquetCompressionCodecException(e);
  }

}
 
Example 2
Source File: DirectCodecFactory.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
private CodecPool(final CompressionCodec codec){
  try {
    boolean supportDirectDecompressor = codec.getClass() == DIRECT_DECOMPRESSION_CODEC_CLASS;
    compressorPool = new GenericObjectPool(new BasePoolableObjectFactory() {
      public Object makeObject() throws Exception {
        return codec.createCompressor();
      }
    }, Integer.MAX_VALUE);

    Object com = compressorPool.borrowObject();
    if (com != null) {
      cPools.put(com.getClass(), compressorPool);
      compressorPool.returnObject(com);
    } else {
      if (LOG.isDebugEnabled()) {
        LOG.debug(String.format(BYTE_BUF_IMPL_NOT_FOUND_MSG, "compressor", codec.getClass().getName()));
      }
    }

    decompressorPool = new GenericObjectPool(new BasePoolableObjectFactory() {
      public Object makeObject() throws Exception {
        return codec.createDecompressor();
      }
    }, Integer.MAX_VALUE);

    Object decom = decompressorPool.borrowObject();
    if (decom != null) {
      dePools.put(decom.getClass(), decompressorPool);
      decompressorPool.returnObject(decom);
    } else {
      if (LOG.isDebugEnabled()) {
        LOG.debug(String.format(BYTE_BUF_IMPL_NOT_FOUND_MSG, "decompressor", codec.getClass().getName()));
      }
    }

    if (supportDirectDecompressor) {
      directDecompressorPool = new GenericObjectPool(
          new BasePoolableObjectFactory() {
            public Object makeObject() throws Exception {
              return CREATE_DIRECT_DECOMPRESSOR_METHOD.invoke(DIRECT_DECOMPRESSION_CODEC_CLASS);
            }
          }, Integer.MAX_VALUE);

      Object ddecom = directDecompressorPool.borrowObject();
      if (ddecom != null) {
        directDePools.put(ddecom.getClass(), directDecompressorPool);
        directDecompressorPool.returnObject(ddecom);

      } else {
        supportDirectDecompressor = false;
        if (LOG.isDebugEnabled()) {
          LOG.debug(String.format(BYTE_BUF_IMPL_NOT_FOUND_MSG, "compressor", codec.getClass().getName()));
        }
      }

    } else {
      directDecompressorPool = null;
    }

    this.supportDirectDecompressor = supportDirectDecompressor;
  } catch (Exception e) {
    throw new ParquetCompressionCodecException("Error creating compression codec pool.", e);
  }
}