KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > bufferio > BufferSaveRequest


1 /*
2  * BufferSaveRequest.java - I/O request
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2000, 2005 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.bufferio;
24
25 //{{{ Imports
26
import java.io.*;
27 import java.util.zip.*;
28
29 import org.gjt.sp.jedit.io.*;
30 import org.gjt.sp.jedit.*;
31 import org.gjt.sp.util.*;
32 //}}}
33

34 /**
35  * A buffer save request.
36  * @author Slava Pestov
37  * @version $Id: BufferSaveRequest.java 8175 2006-12-05 20:36:33Z kpouer $
38  */

39 public class BufferSaveRequest extends BufferIORequest
40 {
41     //{{{ BufferSaveRequest constructor
42
/**
43      * Creates a new buffer I/O request.
44      * @param view The view
45      * @param buffer The buffer
46      * @param session The VFS session
47      * @param vfs The VFS
48      * @param path The path
49      */

50     public BufferSaveRequest(View view, Buffer buffer,
51         Object JavaDoc session, VFS vfs, String JavaDoc path)
52     {
53         super(view,buffer,session,vfs,path);
54     } //}}}
55

56     //{{{ run() method
57
public void run()
58     {
59         /* if the VFS supports renaming files, we first
60          * save to #<filename>#save#, then rename that
61          * to <filename>, so that if the save fails,
62          * data will not be lost.
63          *
64          * as of 4.1pre7 we now call vfs.getTwoStageSaveName()
65          * instead of constructing the path directly
66          * since some VFS's might not allow # in filenames.
67          */

68
69         boolean vfsRenameCap = (vfs.getCapabilities() &
70             VFS.RENAME_CAP) != 0;
71
72         boolean wantTwoStage = wantTwoStageSave(buffer);
73         boolean twoStageSave = vfsRenameCap && wantTwoStage;
74
75         try
76         {
77             String JavaDoc[] args = { vfs.getFileName(path) };
78             setStatus(jEdit.getProperty("vfs.status.save",args));
79
80             // the entire save operation can be aborted...
81
setAbortable(true);
82
83             path = vfs._canonPath(session,path,view);
84             if(!MiscUtilities.isURL(path))
85                 path = MiscUtilities.resolveSymlinks(path);
86
87             String JavaDoc savePath;
88             if(twoStageSave)
89             {
90                 savePath = vfs.getTwoStageSaveName(path);
91                 if (savePath == null)
92                 {
93                     throw new IOException(
94                         "Can't get a temporary path for two-stage save: "
95                         + path);
96                 }
97             }
98             else
99             {
100                 makeBackup();
101                 savePath = path;
102             }
103
104             OutputStream out = vfs._createOutputStream(session,savePath,view);
105             if(out == null)
106             {
107                 buffer.setBooleanProperty(ERROR_OCCURRED,true);
108                 return;
109             }
110             try
111             {
112                 // this must be after the stream is created or
113
// we deadlock with SSHTools.
114
buffer.readLock();
115                 try
116                 {
117                     // Can't use buffer.getName() here because
118
// it is not changed until the save is
119
// complete
120
if(path.endsWith(".gz"))
121                         buffer.setBooleanProperty(Buffer.GZIPPED,true);
122                     else if (buffer.getName().endsWith(".gz"))
123                     {
124                         // The path do not ends with gz.
125
// The buffer name was .gz.
126
// So it means it's blabla.txt.gz -> blabla.txt, I remove
127
// the gz property
128
buffer.setBooleanProperty(Buffer.GZIPPED, false);
129                     }
130
131                     if(buffer.getBooleanProperty(Buffer.GZIPPED))
132                         out = new GZIPOutputStream(out);
133
134                     write(buffer,out);
135
136                     if(twoStageSave)
137                     {
138                         makeBackup();
139                         if(!vfs._rename(session,savePath,path,view))
140                             throw new IOException("Rename failed: " + savePath);
141                     }
142
143                     if(!twoStageSave)
144                         VFSManager.sendVFSUpdate(vfs,path,true);
145                 }
146                 finally
147                 {
148                     buffer.readUnlock();
149                 }
150             }
151             finally
152             {
153                 IOUtilities.closeQuietly(out);
154             }
155         }
156         catch(IOException io)
157         {
158             Log.log(Log.ERROR,this,io);
159             String JavaDoc[] pp = { io.toString() };
160             VFSManager.error(view,path,"ioerror.write-error",pp);
161
162             buffer.setBooleanProperty(ERROR_OCCURRED,true);
163         }
164         catch(WorkThread.Abort a)
165         {
166             buffer.setBooleanProperty(ERROR_OCCURRED,true);
167         }
168         finally
169         {
170             try
171             {
172                 vfs._saveComplete(session,buffer,path,view);
173                 if( twoStageSave )
174                 {
175                     vfs._finishTwoStageSave(session,buffer,path,view);
176                 }
177                 // clean up left-over markers file
178
if(!jEdit.getBooleanProperty("persistentMarkers"))
179                     vfs._delete(session,buffer.getMarkersPath(vfs),view);
180                 vfs._endVFSSession(session,view);
181             }
182             catch(IOException io)
183             {
184                 Log.log(Log.ERROR,this,io);
185                 String JavaDoc[] pp = { io.toString() };
186                 VFSManager.error(view,path,"ioerror.write-error",pp);
187
188                 buffer.setBooleanProperty(ERROR_OCCURRED,true);
189             }
190             catch(WorkThread.Abort a)
191             {
192                 buffer.setBooleanProperty(ERROR_OCCURRED,true);
193             }
194         }
195     } //}}}
196

197     //{{{ makeBackup() method
198
/**
199      * Make the backup.
200      */

201     private void makeBackup() throws IOException
202     {
203         // Only backup once per session
204
if(buffer.getProperty(Buffer.BACKED_UP) == null
205             || jEdit.getBooleanProperty("backupEverySave"))
206         {
207             vfs._backup(session,path,view);
208             buffer.setBooleanProperty(Buffer.BACKED_UP,true);
209         }
210     } //}}}
211

212     //{{{ wantTwoStageSave() method
213
public static boolean wantTwoStageSave(Buffer buffer)
214     {
215         return !buffer.getBooleanProperty("forbidTwoStageSave") &&
216             (buffer.getBooleanProperty("overwriteReadonly") ||
217             jEdit.getBooleanProperty("twoStageSave"));
218     }//}}}
219
}
220
Popular Tags