Preventing a resource leak of an output stream for the jclouds.properties itest file

This commit is contained in:
Andrew Phillips 2012-02-17 22:50:38 +01:00
parent 15a157a163
commit 2f5f0b3474
3 changed files with 36 additions and 5 deletions

View File

@ -19,6 +19,7 @@
package org.jclouds.demo.tweetstore.integration; package org.jclouds.demo.tweetstore.integration;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.Closeables.closeQuietly;
import static java.lang.String.format; import static java.lang.String.format;
import java.io.File; import java.io.File;
@ -45,7 +46,7 @@ public class GoogleDevServer {
String filename = String.format( String filename = String.format(
"%1$s/WEB-INF/jclouds.properties", warfile); "%1$s/WEB-INF/jclouds.properties", warfile);
System.err.println("file: " + filename); System.err.println("file: " + filename);
props.store(new FileOutputStream(filename), "test"); storeProperties(filename, props);
assert new File(filename).exists(); assert new File(filename).exists();
this.server = new Thread(new Runnable() { this.server = new Thread(new Runnable() {
public void run() { public void run() {
@ -64,6 +65,15 @@ public class GoogleDevServer {
TimeUnit.SECONDS.sleep(30); 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 { public void stop() throws Exception {
// KickStart.main opens a process and calls process.waitFor(), which is interruptable // KickStart.main opens a process and calls process.waitFor(), which is interruptable
server.interrupt(); server.interrupt();

View File

@ -19,6 +19,7 @@
package org.jclouds.demo.tweetstore.integration; package org.jclouds.demo.tweetstore.integration;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.Closeables.closeQuietly;
import static java.lang.String.format; import static java.lang.String.format;
import java.io.File; import java.io.File;
@ -45,7 +46,7 @@ public class GoogleDevServer {
String filename = String.format( String filename = String.format(
"%1$s/WEB-INF/jclouds.properties", warfile); "%1$s/WEB-INF/jclouds.properties", warfile);
System.err.println("file: " + filename); System.err.println("file: " + filename);
props.store(new FileOutputStream(filename), "test"); storeProperties(filename, props);
assert new File(filename).exists(); assert new File(filename).exists();
this.server = new Thread(new Runnable() { this.server = new Thread(new Runnable() {
public void run() { public void run() {
@ -64,6 +65,15 @@ public class GoogleDevServer {
TimeUnit.SECONDS.sleep(30); 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 { public void stop() throws Exception {
// KickStart.main opens a process and calls process.waitFor(), which is interruptable // KickStart.main opens a process and calls process.waitFor(), which is interruptable
server.interrupt(); server.interrupt();

View File

@ -18,6 +18,8 @@
*/ */
package org.jclouds.demo.tweetstore.integration; package org.jclouds.demo.tweetstore.integration;
import static com.google.common.io.Closeables.closeQuietly;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -29,9 +31,9 @@ import javax.servlet.ServletException;
import org.apache.commons.cli.ParseException; 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 { public class RunAtCloudServer {
protected StaxSdkAppServer2 server; protected StaxSdkAppServer2 server;
@ -42,7 +44,7 @@ public class RunAtCloudServer {
String filename = String.format( String filename = String.format(
"%1$s/WEB-INF/jclouds.properties", warfile); "%1$s/WEB-INF/jclouds.properties", warfile);
System.err.println("file: " + filename); System.err.println("file: " + filename);
props.store(new FileOutputStream(filename), "test"); storeProperties(filename, props);
assert new File(filename).exists(); assert new File(filename).exists();
server = StaxSdkAppServer2.createServer(new String[] { "-web", warfile, "-port", port, "-env", environments, server = StaxSdkAppServer2.createServer(new String[] { "-web", warfile, "-port", port, "-env", environments,
"-dir", serverBaseDirectory }, new String[0], Thread.currentThread().getContextClassLoader()); "-dir", serverBaseDirectory }, new String[0], Thread.currentThread().getContextClassLoader());
@ -50,6 +52,15 @@ public class RunAtCloudServer {
TimeUnit.SECONDS.sleep(30); 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 { public void stop() throws Exception {
server.stop(); server.stop();
} }