Source:
public static Set<String> findApplicationIdFromLog(final String logFilePath, final Logger log) { final File logFile = new File(logFilePath); if (!logFile.exists()) { throw new IllegalArgumentException("the logFilePath does not exist: " + logFilePath); } final int lastSlash = logFilePath.lastIndexOf('/'); final String dirPath = logFilePath.substring(0, lastSlash); final String logFileName = logFilePath.substring(lastSlash + 1); final File[] logFiles = new File(dirPath).listFiles((dir, name) -> name.startsWith(logFileName)); BufferedReader br = null; final Set<String> applicationIds = new HashSet<>(); try { for (final File curLogFile : logFiles) { if (!curLogFile.exists()) { throw new IllegalArgumentException("the logFilePath does not exist: " + curLogFile.getAbsolutePath()); } if (!curLogFile.isFile()) { throw new IllegalArgumentException("the logFilePath specified is not a valid file: " + curLogFile.getAbsolutePath()); } if (!curLogFile.canRead()) { throw new IllegalArgumentException( "unable to read the logFilePath specified: " + curLogFile.getAbsolutePath()); } br = new BufferedReader(new InputStreamReader( new FileInputStream(curLogFile), StandardCharsets.UTF_8)); String line; while ((line = br.readLine()) != null) { final String[] inputs = line.split("\\s"); for (final String input : inputs) { final Matcher m = APPLICATION_ID_PATTERN.matcher(input); if (m.find()) { final String appId = m.group(1); applicationIds.add(appId); } } } } } catch (final IOException e) { log.error("Error while trying to find applicationId for log", e); } finally { try { if (br != null) { br.close(); } } catch (final Exception e) { } } return applicationIds; }

Target:
public static Set<String> findApplicationIdFromLog(final String logFilePath, final Logger log) { final File logFile = new File(logFilePath); if (!logFile.exists()) { throw new IllegalArgumentException("the logFilePath does not exist: " + logFilePath); } final int lastSlash = logFilePath.lastIndexOf('/'); final String dirPath = logFilePath.substring(0, lastSlash); final String logFileName = logFilePath.substring(lastSlash + 1); final File[] logFiles = new File(dirPath).listFiles((dir, name) -> name.startsWith(logFileName)); BufferedReader br = null; final Set<String> applicationIds = new HashSet<>(); for (final File curLogFile : logFiles) { if (!curLogFile.exists()) { throw new IllegalArgumentException("the logFilePath does not exist: " + curLogFile.getAbsolutePath()); } if (!curLogFile.isFile()) { throw new IllegalArgumentException("the logFilePath specified is not a valid file: " + curLogFile.getAbsolutePath()); } if (!curLogFile.canRead()) { throw new IllegalArgumentException( "unable to read the logFilePath specified: " + curLogFile.getAbsolutePath()); } try { br = new BufferedReader(new InputStreamReader( new FileInputStream(curLogFile), StandardCharsets.UTF_8)); String line; while ((line = br.readLine()) != null) { final String[] inputs = line.split("\\s"); for (final String input : inputs) { final Matcher m = APPLICATION_ID_PATTERN.matcher(input); if (m.find()) { final String appId = m.group(1); applicationIds.add(appId); } } } } catch (final IOException e) { log.error("Error while trying to find applicationId from " + curLogFile.getAbsolutePath() + ". Some MR jobs may leak.", e); } finally { try { if (br != null) { br.close(); } } catch (final IOException e) { } } } return applicationIds; }