001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2008-2010 Sun Microsystems, Inc. 015 * Portions Copyright 2011-2016 ForgeRock AS. 016 */ 017package org.opends.quicksetup; 018 019import java.io.File; 020import java.io.IOException; 021import java.util.logging.FileHandler; 022import java.util.logging.Logger; 023import java.util.Date; 024import java.text.DateFormat; 025 026import org.forgerock.i18n.LocalizableMessage; 027import org.forgerock.i18n.slf4j.LocalizedLogger; 028import org.opends.server.loggers.JDKLogging; 029 030/** This class represents a temporary log file which should be usually deleted if linked operation succeeded. */ 031public class TempLogFile 032{ 033 private static final LocalizedLogger localizedLogger = LocalizedLogger.getLoggerForThisClass(); 034 035 private static final String OPENDS_LOGGER_NAME = "org.opends"; 036 037 /** 038 * Creates a new temporary log file. 039 * <p> 040 * Log file will be generated in the OS temporary directory and its name will have 041 * the following pattern: prefix-[RANDOM_NUMBER_STRING].log 042 * 043 * @param prefix 044 * log file prefix to which log messages will be written. 045 * @return a new temporary log file. 046 */ 047 public static TempLogFile newTempLogFile(final String prefix) 048 { 049 try 050 { 051 return new TempLogFile(File.createTempFile(prefix, ".log")); 052 } 053 catch (final IOException e) 054 { 055 localizedLogger.error(LocalizableMessage.raw("Unable to create temp log file because: " + e.getMessage()), e); 056 return new TempLogFile(); 057 } 058 } 059 060 /** Prevents messages written to loggers from appearing in the console output. */ 061 private static void disableConsoleLogging(final Logger logger) 062 { 063 if (!"true".equalsIgnoreCase(System.getenv("OPENDJ_LOG_TO_STDOUT"))) 064 { 065 logger.setUseParentHandlers(false); 066 } 067 } 068 069 private final File logFile; 070 private final FileHandler fileHandler; 071 072 private TempLogFile() 073 { 074 this.logFile = null; 075 this.fileHandler = null; 076 } 077 078 private TempLogFile(final File file) throws IOException 079 { 080 logFile = file; 081 fileHandler = new FileHandler(logFile.getCanonicalPath()); 082 fileHandler.setFormatter(JDKLogging.getFormatter()); 083 final Logger parentLogger = Logger.getLogger(OPENDS_LOGGER_NAME); 084 parentLogger.addHandler(fileHandler); 085 disableConsoleLogging(parentLogger); 086 final Logger logger = Logger.getLogger(getClass().getPackage().getName()); 087 logger.info("QuickSetup application launched " + DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG) 088 .format(new Date())); 089 } 090 091 /** 092 * Gets the name of the log file. 093 * 094 * @return File representing the log file 095 */ 096 public File getLogFile() 097 { 098 return logFile; 099 } 100 101 /** Closes the log file handler and delete the temp log file . */ 102 public void deleteLogFileAfterSuccess() 103 { 104 if (isEnabled()) 105 { 106 fileHandler.close(); 107 logFile.delete(); 108 } 109 } 110 111 /** 112 * Return {@code true} if a temp log file has been created and could be used to log messages. 113 * @return {@code true} if a temp log file has been created and could be used to log messages. 114 */ 115 public boolean isEnabled() 116 { 117 return logFile != null; 118 } 119 120 /** 121 * Return the absolute path of the temp log file. 122 * @return the absolute path of the temp log file. 123 */ 124 public String getPath() 125 { 126 return logFile.getAbsolutePath(); 127 } 128}