001    package org.shiftone.jrat.core.output;
002    
003    
004    import org.shiftone.jrat.util.io.proxy.ProxyWriter;
005    import org.shiftone.jrat.util.log.Logger;
006    
007    import java.io.IOException;
008    import java.io.Writer;
009    
010    
011    /**
012     * @author jeff@shiftone.org (Jeff Drost)
013     */
014    public class FileOutputWriter extends ProxyWriter implements FileOutput {
015    
016        private static final Logger LOG = Logger.getLogger(FileOutputWriter.class);
017        private final FileOutputRegistry registry;
018        private final Writer target;
019        private final String name;
020        private boolean closed = false;
021    
022        public FileOutputWriter(FileOutputRegistry registry, Writer target, String name) {
023    
024            this.registry = registry;
025            this.target = target;
026            this.name = name;
027        }
028    
029    
030        protected Writer getTarget() throws IOException {
031            return target;
032        }
033    
034    
035        public synchronized void close() throws IOException {
036    
037            if (!closed) {
038                LOG.info("closing");
039    
040                closed = true;
041    
042                registry.remove(this);
043                super.flush();
044                super.close();
045            }
046        }
047    
048    
049        public String toString() {
050            return "Writer[" + name + "]";
051        }
052    }