From e11ea79da900664bf11b56fb3110fa3f6890daaf Mon Sep 17 00:00:00 2001 From: tkellner Date: Tue, 21 Aug 2012 11:55:12 +0000 Subject: Remove leftover JettyContext working dirs on Windows Spawns a separate process on exit if running under Windows, since the directory can only be deleted after the main process finishes. Fixes MOCCA-780 git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@1132 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- .../java/at/gv/egiz/bku/webstart/Container.java | 161 +++++++++------------ 1 file changed, 68 insertions(+), 93 deletions(-) (limited to 'BKUWebStart/src/main/java/at/gv') diff --git a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Container.java b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Container.java index 8a71b1e9..ad589a59 100644 --- a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Container.java +++ b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Container.java @@ -24,39 +24,44 @@ package at.gv.egiz.bku.webstart; -import iaik.utils.StreamCopier; - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.security.KeyStore; -import java.security.cert.Certificate; +import iaik.utils.StreamCopier; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.security.KeyStore; +import java.security.cert.Certificate; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; -import org.mortbay.jetty.Connector; -import org.mortbay.jetty.Server; -import org.mortbay.jetty.nio.SelectChannelConnector; -import org.mortbay.jetty.security.SslSocketConnector; -import org.mortbay.jetty.webapp.WebAppContext; -import org.mortbay.thread.QueuedThreadPool; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.mortbay.jetty.Connector; +import org.mortbay.jetty.Server; +import org.mortbay.jetty.nio.SelectChannelConnector; +import org.mortbay.jetty.security.SslSocketConnector; +import org.mortbay.jetty.webapp.WebAppContext; +import org.mortbay.thread.QueuedThreadPool; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class Container { public static final String HTTP_PORT_PROPERTY = "mocca.http.port"; - public static final String HTTPS_PORT_PROPERTY = "mocca.https.port"; + public static final String HTTPS_PORT_PROPERTY = "mocca.https.port"; + + private static final String JETTY_TEMP_CLEANER_CLASSNAME = "JettyTempCleaner"; + private static Logger log = LoggerFactory.getLogger(Container.class); - static { + static { if (log.isDebugEnabled()) { //Jetty log INFO and WARN, include ignored exceptions //jetty logging may be further restricted by setting level in log4j.properties @@ -201,69 +206,42 @@ public class Container { return webapp.getPath(); } -// private boolean deleteRecursive(File f) -// { -// if (f.isDirectory()) -// { -// String[] children = f.list(); -// for (String child : children) { -// if (!deleteRecursive(new File(f, child))) -// return false; -// } -// } -// return f.delete(); -// } -// -// private void deleteOnExitRecursive(File f) -// { -// if (f.isDirectory()) -// { -// String[] children = f.list(); -// for (String child : children) -// deleteOnExitRecursive(new File(f, child)); -// } -// f.deleteOnExit(); -// } -// -// /** -// * Workaround for Jetty problem where temporary directory -// * doesn't get deleted under Windows due to locking issues. -// * -// * @param tempDir Temporary directory to delete -// */ -// private void deleteTempDir(final File tempDir) { -// try { -// if (tempDir.exists()) { -// log.debug("Temp directory still exists - trying to delete"); -// if (!deleteRecursive(tempDir)) -// { -// log.debug("Deleting temp directory failed - adding shutdown hook"); -// Runtime.getRuntime().addShutdownHook( -// new Thread() { -// public void run() { -// log.debug("Shutdown hook executing"); -// if (tempDir.exists()) { -// log.debug("Temp directory still exists - trying to delete"); -// if (!deleteRecursive(tempDir)) -// { -// log.debug("Deleting temp directory failed"); -// deleteOnExitRecursive(tempDir); -// } -// else -// log.debug("Successfully deleted temp directory"); -// } -// } -// } -// ); -// } -// else -// log.debug("Successfully deleted temp directory"); -// } -// } catch (SecurityException ex) { -// log.debug("Temp directory access failed", ex); -// } -// } - + private void copyCleaner(File dir) throws IOException { + File cleanerClass = new File(dir, JETTY_TEMP_CLEANER_CLASSNAME + ".class"); + log.debug("copying JettyTempCleaner to " + cleanerClass); + InputStream is = getClass().getClassLoader().getResourceAsStream(JETTY_TEMP_CLEANER_CLASSNAME + ".class"); + OutputStream os; + os = new BufferedOutputStream(new FileOutputStream(cleanerClass)); + new StreamCopier(is, os).copyStream(); + os.close(); + } + + private void cleanupJettyTemp() { + String os = System.getProperty("os.name"); + if (os.toLowerCase().contains("windows")) { + try { + File userDir = new File(System.getProperty("user.home") + "/" + Configurator.BKU_USER_DIR); + if (!userDir.exists()) + { + log.error("User directory " + userDir + " not found"); + return; + } + copyCleaner(userDir); + List args = new ArrayList(); + args.add("java"); + args.add(JETTY_TEMP_CLEANER_CLASSNAME); + args.add(tempDir.getAbsolutePath()); + ProcessBuilder pb = new ProcessBuilder(args); + pb.directory(userDir); + log.debug("Starting " + JETTY_TEMP_CLEANER_CLASSNAME + " to remove " + tempDir.getAbsolutePath()); + pb.start(); + } catch (IOException e) { + log.error("Failed to copy jetty temp cleaner", e); + e.printStackTrace(); + } + } + } + /** * grant all permissions, since we need read/write access to save signature data files anywhere (JFileChooser) in the local filesystem * and Jetty does not allow declare (webapp) permissions on a codeBase basis. @@ -285,7 +263,6 @@ public class Container { public void start() throws Exception { server.start(); - // webapp.getBaseResource() File caCertFile = new File(webapp.getTempDirectory(), "webapp/ca.crt"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(caCertFile)); bos.write(caCertificate.getEncoded()); @@ -298,17 +275,15 @@ public class Container { } public void stop() throws Exception { -// log.debug("Container: stop called"); server.stop(); -// deleteTempDir(tempDir); + cleanupJettyTemp(); } public void destroy() { -// log.debug("Container: destroy called"); server.destroy(); -// deleteTempDir(tempDir); + cleanupJettyTemp(); } public void join() throws InterruptedException { -- cgit v1.2.3