Search in sources :

Example 1 with NoSuchVersionException

use of com.github.mob41.osumer.exceptions.NoSuchVersionException in project osumer by mob41.

the class Updater method getPerVersionPerBranchLatestVersion.

public UpdateInfo getPerVersionPerBranchLatestVersion() throws DebuggableException {
    final String thisVersion = Osumer.OSUMER_VERSION;
    final String buildBranch = Osumer.OSUMER_BRANCH;
    final int buildNum = Osumer.OSUMER_BUILD_NUM;
    final int updateSource = config.getUpdateSource();
    JSONObject json = getVersions();
    if (json.isNull("sources")) {
        throw new DebuggableException(VERSION_LIST, "Create JSONObject", "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Structure invalid, missing \"sources\" parameter", false);
    }
    // sources -> snapshot (updateSource) -> 1.0.0 (version) -> (array:
    // index0) 1 (build-number)
    JSONObject sourcesJson = json.getJSONObject("sources");
    JSONArray buildsArr;
    String sourceKey = null;
    switch(updateSource) {
        case UPDATE_SOURCE_SNAPSHOT:
            sourceKey = SOURCE_SNAPSHOT;
            break;
        case UPDATE_SOURCE_BETA:
            sourceKey = SOURCE_BETA;
            break;
        case UPDATE_SOURCE_STABLE:
            sourceKey = SOURCE_STABLE;
            break;
        default:
            throw new InvalidSourceIntegerException(json.toString(5), "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Validate sources' JSONObject and set variable", updateSource);
    }
    if (sourcesJson.isNull(sourceKey)) {
        throw new NoSuchSourceException(json.toString(5), "Convert source integer to string", "Validate sources' JSONObject and set variable", "Validate versions' JSONObject and set variable", sourceKey);
    }
    JSONObject versionsJson = sourcesJson.getJSONObject(sourceKey);
    if (versionsJson.isNull(thisVersion)) {
        throw new NoSuchVersionException(json.toString(5), "Validate sources' JSONObject and set variable", "Validate versions' JSONObject and set variable", "Validate builds JSONArray and set variable", sourceKey, thisVersion);
    }
    buildsArr = versionsJson.getJSONArray(thisVersion);
    if (buildsArr.length() < buildNum) {
        throw new NoSuchBuildNumberException(json.toString(5), "Validate versions' JSONObject and set variable", "Validate builds JSONArray and set variable", "Get latest build number from JSONArray", buildNum);
    }
    int latest = buildsArr.length();
    JSONObject verJson = buildsArr.getJSONObject(latest - 1);
    if ((verJson.isNull("ended") || !verJson.getBoolean("ended")) && (!buildBranch.equals(sourceKey) || latest != buildNum)) {
        String webLink = verJson.isNull("web_link") ? null : verJson.getString("web_link");
        String exeLink = verJson.isNull("exe_link") ? null : verJson.getString("exe_link");
        String jarLink = verJson.isNull("jar_link") ? null : verJson.getString("jar_link");
        String desc = verJson.isNull("desc") ? null : verJson.getString("desc");
        return new UpdateInfo(desc, thisVersion, updateSource, latest, webLink, exeLink, jarLink, false, false);
    }
    // As the version is ended, we are finding a new version here
    Iterator<String> it = versionsJson.keys();
    String key;
    String upgradeNode = null;
    while (it.hasNext()) {
        key = it.next();
        switch(compareVersion(thisVersion, key)) {
            case -2:
                break;
            case -1:
                if ((upgradeNode != null && compareVersion(upgradeNode, key) == -1) || upgradeNode == null) {
                    upgradeNode = key;
                }
                break;
            case 0:
                break;
            case 1:
                break;
        }
    }
    JSONArray upgradeNodeArr = null;
    int upgradedBuildNum = -1;
    if (upgradeNode != null) {
        upgradeNodeArr = versionsJson.getJSONArray(upgradeNode);
        upgradedBuildNum = upgradeNodeArr.length();
    }
    if (upgradedBuildNum == 0) {
        throw new NoBuildsForVersionException(json.toString(5), "Get Upgrade Node", "Validate build info JSON", "Return VersionInfo");
    }
    if (upgradeNode != null && upgradedBuildNum != -1) {
        JSONObject upgradedVerJson = upgradeNodeArr.getJSONObject(upgradedBuildNum - 1);
        String webLink = upgradedVerJson.isNull("web_link") ? null : upgradedVerJson.getString("web_link");
        String exeLink = upgradedVerJson.isNull("exe_link") ? null : upgradedVerJson.getString("exe_link");
        String jarLink = upgradedVerJson.isNull("jar_link") ? null : upgradedVerJson.getString("jar_link");
        String desc = upgradedVerJson.isNull("desc") ? null : upgradedVerJson.getString("desc");
        return new UpdateInfo(desc, upgradeNode, updateSource, upgradedBuildNum, webLink, exeLink, jarLink, false, true);
    } else {
        String webLink = verJson.isNull("web_link") ? null : verJson.getString("web_link");
        String exeLink = verJson.isNull("exe_link") ? null : verJson.getString("exe_link");
        String jarLink = verJson.isNull("jar_link") ? null : verJson.getString("jar_link");
        String desc = verJson.isNull("desc") ? null : verJson.getString("desc");
        return new UpdateInfo(desc, thisVersion, updateSource, buildNum, webLink, exeLink, jarLink, true, false);
    }
}
Also used : InvalidSourceIntegerException(com.github.mob41.osumer.exceptions.InvalidSourceIntegerException) DebuggableException(com.github.mob41.organdebug.exceptions.DebuggableException) NoSuchVersionException(com.github.mob41.osumer.exceptions.NoSuchVersionException) JSONObject(org.json.JSONObject) NoSuchBuildNumberException(com.github.mob41.osumer.exceptions.NoSuchBuildNumberException) NoSuchSourceException(com.github.mob41.osumer.exceptions.NoSuchSourceException) JSONArray(org.json.JSONArray) NoBuildsForVersionException(com.github.mob41.osumer.exceptions.NoBuildsForVersionException)

