From 874f7bd2c0c0435f613b8f9f5b3a16ebf2b4839b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 29 Dec 2008 01:05:04 +0100 Subject: [PATCH] Move UpdateChecker to application pacakge. --- .../todesbaum/jsite/application/UpdateChecker.java | 280 ++++++++++++++++++++ .../jsite/application/UpdateListener.java | 45 ++++ src/de/todesbaum/jsite/gui/UpdateChecker.java | 281 --------------------- src/de/todesbaum/jsite/gui/UpdateListener.java | 45 ---- src/de/todesbaum/jsite/main/Main.java | 4 +- 5 files changed, 327 insertions(+), 328 deletions(-) create mode 100644 src/de/todesbaum/jsite/application/UpdateChecker.java create mode 100644 src/de/todesbaum/jsite/application/UpdateListener.java delete mode 100644 src/de/todesbaum/jsite/gui/UpdateChecker.java delete mode 100644 src/de/todesbaum/jsite/gui/UpdateListener.java diff --git a/src/de/todesbaum/jsite/application/UpdateChecker.java b/src/de/todesbaum/jsite/application/UpdateChecker.java new file mode 100644 index 0000000..a87beab --- /dev/null +++ b/src/de/todesbaum/jsite/application/UpdateChecker.java @@ -0,0 +1,280 @@ +/* + * jSite-remote - UpdateChecker.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package de.todesbaum.jsite.application; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import de.todesbaum.jsite.main.Main; +import de.todesbaum.jsite.main.Version; +import de.todesbaum.util.freenet.fcp2.Client; +import de.todesbaum.util.freenet.fcp2.ClientGet; +import de.todesbaum.util.freenet.fcp2.Connection; +import de.todesbaum.util.freenet.fcp2.Message; +import de.todesbaum.util.freenet.fcp2.Persistence; +import de.todesbaum.util.freenet.fcp2.ReturnType; +import de.todesbaum.util.freenet.fcp2.Verbosity; +import de.todesbaum.util.io.Closer; + +/** + * Checks for newer versions of jSite. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ +public class UpdateChecker implements Runnable { + + /** Counter for connection names. */ + private static int counter = 0; + + /** The edition for the update check URL. */ + private static final int UPDATE_EDITION = 0; + + /** The URL for update checks. */ + private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE"; + + /** Object used for synchronization. */ + private final Object syncObject = new Object(); + + /** Update listeners. */ + private final List updateListeners = new ArrayList(); + + /** Whether the main thread should stop. */ + private boolean shouldStop = false; + + /** Current last found edition of update key. */ + private int lastUpdateEdition = UPDATE_EDITION; + + /** Last found version. */ + private Version lastVersion = Main.getVersion(); + + /** 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 freenetInterface + * The freenet interface + */ + 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; + syncObject.notifyAll(); + } + } + + // + // PRIVATE METHODS + // + + /** + * Returns whether the update checker should stop. + * + * @return true if the update checker should stop, + * false 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 { + connection.connect(); + } catch (IOException e1) { + e1.printStackTrace(); + } + Client client = new Client(connection); + boolean checkNow = false; + int currentEdition = lastUpdateEdition; + while (!shouldStop()) { + checkNow = false; + System.out.println("Trying " + constructUpdateKey(currentEdition)); + ClientGet clientGet = new ClientGet("get-update-key"); + clientGet.setUri(constructUpdateKey(currentEdition)); + clientGet.setPersistence(Persistence.CONNECTION); + clientGet.setReturnType(ReturnType.direct); + clientGet.setVerbosity(Verbosity.ALL); + try { + client.execute(clientGet); + boolean stop = false; + while (!stop) { + Message message = client.readMessage(); + System.out.println(message); + if ("GetFailed".equals(message.getName())) { + if ("27".equals(message.get("code"))) { + String editionString = message.get("redirecturi").split("/")[2]; + int editionNumber = -1; + try { + editionNumber = Integer.parseInt(editionString); + } catch (NumberFormatException nfe1) { + /* ignore. */ + } + if (editionNumber != -1) { + System.out.println("Found new edition " + editionNumber); + currentEdition = editionNumber; + lastUpdateEdition = editionNumber; + checkNow = true; + break; + } + } + } + if ("AllData".equals(message.getName())) { + System.out.println("Update data found."); + InputStream dataInputStream = null; + Properties properties = new Properties(); + try { + dataInputStream = message.getPayloadInputStream(); + properties.load(dataInputStream); + } finally { + Closer.close(dataInputStream); + } + + String foundVersionString = properties.getProperty("jSite.Version"); + if (foundVersionString != null) { + Version foundVersion = Version.parse(foundVersionString); + if (foundVersion != null) { + 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; + } + } + } + } + } catch (IOException e) { + System.out.println("Got IOException: " + e.getMessage()); + e.printStackTrace(); + } + if (!checkNow && !shouldStop()) { + synchronized (syncObject) { + try { + syncObject.wait(15 * 60 * 1000); + } catch (InterruptedException ie1) { + /* ignore. */ + } + } + } + } + } + +} diff --git a/src/de/todesbaum/jsite/application/UpdateListener.java b/src/de/todesbaum/jsite/application/UpdateListener.java new file mode 100644 index 0000000..fbf7b91 --- /dev/null +++ b/src/de/todesbaum/jsite/application/UpdateListener.java @@ -0,0 +1,45 @@ +/* + * jSite-remote - UpdateListener.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package de.todesbaum.jsite.application; + +import java.util.EventListener; + +import de.todesbaum.jsite.main.Version; + +/** + * Listener interface for objects that want to be notified when update data was + * found. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ +public interface UpdateListener extends EventListener { + + /** + * Notifies a listener that data for the given version was found. + * + * @param foundVersion + * The version that was found + * @param versionTimestamp + * The timestamp of the version, or -1 if the + * timestamp is unknown + */ + public void foundUpdateData(Version foundVersion, long versionTimestamp); + +} diff --git a/src/de/todesbaum/jsite/gui/UpdateChecker.java b/src/de/todesbaum/jsite/gui/UpdateChecker.java deleted file mode 100644 index 44be635..0000000 --- a/src/de/todesbaum/jsite/gui/UpdateChecker.java +++ /dev/null @@ -1,281 +0,0 @@ -/* - * jSite-remote - UpdateChecker.java - - * Copyright © 2008 David Roden - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -package de.todesbaum.jsite.gui; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; - -import de.todesbaum.jsite.application.Freenet7Interface; -import de.todesbaum.jsite.main.Main; -import de.todesbaum.jsite.main.Version; -import de.todesbaum.util.freenet.fcp2.Client; -import de.todesbaum.util.freenet.fcp2.ClientGet; -import de.todesbaum.util.freenet.fcp2.Connection; -import de.todesbaum.util.freenet.fcp2.Message; -import de.todesbaum.util.freenet.fcp2.Persistence; -import de.todesbaum.util.freenet.fcp2.ReturnType; -import de.todesbaum.util.freenet.fcp2.Verbosity; -import de.todesbaum.util.io.Closer; - -/** - * Checks for newer versions of jSite. - * - * @author David ‘Bombe’ Roden <bombe@freenetproject.org> - */ -public class UpdateChecker implements Runnable { - - /** Counter for connection names. */ - private static int counter = 0; - - /** The edition for the update check URL. */ - private static final int UPDATE_EDITION = 0; - - /** The URL for update checks. */ - private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE"; - - /** Object used for synchronization. */ - private final Object syncObject = new Object(); - - /** Update listeners. */ - private final List updateListeners = new ArrayList(); - - /** Whether the main thread should stop. */ - private boolean shouldStop = false; - - /** Current last found edition of update key. */ - private int lastUpdateEdition = UPDATE_EDITION; - - /** Last found version. */ - private Version lastVersion = Main.getVersion(); - - /** 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 freenetInterface - * The freenet interface - */ - 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; - syncObject.notifyAll(); - } - } - - // - // PRIVATE METHODS - // - - /** - * Returns whether the update checker should stop. - * - * @return true if the update checker should stop, - * false 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 { - connection.connect(); - } catch (IOException e1) { - e1.printStackTrace(); - } - Client client = new Client(connection); - boolean checkNow = false; - int currentEdition = lastUpdateEdition; - while (!shouldStop()) { - checkNow = false; - System.out.println("Trying " + constructUpdateKey(currentEdition)); - ClientGet clientGet = new ClientGet("get-update-key"); - clientGet.setUri(constructUpdateKey(currentEdition)); - clientGet.setPersistence(Persistence.CONNECTION); - clientGet.setReturnType(ReturnType.direct); - clientGet.setVerbosity(Verbosity.ALL); - try { - client.execute(clientGet); - boolean stop = false; - while (!stop) { - Message message = client.readMessage(); - System.out.println(message); - if ("GetFailed".equals(message.getName())) { - if ("27".equals(message.get("code"))) { - String editionString = message.get("redirecturi").split("/")[2]; - int editionNumber = -1; - try { - editionNumber = Integer.parseInt(editionString); - } catch (NumberFormatException nfe1) { - /* ignore. */ - } - if (editionNumber != -1) { - System.out.println("Found new edition " + editionNumber); - currentEdition = editionNumber; - lastUpdateEdition = editionNumber; - checkNow = true; - break; - } - } - } - if ("AllData".equals(message.getName())) { - System.out.println("Update data found."); - InputStream dataInputStream = null; - Properties properties = new Properties(); - try { - dataInputStream = message.getPayloadInputStream(); - properties.load(dataInputStream); - } finally { - Closer.close(dataInputStream); - } - - String foundVersionString = properties.getProperty("jSite.Version"); - if (foundVersionString != null) { - Version foundVersion = Version.parse(foundVersionString); - if (foundVersion != null) { - 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; - } - } - } - } - } catch (IOException e) { - System.out.println("Got IOException: " + e.getMessage()); - e.printStackTrace(); - } - if (!checkNow && !shouldStop()) { - synchronized (syncObject) { - try { - syncObject.wait(15 * 60 * 1000); - } catch (InterruptedException ie1) { - /* ignore. */ - } - } - } - } - } - -} diff --git a/src/de/todesbaum/jsite/gui/UpdateListener.java b/src/de/todesbaum/jsite/gui/UpdateListener.java deleted file mode 100644 index 4192946..0000000 --- a/src/de/todesbaum/jsite/gui/UpdateListener.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * jSite-remote - UpdateListener.java - - * Copyright © 2008 David Roden - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -package de.todesbaum.jsite.gui; - -import java.util.EventListener; - -import de.todesbaum.jsite.main.Version; - -/** - * Listener interface for objects that want to be notified when update data was - * found. - * - * @author David ‘Bombe’ Roden <bombe@freenetproject.org> - */ -public interface UpdateListener extends EventListener { - - /** - * Notifies a listener that data for the given version was found. - * - * @param foundVersion - * The version that was found - * @param versionTimestamp - * The timestamp of the version, or -1 if the - * timestamp is unknown - */ - public void foundUpdateData(Version foundVersion, long versionTimestamp); - -} diff --git a/src/de/todesbaum/jsite/main/Main.java b/src/de/todesbaum/jsite/main/Main.java index 75c1453..1138dc2 100644 --- a/src/de/todesbaum/jsite/main/Main.java +++ b/src/de/todesbaum/jsite/main/Main.java @@ -48,13 +48,13 @@ import de.todesbaum.jsite.application.FileOption; import de.todesbaum.jsite.application.Freenet7Interface; import de.todesbaum.jsite.application.Node; import de.todesbaum.jsite.application.Project; +import de.todesbaum.jsite.application.UpdateChecker; +import de.todesbaum.jsite.application.UpdateListener; import de.todesbaum.jsite.gui.NodeManagerListener; import de.todesbaum.jsite.gui.NodeManagerPage; import de.todesbaum.jsite.gui.ProjectFilesPage; import de.todesbaum.jsite.gui.ProjectInsertPage; import de.todesbaum.jsite.gui.ProjectPage; -import de.todesbaum.jsite.gui.UpdateChecker; -import de.todesbaum.jsite.gui.UpdateListener; import de.todesbaum.jsite.i18n.I18n; import de.todesbaum.jsite.i18n.I18nContainer; import de.todesbaum.util.image.IconLoader; -- 2.7.4