001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package org.openproj.util;
051:
052: import groovy.lang.GroovyClassLoader;
053:
054: import java.io.BufferedReader;
055: import java.io.InputStream;
056: import java.io.InputStreamReader;
057: import java.net.URL;
058: import java.net.URLEncoder;
059: import java.text.MessageFormat;
060: import java.util.Locale;
061: import java.util.prefs.Preferences;
062:
063: import javax.swing.SwingUtilities;
064:
065: import com.projity.strings.Messages;
066: import com.projity.util.Alert;
067: import com.projity.util.BrowserControl;
068: import com.projity.util.VersionUtils;
069:
070: /*
071: * Format:
072: * version: series of integers separated by dots
073: * name: string
074: *
075: * %UpdateChecker
076: * <groovy formula>
077: */
078: public class UpdateChecker {
079: private static final int UPDATE_CHECKER_VERSION = 1;
080: private static final String updateAddress = "http://www.openproj.org/versions/version-"
081: + UPDATE_CHECKER_VERSION;
082: private static final String downloadAddress = "http://sourceforge.net/project/showfiles.php?group_id=199315";
083:
084: public static void checkForUpdate() {
085: String this Version = VersionUtils.getVersion();//Messages.getString("Release.version");
086: URL url;
087: try {
088: // System.out.println("Encoded: "+URLEncoder.encode(System.getProperty("java.vendor"),"UTF-8"));
089: // System.exit(1);
090: //identify installed version, locale, jvm , to know if an update is available
091: url = new URL(updateAddress
092: + "?version="
093: + URLEncoder.encode(this Version == null ? "0"
094: : this Version, "UTF-8")
095: + "&locale="
096: + URLEncoder.encode(Locale.getDefault().toString(),
097: "UTF-8")
098: + "&osName="
099: + URLEncoder.encode(System.getProperty("os.name"),
100: "UTF-8")
101: + "&osVersion="
102: + URLEncoder.encode(System
103: .getProperty("os.version"), "UTF-8")
104: + "&osArch="
105: + URLEncoder.encode(System.getProperty("os.arch"),
106: "UTF-8")
107: + "&javaVersion="
108: + URLEncoder.encode(System
109: .getProperty("java.version"), "UTF-8")
110: + "&javaVendor="
111: + URLEncoder.encode(System
112: .getProperty("java.vendor"), "UTF-8")
113: + "&validation="
114: + URLEncoder.encode(System.getProperty(
115: "openproj.validation", "0"), "UTF-8")
116: + "&email="
117: + URLEncoder.encode(System.getProperty(
118: "openproj.userEmail", "0"), "UTF-8")); //$NON-NLS-1$
119: InputStream stream = url.openStream();
120: BufferedReader in = new BufferedReader(
121: new InputStreamReader(stream));
122: String latestVersion = in.readLine();
123: if (this Version == null) {
124: in.close();
125: return;
126: }
127: UpdateCheckerFormula f = new UpdateCheckerFormula();
128: if (f.mainCompare(this Version, latestVersion) >= 0) {
129: in.close();
130: return;
131: }
132: if (f.mainCompare(Preferences.userNodeForPackage(
133: UpdateChecker.class)
134: .get("lastVersionChecked", "-1"), latestVersion) >= 0) {
135: in.close();
136: return; //already asked
137: }
138: Preferences.userNodeForPackage(UpdateChecker.class).put(
139: "lastVersionChecked", latestVersion);
140:
141: String latestName = in.readLine();
142:
143: StringBuffer formulaDef = new StringBuffer();
144: String s = null;
145: while ((s = in.readLine()) != null) {
146: if (s.trim().toUpperCase().equals("%UPDATECHECKER"))
147: break;
148: }
149: if (s != null) {
150: while ((s = in.readLine()) != null) {
151: formulaDef.append(s).append('\n');
152: }
153: }
154: in.close();
155:
156: UpdateCheckerFormula formula = getFormula(formulaDef
157: .toString().trim());
158: if (formula.mainCompare(this Version, latestVersion) < 0) {
159: String message = MessageFormat.format(Messages
160: .getString("Text.newVersion"), new Object[] {
161: latestVersion, this Version });
162: if (Alert.okCancel(message))
163: BrowserControl.displayURL(downloadAddress);
164: }
165:
166: } catch (Exception e) {
167: //e.printStackTrace();
168: }
169: }
170:
171: public static UpdateCheckerFormula getFormula(String formulaDef) {
172: if (formulaDef.length() == 0)
173: return new UpdateCheckerFormula();
174: StringBuffer classText = new StringBuffer();
175: classText.append("package org.openproj.util;\n");
176: classText
177: .append("public class UpdateCheckerFormulaImpl extends UpdateCheckerFormula{\n");
178: classText
179: .append("\tpublic int mainCompare(String currentVersion,String latestVersion){\n");
180: classText.append("\t\t").append(formulaDef).append('\n');
181: classText.append("\t}\n");
182: classText.append("}\n");
183: GroovyClassLoader loader = new GroovyClassLoader(
184: UpdateChecker.class.getClassLoader());
185: try {
186: Class groovyClass = loader.parseClass(classText.toString()); //TODO this his horribly slow (~500ms) Can we parse all at once or can we do this lazily or initialize in another thread?
187: return (UpdateCheckerFormula) groovyClass.newInstance();
188: } catch (Exception e) {
189: return new UpdateCheckerFormula();
190: }
191: }
192:
193: public static void checkForUpdateInBackground() {
194: SwingUtilities.invokeLater(new Runnable() {
195: public void run() {
196: checkForUpdate();
197: }
198: });
199: }
200:
201: }
|