jeremy 02/02/22 13:49:54 Modified: src/scratchpad/src/org/apache/cocoon/transformation FileWritingTransformer.java Log: Revision Changes Path 1.6 +102 -42 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/transformation/FileWritingTransformer.java Index: FileWritingTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/transformation/FileWritingTransformer.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- FileWritingTransformer.java 22 Feb 2002 06:58:03 -0000 1.5 +++ FileWritingTransformer.java 22 Feb 2002 21:49:54 -0000 1.6 @@ -137,6 +137,7 @@ private static String FRT_DEFAULT_SERIALIZER = "xml"; private static String FRT_PROTOCOL = "file:"; private static String FRT_ACTION_OVERWRITE = "overwrite"; + private static String FRT_ACTION_NONE = "none"; private static String FRT_ACTION_NEW = "new"; @@ -170,6 +171,12 @@ /** Current error message. */ private String action = null; + /** Target file's final destination. */ + private File target_file = null; + + /** Temporary target file. */ + private File temp_target_file = null; + /** True when inside <write> element. */ private boolean processing; @@ -258,7 +265,7 @@ this.failed = false; this.message = null; this.target = ""; - this.action = FRT_ACTION_OVERWRITE; + this.action = FRT_ACTION_NONE; // look for the Source String src = a.getValue("",FRT_SRC_ATTRIBUTE); @@ -274,40 +281,8 @@ source.recycle(); } - // open the file - if (!this.failed) { - if (!this.target.startsWith(FRT_PROTOCOL)) { - getLogger().error("FileWritingTransformer failed, the src parameter did not resolve to a file:"); - this.failed = true; - this.message = "the src parameter did not resolve to a file:"; - } - File file = new File (this.target.substring(5)); - try { - if (!file.exists()) { - File dir = file.getParentFile(); - if (!dir.exists() && dir.mkdirs() == true) { - getLogger().warn("FileWritingTransformer: made new directories: " + dir.toString()); - } - file = new File (this.target.substring(5)); - this.action = FRT_ACTION_NEW; - } else if (file.isDirectory()) { - getLogger().error("FileWritingTransformer failed, the src parameter cannot point to a directory"); - this.failed = true; - this.message = "the src parameter pointed to a directory"; - } - } catch (SecurityException se) { - getLogger().error("FileWritingTransformer failed, did not have the required file permissions for writing", se); - this.failed = true; - this.message = "could not open the file for writing"; - } - try { - this.fos = new java.io.FileOutputStream(file); - } catch (IOException ioe) { - getLogger().error("FileWritingTransformer failed, could not open the file for writing", ioe); - this.failed = true; - this.message = "could not open the file for writing"; - } - } + // open the file + if (!this.failed) openFos(); // which Serializer? String local_serializer = a.getValue("",FRT_SERIALIZER_ATTRIBUTE); @@ -371,13 +346,8 @@ this.processing = false; getLogger().debug("FileWritingTransformer: Processing Ended"); this.manager.release(this.serializer); - try { - this.fos.close(); - this.fos = null; - } catch (IOException e) { - getLogger().error("FileWritingTransformer failed, could not close the file", e); - this.failed = true; - } + // close the file + closeFos(); // Report result String result = (this.failed) ? "failed" : "success"; AttributesImpl attrs = new AttributesImpl(); @@ -549,4 +519,94 @@ */ public void dispose() { } + + + + /** + * cloase the file output stream + */ + private void closeFos() { + try { + this.fos.close(); + this.fos = null; + if (!this.failed) { + if (target_file.exists()) { + target_file.delete(); + this.action = FRT_ACTION_OVERWRITE; + } else { + this.action = FRT_ACTION_NEW; + } + temp_target_file.renameTo(target_file); + } else { + if (temp_target_file.exists()) { + temp_target_file.delete(); + } + } + } catch (Exception e) { + getLogger().error("FileWritingTransformer failed, could not close the file", e); + this.failed = true; + this.message = "could not close the file"; + try { + temp_target_file.delete(); + } catch (SecurityException se) { + getLogger().error("FileWritingTransformer failed, could not delete the temp file: " + temp_target_file.toString(), e); + } + } + } + + /** + * open a file output stream + * + * Implementation: + * Attempts to avoid writing a broken file by writing to a temporary file, + * then exchanging the temporary for the target on successful completion. + * Attempts to avoid writing while another thread is using the same file, + * by not overwriting the temporary file. + */ + private void openFos() { + + // open the file + if (!this.target.startsWith(FRT_PROTOCOL)) { + getLogger().error("FileWritingTransformer failed, the src parameter did not resolve to a file:"); + this.failed = true; + this.message = "the src parameter did not resolve to a file"; + } else { + try { + target_file = new File (this.target.substring(5)); + temp_target_file = new File (this.target.substring(5) + ".tmp"); + if (!target_file.exists()) { + File dir = target_file.getParentFile(); + if (!dir.exists() && dir.mkdirs() == true) { + getLogger().warn("FileWritingTransformer: made new directories: " + dir.toString()); + } + } else if (target_file.isDirectory()) { + getLogger().error("FileWritingTransformer failed, the src parameter cannot point to a directory"); + this.failed = true; + this.message = "the src parameter pointed to a directory"; + } + if (!this.failed) { + if (temp_target_file.createNewFile() == true) { + this.fos = new java.io.FileOutputStream(temp_target_file); + } else { + getLogger().error("FileWritingTransformer failed, the file was busy"); + this.failed = true; + this.message = "the file was busy"; + } + } + } catch (SecurityException se) { + getLogger().error("FileWritingTransformer failed, did not have the required file permissions for writing", se); + this.failed = true; + this.message = "could not open the file for writing"; + } catch (IOException ioe) { + getLogger().error("FileWritingTransformer failed, could not open the file for writing", ioe); + this.failed = true; + this.message = "could not open the file for writing"; + } catch (NullPointerException npe) { + getLogger().error("FileWritingTransformer failed, could not resolve the target", npe); + this.failed = true; + this.message = "could not resolve the target"; + } + } + } + }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]