mirror of
https://github.com/mitb-archive/filebot
synced 2024-11-04 16:35:08 -05:00
* add simple Timer
* refactoring
This commit is contained in:
parent
ea3c1e179e
commit
d7c08bc4ca
@ -38,13 +38,13 @@ public abstract class BackgroundFileTransferablePolicy<V> extends FileTransferab
|
||||
clear();
|
||||
|
||||
worker = new BackgroundWorker(files);
|
||||
worker.addPropertyChangeListener(new BackgroundWorkerListener());
|
||||
worker.addPropertyChangeListener(backgroundWorkerListener);
|
||||
worker.execute();
|
||||
}
|
||||
|
||||
|
||||
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 class BackgroundWorkerListener extends SwingWorkerPropertyChangeAdapter {
|
||||
private final PropertyChangeListener backgroundWorkerListener = new SwingWorkerPropertyChangeAdapter() {
|
||||
|
||||
@Override
|
||||
public void started(PropertyChangeEvent evt) {
|
||||
@ -121,7 +120,7 @@ public abstract class BackgroundFileTransferablePolicy<V> extends FileTransferab
|
||||
public void done(PropertyChangeEvent evt) {
|
||||
propertyChangeSupport.firePropertyChange(LOADING_PROPERTY, true, false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
|
||||
|
||||
|
@ -3,11 +3,9 @@ package net.sourceforge.filebot.web;
|
||||
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -15,17 +13,16 @@ import javax.swing.Icon;
|
||||
|
||||
import net.sourceforge.filebot.FileBotUtil;
|
||||
import net.sourceforge.filebot.ResourceManager;
|
||||
import net.sourceforge.tuned.Timer;
|
||||
|
||||
|
||||
/**
|
||||
* {@link SubtitleClient} for OpenSubtitles.
|
||||
* SubtitleClient for OpenSubtitles.
|
||||
*/
|
||||
public class OpenSubtitlesSubtitleClient implements SubtitleClient {
|
||||
|
||||
private final OpenSubtitlesClient client = new OpenSubtitlesClient(String.format("%s v%s", FileBotUtil.getApplicationName(), FileBotUtil.getApplicationVersion()));
|
||||
|
||||
private final LogoutTimer logoutTimer = new LogoutTimer();
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
@ -44,19 +41,16 @@ public class OpenSubtitlesSubtitleClient implements SubtitleClient {
|
||||
public List<SearchResult> search(String searchterm) throws Exception {
|
||||
login();
|
||||
|
||||
List result = client.searchMoviesOnIMDB(searchterm);
|
||||
return result;
|
||||
return (List) client.searchMoviesOnIMDB(searchterm);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<SubtitleDescriptor> getSubtitleList(SearchResult searchResult, Locale language) throws Exception {
|
||||
public List<SubtitleDescriptor> getSubtitleList(SearchResult searchResult, Locale language) throws Exception {
|
||||
login();
|
||||
|
||||
int imdbId = ((MovieDescriptor) searchResult).getImdbId();
|
||||
|
||||
return (Collection) client.searchSubtitles(imdbId, language);
|
||||
return (List) client.searchSubtitles(((MovieDescriptor) searchResult).getImdbId(), language);
|
||||
}
|
||||
|
||||
|
||||
@ -69,81 +63,30 @@ public class OpenSubtitlesSubtitleClient implements SubtitleClient {
|
||||
private synchronized void login() throws Exception {
|
||||
if (!client.isLoggedOn()) {
|
||||
client.loginAnonymous();
|
||||
Runtime.getRuntime().addShutdownHook(logoutShutdownHook);
|
||||
}
|
||||
|
||||
logoutTimer.restart();
|
||||
logoutTimer.set(12, TimeUnit.MINUTES, true);
|
||||
}
|
||||
|
||||
|
||||
private synchronized void logout() {
|
||||
logoutTimer.stop();
|
||||
logoutTimer.cancel();
|
||||
|
||||
if (client.isLoggedOn()) {
|
||||
try {
|
||||
client.logout();
|
||||
} catch (Exception 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
|
||||
public void run() {
|
||||
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();
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,8 +27,6 @@ public class DefaultThreadFactory implements ThreadFactory {
|
||||
|
||||
public DefaultThreadFactory(String groupName, int priority, boolean daemon) {
|
||||
group = new ThreadGroup(groupName);
|
||||
group.setDaemon(daemon);
|
||||
group.setMaxPriority(priority);
|
||||
|
||||
this.daemon = daemon;
|
||||
this.priority = priority;
|
||||
|
94
source/net/sourceforge/tuned/Timer.java
Normal file
94
source/net/sourceforge/tuned/Timer.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user