root/trunk/components/common/src/ome/system/UpgradeCheck.java
| Revision 2768, 6.1 kB (checked in by jmoore, 5 months ago) |
|---|
| Line | |
|---|---|
| 1 | /* |
| 2 | * $Id$ |
| 3 | * |
| 4 | * Copyright 2007 Glencoe Software, Inc. All rights reserved. |
| 5 | * Use is subject to license terms supplied in LICENSE.txt |
| 6 | */ |
| 7 | |
| 8 | package ome.system; |
| 9 | |
| 10 | import java.io.BufferedInputStream; |
| 11 | import java.io.IOException; |
| 12 | import java.io.InputStream; |
| 13 | import java.io.UnsupportedEncodingException; |
| 14 | import java.net.URL; |
| 15 | import java.net.URLConnection; |
| 16 | import java.net.URLEncoder; |
| 17 | import java.net.UnknownHostException; |
| 18 | |
| 19 | import org.apache.commons.logging.Log; |
| 20 | import org.apache.commons.logging.LogFactory; |
| 21 | |
| 22 | /** |
| 23 | * Contacts a given URL which should be an OME server which will return either |
| 24 | * an empty String or a URL which points to a needed upgrade. |
| 25 | * |
| 26 | * @author Josh Moore, josh at glencoesoftware.com |
| 27 | * @since 3.0-Beta2.3 |
| 28 | */ |
| 29 | public class UpgradeCheck implements Runnable { |
| 30 | |
| 31 | private final static Log log = LogFactory.getLog(UpgradeCheck.class); |
| 32 | |
| 33 | /** |
| 34 | * Default timeout is 10 seconds. |
| 35 | */ |
| 36 | public final static int DEFAULT_TIMEOUT = 10 * 1000; |
| 37 | |
| 38 | final String url; |
| 39 | final String version; |
| 40 | final int timeout; |
| 41 | final String agent; |
| 42 | |
| 43 | String upgradeUrl = null; |
| 44 | Exception exc = null; |
| 45 | |
| 46 | /** |
| 47 | * Calls {@link UpgradeCheck#UpgradeCheck(String, String, String, int)} |
| 48 | * using {@link #DEFAULT_TIMEOUT} |
| 49 | */ |
| 50 | public UpgradeCheck(String url, String version, String agent) { |
| 51 | this(url, version, agent, DEFAULT_TIMEOUT); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Main constructor. |
| 56 | * |
| 57 | * @param url |
| 58 | * Null or empty value disables check. |
| 59 | * @param version |
| 60 | * Current version as specified in the omero.properties file |
| 61 | * under the "omero.version" property. This can be accessed via |
| 62 | * <code> |
| 63 | * ResourceBundle.getBundle("omero").getString("omero.version"); |
| 64 | * </code> |
| 65 | * @param agent |
| 66 | * Name of the agent which is accessing the registry. This will |
| 67 | * be appended to "OMERO." in order to adhere to the registry |
| 68 | * API. |
| 69 | * @param timeout |
| 70 | * How long to wait for a |
| 71 | */ |
| 72 | public UpgradeCheck(String url, String version, String agent, int timeout) { |
| 73 | this.url = url; |
| 74 | this.version = version; |
| 75 | this.agent = "OMERO." + agent; |
| 76 | this.timeout = timeout; |
| 77 | } |
| 78 | |
| 79 | public boolean isUpgradeNeeded() { |
| 80 | return upgradeUrl != null; |
| 81 | } |
| 82 | |
| 83 | public String getUpgradeUrl() { |
| 84 | return upgradeUrl; |
| 85 | } |
| 86 | |
| 87 | public boolean isExceptionThrown() { |
| 88 | return exc != null; |
| 89 | } |
| 90 | |
| 91 | public Exception getExceptionThrown() { |
| 92 | return exc; |
| 93 | } |
| 94 | |
| 95 | private void set(String results, Exception e) { |
| 96 | this.upgradeUrl = results; |
| 97 | this.exc = e; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * If the {@link #url} has been set to null or the empty string, then no |
| 102 | * upgrade check will be performed (silently). If however the string is an |
| 103 | * invalid URL, a warning will be printed. |
| 104 | * |
| 105 | * This method should <em>never</em> throw an exception. |
| 106 | */ |
| 107 | public void run() { |
| 108 | |
| 109 | // If null or empty, the upgrade check is disabled. |
| 110 | if (url == null || url.length() == 0) { |
| 111 | return; // EARLY EXIT! |
| 112 | } |
| 113 | |
| 114 | StringBuilder query = new StringBuilder(); |
| 115 | try { |
| 116 | query.append(url); |
| 117 | query.append("?version="); |
| 118 | query.append(URLEncoder.encode(version, "UTF-8")); |
| 119 | query.append(";os.name="); |
| 120 | query.append(URLEncoder.encode(System.getProperty("os.name"), |
| 121 | "UTF-8")); |
| 122 | query.append(";os.arch="); |
| 123 | query.append(URLEncoder.encode(System.getProperty("os.arch"), |
| 124 | "UTF-8")); |
| 125 | query.append(";os.version="); |
| 126 | query.append(URLEncoder.encode(System.getProperty("os.version"), |
| 127 | "UTF-8")); |
| 128 | query.append(";java.runtime.version="); |
| 129 | query.append(URLEncoder.encode(System |
| 130 | .getProperty("java.runtime.version"), "UTF-8")); |
| 131 | query.append(";java.vm.vendor="); |
| 132 | query.append(URLEncoder.encode( |
| 133 | System.getProperty("java.vm.vendor"), "UTF-8")); |
| 134 | } catch (UnsupportedEncodingException uee) { |
| 135 | // Internal issue |
| 136 | set(null, uee); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | URL _url; |
| 141 | try { |
| 142 | _url = new URL(query.toString()); |
| 143 | } catch (Exception e) { |
| 144 | set(null, e); |
| 145 | log.error("Invalid URL: " + query.toString()); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | BufferedInputStream bufIn = null; |
| 150 | try { |
| 151 | URLConnection conn = _url.openConnection(); |
| 152 | conn.setUseCaches(false); |
| 153 | conn.addRequestProperty("User-Agent", agent); |
| 154 | conn.setConnectTimeout(timeout); |
| 155 | conn.setReadTimeout(timeout); |
| 156 | conn.connect(); |
| 157 | |
| 158 | log.debug("Attempting to connect to " + query); |
| 159 | |
| 160 | InputStream in = conn.getInputStream(); |
| 161 | bufIn = new BufferedInputStream(in); |
| 162 | |
| 163 | StringBuilder sb = new StringBuilder(); |
| 164 | while (true) { |
| 165 | int data = bufIn.read(); |
| 166 | if (data == -1) { |
| 167 | break; |
| 168 | } else { |
| 169 | sb.append((char) data); |
| 170 | } |
| 171 | } |
| 172 | String result = sb.toString(); |
| 173 | if (result.length() == 0) { |
| 174 | log.info("no update needed"); |
| 175 | set(null, null); |
| 176 | } else { |
| 177 | log.warn("UPGRADE AVAILABLE:" + result); |
| 178 | set(result, null); |
| 179 | } |
| 180 | } catch (UnknownHostException uhe) { |
| 181 | log.error("Unknown host:" + url); |
| 182 | set(null, uhe); |
| 183 | } catch (IOException ioe) { |
| 184 | log.error(String.format("Error reading from url: %s \"%s\"", query, |
| 185 | ioe.getMessage())); |
| 186 | set(null, ioe); |
| 187 | } catch (Exception ex) { |
| 188 | log.error("Unknown exception thrown on UpgradeCheck", ex); |
| 189 | set(null, ex); |
| 190 | } finally { |
| 191 | if (bufIn != null) { |
| 192 | try { |
| 193 | bufIn.close(); |
| 194 | } catch (Exception e) { |
| 195 | // Really not much that can be done, eh? |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
Note: See TracBrowser
for help on using the browser.
