diff options
-rw-r--r-- | BKUWebStart/src/main/java/JettyTempCleaner.java | 57 | ||||
-rw-r--r-- | BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Container.java | 161 |
2 files changed, 125 insertions, 93 deletions
diff --git a/BKUWebStart/src/main/java/JettyTempCleaner.java b/BKUWebStart/src/main/java/JettyTempCleaner.java new file mode 100644 index 00000000..67854ecb --- /dev/null +++ b/BKUWebStart/src/main/java/JettyTempCleaner.java @@ -0,0 +1,57 @@ +import java.io.File; +import java.util.Timer; +import java.util.TimerTask; + +public class JettyTempCleaner extends TimerTask { + + private static final long CLEAN_INTERVAL = 60000; // 1 minute + + private File tmpDir; + + public JettyTempCleaner(File tmpDir) { + this.tmpDir = tmpDir; + } + + private static 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 static void clean(File tmpDir) { + System.out.println("Trying to remove " + tmpDir); + if (deleteRecursive(tmpDir)) { + System.out.println("Successfully removed temporary directory"); + System.exit(0); + } + } + + private void schedule() { + System.out.println("Scheduling cleaner for directory " + tmpDir); + Timer t = new Timer(); + t.scheduleAtFixedRate(this, 1000, CLEAN_INTERVAL); + } + + @Override + public void run() { + clean(tmpDir); + } + + public static void main(String[] args) { + if (args.length != 1) + System.exit(1); + File tmpDir = new File(args[0]); + if (!tmpDir.exists()) + { + System.err.println("Directory " + args[0] + " doesn't exist"); + System.exit(2); + } + JettyTempCleaner cleaner = new JettyTempCleaner(tmpDir); + cleaner.schedule(); + } +} 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<String> args = new ArrayList<String>(); + 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 {
|