/** * Return parser to the pool for reuse * * @param parser parser to return */ private static void releaseParser(PoolSAXParser parser) { parser.reset(); //if this is a different generation, don't put it back //in the pool if (parser.getGeneration() != POOL_GENERATION.get()) { return; } try { SAX_READ_WRITE_LOCK.readLock().lock(); //if there are extra parsers (e.g. after a reset of the pool to a smaller size), // this parser will not be added and will then be gc'd boolean success = SAX_PARSERS.offer(parser); if (! success) { LOG.warning("SAXParser not taken back into pool. If you haven't resized the pool, this could " + "be a sign that there are more calls to 'acquire' than to 'release'"); } } finally { SAX_READ_WRITE_LOCK.readLock().unlock(); } }
/** * This checks context for a user specified {@link SAXParser}. * If one is not found, this reuses a SAXParser from the pool. * * @since Apache Tika 1.19 * @param is InputStream to parse * @param contentHandler handler to use * @param context context to use * @return * @throws TikaException * @throws IOException * @throws SAXException */ public static void parseSAX(InputStream is, DefaultHandler contentHandler, ParseContext context) throws TikaException, IOException, SAXException { SAXParser saxParser = context.get(SAXParser.class); PoolSAXParser poolSAXParser = null; if (saxParser == null) { poolSAXParser = acquireSAXParser(); saxParser = poolSAXParser.getSAXParser(); } try { saxParser.parse(is, contentHandler); } finally { if (poolSAXParser != null) { releaseParser(poolSAXParser); } } }
/** * Return parser to the pool for reuse * * @param parser parser to return */ private static void releaseParser(PoolSAXParser parser) { //if this is a different generation, don't put it back //in the pool if (parser.getGeneration() != POOL_GENERATION.get()) { return; } parser.reset(); try { SAX_READ_WRITE_LOCK.readLock().lock(); //if there are extra parsers (e.g. after a reset of the pool to a smaller size), // this parser will not be added and will then be gc'd boolean success = SAX_PARSERS.offer(parser); if (! success) { LOG.warning("SAXParser not taken back into pool. If you haven't resized the pool, this could " + "be a sign that there are more calls to 'acquire' than to 'release'"); } } finally { SAX_READ_WRITE_LOCK.readLock().unlock(); } }
parser.reset();
/** * This checks context for a user specified {@link SAXParser}. * If one is not found, this reuses a SAXParser from the pool. * * @since Apache Tika 1.19 * @param is InputStream to parse * @param contentHandler handler to use * @param context context to use * @return * @throws TikaException * @throws IOException * @throws SAXException */ public static void parseSAX(InputStream is, DefaultHandler contentHandler, ParseContext context) throws TikaException, IOException, SAXException { SAXParser saxParser = context.get(SAXParser.class); PoolSAXParser poolSAXParser = null; if (saxParser == null) { poolSAXParser = acquireSAXParser(); saxParser = poolSAXParser.getSAXParser(); } try { saxParser.parse(is, contentHandler); } finally { if (poolSAXParser != null) { releaseParser(poolSAXParser); } } }