01: /*
02: * The contents of this file are subject to the Mozilla Public License
03: * Version 1.1 (the "License"); you may not use this file except in
04: * compliance with the License. You may obtain a copy of the License at
05: * http://www.mozilla.org/MPL/
06: *
07: * Software distributed under the License is distributed on an "AS IS"
08: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
09: * License for the specific language governing rights and limitations
10: * under the License.
11: *
12: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
13: *
14: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
15: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
16: *
17: * Contributor(s):
18: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
19: *
20: * If you didn't download this code from the following link, you should check
21: * if you aren't using an obsolete version: http://www.isqlviewer.com
22: */
23: package org.isqlviewer.util;
24:
25: import java.io.File;
26: import java.io.FileOutputStream;
27:
28: import org.isqlviewer.bookmarks.BookmarkFolder;
29: import org.isqlviewer.xml.BookmarkDigester;
30:
31: /**
32: * Runnable code for saving bookmark information in a seperate thread.
33: * <p>
34: * Allows the saving of bookmarks to disk an asynchronous operation.
35: *
36: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
37: * @version 1.0
38: */
39: public class AsynchronousBookmarkSaver extends LoggableObject implements
40: Runnable {
41:
42: // TODO add localized messages to this class//
43: private BookmarkFolder rootFolder = null;
44: private File bookmarksFile = null;
45:
46: /**
47: * Creates a bookmark saver using the given folder to the default location.
48: * <p>
49: *
50: * @param rootFolder bookmark folder to save.
51: * @see IsqlToolkit#getDefaultBookmarksFile()
52: */
53: public AsynchronousBookmarkSaver(BookmarkFolder rootFolder) {
54:
55: this (rootFolder, IsqlToolkit.getDefaultBookmarksFile());
56: }
57:
58: /**
59: * Creates a bookmark saver to the specified file location, using the given folder.
60: * <p>
61: *
62: * @param rootFolder bookmark folder to save.
63: * @param bookmarksFile the location to save the bookmark folder too.
64: */
65: public AsynchronousBookmarkSaver(BookmarkFolder rootFolder,
66: File bookmarksFile) {
67:
68: if (bookmarksFile == null) {
69: throw new NullPointerException(File.class.getName());
70: }
71: this .rootFolder = rootFolder;
72: this .bookmarksFile = bookmarksFile;
73: }
74:
75: public void run() {
76:
77: FileOutputStream fis = null;
78: try {
79: fis = new FileOutputStream(bookmarksFile);
80: BookmarkDigester.writeBookmarkFolder(fis, rootFolder);
81: info("Saved bookmarks to :" + bookmarksFile);
82: } catch (Exception e) {
83: error("error saving bookmarks to file", e);
84: } finally {
85: if (fis != null) {
86: try {
87: fis.close();
88: } catch (Throwable t) {
89:
90: }
91: }
92: System.gc();
93: }
94: }
95:
96: }
|