001: /*
002: * WbVersionReader.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.util;
013:
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017: import java.io.InputStream;
018: import java.net.URL;
019: import java.net.URLConnection;
020: import java.util.Properties;
021: import javax.swing.Timer;
022: import workbench.resource.Settings;
023: import workbench.log.LogMgr;
024: import workbench.resource.ResourceMgr;
025:
026: /**
027: *
028: * @author support@sql-workbench.net
029: */
030: public class WbVersionReader implements ActionListener {
031: private VersionNumber currentDevBuildNumber;
032: private String currentDevBuildDate;
033: private VersionNumber currentStableBuildNumber;
034: private String currentStableBuildDate;
035: private final String userAgent;
036: private boolean success = false;
037: private Timer timeout;
038: private boolean timedOut = false;
039: private ActionListener client;
040: private WbThread readThread;
041:
042: /**
043: * Constructor only for unit testing
044: */
045: WbVersionReader(VersionNumber dev, VersionNumber stable) {
046: this .currentDevBuildNumber = dev;
047: this .currentStableBuildNumber = stable;
048: this .userAgent = "VersionTest";
049: }
050:
051: public WbVersionReader(ActionListener a) {
052: this ("manual", a);
053: }
054:
055: public WbVersionReader(String type, ActionListener a) {
056: this .userAgent = "WbUpdateCheck, "
057: + ResourceMgr.getBuildNumber().toString() + ", " + type
058: + ", "
059: + Settings.getInstance().getLanguage().getLanguage()
060: + ", " + System.getProperty("os.name");
061: this .client = a;
062: }
063:
064: public void startCheckThread() {
065: this .timeout = new Timer(60 * 1000, this );
066: this .timedOut = false;
067: this .timeout.start();
068:
069: this .readThread = new WbThread("VersionReaderThread") {
070: public void run() {
071: readBuildInfo();
072: }
073: };
074: readThread.start();
075: }
076:
077: public boolean success() {
078: return success;
079: }
080:
081: private void readBuildInfo() {
082: long start = System.currentTimeMillis();
083: InputStream in = null;
084: try {
085: URL url = new URL(
086: "http://www.sql-workbench.net/release.property");
087:
088: URLConnection conn = url.openConnection();
089: conn.setRequestProperty("User-Agent", this .userAgent);
090: conn.setRequestProperty("Referer", System
091: .getProperty("java.version"));
092:
093: in = conn.getInputStream();
094:
095: Properties props = new Properties();
096: props.load(in);
097:
098: this .currentDevBuildNumber = new VersionNumber(props
099: .getProperty("dev.build.number", null));
100: this .currentDevBuildDate = props.getProperty(
101: "dev.build.date", null);
102: this .currentStableBuildNumber = new VersionNumber(props
103: .getProperty("release.build.number", null));
104: this .currentStableBuildDate = props.getProperty(
105: "release.build.date", null);
106: success = true;
107:
108: long end = System.currentTimeMillis();
109: LogMgr.logDebug("WbVersionReader.readBuildInfo()",
110: "Retrieving version information took "
111: + (end - start) + "ms");
112: } catch (Exception e) {
113: LogMgr.logWarning("WbVersionReader.readBuildInfo()",
114: "Could not read version information", e);
115: success = false;
116: } finally {
117: FileUtil.closeQuitely(in);
118: if (timeout != null) {
119: timeout.stop();
120: timeout = null;
121: }
122: if (client != null) {
123: ActionEvent e = new ActionEvent(WbVersionReader.this ,
124: 1, success ? "versionChecked" : "error");
125: client.actionPerformed(e);
126: }
127: }
128: }
129:
130: public UpdateVersion getAvailableUpdate() {
131: return getAvailableUpdate(ResourceMgr.getBuildNumber());
132: }
133:
134: public UpdateVersion getAvailableUpdate(VersionNumber current) {
135: if (currentDevBuildNumber != null
136: && currentDevBuildNumber.isNewerThan(current))
137: return UpdateVersion.devBuild;
138: if (currentStableBuildNumber != null
139: && currentStableBuildNumber.isNewerThan(current))
140: return UpdateVersion.stable;
141: return UpdateVersion.none;
142: }
143:
144: public VersionNumber getDevBuildNumber() {
145: return this .currentDevBuildNumber;
146: }
147:
148: public String getDevBuildDate() {
149: return this .currentDevBuildDate;
150: }
151:
152: public VersionNumber getStableBuildNumber() {
153: return this .currentStableBuildNumber;
154: }
155:
156: public String getStableBuildDate() {
157: return this .currentStableBuildDate;
158: }
159:
160: public void actionPerformed(ActionEvent e) {
161: if (e.getSource() == this .timeout) {
162: this .timedOut = true;
163: if (this .readThread != null) {
164: this .readThread.interrupt();
165: }
166: this .success = false;
167: }
168: }
169: }
|