Package xplanetconfigurator.util.logging

Source Code of xplanetconfigurator.util.logging.LoggingInitializer

/*
* LoggingInitializer.java
*
*/

package xplanetconfigurator.util.logging;

import java.io.*;
import java.util.logging.*;
import xplanetconfigurator.util.FileUtil;
import xplanetconfigurator.util.OwnPreferences;


/**
* Initialises the java.util.logging.
*
* @author Tom Wiedenhoeft
*/
public class LoggingInitializer {
    /* logging.properties */
    public static String FILE_NAME = "logging.properties";
    private Logger logger;
   
    /** Creates a new instance of LoggingInitializer */
    public LoggingInitializer() {
        logger = Logger.getLogger(this.getClass().getName());
    }
   
    /**
     * Initialises the java.util.logging using the file given as
     * parameter. Store the file as System Property 'java.util.logging.config.file'
     * and in the user preferences.
     * @param file Absolute path to a file like logging.properties
     * @throws java.lang.Exception
     *         If an SecurityException, or a IOException, or a FileNotFoundExcepiton was thrown.
     */
    public void initLogging(String file) throws Exception
    {
        LogManager manager = LogManager.getLogManager();
       
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(file));
            logger.finer("Reading the logging configuration from file:  '" + file + "'... ");
            manager.readConfiguration(in);
            logger.config("Loaded new logging configuration from file:  '" + file + "'... ");
            in.close();
            in = null;
        } catch(FileNotFoundException e) {
            logger.warning(
                "Could not read logging configuration from file: '" + file
                + "'. " + e.getMessage());
            throw e;
        } finally {
            if(in != null)
            {
                try {
                    in.close();
                } catch(IOException e) {
                    logger.fine("Error closing InputStream for file: '" + file + "'. ");
                }
            }
        }
       
        logger.finer("Setting the System Property 'java.util.logging.config.file' to file: '" + file + "'...");
        System.setProperty("java.util.logging.config.file", file);
       
        logger.finer("Setting the user pref '" + LoggingInitializer.FILE_NAME + "' to file: '" + file + "'...");
        OwnPreferences prefs = OwnPreferences.userNodeForPackage(this.getClass());
        prefs.put(LoggingInitializer.FILE_NAME, file);
       
    }
   
    /**
     * Tries to find a logging.properties and then calls initLoggging(String file).
     * <br>
     * How the method findes the file
     * <br>
     * 1. Look for the System Property java.util.logging.config.file.
     * <br>
     * 2. If it was not found or the file does not exist
     *    ask the user preferences logging congiguration file.
     * <br>
     * 3. If it was not found or the file does not exist
     *    look into the classpath under /net/jfellow/util/logging and copy
     *    it to user.home/logging.properties.
     * <br>
     * 4. If it was not found or the file does not exist
     *    look into java.home/jre/lib.
     *
     * @throws java.lang.Exception If an SecurityException, or a IOException, or a FileNotFoundExcepiton was thrown,
     *         or no file logging.properties was found.
     */
    public void initLoggging() throws Exception
    {
        String file = this.getLoggingConfigurationFile();
        if(file == null)
        {
            throw new Exception("Failed to find a file logging.properties");
        }
        this.initLogging(file);
    }
   
    /**
     * Gets the logging.properties for the java.util.logging.
     * <br>
     * 1. Look for the System Property java.util.logging.config.file.
     * <br>
     * 2. If it was not found or the file does not exist
     *    ask the user preferences logging congiguration file.
     * <br>
     * 3. If it was not found or the file does not exist
     *    look into the classpath under /net/jfellow/util/logging and copy
     *    it to user.home/logging.properties.
     * <br>
     * 4. If it was not found or the file does not exist
     *    look into java.home/jre/lib.
     * @return The absolute path to logging.properies.
     */
    public String getLoggingConfigurationFile()
    {
        // In case the user started the application with the parameter
        // java.util.logging.config.file
        String externallySetLogfile = System.getProperty("java.util.logging.config.file");
        logger.finer("System Properties for 'externallySetLogfile' is: '" + externallySetLogfile + "'.");
        if(externallySetLogfile != null)
        {
            File f = new File(externallySetLogfile);
            if(f.exists())
            {
                logger.finer("Found logging configuration file: " + externallySetLogfile);
                return externallySetLogfile;
            }
            else
            {
                logger.finer("Did not found logging configuratin file: " + externallySetLogfile);
            }
        }
       
        // Find the file store in user prefs.
        OwnPreferences prefs = OwnPreferences.userNodeForPackage(this.getClass());
        String file = prefs.get(LoggingInitializer.FILE_NAME, null);
        logger.finer("Preferences gave logging configuration file: '" + file + "'.");
        if(file != null)
        {
            File f = new File(file);
            if(f.exists())
            {
                logger.finer("Found logging configuratin file: " + file);
                return file;
            }
            else
            {
                logger.finer("Did not found logging configuratin file: " + file);
            }
        }
       
        // Find in classpath
        FileUtil fileUtil = new FileUtil();
        String fileInClasspath = "/xplanetconfigurator/util/logging/" + LoggingInitializer.FILE_NAME;
        String fileContent = null;
        logger.finer("Try to read logging configuration from file: '" + fileInClasspath + "'...");
        try {
            fileContent = fileUtil.getRessourceAsString(fileInClasspath);
        } catch(Exception e) {
            logger.warning("Could not read file from classpath. File: '" + fileInClasspath + "'");
        }
        String userHome = System.getProperty("user.home");
        String defaultConfigFileLocation = userHome + File.separator + LoggingInitializer.FILE_NAME;
        if(fileContent != null)
        {
            logger.finer("Reading file content to file: '" + defaultConfigFileLocation + "'...");
            try {
                fileUtil.printFile(defaultConfigFileLocation, fileContent);
                return defaultConfigFileLocation;
            } catch(Exception e) {
                logger.warning("Failed to write file: '" + defaultConfigFileLocation + "'");
            }
        }
       
        // Try to read from java.home/jre/lib
        String javaHome = System.getProperty("java.home");
        String fileInJavaHome =
            javaHome + File.separator + "jre" + File.separator + "lib" + LoggingInitializer.FILE_NAME;
        try {
            fileContent = fileUtil.getFileAsString(new File(fileInJavaHome));
        } catch(Exception e) {
            logger.warning("Could not read file from classpath. File: '" + fileInClasspath + "'");
        }
        if(fileContent != null)
        {
            logger.finer("Reading file content to file: '" + defaultConfigFileLocation + "'...");
            try {
                fileUtil.printFile(defaultConfigFileLocation, fileContent);
                return defaultConfigFileLocation;
            } catch(Exception e) {
                logger.warning("Failed to write file: '" + defaultConfigFileLocation + "'");
            }
        }
       
        return null;
    }
}
TOP

Related Classes of xplanetconfigurator.util.logging.LoggingInitializer

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.