* add simple Timer

* refactoring
This commit is contained in:
Reinhard Pointner 2008-12-30 16:43:37 +00:00
parent ea3c1e179e
commit d7c08bc4ca
4 changed files with 108 additions and 74 deletions

View File

@ -38,13 +38,13 @@ public abstract class BackgroundFileTransferablePolicy<V> extends FileTransferab
clear(); clear();
worker = new BackgroundWorker(files); worker = new BackgroundWorker(files);
worker.addPropertyChangeListener(new BackgroundWorkerListener()); worker.addPropertyChangeListener(backgroundWorkerListener);
worker.execute(); worker.execute();
} }
public synchronized boolean isActive() { public synchronized boolean isActive() {
return (worker != null) && !worker.isDone(); return worker != null && !worker.isDone();
} }
@ -108,8 +108,7 @@ public abstract class BackgroundFileTransferablePolicy<V> extends FileTransferab
} }
} }
private final PropertyChangeListener backgroundWorkerListener = new SwingWorkerPropertyChangeAdapter() {
private class BackgroundWorkerListener extends SwingWorkerPropertyChangeAdapter {
@Override @Override
public void started(PropertyChangeEvent evt) { public void started(PropertyChangeEvent evt) {
@ -121,7 +120,7 @@ public abstract class BackgroundFileTransferablePolicy<V> extends FileTransferab
public void done(PropertyChangeEvent evt) { public void done(PropertyChangeEvent evt) {
propertyChangeSupport.firePropertyChange(LOADING_PROPERTY, true, false); propertyChangeSupport.firePropertyChange(LOADING_PROPERTY, true, false);
} }
} };
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

View File

@ -3,11 +3,9 @@ package net.sourceforge.filebot.web;
import java.net.URI; import java.net.URI;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Timer; import java.util.concurrent.TimeUnit;
import java.util.TimerTask;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -15,17 +13,16 @@ import javax.swing.Icon;
import net.sourceforge.filebot.FileBotUtil; import net.sourceforge.filebot.FileBotUtil;
import net.sourceforge.filebot.ResourceManager; import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.tuned.Timer;
/** /**
* {@link SubtitleClient} for OpenSubtitles. * SubtitleClient for OpenSubtitles.
*/ */
public class OpenSubtitlesSubtitleClient implements SubtitleClient { public class OpenSubtitlesSubtitleClient implements SubtitleClient {
private final OpenSubtitlesClient client = new OpenSubtitlesClient(String.format("%s v%s", FileBotUtil.getApplicationName(), FileBotUtil.getApplicationVersion())); private final OpenSubtitlesClient client = new OpenSubtitlesClient(String.format("%s v%s", FileBotUtil.getApplicationName(), FileBotUtil.getApplicationVersion()));
private final LogoutTimer logoutTimer = new LogoutTimer();
@Override @Override
public String getName() { public String getName() {
@ -44,19 +41,16 @@ public class OpenSubtitlesSubtitleClient implements SubtitleClient {
public List<SearchResult> search(String searchterm) throws Exception { public List<SearchResult> search(String searchterm) throws Exception {
login(); login();
List result = client.searchMoviesOnIMDB(searchterm); return (List) client.searchMoviesOnIMDB(searchterm);
return result;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public Collection<SubtitleDescriptor> getSubtitleList(SearchResult searchResult, Locale language) throws Exception { public List<SubtitleDescriptor> getSubtitleList(SearchResult searchResult, Locale language) throws Exception {
login(); login();
int imdbId = ((MovieDescriptor) searchResult).getImdbId(); return (List) client.searchSubtitles(((MovieDescriptor) searchResult).getImdbId(), language);
return (Collection) client.searchSubtitles(imdbId, language);
} }
@ -69,81 +63,30 @@ public class OpenSubtitlesSubtitleClient implements SubtitleClient {
private synchronized void login() throws Exception { private synchronized void login() throws Exception {
if (!client.isLoggedOn()) { if (!client.isLoggedOn()) {
client.loginAnonymous(); client.loginAnonymous();
Runtime.getRuntime().addShutdownHook(logoutShutdownHook);
} }
logoutTimer.restart(); logoutTimer.set(12, TimeUnit.MINUTES, true);
} }
private synchronized void logout() { private synchronized void logout() {
logoutTimer.stop(); logoutTimer.cancel();
if (client.isLoggedOn()) { if (client.isLoggedOn()) {
try { try {
client.logout(); client.logout();
} catch (Exception e) { } catch (Exception e) {
Logger.getLogger("global").log(Level.SEVERE, "Exception while deactivating session", e); Logger.getLogger("global").log(Level.SEVERE, "Exception while deactivating session", e);
} finally {
try {
Runtime.getRuntime().removeShutdownHook(logoutShutdownHook);
} catch (IllegalStateException e) {
// shutdown in progress
}
} }
} }
} }
private final Thread logoutShutdownHook = new Thread() { private final Timer logoutTimer = new Timer() {
@Override @Override
public void run() { public void run() {
logout(); logout();
} }
}; };
private class LogoutTimer {
private static final long LOGOUT_DELAY = 12 * 60 * 1000; // 12 minutes
private Timer daemon = null;
private LogoutTimerTask currentTimerTask = null;
public synchronized void restart() {
if (daemon == null) {
daemon = new Timer(getClass().getName(), true);
}
if (currentTimerTask != null) {
currentTimerTask.cancel();
}
currentTimerTask = new LogoutTimerTask();
daemon.schedule(currentTimerTask, LOGOUT_DELAY);
}
public synchronized void stop() {
if (daemon == null)
return;
currentTimerTask.cancel();
currentTimerTask = null;
daemon.cancel();
daemon = null;
}
private class LogoutTimerTask extends TimerTask {
@Override
public void run() {
logout();
};
};
}
} }

View File

@ -27,8 +27,6 @@ public class DefaultThreadFactory implements ThreadFactory {
public DefaultThreadFactory(String groupName, int priority, boolean daemon) { public DefaultThreadFactory(String groupName, int priority, boolean daemon) {
group = new ThreadGroup(groupName); group = new ThreadGroup(groupName);
group.setDaemon(daemon);
group.setMaxPriority(priority);
this.daemon = daemon; this.daemon = daemon;
this.priority = priority; this.priority = priority;

View File

@ -0,0 +1,94 @@
package net.sourceforge.tuned;
import java.util.concurrent.RunnableScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public abstract class Timer implements Runnable {
private final ScheduledThreadPoolExecutor executor;
private RunnableScheduledFuture<?> scheduledFuture;
private Thread shutdownHook;
public Timer() {
executor = new ScheduledThreadPoolExecutor(1);
executor.setKeepAliveTime(200, TimeUnit.MILLISECONDS);
executor.allowCoreThreadTimeOut(true);
}
public synchronized void set(long delay, TimeUnit unit, boolean runBeforeShutdown) {
removeScheduledFuture();
Runnable r = this;
if (runBeforeShutdown) {
addShutdownHook();
// remove shutdown hook after execution
r = new Runnable() {
@Override
public void run() {
try {
Timer.this.run();
} finally {
removeShutdownHook();
}
}
};
} else {
// remove existing shutdown hook, if any
removeShutdownHook();
}
scheduledFuture = (RunnableScheduledFuture<?>) executor.schedule(r, delay, unit);
}
public synchronized void cancel() {
removeScheduledFuture();
removeShutdownHook();
}
private synchronized void removeScheduledFuture() {
if (scheduledFuture != null) {
try {
scheduledFuture.cancel(false);
executor.remove(scheduledFuture);
} finally {
scheduledFuture = null;
}
}
}
private synchronized void addShutdownHook() {
if (shutdownHook == null) {
shutdownHook = new Thread(this);
Runtime.getRuntime().addShutdownHook(shutdownHook);
}
}
private synchronized void removeShutdownHook() {
if (shutdownHook != null) {
try {
if (shutdownHook != Thread.currentThread()) {
// can't remove shutdown hooks anymore, once runtime is shutting down,
// so don't remove the shutdown hook, if we are running on the shutdown hook
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
} finally {
shutdownHook = null;
}
}
}
}