From 6874f44b8643126de7a042657a4349a1c82e231c Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 31 Dec 2011 10:45:22 +0000 Subject: [PATCH] * allow running with restricted permissions --- source/net/sourceforge/tuned/Timer.java | 26 ++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/source/net/sourceforge/tuned/Timer.java b/source/net/sourceforge/tuned/Timer.java index d905f443..4d3fb3ab 100644 --- a/source/net/sourceforge/tuned/Timer.java +++ b/source/net/sourceforge/tuned/Timer.java @@ -6,6 +6,8 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; public abstract class Timer implements Runnable { @@ -16,7 +18,7 @@ public abstract class Timer implements Runnable { private ScheduledFuture scheduledFuture; private Thread shutdownHook; - + public synchronized void set(long delay, TimeUnit unit, boolean runBeforeShutdown) { // create executor if necessary if (executor == null) { @@ -31,7 +33,12 @@ public abstract class Timer implements Runnable { Runnable runnable = this; if (runBeforeShutdown) { - addShutdownHook(); + try { + addShutdownHook(); + } catch (Exception e) { + // may fail if running with restricted permissions + Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getClass().getName() + ": " + e.getMessage()); + } // remove shutdown hook after execution runnable = new Runnable() { @@ -46,14 +53,19 @@ public abstract class Timer implements Runnable { } }; } else { - // remove existing shutdown hook, if any - removeShutdownHook(); + try { + // remove existing shutdown hook, if any + removeShutdownHook(); + } catch (Exception e) { + // may fail if running with restricted permissions + Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getClass().getName() + ": " + e.getMessage()); + } } scheduledFuture = executor.schedule(runnable, delay, unit); } - + public synchronized void cancel() { removeShutdownHook(); @@ -64,7 +76,7 @@ public abstract class Timer implements Runnable { executor = null; } - + private synchronized void addShutdownHook() { if (shutdownHook == null) { shutdownHook = new Thread(this); @@ -72,7 +84,7 @@ public abstract class Timer implements Runnable { } } - + private synchronized void removeShutdownHook() { if (shutdownHook != null) { try {