From 2f5f0b347432a92c4a5e6185a52f7cc3330cd4ed Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Fri, 17 Feb 2012 22:50:38 +0100 Subject: [PATCH] Preventing a resource leak of an output stream for the jclouds.properties itest file --- .../tweetstore/integration/GoogleDevServer.java | 12 +++++++++++- .../tweetstore/integration/GoogleDevServer.java | 12 +++++++++++- .../integration/RunAtCloudServer.java | 17 ++++++++++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/demos/tweetstore/gae-tweetstore-spring/src/test/java/org/jclouds/demo/tweetstore/integration/GoogleDevServer.java b/demos/tweetstore/gae-tweetstore-spring/src/test/java/org/jclouds/demo/tweetstore/integration/GoogleDevServer.java index 41bed848e7..63223607ac 100644 --- a/demos/tweetstore/gae-tweetstore-spring/src/test/java/org/jclouds/demo/tweetstore/integration/GoogleDevServer.java +++ b/demos/tweetstore/gae-tweetstore-spring/src/test/java/org/jclouds/demo/tweetstore/integration/GoogleDevServer.java @@ -19,6 +19,7 @@ package org.jclouds.demo.tweetstore.integration; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.io.Closeables.closeQuietly; import static java.lang.String.format; import java.io.File; @@ -45,7 +46,7 @@ public class GoogleDevServer { String filename = String.format( "%1$s/WEB-INF/jclouds.properties", warfile); System.err.println("file: " + filename); - props.store(new FileOutputStream(filename), "test"); + storeProperties(filename, props); assert new File(filename).exists(); this.server = new Thread(new Runnable() { public void run() { @@ -64,6 +65,15 @@ public class GoogleDevServer { TimeUnit.SECONDS.sleep(30); } + private static void storeProperties(String filename, Properties props) throws IOException { + FileOutputStream targetFile = new FileOutputStream(filename); + try { + props.store(targetFile, "test"); + } finally { + closeQuietly(targetFile); + } + } + public void stop() throws Exception { // KickStart.main opens a process and calls process.waitFor(), which is interruptable server.interrupt(); diff --git a/demos/tweetstore/gae-tweetstore/src/test/java/org/jclouds/demo/tweetstore/integration/GoogleDevServer.java b/demos/tweetstore/gae-tweetstore/src/test/java/org/jclouds/demo/tweetstore/integration/GoogleDevServer.java index 41bed848e7..63223607ac 100644 --- a/demos/tweetstore/gae-tweetstore/src/test/java/org/jclouds/demo/tweetstore/integration/GoogleDevServer.java +++ b/demos/tweetstore/gae-tweetstore/src/test/java/org/jclouds/demo/tweetstore/integration/GoogleDevServer.java @@ -19,6 +19,7 @@ package org.jclouds.demo.tweetstore.integration; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.io.Closeables.closeQuietly; import static java.lang.String.format; import java.io.File; @@ -45,7 +46,7 @@ public class GoogleDevServer { String filename = String.format( "%1$s/WEB-INF/jclouds.properties", warfile); System.err.println("file: " + filename); - props.store(new FileOutputStream(filename), "test"); + storeProperties(filename, props); assert new File(filename).exists(); this.server = new Thread(new Runnable() { public void run() { @@ -64,6 +65,15 @@ public class GoogleDevServer { TimeUnit.SECONDS.sleep(30); } + private static void storeProperties(String filename, Properties props) throws IOException { + FileOutputStream targetFile = new FileOutputStream(filename); + try { + props.store(targetFile, "test"); + } finally { + closeQuietly(targetFile); + } + } + public void stop() throws Exception { // KickStart.main opens a process and calls process.waitFor(), which is interruptable server.interrupt(); diff --git a/demos/tweetstore/runatcloud-tweetstore/src/test/java/org/jclouds/demo/tweetstore/integration/RunAtCloudServer.java b/demos/tweetstore/runatcloud-tweetstore/src/test/java/org/jclouds/demo/tweetstore/integration/RunAtCloudServer.java index ee3374c98e..0272aea3b1 100644 --- a/demos/tweetstore/runatcloud-tweetstore/src/test/java/org/jclouds/demo/tweetstore/integration/RunAtCloudServer.java +++ b/demos/tweetstore/runatcloud-tweetstore/src/test/java/org/jclouds/demo/tweetstore/integration/RunAtCloudServer.java @@ -18,6 +18,8 @@ */ package org.jclouds.demo.tweetstore.integration; +import static com.google.common.io.Closeables.closeQuietly; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -29,9 +31,9 @@ import javax.servlet.ServletException; import org.apache.commons.cli.ParseException; /** - * Basic functionality to start a local google app engine instance. + * Basic functionality to start a local RUN@cloud instance. * - * @author Adrian Cole + * @author Andrew Phillips */ public class RunAtCloudServer { protected StaxSdkAppServer2 server; @@ -42,7 +44,7 @@ public class RunAtCloudServer { String filename = String.format( "%1$s/WEB-INF/jclouds.properties", warfile); System.err.println("file: " + filename); - props.store(new FileOutputStream(filename), "test"); + storeProperties(filename, props); assert new File(filename).exists(); server = StaxSdkAppServer2.createServer(new String[] { "-web", warfile, "-port", port, "-env", environments, "-dir", serverBaseDirectory }, new String[0], Thread.currentThread().getContextClassLoader()); @@ -50,6 +52,15 @@ public class RunAtCloudServer { TimeUnit.SECONDS.sleep(30); } + private static void storeProperties(String filename, Properties props) throws IOException { + FileOutputStream targetFile = new FileOutputStream(filename); + try { + props.store(targetFile, "test"); + } finally { + closeQuietly(targetFile); + } + } + public void stop() throws Exception { server.stop(); }