~ubuntu-branches/ubuntu/natty/jabref/natty

« back to all changes in this revision

Viewing changes to src/java/net/sf/jabref/export/SaveSession.java

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2010-04-27 16:49:34 UTC
  • mfrom: (2.1.8 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100427164934-ozu2dvinslmo3444
Tags: 2.6+ds-2
debian/control: add "Recommends: xdg-utils"; thanks to Vincent Fourmond
for the bug report (closes: #579346). Change xpdf to xpdf-reader in
Suggests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 */
27
27
public class SaveSession {
28
28
 
 
29
    public static final String LOCKFILE_SUFFIX = ".lock";
 
30
    // The age in ms of a lockfile before JabRef will offer to "steal" the locked file:
 
31
    public static final long LOCKFILE_CRITICAL_AGE = 60000;
 
32
 
29
33
    private static final String TEMP_PREFIX = "jabref";
30
34
    private static final String TEMP_SUFFIX = "save.bib";
 
35
 
31
36
    File file, tmp, backupFile;
32
37
    String encoding;
33
 
    boolean backup;
 
38
    boolean backup, useLockFile;
34
39
    VerifyingWriter writer;
35
40
 
36
41
    public SaveSession(File file, String encoding, boolean backup) throws IOException,
37
42
        UnsupportedCharsetException {
38
43
        this.file = file;
39
44
        tmp = File.createTempFile(TEMP_PREFIX, TEMP_SUFFIX);
 
45
        useLockFile = Globals.prefs.getBoolean("useLockFiles");
40
46
        this.backup = backup;
41
47
        this.encoding = encoding;
42
48
        writer = new VerifyingWriter(new FileOutputStream(tmp), encoding);
50
56
        return encoding;
51
57
    }
52
58
 
 
59
    public void setUseBackup(boolean useBackup) {
 
60
        this.backup = useBackup;
 
61
    }
 
62
 
53
63
    public void commit() throws SaveException {
54
64
        if (file == null)
55
65
            return;
60
70
            try {
61
71
                Util.copyFile(file, backupFile, true);
62
72
            } catch (IOException ex) {
63
 
                throw new SaveException(Globals.lang("Save failed during backup creation")+": "+ex.getMessage());
 
73
                ex.printStackTrace();
 
74
                throw SaveException.BACKUP_CREATION;
 
75
                //throw new SaveException(Globals.lang("Save failed during backup creation")+": "+ex.getMessage());
64
76
            }
65
77
        }
66
78
        try {
 
79
            if (useLockFile) {
 
80
                try {
 
81
                    if (createLockFile()) {
 
82
                        // Oops, the lock file already existed. Try to wait it out:
 
83
                        if (!Util.waitForFileLock(file, 10))
 
84
                            throw SaveException.FILE_LOCKED;
 
85
 
 
86
                    }
 
87
                } catch (IOException ex) {
 
88
                    System.err.println("Error when creating lock file");
 
89
                    ex.printStackTrace();
 
90
                }
 
91
            }
 
92
 
67
93
            Util.copyFile(tmp, file, true);
68
94
        } catch (IOException ex2) {
69
95
            // If something happens here, what can we do to correct the problem? The file is corrupted, but we still
71
97
            // repeating the action will have a different result.
72
98
            // On the other hand, our temporary file should still be clean, and won't be deleted.
73
99
            throw new SaveException(Globals.lang("Save failed while committing changes")+": "+ex2.getMessage());
 
100
        } finally {
 
101
            if (useLockFile) {
 
102
                try {
 
103
                    deleteLockFile();
 
104
                } catch (IOException ex) {
 
105
                    System.err.println("Error when deleting lock file");
 
106
                    ex.printStackTrace();
 
107
                }
 
108
            }
74
109
        }
75
110
 
76
111
        tmp.delete();
80
115
        tmp.delete();
81
116
    }
82
117
 
 
118
 
 
119
    /**
 
120
     * Check if a lock file exists, and create it if it doesn't.
 
121
     * @return true if the lock file already existed
 
122
     * @throws IOException if something happens during creation.
 
123
     */
 
124
    private boolean createLockFile() throws IOException {
 
125
        File lock = new File(file.getPath()+LOCKFILE_SUFFIX);
 
126
        if (lock.exists()) {
 
127
            return true;
 
128
        }
 
129
        FileOutputStream out = new FileOutputStream(lock);
 
130
        out.write(0);
 
131
        try {
 
132
            out.close();
 
133
        } catch (IOException ex) {
 
134
            System.err.println("Error when creating lock file");
 
135
            ex.printStackTrace();
 
136
        }
 
137
        lock.deleteOnExit();
 
138
        return false;
 
139
    }
 
140
 
 
141
    /**
 
142
     * Check if a lock file exists, and delete it if it does.
 
143
     * @return true if the lock file existed, false otherwise.
 
144
     * @throws IOException if something goes wrong.
 
145
     */
 
146
    private boolean deleteLockFile() throws IOException {
 
147
        File lock = new File(file.getPath()+LOCKFILE_SUFFIX);
 
148
        if (!lock.exists()) {
 
149
            return false;
 
150
        }
 
151
        lock.delete();
 
152
        return true;
 
153
    }
 
154
 
83
155
    public File getTemporaryFile() {
84
156
        return tmp;
85
157
    }