ForkParser extends AbstractParser implements Closeable { public void parse( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { if (stream == null) { throw new NullPointerException("null stream"); } Throwable t; boolean alive = false; ForkClient client = acquireClient(); try { ContentHandler tee = (handler instanceof AbstractRecursiveParserWrapperHandler) ? handler : new TeeContentHandler( handler, new MetadataContentHandler(metadata)); t = client.call("parse", stream, tee, metadata, context); alive = true; } catch (TikaException te) { alive = true; throw te; } catch (IOException e) { throw new TikaException( "Failed to communicate with a forked parser process." + " The process has most likely crashed due to some error" + " like running out of memory. A new process will be" + " started for the next parsing request.", e); } finally { releaseClient(client, alive); } if (t instanceof IOException) { throw (IOException) t; } else if (t instanceof SAXException) { throw (SAXException) t; } else if (t instanceof TikaException) { throw (TikaException) t; } else if (t != null) { throw new TikaException( "Unexpected error in forked server process", t); } } ForkParser(Path tikaBin, ParserFactoryFactory factoryFactory);  ForkParser(Path tikaBin, ParserFactoryFactory parserFactoryFactory, ClassLoader classLoader);  ForkParser(ClassLoader loader, Parser parser);  ForkParser(ClassLoader loader);  ForkParser(); synchronized int getPoolSize(); synchronized void setPoolSize(int poolSize); @Deprecated String getJavaCommand(); List<String> getJavaCommandAsList(); void setJavaCommand(List<String> java); @Deprecated void setJavaCommand(String java); Set<MediaType> getSupportedTypes(ParseContext context); void parse(
            InputStream stream, ContentHandler handler,
            Metadata metadata, ParseContext context); synchronized void close(); void setServerPulseMillis(long serverPulseMillis); void setServerParseTimeoutMillis(long serverParseTimeoutMillis); void setServerWaitTimeoutMillis(long serverWaitTimeoutMillis); void setMaxFilesProcessedPerServer(int maxFilesProcessedPerClient);  }
@Test public void testHelloWorld() throws Exception { try (ForkParser parser = new ForkParser(ForkParserTest.class.getClassLoader(), new ForkTestParser())) { Metadata metadata = new Metadata(); ContentHandler output = new BodyContentHandler(); InputStream stream = new ByteArrayInputStream(new byte[0]); ParseContext context = new ParseContext(); parser.parse(stream, output, metadata, context); assertEquals("Hello, World!", output.toString().trim()); assertEquals("text/plain", metadata.get(Metadata.CONTENT_TYPE)); } }
@Test public void testSerialParsing() throws Exception { try (ForkParser parser = new ForkParser(ForkParserTest.class.getClassLoader(), new ForkTestParser())) { ParseContext context = new ParseContext(); for (int i = 0; i < 10; i++) { ContentHandler output = new BodyContentHandler(); InputStream stream = new ByteArrayInputStream(new byte[0]); parser.parse(stream, output, new Metadata(), context); assertEquals("Hello, World!", output.toString().trim()); } } }
@Test public void testParallelParsing() throws Exception { try (ForkParser parser = new ForkParser(ForkParserTest.class.getClassLoader(), new ForkTestParser())) { final ParseContext context = new ParseContext(); Thread[] threads = new Thread[10]; ContentHandler[] output = new ContentHandler[threads.length]; for (int i = 0; i < threads.length; i++) { final ContentHandler o = new BodyContentHandler(); output[i] = o; threads[i] = new Thread() { public void run() { try { InputStream stream = new ByteArrayInputStream(new byte[0]); parser.parse(stream, o, new Metadata(), context); } catch (Exception e) { e.printStackTrace(); } } }; threads[i].start(); } for (int i = 0; i < threads.length; i++) { threads[i].join(); assertEquals("Hello, World!", output[i].toString().trim()); } } }
@Test public void testPackageCanBeAccessed() throws Exception { try (ForkParser parser = new ForkParser(ForkParserTest.class.getClassLoader(), new ForkTestParser.ForkTestParserAccessingPackage())) { Metadata metadata = new Metadata(); ContentHandler output = new BodyContentHandler(); InputStream stream = new ByteArrayInputStream(new byte[0]); ParseContext context = new ParseContext(); parser.parse(stream, output, metadata, context); assertEquals("Hello, World!", output.toString().trim()); assertEquals("text/plain", metadata.get(Metadata.CONTENT_TYPE)); } }