001 /*--------------------------------------------------------------------------+ 002 $Id: VersionUtils.java 26283 2010-02-18 11:18:57Z juergens $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.commons.version; 019 020 import java.io.IOException; 021 import java.io.InputStream; 022 import java.net.URL; 023 024 import javax.swing.JOptionPane; 025 026 import edu.tum.cs.commons.error.FormatException; 027 import edu.tum.cs.commons.filesystem.FileSystemUtils; 028 029 /** 030 * Utility code for dealing with versions. 031 * 032 * @author hummelb 033 * @author $Author: juergens $ 034 * @version $Rev: 26283 $ 035 * @levd.rating GREEN Hash: 38C0D10E9CCCD8B82949F4612A48AB1B 036 */ 037 public class VersionUtils { 038 039 /** 040 * Checks whether a new version of an application is available. This 041 * retrieves the most recent version from an URL using 042 * {@link #getMostRecentVersion(String)}. If the version could be retrieved 043 * and is more recent than the running version, an informative message box 044 * appears (Swing). 045 */ 046 public static void checkForNewVersion(final String appName, 047 String versionFileUrl, Version runningVersion) { 048 final Version currentVersion; 049 try { 050 currentVersion = getMostRecentVersion(versionFileUrl); 051 } catch (Exception e) { 052 // just ignore 053 return; 054 } 055 056 if (currentVersion.compareTo(runningVersion) > 0) { 057 javax.swing.SwingUtilities.invokeLater(new Runnable() { 058 public void run() { 059 JOptionPane 060 .showMessageDialog(null, "The newer version " 061 + currentVersion + " of " + appName 062 + " is available for download!", 063 "Update available", 064 JOptionPane.INFORMATION_MESSAGE); 065 } 066 }); 067 } 068 } 069 070 /** Get the most recent version from a file available via an URL. */ 071 public static Version getMostRecentVersion(String versionFileUrl) 072 throws IOException, FormatException { 073 URL versionURL = new URL(versionFileUrl); 074 InputStream in = versionURL.openStream(); 075 String versionString = FileSystemUtils.readStream(in); 076 in.close(); 077 return Version.parseVersion(versionString); 078 } 079 }