001    package org.shiftone.jrat.core.output;
002    
003    
004    import org.shiftone.jrat.core.Environment;
005    import org.shiftone.jrat.util.io.nop.NullOutputStream;
006    import org.shiftone.jrat.util.io.nop.NullPrintWriter;
007    import org.shiftone.jrat.util.io.nop.NullWriter;
008    import org.shiftone.jrat.util.log.Logger;
009    
010    import java.io.*;
011    
012    
013    /**
014     * @author jeff@shiftone.org (Jeff Drost)
015     */
016    public class FileOutputFactory {
017    
018        private static final Logger LOG = Logger.getLogger(FileOutputFactory.class);
019        private final FileOutputRegistry fileOutputRegistry;
020        private final int bufferSize;
021    
022        public FileOutputFactory(FileOutputRegistry fileOutputRegistry, int bufferSize) {
023    
024            this.fileOutputRegistry = fileOutputRegistry;
025            this.bufferSize = bufferSize;
026    
027        }
028    
029    
030        public FileOutputFactory(FileOutputRegistry fileOutputRegistry) {
031            this(fileOutputRegistry, Environment.getSettings().getOutputBufferSize());
032        }
033    
034    
035        public OutputStream createOutputStreamSafely(File file) {
036    
037            try {
038                return createOutputStream(file);
039            }
040            catch (Throwable e) {
041                LOG.error("unable to column OutputStream for '" + file + "' return /dev/null");
042    
043                return NullOutputStream.INSTANCE;
044            }
045        }
046    
047    
048        public Writer createWriterSafely(File file) {
049    
050            try {
051                return createWriter(file);
052            }
053            catch (Throwable e) {
054                LOG.error("unable to column Writer for '" + file + "' return /dev/null");
055    
056                return NullWriter.INSTANCE;
057            }
058        }
059    
060    
061        public PrintWriter createPrintWriterSafely(File file) {
062    
063            try {
064                return createPrintWriter(file);
065            }
066            catch (Throwable e) {
067                LOG.error("unable to column PrintWriter for '" + file + "' return /dev/null");
068    
069                return NullPrintWriter.INSTANCE;
070            }
071        }
072    
073    
074        public OutputStream createOutputStream(File file) throws IOException {
075    
076            LOG.info("createOutputStream " + file.getAbsolutePath());
077    
078            OutputStream out = internalCreateOutputStream(file);
079    
080            return fileOutputRegistry.add(out, file.getName());
081        }
082    
083    
084        public Writer createWriter(File file) throws IOException {
085    
086            LOG.info("createWriter " + file.getAbsolutePath());
087    
088            Writer out = new OutputStreamWriter(internalCreateOutputStream(file));
089    
090            return fileOutputRegistry.add(out, file.getName());
091        }
092    
093    
094        public PrintWriter createPrintWriter(File file) throws IOException {
095    
096            LOG.info("createPrintWriter " + file.getAbsolutePath());
097    
098            PrintWriter out = new PrintWriter(new OutputStreamWriter(internalCreateOutputStream(file)));
099    
100            return fileOutputRegistry.add(out, file.getName());
101        }
102    
103    
104        private OutputStream internalCreateOutputStream(File file) throws IOException {
105    
106            LOG.info("createOutputStream " + file.getAbsolutePath());
107    
108            File parent = file.getParentFile();
109    
110            if (!parent.exists()) {
111                LOG.info("creating parent directory : " + parent.getAbsolutePath());
112                parent.mkdirs();
113            }
114    
115            OutputStream out = new FileOutputStream(file);
116    
117            if (bufferSize > 0) {
118                out = new BufferedOutputStream(out, bufferSize);
119            }
120    
121            return out;
122        }
123    
124    
125        public String toString() {
126            return "FileOutputFactory[" + fileOutputRegistry + "]";
127        }
128    }