@Override protected void onFirstPeerRegistered() { AsyncPrettyPrinterExecutorHolder.ensureInitialized(); if (mAsyncPrettyPrinterRegistry == null && mPrettyPrinterInitializer != null) { mAsyncPrettyPrinterRegistry = new AsyncPrettyPrinterRegistry(); mPrettyPrinterInitializer.populatePrettyPrinters(mAsyncPrettyPrinterRegistry); } mResponseBodyFileManager.cleanupFiles(); }
@Override protected void onLastPeerUnregistered() { mResponseBodyFileManager.cleanupFiles(); AsyncPrettyPrinterExecutorHolder.shutdown(); } };
private String prettyPrintContentWithTimeOut( AsyncPrettyPrinter asyncPrettyPrinter, InputStream in) throws IOException { AsyncPrettyPrintingCallable prettyPrintingCallable = new AsyncPrettyPrintingCallable( in, asyncPrettyPrinter); ExecutorService executorService = AsyncPrettyPrinterExecutorHolder.getExecutorService(); if (executorService == null) { //last peer is unregistered... return null; } Future<String> future = executorService.submit(prettyPrintingCallable); try { return Util.getUninterruptibly(future, PRETTY_PRINT_TIMEOUT_SEC, TimeUnit.SECONDS); } catch (TimeoutException e) { future.cancel(true); return "Time out after " + PRETTY_PRINT_TIMEOUT_SEC + " seconds of attempting to pretty print\n" + Util.readAsUTF8(in); } catch (ExecutionException e) { Throwable cause = e.getCause(); ExceptionUtil.propagateIfInstanceOf(cause, IOException.class); throw ExceptionUtil.propagate(cause); } }
return getErrorAsyncPrettyPrinter(headerName, headerValue); } else { ExecutorService executorService = AsyncPrettyPrinterExecutorHolder.getExecutorService(); if (executorService == null) {
@Before public void setup() { mPrettyPrinterTestFactory = new PrettyPrinterTestFactory(); mResponseBodyFileManager = mock(ResponseBodyFileManager.class); mAsyncPrettyPrinterRegistry = new AsyncPrettyPrinterRegistry(); mAsyncPrettyPrinterRegistry.register(TEST_HEADER_NAME, mPrettyPrinterTestFactory); AsyncPrettyPrinterExecutorHolder.ensureInitialized(); }