Example 2 with NoSuchVersionException

use of com.github.mob41.osumer.exceptions.NoSuchVersionException in project osumer by mob41.

the class UIFrame method checkUpdate.

public void checkUpdate() {
    if (checkingUpdate) {
        return;
    }
    checkingUpdate = true;
    Thread thread = new Thread() {

        public void run() {
            lblUpdateStatus.setForeground(Color.BLACK);
            lblUpdateStatus.setText("Checking for updates...");
            UpdateInfo verInfo = null;
            try {
                verInfo = getUpdateInfoByConfig();
            } catch (NoBuildsForVersionException e) {
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("No builds available for the new version. See dump.");
                checkingUpdate = false;
                return;
            } catch (NoSuchVersionException e) {
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("No current version in the selected branch. See dump.");
                JOptionPane.showMessageDialog(UIFrame.this, "We don't have version " + Osumer.OSUMER_VERSION + " in the current update branch\n\nPlease try another update branch (snapshot, beta, stable).", "Version not available", JOptionPane.INFORMATION_MESSAGE);
                checkingUpdate = false;
                return;
            } catch (NoSuchBuildNumberException e) {
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("This version has a invalid build number. See dump");
                JOptionPane.showMessageDialog(UIFrame.this, "We don't have build number greater or equal to " + Osumer.OSUMER_BUILD_NUM + " in version " + Osumer.OSUMER_VERSION + ".\n" + "If you are using a modified/development osumer,\n" + " you can just ignore this message.\n" + "If not, this might be the versions.json in GitHub goes wrong,\n" + " post a new issue about this.", "Build not available", JOptionPane.WARNING_MESSAGE);
                checkingUpdate = false;
                return;
            } catch (DebuggableException e) {
                e.printStackTrace();
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("Could not connect to update server.");
                JOptionPane.showMessageDialog(UIFrame.this, "Could not connect to update server.", "Error", JOptionPane.ERROR_MESSAGE);
                checkingUpdate = false;
                return;
            }
            if (verInfo == null) {
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("Could not obtain update info.");
                JOptionPane.showMessageDialog(UIFrame.this, "Could not obtain update info.", "Error", JOptionPane.ERROR_MESSAGE);
                checkingUpdate = false;
                return;
            }
            if (verInfo.isThisVersion()) {
                lblUpdateStatus.setForeground(Color.BLACK);
                lblUpdateStatus.setText("You are running the latest version of osumer" + " (" + verInfo.getVersion() + "-" + Updater.getBranchStr(verInfo.getBranch()) + "-" + verInfo.getBuildNum() + ")");
                checkingUpdate = false;
                return;
            }
            lblUpdateStatus.setForeground(new Color(0, 153, 0));
            lblUpdateStatus.setText((verInfo.isUpgradedVersion() ? "Upgrade" : "Update") + " available! New version: " + verInfo.getVersion() + "-" + Updater.getBranchStr(verInfo.getBranch()) + "-b" + verInfo.getBuildNum());
            int option;
            String desc = verInfo.getDescription();
            if (desc == null) {
                option = JOptionPane.showOptionDialog(UIFrame.this, "New " + (verInfo.isUpgradedVersion() ? "upgrade" : "update") + " available! New version:\n" + verInfo.getVersion() + "-" + Updater.getBranchStr(verInfo.getBranch()) + "-b" + verInfo.getBuildNum() + "\n\n" + "Do you want to update it now?", "Update available", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, JOptionPane.NO_OPTION);
            } else {
                option = JOptionPane.showOptionDialog(UIFrame.this, "New " + (verInfo.isUpgradedVersion() ? "upgrade" : "update") + " available! New version:\n" + verInfo.getVersion() + "-" + Updater.getBranchStr(verInfo.getBranch()) + "-b" + verInfo.getBuildNum() + "\n\n" + "Do you want to update it now?", "Update available", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Yes", "No", "Description/Changelog" }, JOptionPane.NO_OPTION);
                if (option == 2) {
                    option = JOptionPane.showOptionDialog(UIFrame.this, new TextPanel(desc), "Update description/change-log", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, 0);
                }
            }
            if (option == JOptionPane.YES_OPTION) {
                /*
                    try {
                        Desktop.getDesktop().browse(new URI(verInfo.getWebLink()));
                    } catch (IOException | URISyntaxException e) {
                        DebugDump dump = new DebugDump(
                                verInfo.getWebLink(),
                                "Show option dialog of updating osumer or not",
                                "Set checkingUpdate to false",
                                "(End of function / thread)",
                                "Error when opening the web page",
                                false,
                                e);
                        DumpManager.getInstance().addDump(dump);
                        DebugDump.showDebugDialog(dump);
                    }
                    */
                try {
                    String updaterLink = Updater.getUpdaterLink();
                    if (updaterLink == null) {
                        System.out.println("No latest updater .exe defined! Falling back to legacy updater!");
                        updaterLink = Updater.LEGACY_UPDATER_JAR;
                    }
                    URL url;
                    try {
                        url = new URL(updaterLink);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                        JOptionPane.showMessageDialog(null, "Error:\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    final String folder = System.getProperty("java.io.tmpdir");
                    final String fileName = "osumer_updater_" + Calendar.getInstance().getTimeInMillis() + ".exe";
                    mgr.addQueue(new Queue("osumer Updater", new URLDownloader(folder, fileName, url), null, null, new QueueAction[] { new UpdaterRunAction(folder + fileName) }));
                    tab.setSelectedIndex(1);
                    new Thread() {

                        public void run() {
                            JOptionPane.showMessageDialog(UIFrame.this, "The web updater will be downloaded and started very soon.", "Notice", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }.start();
                } catch (DebuggableException e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Error:\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
                    checkingUpdate = false;
                }
            }
            checkingUpdate = false;
        }
    };
    thread.start();
}
Also used : NoSuchVersionException(com.github.mob41.osumer.exceptions.NoSuchVersionException) DebuggableException(com.github.mob41.organdebug.exceptions.DebuggableException) MalformedURLException(java.net.MalformedURLException) UpdaterRunAction(com.github.mob41.osumer.io.queue.actions.UpdaterRunAction) Color(java.awt.Color) QueueAction(com.github.mob41.osumer.io.queue.QueueAction) NoBuildsForVersionException(com.github.mob41.osumer.exceptions.NoBuildsForVersionException) URLDownloader(com.github.mob41.osumer.io.legacy.URLDownloader) URL(java.net.URL) SockThread(com.github.mob41.osumer.sock.SockThread) NoSuchBuildNumberException(com.github.mob41.osumer.exceptions.NoSuchBuildNumberException) Queue(com.github.mob41.osumer.io.queue.Queue) EventQueue(java.awt.EventQueue) UpdateInfo(com.github.mob41.osumer.updater.UpdateInfo)

Aggregations

DebuggableException (com.github.mob41.organdebug.exceptions.DebuggableException)2 NoBuildsForVersionException (com.github.mob41.osumer.exceptions.NoBuildsForVersionException)2 NoSuchBuildNumberException (com.github.mob41.osumer.exceptions.NoSuchBuildNumberException)2 NoSuchVersionException (com.github.mob41.osumer.exceptions.NoSuchVersionException)2 InvalidSourceIntegerException (com.github.mob41.osumer.exceptions.InvalidSourceIntegerException)1 NoSuchSourceException (com.github.mob41.osumer.exceptions.NoSuchSourceException)1 URLDownloader (com.github.mob41.osumer.io.legacy.URLDownloader)1 Queue (com.github.mob41.osumer.io.queue.Queue)1 QueueAction (com.github.mob41.osumer.io.queue.QueueAction)1 UpdaterRunAction (com.github.mob41.osumer.io.queue.actions.UpdaterRunAction)1 SockThread (com.github.mob41.osumer.sock.SockThread)1 UpdateInfo (com.github.mob41.osumer.updater.UpdateInfo)1 Color (java.awt.Color)1 EventQueue (java.awt.EventQueue)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1