001: package net.sourceforge.squirrel_sql.client.util;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.io.File;
022: import java.io.FilenameFilter;
023: import java.util.Arrays;
024:
025: import net.sourceforge.squirrel_sql.fw.util.IJavaPropertyNames;
026:
027: import net.sourceforge.squirrel_sql.client.ApplicationArguments;
028:
029: /**
030: * This class contains information about files and directories used by the
031: * application.
032: *
033: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
034: */
035: public class ApplicationFiles {
036: /** Name of directory to contain users settings. */
037: private String _userSettingsDir;
038:
039: /** Name of folder that contains Squirrel app. */
040: private final File _squirrelHomeDir;
041:
042: /** Name of folder that contains plugins. */
043: private String _squirrelPluginsDir;
044:
045: /** Documentation folder. */
046: private String _documentationDir;
047:
048: /** Flag for cleaning up execution log files on app entry. **/
049: private static boolean needExecutionLogCleanup = true;
050:
051: /** Flag for cleaning up debug log files on app entry. **/
052: private static boolean needDebugLogCleanup = true;
053:
054: /**
055: * Ctor.
056: */
057: public ApplicationFiles() {
058: super ();
059: ApplicationArguments args = ApplicationArguments.getInstance();
060:
061: final String homeDir = args.getSquirrelHomeDirectory();
062: _squirrelHomeDir = new File(homeDir != null ? homeDir : System
063: .getProperty(IJavaPropertyNames.USER_DIR));
064: _squirrelPluginsDir = _squirrelHomeDir.getPath()
065: + File.separator + "plugins";
066: _documentationDir = _squirrelHomeDir.getPath() + File.separator
067: + "doc";
068:
069: _userSettingsDir = args.getUserSettingsDirectoryOverride();
070: if (_userSettingsDir == null) {
071: _userSettingsDir = System
072: .getProperty(IJavaPropertyNames.USER_HOME)
073: + File.separator + ".squirrel-sql";
074: }
075: try {
076: new File(_userSettingsDir).mkdirs();
077: } catch (Exception ex) {
078: System.out
079: .println("Error creating user settings directory: "
080: + _userSettingsDir);
081: System.out.println(ex.toString());
082: }
083: try {
084: final File logsDir = getExecutionLogFile().getParentFile();
085: logsDir.mkdirs();
086: } catch (Exception ex) {
087: System.out.println("Error creating logs directory");
088: System.out.println(ex.toString());
089: }
090: }
091:
092: public File getUserSettingsDirectory() {
093: return new File(_userSettingsDir);
094: }
095:
096: public File getPluginsDirectory() {
097: return new File(_squirrelPluginsDir);
098: }
099:
100: /**
101: * @return file that contains database aliases.
102: */
103: public File getDatabaseAliasesFile() {
104: return new File(_userSettingsDir + File.separator
105: + "SQLAliases23.xml");
106: }
107:
108: public File getDatabaseAliasesFile_before_version_2_3() {
109: return new File(_userSettingsDir + File.separator
110: + "SQLAliases.xml");
111: }
112:
113: /**
114: * @return file that contains JDBC driver definitions.
115: */
116: public File getDatabaseDriversFile() {
117: return new File(_userSettingsDir + File.separator
118: + "SQLDrivers.xml");
119: }
120:
121: /**
122: * @return file that contains JDBC driver definitions.
123: */
124: public File getUserPreferencesFile() {
125: return new File(_userSettingsDir + File.separator + "prefs.xml");
126: }
127:
128: /**
129: * @return file that contains the selections user chose for Cell import/export.
130: */
131: public File getCellImportExportSelectionsFile() {
132: return new File(_userSettingsDir + File.separator
133: + "cellImportExport.xml");
134: }
135:
136: /**
137: * @return file that contains the selections user chose
138: * for DataType-specific properties.
139: */
140: public File getDTPropertiesFile() {
141: return new File(_userSettingsDir + File.separator
142: + "DTproperties.xml");
143: }
144:
145: /**
146: * @return file that contains the selections user chose for specific columns to use
147: * in the WHERE clause when editing a cell in a DB table.
148: */
149: public File getEditWhereColsFile() {
150: return new File(_userSettingsDir + File.separator
151: + "editWhereCols.xml");
152: }
153:
154: /**
155: * @return file to log execution information to.
156: */
157: public File getExecutionLogFile() {
158: final String dirPath = _userSettingsDir + File.separator
159: + "logs";
160: final String logBaseName = "squirrel-sql.log";
161:
162: if (needExecutionLogCleanup) {
163: // first time through this method in program, so go cleanup
164: // old log files
165: deleteOldFiles(dirPath, logBaseName);
166: needExecutionLogCleanup = false;
167: }
168: return new File(dirPath + File.separator + logBaseName);
169: }
170:
171: /**
172: * @return file to log JDBC debug information to.
173: */
174: public File getJDBCDebugLogFile() {
175: final String dirPath = _userSettingsDir + File.separator
176: + "logs";
177: final String logBaseName = "jdbcdebug.log";
178:
179: if (needDebugLogCleanup) {
180: // first time through this method in program, so go cleanup
181: // old log files
182: deleteOldFiles(dirPath, logBaseName);
183: needDebugLogCleanup = false;
184: }
185: return new File(dirPath + File.separator + logBaseName);
186: }
187:
188: /**
189: * @return file to log debug information to.
190: */
191: // public File getDebugLogFile()
192: // {
193: // return new File(_userSettingsDir + File.separator + "squirrel-sql-debug.log");
194: // }
195: /**
196: * @return serialized Vector containing history of SQL queries executed
197: */
198: public File getUserSQLHistoryFile() {
199: return new File(_userSettingsDir + File.separator
200: + "sql_history.xml");
201: }
202:
203: public File getSquirrelHomeDir() {
204: return _squirrelHomeDir;
205: }
206:
207: /**
208: * @return directory that contains plugin specific user settings
209: */
210: public File getPluginsUserSettingsDirectory() {
211: return new File(_userSettingsDir + File.separator + "plugins");
212: }
213:
214: /**
215: * @return the quickstart guide.
216: */
217: public File getQuickStartGuideFile() {
218: return new File(_documentationDir + File.separator
219: + "quick_start.html");
220: }
221:
222: /**
223: * @return the FAQ.
224: */
225: public File getFAQFile() {
226: return new File(_documentationDir + File.separator + "faq.html");
227: }
228:
229: /**
230: * @return the changelog.
231: */
232: public File getChangeLogFile() {
233: return new File(_documentationDir + File.separator
234: + "changes.txt");
235: }
236:
237: /**
238: * @return the licence file.
239: */
240: public File getLicenceFile() {
241: return new File(_documentationDir + File.separator
242: + "licences/squirrel_licence.txt");
243: }
244:
245: /**
246: * @return the Welcome document..
247: */
248: public File getWelcomeFile() {
249: return new File(_documentationDir + File.separator
250: + "welcome.html");
251: }
252:
253: /**
254: * Internal method to remove old files such as log files.
255: * The dirPath is the path name of the directory containing the files.
256: * The fileBase is the base name of all files in the set to be culled,
257: * i.e. this method removes old versions of files named <fileBase>*,
258: * but not the file named <fileBase> or recent versions of that file.
259: * It is assumed that files are named with dates such that the names of
260: * older files are alphabetically before newer files.
261: */
262: private void deleteOldFiles(String dirPath, String fileBase) {
263:
264: // the number of files to keep is arbitrarilly set here
265: final int numberToKeep = 3;
266:
267: // define filter to select only names using the fileBase
268: class OldFileNameFilter implements FilenameFilter {
269: String fBase;
270:
271: OldFileNameFilter(String fileBase) {
272: fBase = fileBase;
273: }
274:
275: public boolean accept(File dir, String name) {
276: if (name.startsWith(fBase))
277: return true;
278: return false;
279: }
280: }
281:
282: // get the directory
283: File dir = new File(dirPath);
284:
285: // create filename filter and attach to directory
286: OldFileNameFilter fileFilter = new OldFileNameFilter(fileBase);
287:
288: // get list of files using that base name
289: String fileNames[] = dir.list(fileFilter);
290: if (fileNames == null || fileNames.length <= numberToKeep)
291: return; // not too many old files
292:
293: // we do not expect a lot of files in this directory,
294: // so just do things linearly
295:
296: // sort the list
297: Arrays.sort(fileNames);
298:
299: // If the file using the base name with no extention exists,
300: // it is first. The other files are in order from oldest to newest.
301: // The set of files to delete is slightly different depending on
302: // whether the base name file exists or not.
303: int startIndex = 0;
304: int endIndex = fileNames.length - numberToKeep;
305: if (fileNames[0].equals(fileBase)) {
306: // since the base name file exists, we need to skip it
307: // and bump up the endIndex
308: startIndex = 1;
309: endIndex++;
310: }
311:
312: for (int i = startIndex; i < endIndex; i++) {
313: // delete the old file
314: File oldFile = new File(dirPath + File.separator
315: + fileNames[i]);
316: oldFile.delete();
317: }
318: }
319:
320: public File getSQuirrelJarFile() {
321: File ret = new File(_squirrelHomeDir.getPath() + File.separator
322: + "lib" + File.separator + "squirrel-sql.jar");
323:
324: if (false == ret.exists()) {
325: ret = new File(_squirrelHomeDir.getPath() + File.separator
326: + "squirrel-sql.jar");
327: }
328: return ret;
329: }
330:
331: public File getFwJarFile() {
332: return new File(_squirrelHomeDir.getPath() + File.separator
333: + "lib" + File.separator + "fw.jar");
334: }
335: }
|