001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2005  Emmanuel Bourg
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License
007     * as published by the Free Software Foundation; either version 2
008     * of the License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018     */
019    
020    package net.jetrix.services;
021    
022    import java.awt.TrayIcon;
023    import java.io.BufferedReader;
024    import java.io.IOException;
025    import java.io.InputStreamReader;
026    import java.net.HttpURLConnection;
027    import java.net.URL;
028    import java.util.logging.Level;
029    
030    import net.jetrix.SystrayManager;
031    import net.jetrix.config.ServerConfig;
032    
033    /**
034     * Service checking the availability of a new release.
035     *
036     * @since 0.2
037     * 
038     * @author Emmanuel Bourg
039     * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
040     */
041    public class VersionService extends CronService
042    {
043        /** The latest stable version known. */
044        private static String latestVersion;
045    
046        public VersionService()
047        {
048            // default frequency : once a day
049            setPattern("0 0 * * * *");
050        }
051    
052        public String getName()
053        {
054            return "Version Checker";
055        }
056    
057        protected void run()
058        {
059            updateLatestVersion();
060    
061            if (isNewVersionAvailable())
062            {
063                String message = "A new version is available (" + VersionService.getLatestVersion() + "), download it on http://jetrix.sf.net now!";
064                log.warning(message);
065                SystrayManager.notify(message, TrayIcon.MessageType.INFO);
066            }
067        }
068    
069        /**
070         * Return the version of the latest release. The version of the last stable
071         * release is stored on the Jetrix site (http://jetrix.sf.net/version.php),
072         * this file is build dynamically everyday and reuse the version specified
073         * in the project.properties file.
074         */
075        private static String fetchLatestVersion()
076        {
077            String version = null;
078    
079            try
080            {
081                URL url = new URL("http://jetrix.sourceforge.net/version.php");
082    
083                HttpURLConnection conn = null;
084                try
085                {
086                    conn = (HttpURLConnection) url.openConnection();
087    
088                    // read the first line of the file
089                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
090                    version = in.readLine();
091                }
092                finally
093                {
094                    conn.disconnect();
095                }
096            }
097            catch (IOException e)
098            {
099                log.log(Level.WARNING, "Unable to check the availability of a new version", e);
100            }
101    
102            return version;
103        }
104    
105        /**
106         * Update the latest stable version known.
107         */
108        public static void updateLatestVersion()
109        {
110            latestVersion = fetchLatestVersion();
111        }
112    
113        /**
114         * Return the latest stable version known.
115         */
116        public static String getLatestVersion()
117        {
118            return latestVersion;
119        }
120    
121        /**
122         * Check if the latest stable version is more recent than the current version.
123         */
124        public static boolean isNewVersionAvailable()
125        {
126            return latestVersion != null && ServerConfig.VERSION.compareTo(latestVersion) < 0;
127        }
128    }