001: /*
002: * CheckVersionCommand.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery.actions.helpcommands;
023:
024: import java.awt.event.ActionEvent;
025:
026: import java.io.BufferedReader;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030: import java.net.InetAddress;
031: import java.net.MalformedURLException;
032: import java.net.Socket;
033:
034: import java.net.URL;
035:
036: import javax.swing.JOptionPane;
037: import javax.swing.SwingUtilities;
038: import org.executequery.Constants;
039:
040: import org.executequery.GUIUtilities;
041: import org.underworldlabs.util.SystemProperties;
042: import org.underworldlabs.swing.actions.BaseCommand;
043: import org.executequery.gui.InformationDialog;
044: import org.executequery.util.Log;
045: import org.underworldlabs.swing.GUIUtils;
046: import org.underworldlabs.swing.InterruptibleProgressDialog;
047: import org.underworldlabs.swing.util.InterruptibleProcess;
048: import org.underworldlabs.swing.util.SwingWorker;
049:
050: /* ----------------------------------------------------------
051: * CVS NOTE: Changes to the CVS repository prior to the
052: * release of version 3.0.0beta1 has meant a
053: * resetting of CVS revision numbers.
054: * ----------------------------------------------------------
055: */
056:
057: /**
058: * Checks to see if a newer version of Execute Query
059: * is available. Some of the code here was taken from
060: * a similar function in JEdit by Slava Pestov
061: * from http://jedit.org. Thanks.
062: *
063: * @author Takis Diakoumis
064: * @version $Revision: 1.9 $
065: * @date $Date: 2006/10/01 00:15:08 $
066: */
067: public class CheckVersionCommand implements BaseCommand,
068: InterruptibleProcess {
069:
070: /** Thread worker object */
071: private SwingWorker worker;
072:
073: /** The progress dialog */
074: private InterruptibleProgressDialog progressDialog;
075:
076: public void execute(ActionEvent e) {
077: worker = new SwingWorker() {
078: public Object construct() {
079: return doWork();
080: }
081:
082: public void finished() {
083: closeProgressDialog();
084: GUIUtilities.showNormalCursor();
085: }
086: };
087:
088: progressDialog = new InterruptibleProgressDialog(
089: GUIUtilities.getParentFrame(),
090: "Check for update",
091: "Checking for updated version from http://executequery.org",
092: this );
093:
094: worker.start();
095: progressDialog.run();
096: }
097:
098: private void closeProgressDialog() {
099: SwingUtilities.invokeLater(new Runnable() {
100: public void run() {
101: if (progressDialog != null
102: && progressDialog.isVisible()) {
103: progressDialog.dispose();
104: }
105: progressDialog = null;
106: }
107: });
108: }
109:
110: private Object doWork() {
111: try {
112: Log
113: .info("Checking for new version update from http://executequery.org");
114:
115: // do a socket check for access
116: String address = "www.executequery.org";
117: Socket socket = new Socket(InetAddress.getByName(address),
118: 80);
119: socket.close();
120:
121: URL url = new URL(SystemProperties.getProperty("system",
122: "check.version.url"));
123: InputStream input = url.openStream();
124: BufferedReader reader = new BufferedReader(
125: new InputStreamReader(input));
126:
127: String line;
128: String version = null;
129: String build = null;
130:
131: while ((line = reader.readLine()) != null) {
132:
133: if (line.startsWith("version")) {
134: version = line.substring(8).trim();
135: } else if (line.startsWith("build")) {
136: build = line.substring(6).trim();
137: }
138:
139: }
140:
141: reader.close();
142:
143: if (version != null && build != null) {
144: String c_build = System
145: .getProperty("executequery.build");
146:
147: if (c_build.compareTo(build) < 0) {
148: closeProgressDialog();
149:
150: Log.info("New version " + version + " available.");
151:
152: int yesNo = GUIUtilities
153: .displayYesNoDialog(
154: "New version "
155: + version
156: + " (Build "
157: + build
158: + ") is available for download at. "
159: + "http://executequery.org.\nDo you wish to view the "
160: + "version notes for this release?",
161: "Execute Query Update");
162:
163: if (yesNo == JOptionPane.YES_OPTION) {
164: return displayNewVersionInfo();
165: }
166:
167: } else {
168: closeProgressDialog();
169: Log.info("No version update available.");
170: GUIUtilities
171: .displayInformationMessage("No update available.\n"
172: + "This version of Execute Query is up to date.\n"
173: + "Please check back here periodically to ensure you have "
174: + "the latest version.");
175: }
176:
177: return Constants.WORKER_SUCCESS;
178: } else {
179: closeProgressDialog();
180: GUIUtilities
181: .displayErrorMessage("An error occured trying to communicate "
182: + " with the server at http://executequery.org."
183: + "\nPlease try again later.");
184: return Constants.WORKER_FAIL;
185: }
186:
187: } catch (MalformedURLException urlExc) {
188: showError();
189: return Constants.WORKER_FAIL;
190: } catch (IOException ioExc) {
191: closeProgressDialog();
192: GUIUtilities
193: .displayErrorMessage("The version file at http://executequery.org "
194: + "could not be opened.\nThis feature requires an "
195: + "active internet connection.\nIf using a proxy server, "
196: + "please configure this through the user preferences "
197: + "> general selection.");
198: return Constants.WORKER_FAIL;
199: }
200: }
201:
202: private Object displayNewVersionInfo() {
203: try {
204:
205: GUIUtilities.showWaitCursor();
206:
207: SwingUtilities.invokeLater(new Runnable() {
208: public void run() {
209: progressDialog = new InterruptibleProgressDialog(
210: GUIUtilities.getParentFrame(),
211: "Check for update",
212: "Retrieving new version release notes from http://executequery.org",
213: CheckVersionCommand.this );
214: progressDialog.run();
215: }
216: });
217:
218: URL url = new URL(SystemProperties.getProperty("system",
219: "check.version.notes.url"));
220:
221: InputStream input = url.openStream();
222: BufferedReader reader = new BufferedReader(
223: new InputStreamReader(input));
224:
225: String line;
226: StringBuffer sb = new StringBuffer(1000);
227: char NEW_LINE = '\n';
228:
229: while ((line = reader.readLine()) != null) {
230: sb.append(line).append(NEW_LINE);
231: }
232:
233: reader.close();
234:
235: final String notes = sb.toString();
236: closeProgressDialog();
237:
238: GUIUtils.invokeAndWait(new Runnable() {
239: public void run() {
240: new InformationDialog("Latest Version Info", notes,
241: InformationDialog.TEXT_CONTENT_VALUE);
242: }
243: });
244:
245: return Constants.WORKER_SUCCESS;
246: } catch (MalformedURLException urlExc) {
247: showError();
248: return Constants.WORKER_FAIL;
249: } catch (IOException ioExc) {
250: showError();
251: return Constants.WORKER_FAIL;
252: } finally {
253: GUIUtilities.showNormalCursor();
254: }
255:
256: }
257:
258: private void showError() {
259: GUIUtilities.showNormalCursor();
260: GUIUtilities
261: .displayErrorMessage("An error occured opening the version info file\n"
262: + "at http://executequery.org. Please try again later.");
263: }
264:
265: /**
266: * Sets the process cancel flag as specified.
267: */
268: public void setCancelled(boolean cancelled) {
269: interrupt();
270: }
271:
272: /**
273: * Indicates thatthis process should be interrupted.
274: */
275: public void interrupt() {
276: worker.interrupt();
277: }
278:
279: }
|