001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.client.plugin;
020:
021: import java.io.File;
022: import java.io.IOException;
023:
024: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
025: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
026:
027: public class PreferenceUtil {
028:
029: /** Logger for this class. */
030: private final static ILogger s_log = LoggerController
031: .createLogger(PreferenceUtil.class);
032:
033: /** Name of preferences file. */
034: private static final String USER_PREFS_FILE_NAME = "prefs.xml";
035:
036: /**
037: * Check to see if the user wants to migrate previous settings. If yes, then
038: * then oldSQuirreLInstallDir contains the user's latest settings so prefer
039: * that one, unless the user settings directory has a file that is newer.
040: * @return
041: */
042: public static File getPreferenceFileToReadFrom(IPlugin p)
043: throws IOException {
044: File userSettingsFolder = p.getPluginUserSettingsFolder();
045: final File newUserPreferenceFile = new File(userSettingsFolder,
046: USER_PREFS_FILE_NAME);
047:
048: File result = newUserPreferenceFile;
049:
050: String migratePrefsProperty = System.getProperty(
051: "migratePreferences", "false");
052: if (migratePrefsProperty != null
053: && migratePrefsProperty.equalsIgnoreCase("true")) {
054: String oldSquirrelLocation = System
055: .getProperty("oldSQuirreLInstallDir");
056:
057: if (oldSquirrelLocation == null
058: || oldSquirrelLocation.equals("")) {
059: throw new IllegalStateException(
060: "migratePreferences was set to true, but "
061: + "oldSQuirreLInstallDir wasn't set.");
062: }
063:
064: final File oldAppPreferenceFile = new File(
065: oldSquirrelLocation + File.separator + "plugins"
066: + File.separator + p.getInternalName()
067: + File.separator + USER_PREFS_FILE_NAME);
068:
069: // if the old preference file exists and is newer than the
070: // newUserPreference file, then use it for reading preferences
071: if (oldAppPreferenceFile.exists()) {
072:
073: if (oldAppPreferenceFile.lastModified() > newUserPreferenceFile
074: .lastModified()) {
075: result = oldAppPreferenceFile;
076: s_log
077: .info("-DmigratePreferences was specified; using "
078: + oldAppPreferenceFile
079: .getAbsolutePath()
080: + " as the source for preferences - will save "
081: + "them to "
082: + newUserPreferenceFile
083: .getAbsolutePath());
084: } else {
085: s_log
086: .info("-DmigratePreferences was specified, but file "
087: + newUserPreferenceFile
088: .getAbsolutePath()
089: + " is newer "
090: + "than "
091: + oldAppPreferenceFile
092: .getAbsolutePath()
093: + ": migration will be skipped");
094: }
095:
096: } else {
097: s_log
098: .info("-DmigratePreferences was specified, but file "
099: + oldAppPreferenceFile
100: .getAbsolutePath()
101: + " does not "
102: + "exist! Please remove -DmigratePreferences from the "
103: + "launch script, or fix -DoldSquirrelLocation to "
104: + "point to a valid previous SQuirreL installation "
105: + "directory");
106: }
107: }
108: return result;
109: }
110:
111: }
|