2011-06-18

Auto-Update for Java Applications

Java WebStart-Applications have a great update mechanism using JNLP. It checks during launch if there is a new version available on the server and downloads the new version to the client automatically. This way the client always runs with the latests version available on the server. But what about standalone Java applications?

Appcast - an RSS 2.0 update feed

Similar to podcasts a appcast is a RSS 2.0 feed to announce a new release of a software product. It basically uses standard RSS 2.0. The reference to the new version file can be specified in the enclosure tag:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
   <channel>
      <title>MyApp Changelog</title>
      <link>http://example.com/MyApp/appcast.xml</link>
      <description>Latest changes and updates.</description>
      <language>en</language>
      <item>
         <title>Version 1.3.2</title>
         <pubDate>Wed, 18 Jun 2011 09:20:11 +0000</pubDate>
         <enclosure url="http://example.com/MyApp-1.3.2.zip" length="1234567" type="application/octet-stream" />
      </item>
   </channel>
</rss>

Just place this as appcast.xml on some host that is accessible by the application. The application can check if there is a new update available using this appcast.xml.

Appcast Framework for Mac OS X

Andy Matuschak's Sparkle is a free software update framework for Mac OS X that is very widespread. It has some appcast extensions for custom version strings and DSA signature for the update file. An complete sparkle appcast example can be found here. With these extensions is also possible to include a link to the release notes file.

Appcast updates for Java Applications?

The RSS 2.0 structure is very easy and there are already java parsers available. The sparkle extensions can also be parsed in Java. You can use JAXB to get the values out of the enclosure tag:
@XmlAccessorType(XmlAccessType.FIELD)
public class Enclosure {
    @XmlAttribute
    String url;
    @XmlAttribute
    long length;
    @XmlAttribute
    String type;
    @XmlAttribute(namespace="http://www.andymatuschak.org/xml-namespaces/sparkle")
    String version;
    @XmlAttribute(namespace="http://www.andymatuschak.org/xml-namespaces/sparkle")
    String shortVersionString;
    @XmlAttribute(namespace="http://www.andymatuschak.org/xml-namespaces/sparkle")
    String dsaSignature;
...
The other tags can be processed the same way. You only need to compare the current application version string with the one from the appcast enclosure. If the appcast version is newer, download the update file.

But how do you handle applications with multiple JAR files if every component has it's own versioning? The version string from the MANIFEST.MF file can be used or one can add a new version string, e.g. 'Appcast-Version: 1.2.3', to the JAR's MANIFEST.MF file. This can be read easily at runtime with java.util.jar.Manifest class:
Manifest manifest = new Manifest(new FileInputStream(new File("manifest.mf")));
Attributes mainAttributes = manifest.getMainAttributes();
if (mainAttributes != null) {
   return mainAttributes.getValue("Appcast-Version");
}

The appcast URL itself can also be placed inside the MANIFEST.MF file for each application module, e.g. using "Appcast-Url: http://example.com/MyApp/appcast.xml".

Keep in mind that it is not possible to replace a JAR file of a currently running application. So you need a bootstrap mechanism to check and download updates and apply them during next start of the application.




No comments:

Post a Comment