[OPINION] Lightweight Updater

Discussion in 'Plugin Development' started by PatoTheBest, Mar 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    PatoTheBest

    What do you guys think?
    Code:java
    1. package org.pato.tnttag.util;
    2.  
    3. import java.io.InputStream;
    4. import java.net.MalformedURLException;
    5. import java.net.URL;
    6.  
    7. import javax.xml.parsers.DocumentBuilderFactory;
    8.  
    9. import org.pato.tnttag.core.TNTTag;
    10. import org.w3c.dom.Document;
    11. import org.w3c.dom.Node;
    12. import org.w3c.dom.NodeList;
    13.  
    14. public class Updater {
    15.  
    16. private TNTTag plugin;
    17. private URL filesFeed;
    18. private String name;
    19. private String version;
    20. private String link;
    21. private String description;
    22. private String pubDate;
    23. private String fullName;
    24. private String host = "[URL]http://dev.bukkit.org[/URL]";
    25. private String query = "/bukkit-plugins/";
    26. private String file = "/files.rss";
    27. private String URL;
    28. private Updater.updateResult result;
    29. public enum updateResult{
    30. AVAILABLE,
    31. FAIL_DBO,
    32. UP_TO_DATE
    33. }
    34.  
    35. public Updater(TNTTag plugin, String APIKey){
    36. this.URL = host + query + APIKey + file;
    37. this.plugin = plugin;
    38. try {
    39. this.filesFeed = new URL(URL);
    40. } catch (MalformedURLException e) {
    41. e.printStackTrace();
    42. }
    43. }
    44.  
    45. public void checkUpdate(){
    46. try {
    47. InputStream input = this.filesFeed.openConnection().getInputStream();
    48. Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
    49.  
    50. Node latestFile = document.getElementsByTagName("item").item(0);
    51. NodeList children = latestFile.getChildNodes();
    52.  
    53. this.version = children.item(1).getTextContent().replaceAll("[a-zA-Z]", "");
    54. this.name = children.item(1).getTextContent().replaceAll(" v" + this.version, "");
    55. this.fullName = children.item(1).getTextContent();
    56. this.link = children.item(3).getTextContent();
    57. this.description = children.item(5).getTextContent();
    58. this.pubDate = children.item(7).getTextContent();
    59.  
    60. if (plugin.getDescription().getVersion() != version){
    61. result = updateResult.AVAILABLE;
    62. return;
    63. }
    64. } catch (Exception e) {
    65. result = updateResult.FAIL_DBO;
    66. return;
    67. }
    68. result = updateResult.UP_TO_DATE;
    69. return;
    70. }
    71.  
    72.  
    73.  
    74. public String getFullName() {
    75. return fullName;
    76. }
    77.  
    78. public String getLatestName() {
    79. return name;
    80. }
    81.  
    82. public String getDescription() {
    83. return description;
    84. }
    85.  
    86. public String getPubDate() {
    87. return pubDate;
    88. }
    89.  
    90. public String getVersion(){
    91. return this.version;
    92. }
    93.  
    94. public String getLatestFileLink(){
    95. return this.link;
    96. }
    97.  
    98. public updateResult getResult(){
    99. return result;
    100. }
    101.  
    102. }


    One way to use it (Obviously goes on the onEnable()):
    Code:java
    1. private void checkUpdate() {
    2. Updater updater = new Updater(this, "73538");
    3. updater.checkUpdate();
    4. result = updater.getResult();
    5. switch(result){
    6. case AVAILABLE:
    7. name = updater.getLatestName();
    8. version = updater.getVersion();
    9. pubDate = updater.getPubDate();
    10. link = updater.getLatestFileLink();
    11. fullName = updater.getFullName();
    12. this.log.log(Level.INFO, "============================================");
    13. this.log.log(Level.INFO, "An update is available:");
    14. this.log.log(Level.INFO, fullName + " is available for download at");
    15. this.log.log(Level.INFO, link);
    16. this.log.log(Level.INFO, "============================================");
    17. TNTTag.versionDiff = true;
    18. break;
    19. case UP_TO_DATE:
    20. this.log.log(Level.INFO, "TNT Tag is up to date.");
    21. TNTTag.versionDiff = false;
    22. break;
    23. case FAIL_DBO:
    24. this.log.log(Level.INFO, "The updater could not contact dev.bukkit.org.");
    25. break;
    26. }
    27. }


    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    ImPhantom

    I like it. If i hadn't already began using Gravitys updater, i would use this.
     
    Mattkx4 likes this.
  3. Offline

    PatoTheBest

    ImPhantom
    Rhanks! Does not use Thread so very lightweihht.
     
Thread Status:
Not open for further replies.

Share This Page