From e6817d60624b7e3f97b579fe5b280f829b78902f Mon Sep 17 00:00:00 2001 From: Andreas Abraham Date: Fri, 7 Sep 2018 11:33:53 +0200 Subject: refactored mocca updater --- .../src/main/java/at/gv/egiz/upater/Constants.java | 10 -- .../main/java/at/gv/egiz/upater/MoccaUpdater.java | 112 ----------------- .../java/at/gv/egiz/upater/NewVersionDialog.java | 133 --------------------- .../java/at/gv/egiz/upater/VersionComparator.java | 101 ---------------- .../main/java/at/gv/egiz/updater/Constants.java | 10 ++ .../main/java/at/gv/egiz/updater/MoccaUpdater.java | 112 +++++++++++++++++ .../java/at/gv/egiz/updater/NewVersionDialog.java | 132 ++++++++++++++++++++ .../java/at/gv/egiz/updater/VersionComparator.java | 101 ++++++++++++++++ 8 files changed, 355 insertions(+), 356 deletions(-) delete mode 100644 utils/src/main/java/at/gv/egiz/upater/Constants.java delete mode 100644 utils/src/main/java/at/gv/egiz/upater/MoccaUpdater.java delete mode 100644 utils/src/main/java/at/gv/egiz/upater/NewVersionDialog.java delete mode 100644 utils/src/main/java/at/gv/egiz/upater/VersionComparator.java create mode 100644 utils/src/main/java/at/gv/egiz/updater/Constants.java create mode 100644 utils/src/main/java/at/gv/egiz/updater/MoccaUpdater.java create mode 100644 utils/src/main/java/at/gv/egiz/updater/NewVersionDialog.java create mode 100644 utils/src/main/java/at/gv/egiz/updater/VersionComparator.java (limited to 'utils/src/main/java') diff --git a/utils/src/main/java/at/gv/egiz/upater/Constants.java b/utils/src/main/java/at/gv/egiz/upater/Constants.java deleted file mode 100644 index 5a610039..00000000 --- a/utils/src/main/java/at/gv/egiz/upater/Constants.java +++ /dev/null @@ -1,10 +0,0 @@ -package at.gv.egiz.upater; - -public class Constants { - - public static final String WEBSTART_URL = "https://webstart.buergerkarte.at/"; - public static final String MOCCA_STR = "mocca/"; - public static final String VERSION_FILE = "Release.txt"; - public static final String PATH_TO_VERSION_FILE = WEBSTART_URL + MOCCA_STR + VERSION_FILE; - -} diff --git a/utils/src/main/java/at/gv/egiz/upater/MoccaUpdater.java b/utils/src/main/java/at/gv/egiz/upater/MoccaUpdater.java deleted file mode 100644 index c6907acd..00000000 --- a/utils/src/main/java/at/gv/egiz/upater/MoccaUpdater.java +++ /dev/null @@ -1,112 +0,0 @@ -package at.gv.egiz.upater; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; - -import javax.swing.JDialog; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import at.gv.egiz.slbinding.impl.SignatureLocationType; - - -public class MoccaUpdater { - - private String MoccaVersionLocal = null; - private String MoccaVersionOnline = null; - private final Logger log = LoggerFactory.getLogger(SignatureLocationType.class); - - public MoccaUpdater(String version) { - setMoccaVersionLocal(version); - } - - private String getMoccaVersionOnline() { - return MoccaVersionOnline; - } - - private void setMoccaVersionOnline(String moccaVersionOnline) { - MoccaVersionOnline = moccaVersionOnline; - } - - private String getMoccaVersionLocal() { - return MoccaVersionLocal; - } - - private void setMoccaVersionLocal(String moccaVersionLocal) { - MoccaVersionLocal = moccaVersionLocal; - } - - - public void run() { - -// if (getMoccaVersionLocal().toLowerCase().equals("unknown")) { -// // we are finished here -// return; -// } - - gatherOnlineMoccaVersion(); - boolean isOnlineNewer = isOnlineVersionNewer(); - if (isOnlineNewer) { - notifyUserNewerVersionOnline(); - } - } - - - private void gatherOnlineMoccaVersion() { - try { - log.info("Requesting Mocca Online Version"); - URL url = new URL(Constants.PATH_TO_VERSION_FILE); - log.debug("Going to GET mocca Version from: " + Constants.PATH_TO_VERSION_FILE); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String line; - StringBuilder result = new StringBuilder(); - while ((line = rd.readLine()) != null) { - result.append(line); - } - rd.close(); - setMoccaVersionOnline(result.toString()); - log.info("Online Mocca Version: " + result.toString()); - } catch (IOException e) { - log.error("Error when gathering Mocca Online Version " + e.getMessage()); - } - - } - - private boolean isOnlineVersionNewer() { - - VersionComparator comparator = new VersionComparator(); - int result = comparator.compare(getMoccaVersionOnline(), getMoccaVersionLocal()); - return (result>0) ? true : false; - } - - private void notifyUserNewerVersionOnline() { - try { - NewVersionDialog dialog = new NewVersionDialog(getMoccaVersionOnline()); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - dialog.setVisible(true); - } catch (Exception e) { - log.error(e.getMessage()); - } - - } - - public static void main(String[] args) { - - MoccaUpdater updater = new MoccaUpdater("1.2.3"); - updater.run(); - - } - - - - - - - -} diff --git a/utils/src/main/java/at/gv/egiz/upater/NewVersionDialog.java b/utils/src/main/java/at/gv/egiz/upater/NewVersionDialog.java deleted file mode 100644 index 2199b7f7..00000000 --- a/utils/src/main/java/at/gv/egiz/upater/NewVersionDialog.java +++ /dev/null @@ -1,133 +0,0 @@ -package at.gv.egiz.upater; - -import java.awt.BorderLayout; -import java.awt.Desktop; -import java.awt.FlowLayout; -import java.awt.Image; - -import javax.imageio.ImageIO; -import javax.swing.ImageIcon; -import javax.swing.JButton; -import javax.swing.JDialog; -import javax.swing.JPanel; -import javax.swing.border.EmptyBorder; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.swing.JLabel; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; - - - -public class NewVersionDialog extends JDialog { - - /** - * - */ - private static final long serialVersionUID = -7913669490723497774L; - private final JPanel contentPanel = new JPanel(); - private final Logger log = LoggerFactory.getLogger(NewVersionDialog.class); - - /** - * Launch the application. - */ - public static void main(String[] args) { - try { - NewVersionDialog dialog = new NewVersionDialog("dummy"); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - dialog.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Create the dialog. - * @throws IOException - */ - public NewVersionDialog(String version) throws IOException { - setTitle("New Version Available!"); - setResizable(false); - setBounds(100, 100, 330, 180); - getContentPane().setLayout(new BorderLayout()); - contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); - getContentPane().add(contentPanel, BorderLayout.CENTER); - contentPanel.setLayout(null); - BufferedImage myPicture = ImageIO.read(new File("C:/Users/aabraham/Downloads/information-icon.png")); - ImageIcon icon = new ImageIcon(myPicture.getScaledInstance(50, 50, Image.SCALE_SMOOTH)); - { - JLabel lblNewVersion = new JLabel("New Mocca Version " + version + " available."); - lblNewVersion.setBounds(76, 36, 228, 27); - contentPanel.add(lblNewVersion); - } - { - JLabel lblNewLabel = new JLabel("Open download page now?"); - lblNewLabel.setBounds(76, 55, 186, 27); - contentPanel.add(lblNewLabel); - } - { - - JLabel lblIcon = new JLabel(icon); - lblIcon.setBounds(10, 36, 56, 49); - contentPanel.add(lblIcon); - } - { - JPanel buttonPane = new JPanel(); - buttonPane.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - - - } - }); - buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); - getContentPane().add(buttonPane, BorderLayout.SOUTH); - { - JButton okButton = new JButton("OK"); - okButton.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - openLink(); - } - }); - okButton.setActionCommand("OK"); - buttonPane.add(okButton); - getRootPane().setDefaultButton(okButton); - } - { - JButton cancelButton = new JButton("Cancel"); - cancelButton.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - close(); - } - }); - cancelButton.setActionCommand("Cancel"); - buttonPane.add(cancelButton); - } - } - } - - private void close() { - this.dispose(); - } - - - private void openLink() { - if (Desktop.isDesktopSupported()) { - try { - Desktop.getDesktop().browse(new URI(Constants.WEBSTART_URL+Constants.MOCCA_STR)); - } catch (IOException | URISyntaxException e) { - log.error(e.getMessage()); - } - } - } - -} diff --git a/utils/src/main/java/at/gv/egiz/upater/VersionComparator.java b/utils/src/main/java/at/gv/egiz/upater/VersionComparator.java deleted file mode 100644 index c1196cf7..00000000 --- a/utils/src/main/java/at/gv/egiz/upater/VersionComparator.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2012 by A-SIT, Secure Information Technology Center Austria - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * http://joinup.ec.europa.eu/software/page/eupl - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - */ -package at.gv.egiz.upater; - -// Imports -import java.util.Comparator; - -/** - * - */ -public class VersionComparator implements Comparator { - /* (non-Javadoc) - * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) - */ - @Override - public int compare(String v1, String v2) { - String[] v1Parts = v1.split("\\.|-"); - String[] v2Parts = v2.split("\\.|-"); - - int length = Math.max(v1Parts.length, v2Parts.length); - for (int i = 0; i < length; ++i) { - int v1Part = 0; - try { - if (i < v1Parts.length) - v1Part = Integer.parseInt(v1Parts[i]); - } catch (NumberFormatException e) { - if (v1Parts[i].equals("SNAPSHOT")) - v1Part = Integer.MAX_VALUE; - } - - int v2Part = 0; - try { - if (i < v2Parts.length) - v2Part = Integer.parseInt(v2Parts[i]); - } catch (NumberFormatException e) { - if (v2Parts[i].equals("SNAPSHOT")) - v2Part = Integer.MAX_VALUE; - } - - if (v1Part < v2Part) - return -1; - if (v1Part > v2Part) - return 1; - } - return 0; - } - - /** - * Compare two version strings (static version) - * @param v1 version 1 - * @param v2 version 2 - * @return -1 if v1 < v2, 0 if v1 = v2, 1 if v1 > v2 - */ - public static int compare_s(String v1, String v2) { - VersionComparator vc = new VersionComparator(); - return vc.compare(v1, v2); - } - - /** - * Check two version strings for equality - * @param v1 version 1 - * @param v2 version 2 - * @return v1 == v2 - */ - public static boolean equals(String v1, String v2) { - return compare_s(v1, v2) == 0; - } - - /** - * Check two version strings for order - * @param v1 version 1 - * @param v2 version 2 - * @return v1 < v2 - */ - public static boolean before(String v1, String v2) { - return compare_s(v1, v2) < 0; - } - - /** - * Check two version strings for order - * @param v1 version 1 - * @param v2 version 2 - * @return v1 > v2 - */ - public static boolean after(String v1, String v2) { - return compare_s(v1, v2) > 0; - } -} diff --git a/utils/src/main/java/at/gv/egiz/updater/Constants.java b/utils/src/main/java/at/gv/egiz/updater/Constants.java new file mode 100644 index 00000000..e836bae7 --- /dev/null +++ b/utils/src/main/java/at/gv/egiz/updater/Constants.java @@ -0,0 +1,10 @@ +package at.gv.egiz.updater; + +public class Constants { + + public static final String WEBSTART_URL = "https://webstart.buergerkarte.at/"; + public static final String MOCCA_STR = "mocca/"; + public static final String VERSION_FILE = "Release.txt"; + public static final String PATH_TO_VERSION_FILE = WEBSTART_URL + MOCCA_STR + VERSION_FILE; + +} diff --git a/utils/src/main/java/at/gv/egiz/updater/MoccaUpdater.java b/utils/src/main/java/at/gv/egiz/updater/MoccaUpdater.java new file mode 100644 index 00000000..1e1bc6c6 --- /dev/null +++ b/utils/src/main/java/at/gv/egiz/updater/MoccaUpdater.java @@ -0,0 +1,112 @@ +package at.gv.egiz.updater; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +import javax.swing.JDialog; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.slbinding.impl.SignatureLocationType; + + +public class MoccaUpdater { + + private String MoccaVersionLocal = null; + private String MoccaVersionOnline = null; + private final Logger log = LoggerFactory.getLogger(SignatureLocationType.class); + + public MoccaUpdater(String version) { + setMoccaVersionLocal(version); + } + + private String getMoccaVersionOnline() { + return MoccaVersionOnline; + } + + private void setMoccaVersionOnline(String moccaVersionOnline) { + MoccaVersionOnline = moccaVersionOnline; + } + + private String getMoccaVersionLocal() { + return MoccaVersionLocal; + } + + private void setMoccaVersionLocal(String moccaVersionLocal) { + MoccaVersionLocal = moccaVersionLocal; + } + + + public void run() { + +// if (getMoccaVersionLocal().toLowerCase().equals("unknown")) { +// // we are finished here +// return; +// } + + gatherOnlineMoccaVersion(); + boolean isOnlineNewer = isOnlineVersionNewer(); + if (isOnlineNewer) { + notifyUserNewerVersionOnline(); + } + } + + + private void gatherOnlineMoccaVersion() { + try { + log.info("Requesting Mocca Online Version"); + URL url = new URL(Constants.PATH_TO_VERSION_FILE); + log.debug("Going to GET mocca Version from: " + Constants.PATH_TO_VERSION_FILE); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String line; + StringBuilder result = new StringBuilder(); + while ((line = rd.readLine()) != null) { + result.append(line); + } + rd.close(); + setMoccaVersionOnline(result.toString()); + log.info("Online Mocca Version: " + result.toString()); + } catch (IOException e) { + log.error("Error when gathering Mocca Online Version " + e.getMessage()); + } + + } + + private boolean isOnlineVersionNewer() { + + VersionComparator comparator = new VersionComparator(); + int result = comparator.compare(getMoccaVersionOnline(), getMoccaVersionLocal()); + return (result>0) ? true : false; + } + + private void notifyUserNewerVersionOnline() { + try { + NewVersionDialog dialog = new NewVersionDialog(getMoccaVersionOnline()); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + } catch (Exception e) { + log.error(e.getMessage()); + } + + } + + public static void main(String[] args) { + + MoccaUpdater updater = new MoccaUpdater("1.2.3"); + updater.run(); + + } + + + + + + + +} diff --git a/utils/src/main/java/at/gv/egiz/updater/NewVersionDialog.java b/utils/src/main/java/at/gv/egiz/updater/NewVersionDialog.java new file mode 100644 index 00000000..2765bc84 --- /dev/null +++ b/utils/src/main/java/at/gv/egiz/updater/NewVersionDialog.java @@ -0,0 +1,132 @@ +package at.gv.egiz.updater; + +import java.awt.BorderLayout; +import java.awt.Desktop; +import java.awt.FlowLayout; +import java.awt.Image; + +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.JLabel; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + + + +public class NewVersionDialog extends JDialog { + + /** + * + */ + private static final long serialVersionUID = -7913669490723497774L; + private final JPanel contentPanel = new JPanel(); + private final Logger log = LoggerFactory.getLogger(NewVersionDialog.class); + + /** + * Launch the application. + */ + public static void main(String[] args) { + try { + NewVersionDialog dialog = new NewVersionDialog("dummy"); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Create the dialog. + * @throws IOException + */ + public NewVersionDialog(String version) throws IOException { + setTitle("New Version Available!"); + setResizable(false); + setBounds(100, 100, 330, 180); + getContentPane().setLayout(new BorderLayout()); + contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(contentPanel, BorderLayout.CENTER); + contentPanel.setLayout(null); + BufferedImage myPicture = ImageIO.read(getClass().getResource("/at/gv/egiz/updater/information-icon.png")); + ImageIcon icon = new ImageIcon(myPicture.getScaledInstance(50, 50, Image.SCALE_SMOOTH)); + { + JLabel lblNewVersion = new JLabel("New Mocca Version " + version + " available."); + lblNewVersion.setBounds(76, 36, 228, 27); + contentPanel.add(lblNewVersion); + } + { + JLabel lblNewLabel = new JLabel("Open download page now?"); + lblNewLabel.setBounds(76, 55, 186, 27); + contentPanel.add(lblNewLabel); + } + { + + JLabel lblIcon = new JLabel(icon); + lblIcon.setBounds(10, 36, 56, 49); + contentPanel.add(lblIcon); + } + { + JPanel buttonPane = new JPanel(); + buttonPane.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + + + } + }); + buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + { + JButton okButton = new JButton("OK"); + okButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + openLink(); + } + }); + okButton.setActionCommand("OK"); + buttonPane.add(okButton); + getRootPane().setDefaultButton(okButton); + } + { + JButton cancelButton = new JButton("Cancel"); + cancelButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + close(); + } + }); + cancelButton.setActionCommand("Cancel"); + buttonPane.add(cancelButton); + } + } + } + + private void close() { + this.dispose(); + } + + + private void openLink() { + if (Desktop.isDesktopSupported()) { + try { + Desktop.getDesktop().browse(new URI(Constants.WEBSTART_URL+Constants.MOCCA_STR)); + } catch (IOException | URISyntaxException e) { + log.error(e.getMessage()); + } + } + } + +} diff --git a/utils/src/main/java/at/gv/egiz/updater/VersionComparator.java b/utils/src/main/java/at/gv/egiz/updater/VersionComparator.java new file mode 100644 index 00000000..19a96a68 --- /dev/null +++ b/utils/src/main/java/at/gv/egiz/updater/VersionComparator.java @@ -0,0 +1,101 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.gv.egiz.updater; + +// Imports +import java.util.Comparator; + +/** + * + */ +public class VersionComparator implements Comparator { + /* (non-Javadoc) + * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) + */ + @Override + public int compare(String v1, String v2) { + String[] v1Parts = v1.split("\\.|-"); + String[] v2Parts = v2.split("\\.|-"); + + int length = Math.max(v1Parts.length, v2Parts.length); + for (int i = 0; i < length; ++i) { + int v1Part = 0; + try { + if (i < v1Parts.length) + v1Part = Integer.parseInt(v1Parts[i]); + } catch (NumberFormatException e) { + if (v1Parts[i].equals("SNAPSHOT")) + v1Part = Integer.MAX_VALUE; + } + + int v2Part = 0; + try { + if (i < v2Parts.length) + v2Part = Integer.parseInt(v2Parts[i]); + } catch (NumberFormatException e) { + if (v2Parts[i].equals("SNAPSHOT")) + v2Part = Integer.MAX_VALUE; + } + + if (v1Part < v2Part) + return -1; + if (v1Part > v2Part) + return 1; + } + return 0; + } + + /** + * Compare two version strings (static version) + * @param v1 version 1 + * @param v2 version 2 + * @return -1 if v1 < v2, 0 if v1 = v2, 1 if v1 > v2 + */ + public static int compare_s(String v1, String v2) { + VersionComparator vc = new VersionComparator(); + return vc.compare(v1, v2); + } + + /** + * Check two version strings for equality + * @param v1 version 1 + * @param v2 version 2 + * @return v1 == v2 + */ + public static boolean equals(String v1, String v2) { + return compare_s(v1, v2) == 0; + } + + /** + * Check two version strings for order + * @param v1 version 1 + * @param v2 version 2 + * @return v1 < v2 + */ + public static boolean before(String v1, String v2) { + return compare_s(v1, v2) < 0; + } + + /** + * Check two version strings for order + * @param v1 version 1 + * @param v2 version 2 + * @return v1 > v2 + */ + public static boolean after(String v1, String v2) { + return compare_s(v1, v2) > 0; + } +} -- cgit v1.2.3