001    package org.shiftone.jrat.core.output;
002    
003    
004    import org.shiftone.jrat.core.shutdown.ShutdownListener;
005    import org.shiftone.jrat.util.HtmlUtil;
006    import org.shiftone.jrat.util.log.Logger;
007    
008    import java.io.OutputStream;
009    import java.io.PrintWriter;
010    import java.io.Writer;
011    import java.util.Iterator;
012    import java.util.Stack;
013    
014    
015    /**
016     * @author jeff@shiftone.org (Jeff Drost)
017     */
018    public class FileOutputRegistry implements FileOutputRegistryMBean, ShutdownListener {
019    
020        private static final Logger LOG = Logger.getLogger(FileOutputRegistry.class);
021        private Stack fileOutputs = new Stack();
022    
023        public int getRegisteredFileOutputCount() {
024            return fileOutputs.size();
025        }
026    
027    
028        public String getRegisteredFileOutputsHtml() {
029            return HtmlUtil.toHtml(fileOutputs);
030        }
031    
032    
033        public synchronized OutputStream add(OutputStream outputStream, String title) {
034            return (OutputStream) add(new FileOutputOutputStream(this, outputStream, title));
035        }
036    
037    
038        public synchronized Writer add(Writer writer, String title) {
039            return (Writer) add(new FileOutputWriter(this, writer, title));
040        }
041    
042    
043        public synchronized PrintWriter add(PrintWriter printWriter, String title) {
044            return (PrintWriter) add(new FileOutputPrintWriter(this, printWriter, title));
045        }
046    
047    
048        public synchronized FileOutput add(FileOutput fileOutput) {
049    
050            LOG.info("add " + fileOutput);
051            fileOutputs.push(fileOutput);
052    
053            return fileOutput;
054        }
055    
056    
057        synchronized void remove(FileOutput fileOutput) {
058            LOG.info("remove " + fileOutput);
059            fileOutputs.remove(fileOutput);
060        }
061    
062    
063        public synchronized void closeFileOutputs() {
064    
065            LOG.info("closeFileOutputs " + fileOutputs);
066    
067            while (!fileOutputs.isEmpty()) {
068                close((FileOutput) fileOutputs.pop());
069            }
070        }
071    
072    
073        public synchronized void flushFileOutputs() {
074    
075            LOG.info("flushFileOutputs " + fileOutputs);
076    
077            Iterator iterator = fileOutputs.iterator();
078    
079            while (iterator.hasNext()) {
080                flush((FileOutput) iterator.next());
081            }
082        }
083    
084    
085        public static void close(FileOutput fileOutput) {
086    
087            if (fileOutput != null) {
088                try {
089                    LOG.info("closing : " + fileOutput);
090                    fileOutput.close();
091                }
092                catch (Throwable e) {
093                    LOG.error("unable to close " + fileOutput, e);
094                }
095            }
096        }
097    
098    
099        public static void flush(FileOutput fileOutput) {
100    
101            if (fileOutput != null) {
102                try {
103                    LOG.info("flushing : " + fileOutput);
104                    fileOutput.flush();
105                }
106                catch (Throwable e) {
107                    LOG.error("unable to flush " + fileOutput, e);
108                }
109            }
110        }
111    
112    
113        public void shutdown() {
114            closeFileOutputs();
115        }
116    
117    
118        public String toString() {
119            return "FileOutputRegistry" + fileOutputs;
120        }
121    }