~ubuntu-branches/ubuntu/trusty/jabref/trusty

« 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: 2009-12-01 18:52:51 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20091201185251-08e2unpn1jl8ijq8
Tags: 2.6~beta2-1
* Remove alternative dependency on sun-java5-jre which was removed from the
  archive. Also adjust README.Debian and /usr/bin/jabref.
* New upstream beta release:
  - fixes "allow to disable (re)storing of current tab for entries"
    (closes: #447767)
  - thanks again to Philip Rinn for his help with the upgrade
* debian/copyright: add new developer.
* Refresh patches 01_free_javac.patch and 06_BibtexEntry.patch.
* Adjust debian/jabref.links and debian/jabref.install to new jar file.
* Simplify debian/jabref.dirs.
* Add a presubj fragment for reportbug.

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);
64
70
            }
65
71
        }
66
72
        try {
 
73
            if (useLockFile) {
 
74
                try {
 
75
                    if (createLockFile()) {
 
76
                        // Oops, the lock file already existed. Try to wait it out:
 
77
                        if (!Util.waitForFileLock(file, 10))
 
78
                            throw SaveException.FILE_LOCKED;
 
79
 
 
80
                    }
 
81
                } catch (IOException ex) {
 
82
                    System.err.println("Error when creating lock file");
 
83
                    ex.printStackTrace();
 
84
                }
 
85
            }
 
86
 
67
87
            Util.copyFile(tmp, file, true);
68
88
        } catch (IOException ex2) {
69
89
            // If something happens here, what can we do to correct the problem? The file is corrupted, but we still
71
91
            // repeating the action will have a different result.
72
92
            // On the other hand, our temporary file should still be clean, and won't be deleted.
73
93
            throw new SaveException(Globals.lang("Save failed while committing changes")+": "+ex2.getMessage());
 
94
        } finally {
 
95
            if (useLockFile) {
 
96
                try {
 
97
                    deleteLockFile();
 
98
                } catch (IOException ex) {
 
99
                    System.err.println("Error when deleting lock file");
 
100
                    ex.printStackTrace();
 
101
                }
 
102
            }
74
103
        }
75
104
 
76
105
        tmp.delete();
80
109
        tmp.delete();
81
110
    }
82
111
 
 
112
 
 
113
    /**
 
114
     * Check if a lock file exists, and create it if it doesn't.
 
115
     * @return true if the lock file already existed
 
116
     * @throws IOException if something happens during creation.
 
117
     */
 
118
    private boolean createLockFile() throws IOException {
 
119
        File lock = new File(file.getPath()+LOCKFILE_SUFFIX);
 
120
        if (lock.exists()) {
 
121
            return true;
 
122
        }
 
123
        FileOutputStream out = new FileOutputStream(lock);
 
124
        out.write(0);
 
125
        try {
 
126
            out.close();
 
127
        } catch (IOException ex) {
 
128
            System.err.println("Error when creating lock file");
 
129
            ex.printStackTrace();
 
130
        }
 
131
        lock.deleteOnExit();
 
132
        return false;
 
133
    }
 
134
 
 
135
    /**
 
136
     * Check if a lock file exists, and delete it if it does.
 
137
     * @return true if the lock file existed, false otherwise.
 
138
     * @throws IOException if something goes wrong.
 
139
     */
 
140
    private boolean deleteLockFile() throws IOException {
 
141
        File lock = new File(file.getPath()+LOCKFILE_SUFFIX);
 
142
        if (!lock.exists()) {
 
143
            return false;
 
144
        }
 
145
        lock.delete();
 
146
        return true;
 
147
    }
 
148
 
83
149
    public File getTemporaryFile() {
84
150
        return tmp;
85
151
    }