KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > VFSOutputStream


1 /* ========================================================================== *
2  * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
3  * All rights reserved. *
4  * ========================================================================== *
5  * *
6  * Licensed under the Apache License, Version 2.0 (the "License"). You may *
7  * not use this file except in compliance with the License. You may obtain a *
8  * copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>. *
9  * *
10  * Unless required by applicable law or agreed to in writing, software *
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT *
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
13  * License for the specific language governing permissions and limitations *
14  * under the License. *
15  * *
16  * ========================================================================== */

17 package com.sslexplorer.vfs;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.commons.vfs.FileObject;
26 import org.apache.commons.vfs.FileSystemException;
27
28 import com.sslexplorer.vfs.webdav.DAVException;
29 import com.sslexplorer.vfs.webdav.DAVListener;
30
31
32 /**
33  * <p>A specialized {@link OutputStream} to write to {@link VFSResource}s.</p>
34  *
35  * <p>When writing to this {@link OutputStream} the data will be written to
36  * a temporary file. This temporary file will be moved to its final destination
37  * (the original file identifying the resource) when the {@link #close()}
38  * method is called.</p>
39  *
40  * <p>This specialized {@link OutputStream} never throws {@link IOException}s,
41  * but rather relies on the unchecked {@link DAVException} to notify the
42  * framework of the correct DAV errors.</p>
43  *
44  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
45  */

46 public class VFSOutputStream extends OutputStream JavaDoc {
47
48     final static Log log = LogFactory.getLog(VFSOutputStream.class);
49     
50     /** <p>The index of the temporary file.</p> */
51     private static int tmpno = (int)( Math.random() * 100000 );
52     /** <p>The original resource {@link File}.</p> */
53     private FileObject temporary = null;
54     /** <p>The {@link OutputStream} of the temporary {@link File}. </p> */
55     private OutputStream JavaDoc output = null;
56     /** <p>The {@link VFSResource} associated with this instance. </p> */
57     private VFSResource resource = null;
58
59     /**
60      * <p>Create a new {@link VFSOutputStream} instance.</p>
61      */

62     protected VFSOutputStream(VFSResource resource) {
63         if (resource == null) throw new NullPointerException JavaDoc();
64         this.resource = resource;
65
66         try {
67             if(resource instanceof FileObjectVFSResource) {
68                 // LDP - changed to get the parent using the FileObject rather than
69
// the DAVResource beause we may have a null parent if the resource
70
// is located at the root of a mount.
71
this.temporary = resource.getFile().getParent();
72                 this.temporary = this.temporary.resolveFile(
73                                 VFSResource.PREFIX + ( tmpno++) + VFSResource.SUFFIX);
74                 this.output = this.temporary.getContent().getOutputStream();
75             }
76             else {
77                 throw new IOException JavaDoc("DAV resource is not a true file.");
78             }
79         } catch (IOException JavaDoc e) {
80             String JavaDoc message = "Unable to create temporary file. " + e.getMessage();
81             throw new DAVException(507, message, e, resource);
82         }
83     }
84
85     /**
86      * <p>Rename the temporary {@link File} to the original one.</p>
87      */

88     protected void rename(FileObject temporary, FileObject original)
89     throws IOException JavaDoc {
90         if ((original.exists()) && (!original.delete())) {
91             throw new IOException JavaDoc("Unable to delete original file");
92         }
93         temporary.moveTo(original);
94     }
95
96     /**
97      * <p>Abort any data written to the temporary file and delete it.</p>
98      */

99     public void abort() {
100         try {
101             if (this.temporary.exists())
102                 this.temporary.delete();
103         } catch (FileSystemException e) {
104             log.error(e);
105         }
106         if (this.output != null) try {
107             this.output.close();
108         } catch (IOException JavaDoc exception) {
109             // Swallow the IOException on close
110
} finally {
111             this.output = null;
112         }
113     }
114
115     /**
116      * <p>Close this {@link OutputStream} {@link #rename(File,File) renaming}
117      * the temporary file to the {@link VFSResource#getFile() original} one.</p>
118      */

119     public void close() {
120         if (this.output == null) return;
121         try {
122             /* What kind of event should this invocation trigger? */
123             int event = ((FileObjectVFSResource)this.resource).getFile().exists() ?
124                         DAVListener.RESOURCE_MODIFIED:
125                         DAVListener.RESOURCE_CREATED;
126
127             /* Make sure that everything is closed and named properly */
128             this.output.close();
129             this.output = null;
130             this.rename(this.temporary, ((FileObjectVFSResource)this.resource).getFile());
131
132             /* Send notifications to all listeners of the repository */
133             this.resource.getMount().getStore().getRepository().notify(this.resource, event);
134
135         } catch (IOException JavaDoc e) {
136             String JavaDoc message = "Error processing temporary file";
137             throw new DAVException(507, message, e, this.resource);
138         } finally {
139             this.abort();
140         }
141     }
142
143     /**
144      * <p>Flush any unwritten data to the disk.</p>
145      */

146     public void flush() {
147         if (this.output == null) throw new IllegalStateException JavaDoc("Closed");
148         try {
149             this.output.flush();
150         } catch (IOException JavaDoc e) {
151             this.abort();
152             String JavaDoc message = "Unable to flush buffers";
153             throw new DAVException(507, message, e, this.resource);
154         }
155     }
156
157     /**
158      * <p>Write data to this {@link OutputStream}.</p>
159      */

160     public void write(int b) {
161         if (this.output == null) throw new IllegalStateException JavaDoc("Closed");
162         try {
163             this.output.write(b);
164         } catch (IOException JavaDoc e) {
165             this.abort();
166             String JavaDoc message = "Unable to write data";
167             throw new DAVException(507, message, e, this.resource);
168         }
169     }
170     
171     /**
172      * <p>Write data to this {@link OutputStream}.</p>
173      */

174     public void write(byte b[]) {
175         if (this.output == null) throw new IllegalStateException JavaDoc("Closed");
176         try {
177             this.output.write(b);
178         } catch (IOException JavaDoc e) {
179             this.abort();
180             String JavaDoc message = "Unable to write data";
181             throw new DAVException(507, message, e, this.resource);
182         }
183     }
184     
185     /**
186      * <p>Write data to this {@link OutputStream}.</p>
187      */

188     public void write(byte b[], int o, int l) {
189         if (this.output == null) throw new IllegalStateException JavaDoc("Closed");
190         try {
191             this.output.write(b, o, l);
192         } catch (IOException JavaDoc e) {
193             this.abort();
194             String JavaDoc message = "Unable to write data";
195             throw new DAVException(507, message, e, this.resource);
196         }
197     }
198     
199     /**
200      * <p>Finalize this {@link VFSOutputStream} instance.</p>
201      */

202     public void finalize() {
203         this.abort();
204     }
205 }
206
Popular Tags