Example usage for org.apache.commons.pool ObjectPool returnObject

List of usage examples for org.apache.commons.pool ObjectPool returnObject

Introduction

In this page you can find the example usage for org.apache.commons.pool ObjectPool returnObject.

Prototype

void returnObject(Object p0) throws Exception;

Source Link

Usage

From source file:org.mule.transport.sftp.SftpConnector.java

public void releaseClient(ImmutableEndpoint endpoint, SftpClient client) throws Exception {
    if (useConnectionPool()) {
        if (getDispatcherFactory().isCreateDispatcherPerRequest()) {
            destroyClient(endpoint, client);
        } else {//from  w  w w .j  a v  a2s .c  om
            if (client != null && client.isConnected()) {
                ObjectPool pool = getClientPool(endpoint);
                if (logger.isDebugEnabled()) {
                    logger.debug("Releasing connection for endpoint " + endpoint.getEndpointURI());
                }
                pool.returnObject(client);
            }
        }
    } else {
        client.disconnect();
    }
}

From source file:org.openrepose.commons.utils.transform.jaxb.JaxbEntityToXml.java

@Override
public String transform(final JAXBElement source) {
    String rtn = null;/* w  w w.jav  a2 s .co  m*/
    Marshaller pooledObject;
    final ObjectPool<Marshaller> objectPool = getMarshallerPool();
    try {
        pooledObject = objectPool.borrowObject();
        try {
            final StringWriter w = new StringWriter();
            pooledObject.marshal(source, w);
            rtn = w.getBuffer().toString();
        } catch (JAXBException jaxbe) {
            objectPool.invalidateObject(pooledObject);
            pooledObject = null;
            throw new ResourceConstructionException(jaxbe.getMessage(), jaxbe);
        } catch (Exception e) {
            objectPool.invalidateObject(pooledObject);
            pooledObject = null;
            LOG.error("Failed to utilize the Marshaller. Reason: {}", e.getLocalizedMessage());
            LOG.trace("", e);
        } finally {
            if (pooledObject != null) {
                objectPool.returnObject(pooledObject);
            }
        }
    } catch (ResourceConstructionException e) {
        throw e;
    } catch (Exception e) {
        LOG.error("Failed to obtain a Marshaller. Reason: {}", e.getLocalizedMessage());
        LOG.trace("", e);
    }
    return rtn;
}

From source file:org.openrepose.commons.utils.transform.jaxb.JaxbToStreamTransform.java

@Override
public void transform(final JAXBElement source, final T target) {
    Marshaller pooledObject;/*from w w  w .  ja  v a  2 s. c o m*/
    final ObjectPool<Marshaller> objectPool = getMarshallerPool();
    try {
        pooledObject = objectPool.borrowObject();
        try {
            pooledObject.marshal(source, target);
        } catch (JAXBException jaxbe) {
            objectPool.invalidateObject(pooledObject);
            pooledObject = null;
            throw new ResourceContextException(jaxbe.getMessage(), jaxbe);
        } catch (Exception e) {
            objectPool.invalidateObject(pooledObject);
            pooledObject = null;
            LOG.error("Failed to utilize the Marshaller. Reason: {}", e.getLocalizedMessage());
            LOG.trace("", e);
        } finally {
            if (pooledObject != null) {
                objectPool.returnObject(pooledObject);
            }
        }
    } catch (ResourceContextException e) {
        throw e;
    } catch (Exception e) {
        LOG.error("Failed to obtain a Marshaller. Reason: {}", e.getLocalizedMessage());
        LOG.trace("", e);
    }
}

From source file:org.openrepose.commons.utils.transform.jaxb.StreamToJaxbTransform.java

@Override
public JAXBElement<T> transform(final InputStream source) {
    JAXBElement<T> rtn = null;
    Unmarshaller pooledObject;//from  w w  w.  j av a  2 s. c  om
    final ObjectPool<Unmarshaller> objectPool = getUnmarshallerPool();
    try {
        pooledObject = objectPool.borrowObject();
        try {
            rtn = (JAXBElement<T>) pooledObject.unmarshal(source);
        } catch (JAXBException jbe) {
            objectPool.invalidateObject(pooledObject);
            pooledObject = null;
            throw new ResourceContextException(jbe.getMessage(), jbe);
        } catch (Exception e) {
            objectPool.invalidateObject(pooledObject);
            pooledObject = null;
            LOG.error("Failed to utilize the Marshaller. Reason: {}", e.getLocalizedMessage());
            LOG.trace("", e);
        } finally {
            if (pooledObject != null) {
                objectPool.returnObject(pooledObject);
            }
        }
    } catch (ResourceContextException e) {
        throw e;
    } catch (Exception e) {
        LOG.error("Failed to obtain a Marshaller. Reason: {}", e.getLocalizedMessage());
        LOG.trace("", e);
    }
    return rtn;
}

From source file:org.openrepose.commons.utils.transform.xslt.StreamToXsltTransform.java

@Override
public void transform(final InputStream source, final OutputStream target) {
    Transformer pooledObject;//w w w.  j a v a 2 s  .  c  om
    final ObjectPool<Transformer> objectPool = getXslTransformerPool();
    try {
        pooledObject = objectPool.borrowObject();
        try {
            pooledObject.transform(new StreamSource(source), new StreamResult(target));
        } catch (TransformerException te) {
            objectPool.invalidateObject(pooledObject);
            pooledObject = null;
            throw new XsltTransformationException("Failed while attempting XSLT transformation.", te);
        } catch (Exception e) {
            objectPool.invalidateObject(pooledObject);
            pooledObject = null;
            throw new XsltTransformationException("Failed while attempting XSLT transformation.", e);
        } finally {
            if (pooledObject != null) {
                objectPool.returnObject(pooledObject);
            }
        }
    } catch (XsltTransformationException e) {
        throw e;
    } catch (Exception e) {
        throw new XsltTransformationException("Failed to obtain a Transformer for XSLT transformation.", e);
    }
}

