1 17 package org.apache.log.output.io; 18 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import org.apache.log.format.Formatter; 23 24 30 public class FileTarget 31 extends StreamTarget 32 { 33 private File m_file; 35 36 private boolean m_append; 38 39 47 public FileTarget( final File file, final boolean append, final Formatter formatter ) 48 throws IOException 49 { 50 super( null, formatter ); 51 52 if( null != file ) 53 { 54 setFile( file, append ); 55 openFile(); 56 } 57 } 58 59 66 protected synchronized void setFile( final File file, final boolean append ) 67 throws IOException 68 { 69 if( null == file ) 70 { 71 throw new NullPointerException ( "file property must not be null" ); 72 } 73 74 if( isOpen() ) 75 { 76 throw new IOException ( "target must be closed before " 77 + "file property can be set" ); 78 } 79 80 m_append = append; 81 m_file = file; 82 } 83 84 90 protected synchronized void openFile() 91 throws IOException 92 { 93 if( isOpen() ) 94 { 95 close(); 96 } 97 98 final File file = getFile().getCanonicalFile(); 99 100 final File parent = file.getParentFile(); 101 if( null != parent && !parent.exists() ) 102 { 103 parent.mkdirs(); 104 } 105 106 final FileOutputStream outputStream = 107 new FileOutputStream ( file.getPath(), m_append ); 108 109 setOutputStream( outputStream ); 110 open(); 111 } 112 113 119 protected synchronized File getFile() 120 { 121 return m_file; 122 } 123 } 124 | Popular Tags |