import java.util.List;
import java.util.Properties;
-import javax.swing.JFrame;
-
import de.todesbaum.jsite.application.Freenet7Interface;
import de.todesbaum.jsite.main.Main;
import de.todesbaum.jsite.main.Version;
/** Last found version. */
private Version lastVersion = Main.getVersion();
- /** The parent of the dialog. */
- private final JFrame parent;
-
/** The freenet interface. */
private final Freenet7Interface freenetInterface;
* Creates a new update checker that uses the given frame as its parent and
* communications via the given freenet interface.
*
- * @param parent
- * The parent of the dialog
* @param freenetInterface
* The freenet interface
*/
- public UpdateChecker(JFrame parent, Freenet7Interface freenetInterface) {
- this.parent = parent;
+ public UpdateChecker(Freenet7Interface freenetInterface) {
this.freenetInterface = freenetInterface;
}
// EVENT LISTENER MANAGEMENT
//
+ /**
+ * Adds an update listener to the list of registered listeners.
+ *
+ * @param updateListener
+ * The update listener to add
+ */
public void addUpdateListener(UpdateListener updateListener) {
updateListeners.add(updateListener);
}
+ /**
+ * Removes the given listener from the list of registered listeners.
+ *
+ * @param updateListener
+ * The update listener to remove
+ */
public void removeUpdateListener(UpdateListener updateListener) {
updateListeners.remove(updateListener);
}
+ /**
+ * Notifies all listeners that a version was found.
+ *
+ * @param foundVersion
+ * The version that was found
+ * @param versionTimestamp
+ * The timestamp of the version
+ */
protected void fireUpdateFound(Version foundVersion, long versionTimestamp) {
for (UpdateListener updateListener : updateListeners) {
updateListener.foundUpdateData(foundVersion, versionTimestamp);
}
//
+ // ACCESSORS
+ //
+
+ /**
+ * Returns the latest version that was found.
+ *
+ * @return The latest found version
+ */
+ public Version getLatestVersion() {
+ return lastVersion;
+ }
+
+ //
// ACTIONS
//
+ /**
+ * Starts the update checker.
+ */
public void start() {
new Thread(this).start();
}
+ /**
+ * Stops the update checker.
+ */
public void stop() {
synchronized (syncObject) {
shouldStop = true;
// PRIVATE METHODS
//
+ /**
+ * Returns whether the update checker should stop.
+ *
+ * @return <code>true</code> if the update checker should stop,
+ * <code>false</code> otherwise
+ */
private boolean shouldStop() {
synchronized (syncObject) {
return shouldStop;
}
}
+ /**
+ * Creates the URI of the update file for the given edition.
+ *
+ * @param edition
+ * The edition number
+ * @return The URI for the update file for the given edition
+ */
private String constructUpdateKey(int edition) {
return UPDATE_KEY + "/jSite/" + edition + "/jSite.properties";
}
// INTERFACE Runnable
//
+ /**
+ * {@inheritDoc}
+ */
public void run() {
Connection connection = freenetInterface.getConnection("jSite-" + ++counter + "-UpdateChecker");
try {
e1.printStackTrace();
}
Client client = new Client(connection);
+ boolean checkNow = false;
+ int currentEdition = lastUpdateEdition;
while (!shouldStop()) {
- System.out.println("Trying " + constructUpdateKey(lastUpdateEdition));
+ checkNow = false;
+ System.out.println("Trying " + constructUpdateKey(currentEdition));
ClientGet clientGet = new ClientGet("get-update-key");
- clientGet.setUri(constructUpdateKey(lastUpdateEdition));
+ clientGet.setUri(constructUpdateKey(currentEdition));
clientGet.setPersistence(Persistence.CONNECTION);
clientGet.setReturnType(ReturnType.direct);
clientGet.setVerbosity(Verbosity.ALL);
}
if (editionNumber != -1) {
System.out.println("Found new edition " + editionNumber);
+ currentEdition = editionNumber;
lastUpdateEdition = editionNumber;
+ checkNow = true;
break;
}
}
if (foundVersionString != null) {
Version foundVersion = Version.parse(foundVersionString);
if (foundVersion != null) {
- if (foundVersion.compareTo(Main.getVersion()) > 0) {
- lastVersion = foundVersion;
- String versionTimestampString = properties.getProperty("jSite.Date");
- long versionTimestamp = -1;
- try {
- versionTimestamp = Long.parseLong(versionTimestampString);
- } catch (NumberFormatException nfe1) {
- /* ignore. */
- }
- fireUpdateFound(foundVersion, versionTimestamp);
+ lastVersion = foundVersion;
+ String versionTimestampString = properties.getProperty("jSite.Date");
+ System.out.println(versionTimestampString);
+ long versionTimestamp = -1;
+ try {
+ versionTimestamp = Long.parseLong(versionTimestampString);
+ } catch (NumberFormatException nfe1) {
+ /* ignore. */
}
+ fireUpdateFound(foundVersion, versionTimestamp);
+ stop = true;
+ checkNow = true;
+ ++currentEdition;
}
}
}
System.out.println("Got IOException: " + e.getMessage());
e.printStackTrace();
}
+ if (!checkNow && !shouldStop()) {
+ synchronized (syncObject) {
+ try {
+ syncObject.wait(15 * 60 * 1000);
+ } catch (InterruptedException ie1) {
+ /* ignore. */
+ }
+ }
+ }
}
}
+
}