001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.autoupdate.ui.actions;
043:
044: import java.io.BufferedReader;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.InputStreamReader;
048: import java.util.Date;
049: import java.util.UUID;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import java.util.prefs.Preferences;
053: import org.netbeans.modules.autoupdate.ui.Utilities;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.Repository;
056: import org.openide.util.NbPreferences;
057:
058: /**
059: *
060: * @author Jiri Rechtacek
061: */
062: public class AutoupdateSettings {
063:
064: private static String tempIdeIdentity = null;
065: private static final Logger err = Logger
066: .getLogger(AutoupdateSettings.class.getName());
067: private static final String PROP_IDE_IDENTITY = "ideIdentity"; // NOI18N
068: private static final String PROP_PERIOD = "period"; // NOI18N
069: private static final String PROP_LAST_CHECK = "lastCheckTime"; // NOI18N
070:
071: public static final int EVERY_STARTUP = 0;
072: public static final int EVERY_DAY = 1;
073: public static final int EVERY_WEEK = 2;
074: public static final int EVERY_2WEEKS = 3;
075: public static final int EVERY_MONTH = 4;
076: public static final int NEVER = 5;
077: public static final int CUSTOM_CHECK_INTERVAL = 6;
078:
079: private static final String[][] KNOWN = { { "EVERY_STARTUP", "0" },
080: { "EVERY_DAY", "1" }, { "EVERY_WEEK", "2" },
081: { "EVERY_2WEEKS", "3" }, { "EVERY_MONTH", "4" },
082: { "NEVER", "5" }, };
083:
084: private static int checkInterval = 0;
085:
086: private AutoupdateSettings() {
087: }
088:
089: public static String getIdeIdentity() {
090: if (tempIdeIdentity instanceof String) {
091: return tempIdeIdentity;
092: }
093: Object oldIdeIdentity = getPreferences().get(PROP_IDE_IDENTITY,
094: null);
095: String newIdeIdentity = null;
096: if (oldIdeIdentity == null) {
097: newIdeIdentity = modifyIdeIdentityIfNeeded(generateNewId());
098: } else {
099: newIdeIdentity = modifyIdeIdentityIfNeeded((String) oldIdeIdentity);
100: }
101: tempIdeIdentity = newIdeIdentity;
102: if (!newIdeIdentity.equals(oldIdeIdentity)) {
103: err.log(Level.FINE,
104: "Put new value of PROP_IDE_IDENTITY to "
105: + newIdeIdentity);
106: getPreferences().put(PROP_IDE_IDENTITY, newIdeIdentity);
107: }
108: return tempIdeIdentity;
109: }
110:
111: public static int getPeriod() {
112: boolean stillDefault = getPreferences().get(PROP_PERIOD, null) == null;
113: Integer defaultCheckInterval = null;
114: if (stillDefault) {
115: defaultCheckInterval = parse(Utilities
116: .getCustomCheckIntervalInMinutes());
117: }
118: if (defaultCheckInterval == null) {
119: defaultCheckInterval = EVERY_WEEK;
120: }
121: err.log(Level.FINEST, "getPeriod () returns "
122: + getPreferences().getInt(PROP_PERIOD,
123: defaultCheckInterval));
124: return getPreferences().getInt(PROP_PERIOD,
125: defaultCheckInterval);
126: }
127:
128: public static int getCheckInterval() {
129: err.log(Level.FINEST, "getCheckInterval () returns "
130: + checkInterval + "ms");
131: return checkInterval;
132: }
133:
134: public static void setPeriod(int period) {
135: err.log(Level.FINEST, "Called setPeriod (" + period + ")");
136: getPreferences().putInt(PROP_PERIOD, period);
137: }
138:
139: public static Date getLastCheck() {
140: long t = getPreferences().getLong(PROP_LAST_CHECK, -1);
141: return (t > 0) ? new Date(t) : null;
142:
143: }
144:
145: public static void setLastCheck(Date lastCheck) {
146: err.log(Level.FINER, "Set the last check to " + lastCheck);
147: if (lastCheck != null) {
148: getPreferences().putLong(PROP_LAST_CHECK,
149: lastCheck.getTime());
150: } else {
151: getPreferences().remove(PROP_LAST_CHECK);
152: }
153: }
154:
155: private static Preferences getPreferences() {
156: return NbPreferences.root().node(
157: "/org/netbeans/modules/autoupdate");
158: }
159:
160: // helper methods
161: private static String modifyIdeIdentityIfNeeded(
162: String oldIdeIdentity) {
163: int idx = oldIdeIdentity.indexOf('0');
164: String[] ideIdentityArr = oldIdeIdentity.split("\\d"); // NOI18N
165: String id = null;
166: String oldPrefix = null;
167:
168: // easy way -> no need to modify
169: if (ideIdentityArr.length == 0 || idx == 0) {
170: id = oldIdeIdentity;
171: oldPrefix = "";
172: // a way for UUID
173: } else if (idx != -1
174: && oldIdeIdentity.substring(ideIdentityArr[0].length())
175: .startsWith("0")) {
176: oldPrefix = oldIdeIdentity.substring(0, idx);
177: id = oldIdeIdentity.substring(oldPrefix.length());
178: // old way for stored IDs Random.nextInt()
179: } else {
180: oldPrefix = ideIdentityArr[0];
181: id = oldIdeIdentity.substring(oldPrefix.length());
182: }
183: err.log(Level.FINER, "Old IDE Identity Prefix: " + oldPrefix); // NOI18N
184: err.log(Level.FINER, "Old IDE Identity ID: " + id); // NOI18N
185: String newPrefix = "";
186: try {
187: FileObject fo = Repository.getDefault()
188: .getDefaultFileSystem().findResource("/productid"); // NOI18N
189: if (fo != null) {
190: InputStream is = fo.getInputStream();
191: try {
192: BufferedReader r = new BufferedReader(
193: new InputStreamReader(is));
194: newPrefix = r.readLine().trim();
195: } finally {
196: is.close();
197: }
198: }
199: } catch (IOException ignore) {
200: err.log(Level.FINER, ignore.getMessage(), ignore);
201: }
202: if (!newPrefix.equals(oldPrefix)) {
203: err.log(Level.FINER, "New IDE Identity Prefix: "
204: + newPrefix); // NOI18N
205: } else {
206: err.log(Level.FINER, "No new prefix."); // NOI18N
207: }
208: return newPrefix + id;
209: }
210:
211: private static String generateNewId() {
212: return "0" + UUID.randomUUID().toString();
213: }
214:
215: private static Integer parse(String s) {
216: if (s == null || s.trim().length() == 0) {
217: return null;
218: }
219: Integer period = null;
220: for (String[] pair : KNOWN) {
221: if (pair[0].equalsIgnoreCase(s)) {
222: try {
223: period = Integer.parseInt(pair[1]);
224: } catch (NumberFormatException nfe) {
225: assert false : "Invalid value " + pair + " throws "
226: + nfe;
227: }
228: }
229: }
230: if (period == null) {
231: try {
232: checkInterval = Long.parseLong(s) * 1000 * 60 > Integer.MAX_VALUE ? Integer.MAX_VALUE
233: : Integer.parseInt(s) * 1000 * 60;
234: period = CUSTOM_CHECK_INTERVAL;
235: err.log(Level.FINE, "Custom value of "
236: + Utilities.PLUGIN_MANAGER_CHECK_INTERVAL
237: + " is " + s + " minutes.");
238: } catch (NumberFormatException nfe) {
239: err.log(Level.FINE, "Invalid value " + s + " of "
240: + Utilities.PLUGIN_MANAGER_CHECK_INTERVAL
241: + " throws " + nfe);
242: }
243: } else {
244: err.log(Level.FINE, "Custom value of "
245: + Utilities.PLUGIN_MANAGER_CHECK_INTERVAL + " is "
246: + s);
247: }
248: return period;
249: }
250:
251: }
|