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

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

Introduction

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

Prototype

void invalidateObject(Object p0) throws Exception;

Source Link

Usage

From source file:com.meidusa.amoeba.net.poolable.MultipleLoadBalanceObjectPool.java

public void invalidateObject(Object obj) throws Exception {
    PoolableObject poolableObject = (PoolableObject) obj;
    ObjectPool pool = poolableObject.getObjectPool();
    pool.invalidateObject(obj);
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestObjectPool.java

public void testPOFInvalidateObjectUsages() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    final ObjectPool pool;
    try {/*w w  w .  j av a  2  s  .c  om*/
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();
    Object obj;

    /// Test correct behavior code paths

    obj = pool.borrowObject();
    clear(factory, expectedMethods);

    // invalidated object should be destroyed
    pool.invalidateObject(obj);
    expectedMethods.add(new MethodCall("destroyObject", obj));
    assertEquals(expectedMethods, factory.getMethodCalls());

    //// Test exception handling of invalidateObject
    reset(pool, factory, expectedMethods);
    obj = pool.borrowObject();
    clear(factory, expectedMethods);
    factory.setDestroyObjectFail(true);
    try {
        pool.invalidateObject(obj);
        fail("Expecting destroy exception to propagate");
    } catch (PrivateException ex) {
        // Expected
    }
    Thread.sleep(250); // could be defered
    removeDestroyObjectCall(factory.getMethodCalls());
    assertEquals(expectedMethods, factory.getMethodCalls());
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestObjectPool.java

public void testClosedPoolBehavior() throws Exception {
    final ObjectPool pool;
    try {//from   ww w.  j ava2  s .  c  o  m
        pool = makeEmptyPool(new MethodCallPoolableObjectFactory());
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    Object o1 = pool.borrowObject();
    Object o2 = pool.borrowObject();

    pool.close();

    try {
        pool.addObject();
        fail("A closed pool must throw an IllegalStateException when addObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    try {
        pool.borrowObject();
        fail("A closed pool must throw an IllegalStateException when borrowObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    // The following should not throw exceptions just because the pool is closed.
    if (pool.getNumIdle() >= 0) {
        assertEquals("A closed pool shouldn't have any idle objects.", 0, pool.getNumIdle());
    }
    if (pool.getNumActive() >= 0) {
        assertEquals("A closed pool should still keep count of active objects.", 2, pool.getNumActive());
    }
    pool.returnObject(o1);
    if (pool.getNumIdle() >= 0) {
        assertEquals("returnObject should not add items back into the idle object pool for a closed pool.", 0,
                pool.getNumIdle());
    }
    if (pool.getNumActive() >= 0) {
        assertEquals("A closed pool should still keep count of active objects.", 1, pool.getNumActive());
    }
    pool.invalidateObject(o2);
    if (pool.getNumIdle() >= 0) {
        assertEquals("invalidateObject must not add items back into the idle object pool.", 0,
                pool.getNumIdle());
    }
    if (pool.getNumActive() >= 0) {
        assertEquals("A closed pool should still keep count of active objects.", 0, pool.getNumActive());
    }
    pool.clear();
    pool.close();
}

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

public void destroyClient(ImmutableEndpoint endpoint, SftpClient client) throws Exception {
    if (useConnectionPool()) {
        if ((client != null) && (client.isConnected())) {
            ObjectPool pool = getClientPool(endpoint);
            pool.invalidateObject(client);
        }//w ww . j av a 2  s .c  o  m
    }
}

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

@Override
public String transform(final JAXBElement source) {
    String rtn = null;//from ww w  . ja va 2  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;/*  ww w.j  a va 2 s . c  om*/
    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;/* w  ww  .  j  ava 2 s  .  c  o  m*/
    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.jav a  2  s.co  m*/
    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;/*from  w ww  .  ja  va2 s .c  o m*/
    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;/*w ww. j ava 2s.  c om*/
    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;
}