Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 51C7D200C76 for ; Sat, 8 Apr 2017 06:56:29 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 506F5160B97; Sat, 8 Apr 2017 04:56:29 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 714CF160BA2 for ; Sat, 8 Apr 2017 06:56:28 +0200 (CEST) Received: (qmail 49723 invoked by uid 500); 8 Apr 2017 04:56:27 -0000 Mailing-List: contact commits-help@beam.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@beam.apache.org Delivered-To: mailing list commits@beam.apache.org Received: (qmail 49702 invoked by uid 99); 8 Apr 2017 04:56:27 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 Apr 2017 04:56:27 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 4D16CE0F98; Sat, 8 Apr 2017 04:56:27 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: thw@apache.org To: commits@beam.apache.org Date: Sat, 08 Apr 2017 04:56:27 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] beam git commit: Fix for potentially unclosed streams in ApexYarnLauncher archived-at: Sat, 08 Apr 2017 04:56:29 -0000 Repository: beam Updated Branches: refs/heads/master 0c3dc0051 -> 810db7f99 Fix for potentially unclosed streams in ApexYarnLauncher Project: http://git-wip-us.apache.org/repos/asf/beam/repo Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/42f63342 Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/42f63342 Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/42f63342 Branch: refs/heads/master Commit: 42f63342be667649eff5ea65dd9714c8b2f75cff Parents: 0c3dc00 Author: rjoshi2 Authored: Fri Apr 7 18:12:09 2017 -0700 Committer: Thomas Weise Committed: Fri Apr 7 21:55:17 2017 -0700 ---------------------------------------------------------------------- .../beam/runners/apex/ApexYarnLauncher.java | 109 ++++++++++--------- 1 file changed, 55 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/beam/blob/42f63342/runners/apex/src/main/java/org/apache/beam/runners/apex/ApexYarnLauncher.java ---------------------------------------------------------------------- diff --git a/runners/apex/src/main/java/org/apache/beam/runners/apex/ApexYarnLauncher.java b/runners/apex/src/main/java/org/apache/beam/runners/apex/ApexYarnLauncher.java index 6bc42f0..198b9bf 100644 --- a/runners/apex/src/main/java/org/apache/beam/runners/apex/ApexYarnLauncher.java +++ b/runners/apex/src/main/java/org/apache/beam/runners/apex/ApexYarnLauncher.java @@ -173,56 +173,57 @@ public class ApexYarnLauncher { * @throws IOException when dependency information cannot be read */ public static List getYarnDeployDependencies() throws IOException { - InputStream dependencyTree = ApexRunner.class.getResourceAsStream("dependency-tree"); - BufferedReader br = new BufferedReader(new InputStreamReader(dependencyTree)); - String line = null; - List excludes = new ArrayList<>(); - int excludeLevel = Integer.MAX_VALUE; - while ((line = br.readLine()) != null) { - for (int i = 0; i < line.length(); i++) { - char c = line.charAt(i); - if (Character.isLetter(c)) { - if (i > excludeLevel) { - excludes.add(line.substring(i)); - } else { - if (line.substring(i).startsWith("org.apache.hadoop")) { - excludeLevel = i; - excludes.add(line.substring(i)); - } else { - excludeLevel = Integer.MAX_VALUE; + try (InputStream dependencyTree = ApexRunner.class.getResourceAsStream("dependency-tree")) { + try (BufferedReader br = new BufferedReader(new InputStreamReader(dependencyTree))) { + String line; + List excludes = new ArrayList<>(); + int excludeLevel = Integer.MAX_VALUE; + while ((line = br.readLine()) != null) { + for (int i = 0; i < line.length(); i++) { + char c = line.charAt(i); + if (Character.isLetter(c)) { + if (i > excludeLevel) { + excludes.add(line.substring(i)); + } else { + if (line.substring(i).startsWith("org.apache.hadoop")) { + excludeLevel = i; + excludes.add(line.substring(i)); + } else { + excludeLevel = Integer.MAX_VALUE; + } + } + break; } } - break; } - } - } - br.close(); - - Set excludeJarFileNames = Sets.newHashSet(); - for (String exclude : excludes) { - String[] mvnc = exclude.split(":"); - String fileName = mvnc[1] + "-"; - if (mvnc.length == 6) { - fileName += mvnc[4] + "-" + mvnc[3]; // with classifier - } else { - fileName += mvnc[3]; - } - fileName += ".jar"; - excludeJarFileNames.add(fileName); - } - ClassLoader classLoader = ApexYarnLauncher.class.getClassLoader(); - URL[] urls = ((URLClassLoader) classLoader).getURLs(); - List dependencyJars = new ArrayList<>(); - for (int i = 0; i < urls.length; i++) { - File f = new File(urls[i].getFile()); - // dependencies can also be directories in the build reactor, - // the Apex client will automatically create jar files for those. - if (f.exists() && !excludeJarFileNames.contains(f.getName())) { - dependencyJars.add(f); + Set excludeJarFileNames = Sets.newHashSet(); + for (String exclude : excludes) { + String[] mvnc = exclude.split(":"); + String fileName = mvnc[1] + "-"; + if (mvnc.length == 6) { + fileName += mvnc[4] + "-" + mvnc[3]; // with classifier + } else { + fileName += mvnc[3]; + } + fileName += ".jar"; + excludeJarFileNames.add(fileName); + } + + ClassLoader classLoader = ApexYarnLauncher.class.getClassLoader(); + URL[] urls = ((URLClassLoader) classLoader).getURLs(); + List dependencyJars = new ArrayList<>(); + for (int i = 0; i < urls.length; i++) { + File f = new File(urls[i].getFile()); + // dependencies can also be directories in the build reactor, + // the Apex client will automatically create jar files for those. + if (f.exists() && !excludeJarFileNames.contains(f.getName())) { + dependencyJars.add(f); + } + } + return dependencyJars; } } - return dependencyJars; } /** @@ -238,17 +239,17 @@ public class ApexYarnLauncher { throw new RuntimeException("Failed to remove " + jarFile); } URI uri = URI.create("jar:" + jarFile.toURI()); - try (final FileSystem zipfs = FileSystems.newFileSystem(uri, env);) { + try (final FileSystem zipfs = FileSystems.newFileSystem(uri, env)) { File manifestFile = new File(dir, JarFile.MANIFEST_NAME); Files.createDirectory(zipfs.getPath("META-INF")); - final OutputStream out = Files.newOutputStream(zipfs.getPath(JarFile.MANIFEST_NAME)); - if (!manifestFile.exists()) { - new Manifest().write(out); - } else { - FileUtils.copyFile(manifestFile, out); + try (final OutputStream out = Files.newOutputStream(zipfs.getPath(JarFile.MANIFEST_NAME))) { + if (!manifestFile.exists()) { + new Manifest().write(out); + } else { + FileUtils.copyFile(manifestFile, out); + } } - out.close(); final java.nio.file.Path root = dir.toPath(); Files.walkFileTree(root, new java.nio.file.SimpleFileVisitor() { @@ -274,9 +275,9 @@ public class ApexYarnLauncher { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String name = relativePath + file.getFileName(); if (!JarFile.MANIFEST_NAME.equals(name)) { - final OutputStream out = Files.newOutputStream(zipfs.getPath(name)); - FileUtils.copyFile(file.toFile(), out); - out.close(); + try (final OutputStream out = Files.newOutputStream(zipfs.getPath(name))) { + FileUtils.copyFile(file.toFile(), out); + } } return super.visitFile(file, attrs); }