From source file:org.openrepose.filters.ratelimiting.util.combine.CombinedLimitsTransformer.java

@Override
public void transform(final LimitsTransformPair source, final OutputStream target) {
    Transformer pooledObject;/*  www. jav  a 2s  .c om*/
    final ObjectPool<Transformer> objectPool = getXslTransformerPool();
    try {
        pooledObject = objectPool.borrowObject();
        try {
            final InputStreamUriParameter inputStreamUriParameter = new InputStreamUriParameter(
                    source.getInputStream());
            final StreamResult resultWriter = new StreamResult(target);
            // The XSL requires a parameter to represent the absolute limits.
            // This harness cheats and provides the input stream directly.
            pooledObject.setURIResolver(inputStreamUriParameter);
            pooledObject.setParameter("absoluteURL", inputStreamUriParameter.getHref());
            final Limits limitsObject = new Limits();
            limitsObject.setRates(source.getRateLimitList());
            pooledObject.transform(new JAXBSource(jaxbContext, factory.createLimits(limitsObject)),
                    resultWriter);
        } catch (Exception e) {
            objectPool.invalidateObject(pooledObject);
            pooledObject = null;
            throw new XsltTransformationException("Failed while attempting XSLT transformation.", e);
        } finally {
            if (pooledObject != null) {
                objectPool.returnObject(pooledObject);
            }
        }
    } catch (XsltTransformationException e) {
        throw e;
    } catch (Exception e) {
        LOG.error("Failed to obtain a Transformer. Reason: {}", e.getLocalizedMessage());
        LOG.trace("", e);
    }
}

From source file:org.openrepose.filters.translation.httpx.HttpxMarshaller.java

public <T> T unmarshall(InputStream xml) {
    T rtnObject = null;/*from   w  ww  .  j  a v  a2 s.  co  m*/
    Unmarshaller pooledObject = null;
    ObjectPool<Unmarshaller> unmarshallerPool = HttpxMarshallerUtility.unmarshallerPool;
    try {
        try {
            pooledObject = unmarshallerPool.borrowObject();
            XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
            SAXSource source = new SAXSource(xmlReader, new InputSource(xml));
            Object result = pooledObject.unmarshal(source);
            if (result instanceof JAXBElement) {
                JAXBElement element = (JAXBElement) result;
                rtnObject = (T) element.getValue();
            } else {
                rtnObject = (T) result;
            }
        } catch (Exception ex) {
            unmarshallerPool.invalidateObject(pooledObject);
            pooledObject = null;
            throw new HttpxException("Error unmarshalling xml input", ex);
        } finally {
            if (pooledObject != null) {
                unmarshallerPool.returnObject(pooledObject);
            }
        }
    } catch (Exception e) {
        LOG.error("Error unmarshalling xml input", e);
        throw new HttpxException("Error unmarshalling xml input", e);
    }
    return rtnObject;
}

From source file:org.openrepose.filters.translation.httpx.HttpxMarshaller.java

private void marshall(Object o, OutputStream out) {
    Marshaller pooledObject = null;
    ObjectPool<Marshaller> marshallerPool = HttpxMarshallerUtility.marshallerPool;
    try {//from  w  ww . ja v a2s.  c  o  m
        try {
            pooledObject = marshallerPool.borrowObject();
            pooledObject.marshal(o, out);
        } catch (Exception ex) {
            marshallerPool.invalidateObject(pooledObject);
            pooledObject = null;
            throw new HttpxException("Error marshalling HTTPX object", ex);
        } finally {
            if (pooledObject != null) {
                marshallerPool.returnObject(pooledObject);
            }
        }
    } catch (Exception e) {
        throw new HttpxException("Error marshalling HTTPX object", e);
    }
}

From source file:org.ow2.petals.binding.soap.SoapComponentContext.java

/**
 * Release the service client to the pool
 * //w ww .  j a v a2s  . co m
 * @param address
 * @param operation
 * @param mep
 * @param serviceClient
 * @throws MessagingException
 */
public void returnServiceClient(final String address, final QName operation, final URI mep,
        final ServiceClient serviceClient, final String soapAction) throws MessagingException {

    try {

        String resolvedOp = null;
        if (operation != null) {
            resolvedOp = operation.toString();
        } else if (soapAction != null) {
            resolvedOp = soapAction;
        } else {
            throw new MessagingException(
                    "Unable to resolve the operation. Set it in the Jbi exchange or SoapAction.");
        }

        ObjectPool pool = this.serviceClientPools.get(new ServiceClientKey(address, resolvedOp, mep));
        if (pool != null) {
            pool.returnObject(serviceClient);
        }

    } catch (final Exception e) {
        throw new MessagingException("Can't return the Axis service client to the pool", e);
    }
}

From source file:org.springframework.jms.listener.serversession.CommonsPoolServerSessionFactory.java

/**
 * Returns the given ServerSession, which just finished an execution
 * of its listener, back to the pool./*from   ww  w  .  jav  a 2s  .  c om*/
 */
protected void serverSessionFinished(ServerSession serverSession, ListenerSessionManager sessionManager) {
    ObjectPool pool = (ObjectPool) this.serverSessionPools.get(sessionManager);
    try {
        pool.returnObject(serverSession);
    } catch (Exception ex) {
        logger.error("Failed to return ServerSession to pool", ex);
    }
}