Added threading while posting to del.icio.us.

This commit is contained in:
Erik C. Thauvin 2005-03-06 16:28:56 +00:00
parent c7dcbcc36c
commit 494333ddde
6 changed files with 265 additions and 112 deletions

View file

@ -71,9 +71,18 @@ public class DeliciousPoster
* @param entry The entry to add.
* @param extended The del.icio.us extended data.
*/
public final void addPost(EntryLink entry, String extended)
public final void addPost(final EntryLink entry, final String extended)
{
_delicious.addPost(entry.getLink(), entry.getTitle(), extended, _tags, entry.getDate());
final SwingWorker worker = new SwingWorker()
{
public Object construct()
{
return new Boolean(_delicious.addPost(entry.getLink(), entry.getTitle(), extended, _tags,
entry.getDate()));
}
};
worker.start();
}
/**
@ -81,8 +90,16 @@ public class DeliciousPoster
*
* @param entry The entry to delete.
*/
public final void deletePost(EntryLink entry)
public final void deletePost(final EntryLink entry)
{
_delicious.deletePost(entry.getLink());
final SwingWorker worker = new SwingWorker()
{
public Object construct()
{
return new Boolean(_delicious.deletePost(entry.getLink()));
}
};
worker.start();
}
}

View file

@ -1122,7 +1122,7 @@ public class Mobibot extends PircBot
if (_delicious != null)
{
_delicious.addPost(entry, postedBy(sender, channel));
_delicious.addPost(entry, postedBy(entry, channel));
}
saveEntries(isBackup);
@ -1395,7 +1395,7 @@ public class Mobibot extends PircBot
if (_delicious != null)
{
_delicious.addPost(entry, postedBy(sender, channel));
_delicious.addPost(entry, postedBy(entry, channel));
}
send(getChannel(), buildLink(index, entry));
@ -1421,7 +1421,7 @@ public class Mobibot extends PircBot
if (_delicious != null)
{
_delicious.addPost(entry, postedBy(sender, channel));
_delicious.addPost(entry, postedBy(entry, channel));
}
send(getChannel(), buildLink(index, entry));
@ -2277,14 +2277,14 @@ public class Mobibot extends PircBot
/**
* Returns he del.icio.us extended attribution line.
*
* @param sender The sender.
* @param entry The entry.
* @param channel The channel
*
* @return The extended attribution line.
*/
private final String postedBy(String sender, String channel)
private String postedBy(EntryLink entry, String channel)
{
return "Posted by " + sender + " on " + channel + " (" + _ircServer + ')';
return "Posted by " + entry.getNick() + " on " + channel + " (" + _ircServer + ')';
}
/**

View file

@ -1,5 +1,5 @@
/* Created by JReleaseInfo AntTask from Open Source Competence Group */
/* Creation date Sat Mar 05 13:36:00 PST 2005 */
/* Creation date Sun Mar 06 08:26:51 PST 2005 */
package net.thauvin.erik.mobibot;
import java.util.Date;
@ -12,21 +12,21 @@ import java.util.Date;
public class ReleaseInfo {
/** buildDate (set during build process to 1110058560437L). */
private static Date buildDate = new Date(1110058560437L);
/** buildDate (set during build process to 1110126411140L). */
private static Date buildDate = new Date(1110126411140L);
/**
* Get buildDate (set during build process to Sat Mar 05 13:36:00 PST 2005).
* Get buildDate (set during build process to Sun Mar 06 08:26:51 PST 2005).
* @return Date buildDate
*/
public static final Date getBuildDate() { return buildDate; }
/**
* Get buildNumber (set during build process to 0).
* Get buildNumber (set during build process to 2).
* @return int buildNumber
*/
public static final int getBuildNumber() { return 0; }
public static final int getBuildNumber() { return 2; }
/** version (set during build process to "0.3"). */

View file

@ -0,0 +1,131 @@
package net.thauvin.erik.mobibot;
import javax.swing.SwingUtilities;
/**
* This is the 3rd version of SwingWorker (also known as
* SwingWorker 3), an abstract class that you subclass to
* perform GUI-related work in a dedicated thread. For
* instructions on and examples of using this class, see:
*
* http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
*
* Note that the API changed slightly in the 3rd version:
* You must now invoke start() on the SwingWorker after
* creating it.
*/
public abstract class SwingWorker {
private Object value; // see getValue(), setValue()
/**
* Class to maintain reference to current worker thread
* under separate synchronization control.
*/
private static class ThreadVar {
private Thread thread;
ThreadVar(Thread t) { thread = t; }
synchronized Thread get() { return thread; }
synchronized void clear() { thread = null; }
}
private ThreadVar threadVar;
/**
* Get the value produced by the worker thread, or null if it
* hasn't been constructed yet.
*/
protected synchronized Object getValue() {
return value;
}
/**
* Set the value produced by worker thread
*/
private synchronized void setValue(Object x) {
value = x;
}
/**
* Compute the value to be returned by the <code>get</code> method.
*/
public abstract Object construct();
/**
* Called on the event dispatching thread (not on the worker thread)
* after the <code>construct</code> method has returned.
*/
public void finished() {
}
/**
* A new method that interrupts the worker thread. Call this method
* to force the worker to stop what it's doing.
*/
public void interrupt() {
Thread t = threadVar.get();
if (t != null) {
t.interrupt();
}
threadVar.clear();
}
/**
* Return the value created by the <code>construct</code> method.
* Returns null if either the constructing thread or the current
* thread was interrupted before a value was produced.
*
* @return the value created by the <code>construct</code> method
*/
public Object get() {
while (true) {
Thread t = threadVar.get();
if (t == null) {
return getValue();
}
try {
t.join();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt(); // propagate
return null;
}
}
}
/**
* Start a thread that will call the <code>construct</code> method
* and then exit.
*/
public SwingWorker() {
final Runnable doFinished = new Runnable() {
public void run() { finished(); }
};
Runnable doConstruct = new Runnable() {
public void run() {
try {
setValue(construct());
}
finally {
threadVar.clear();
}
SwingUtilities.invokeLater(doFinished);
}
};
Thread t = new Thread(doConstruct);
threadVar = new ThreadVar(t);
}
/**
* Start the worker thread.
*/
public void start() {
Thread t = threadVar.get();
if (t != null) {
t.start();
}
}
}