Package simtools.util

Source Code of simtools.util.DefaultErrOutWriter

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2007, by :
*     Corporate:
*         EADS Astrium
*     Individual:
*         Claude Cazenave
*
* $Id: DefaultErrOutWriter.java,v 1.2 2008/01/14 18:20:01 booba_skaya Exp $
*
* Changes
* -------
* Oct 17, 2007  : Initial public release
*
*/
package simtools.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import simtools.util.SysExec.ErrOutWriter;

/**
* Type DefaultErrOutWriter
* <br><b>Summary:</b><br>
* A default implementation of the ErrOutWriter interface.
*/
public class DefaultErrOutWriter implements ErrOutWriter {
    // a logger for debug purpose
    static Logger logger = simtools.util.LogConfigurator.getLogger(DefaultErrOutWriter.class.getName());

    /**<b>(BufferedWriter)</b> outWriter: The outWriter to write out message.*/
    private BufferedWriter outWriter;

    /**<b>(BufferedWriter)</b> errWriter: The errWriter to write err message.*/
    private BufferedWriter errWriter;

    /**
     * Constructor.
     * <br><b>Summary:</b><br>
     * The constructor of the Class DefaultErrOutWriter.
     * @param outputFile    The file to write the outputs.
     * @param errFile       The file to write the errors.
     */
    public DefaultErrOutWriter(String outputFile, String errFile) {
        if (outputFile != null) {
            File oFile = new File(outputFile);
           
            try {
                if(!oFile.exists()){
                    oFile.createNewFile();
                }
                if (oFile.canWrite()) {
                    outWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile)));
                }
            } catch (FileNotFoundException e) {
                //Use logger to track SEVERE message.
                logger.log(Level.WARNING, "Can not find file " + outputFile + ", print outputs to void.", e);
            } catch (IOException e) {
                //Use logger to track SEVERE message.
                logger.log(Level.WARNING, "Can not create output file " + outputFile + ", print outputs to void.", e);
            }
        }
        if (errFile != null) {
            File eFile = new File(errFile);
            try {
                if(!eFile.exists()){
                    eFile.createNewFile();
                }
                if (eFile.canWrite()) {
                    errWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(eFile)));
                }
            } catch (FileNotFoundException e) {
                //Use logger to track SEVERE message.
                logger.log(Level.WARNING, "Can not find file " + errFile + ", print errors to void.", e);
            } catch (IOException e) {
                //Use logger to track SEVERE message.
                logger.log(Level.WARNING, "Can not create output file " + outputFile + ", print outputs to void.", e);
            }
        }
    }

    /**
     * @return <b>(BufferedWriter})</b> the outWriter
     */
    public BufferedWriter getOutWriter() {
        return outWriter;
    }

    /**
     * @return <b>(BufferedWriter})</b> the errWriter
     */
    public BufferedWriter getErrWriter() {
        return errWriter;
    }

    /* (non-Javadoc)
     * @see simtools.util.SysExec.ErrOutWriter#errPrintln(java.lang.String)
     */
    public void errPrintln(String line) {
        if (errWriter != null) {
            try {
                errWriter.write(line);
            } catch (IOException e) {
                //Use logger to track SEVERE message.
                logger.log(Level.WARNING, "Failed to print error to err stream: '" + line + "'", e);
            }
        }
    }

    /* (non-Javadoc)
     * @see simtools.util.SysExec.ErrOutWriter#outPrintln(java.lang.String)
     */
    public void outPrintln(String line) {
        if (outWriter != null) {
            try {
                outWriter.write(line);
            } catch (IOException e) {
                //Use logger to track SEVERE message.
                logger.log(Level.WARNING, "Failed to print output to out stream: '" + line + "'", e);
            }
        }
    }

    /**
     * This permits to flush and close the err and out streams.
     */
    public void closeStreams() {
        try{
            if(outWriter != null){
                outWriter.flush();
                outWriter.close();
                outWriter = null;
            }
            if(errWriter != null){
                errWriter.flush();
                errWriter.close();
                errWriter = null;
            }
        }catch(IOException e){
            //Use logger to track SEVERE message.
            logger.log(Level.WARNING, "An IOException occured while closing DefaultErrOutWriter streams.", e);
        }
    }
   
    /* (non-Javadoc)
     * @see java.lang.Object#finalize()
     */
    protected void finalize() throws Throwable {
        closeStreams();
    }
}
TOP

Related Classes of simtools.util.DefaultErrOutWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.