diff options
Diffstat (limited to 'pki/base/common/src/com/netscape/cms/logging/RollingLogFile.java')
-rw-r--r-- | pki/base/common/src/com/netscape/cms/logging/RollingLogFile.java | 252 |
1 files changed, 127 insertions, 125 deletions
diff --git a/pki/base/common/src/com/netscape/cms/logging/RollingLogFile.java b/pki/base/common/src/com/netscape/cms/logging/RollingLogFile.java index e51a117a..2f97dcaa 100644 --- a/pki/base/common/src/com/netscape/cms/logging/RollingLogFile.java +++ b/pki/base/common/src/com/netscape/cms/logging/RollingLogFile.java @@ -17,6 +17,7 @@ // --- END COPYRIGHT BLOCK --- package com.netscape.cms.logging; + import java.io.File; import java.io.FileNotFoundException; import java.io.FilenameFilter; @@ -40,11 +41,12 @@ import com.netscape.certsrv.logging.ILogger; import com.netscape.certsrv.logging.SystemEvent; import com.netscape.cmsutil.util.Utils; + /** * A rotating log file for Certificate log events. This class loosely follows * the Netscape Common Log API implementing rollover interval, size and file * naming conventions. It does not yet implement Disk Usage. - * + * * @version $Revision$, $Date$ */ public class RollingLogFile extends LogFile { @@ -102,7 +104,8 @@ public class RollingLogFile extends LogFile { */ private Object mExpLock = new Object(); - private final static String LOGGING_SIGNED_AUDIT_LOG_DELETE = "LOGGING_SIGNED_AUDIT_LOG_DELETE_3"; + private final static String LOGGING_SIGNED_AUDIT_LOG_DELETE = + "LOGGING_SIGNED_AUDIT_LOG_DELETE_3"; /** * Construct a RollingLogFile @@ -112,15 +115,16 @@ public class RollingLogFile extends LogFile { /** * Initialize and open a RollingLogFile using the prop config store - * + * * @param config The property config store to find values in */ - public void init(IConfigStore config) throws IOException, EBaseException { + public void init(IConfigStore config) throws IOException, + EBaseException { super.init(config); rl_init(config.getInteger(PROP_MAX_FILE_SIZE, MAX_FILE_SIZE), - config.getString(PROP_ROLLOVER_INTERVAL, ROLLOVER_INTERVAL), - config.getString(PROP_EXPIRATION_TIME, EXPIRATION_TIME)); + config.getString(PROP_ROLLOVER_INTERVAL, ROLLOVER_INTERVAL), + config.getString(PROP_EXPIRATION_TIME, EXPIRATION_TIME)); } /** @@ -128,7 +132,7 @@ public class RollingLogFile extends LogFile { * attributes. */ protected void rl_init(int maxFileSize, String rolloverInterval, - String expirationTime) { + String expirationTime) { mMaxFileSize = maxFileSize * 1024; setRolloverTime(rolloverInterval); setExpirationTime(expirationTime); @@ -149,9 +153,9 @@ public class RollingLogFile extends LogFile { /** * Set the rollover interval - * - * @param rolloverSeconds The amount of time in seconds until the log is - * rotated. A value of 0 will disable log rollover. + * + * @param rolloverSeconds The amount of time in seconds until the log + * is rotated. A value of 0 will disable log rollover. **/ public synchronized void setRolloverTime(String rolloverSeconds) { mRolloverInterval = Long.valueOf(rolloverSeconds).longValue() * 1000; @@ -167,8 +171,8 @@ public class RollingLogFile extends LogFile { /** * Get the rollover interval - * - * @return The interval in seconds in which the log is rotated + * + * @return The interval in seconds in which the log is rotated **/ public synchronized int getRolloverTime() { return (int) (mRolloverInterval / 1000); @@ -176,9 +180,9 @@ public class RollingLogFile extends LogFile { /** * Set the file expiration time - * - * @param expirationSeconds The amount of time in seconds until log files - * are deleted + * + * @param expirationSeconds The amount of time in seconds until log files + * are deleted **/ public void setExpirationTime(String expirationSeconds) { @@ -201,8 +205,8 @@ public class RollingLogFile extends LogFile { /** * Get the expiration time - * - * @return The age in seconds in which log files are delete + * + * @return The age in seconds in which log files are delete **/ public int getExpirationTime() { return (int) (mExpirationTime / 1000); @@ -212,86 +216,90 @@ public class RollingLogFile extends LogFile { * Rotate the log file to a backup file with a incrementing integer * extension **/ - public synchronized void rotate() throws IOException { + public synchronized void rotate() + throws IOException { - // File backupFile = new File(mFileName + "." + mFileNumber); - File backupFile = new File(mFileName + "." - + mLogFileDateFormat.format(mDate)); + //File backupFile = new File(mFileName + "." + mFileNumber); + File backupFile = new File(mFileName + "." + mLogFileDateFormat.format(mDate)); // close, backup, and reopen the log file zeroizing its contents super.close(); try { - if (Utils.isNT()) { + if( Utils.isNT() ) { // NT is very picky on the path - Utils.exec("copy " - + mFile.getCanonicalPath().replace('/', '\\') + " " - + backupFile.getCanonicalPath().replace('/', '\\')); + Utils.exec( "copy " + + mFile.getCanonicalPath().replace( '/', '\\' ) + + " " + + backupFile.getCanonicalPath().replace( '/', + '\\' ) ); } else { // Create a copy of the original file which // preserves the original file permissions. - Utils.exec("cp -p " + mFile.getCanonicalPath() + " " - + backupFile.getCanonicalPath()); + Utils.exec( "cp -p " + mFile.getCanonicalPath() + " " + + backupFile.getCanonicalPath() ); } // Zeroize the original file if and only if // the backup copy was successful. - if (backupFile.exists()) { + if( backupFile.exists() ) { // Make certain that the backup file has // the correct permissions. - if (!Utils.isNT()) { - Utils.exec("chmod 00640 " + backupFile.getCanonicalPath()); + if( !Utils.isNT() ) { + Utils.exec( "chmod 00640 " + backupFile.getCanonicalPath() ); } try { // Open and close the original file // to zeroize its contents. - PrintWriter pw = new PrintWriter(mFile); + PrintWriter pw = new PrintWriter( mFile ); pw.close(); // Make certain that the original file retains // the correct permissions. - if (!Utils.isNT()) { - Utils.exec("chmod 00640 " + mFile.getCanonicalPath()); + if( !Utils.isNT() ) { + Utils.exec( "chmod 00640 " + mFile.getCanonicalPath() ); } - } catch (FileNotFoundException e) { - CMS.debug("Unable to zeroize " + mFile.toString()); + } catch ( FileNotFoundException e ) { + CMS.debug( "Unable to zeroize " + + mFile.toString() ); } } else { - CMS.debug("Unable to backup " + mFile.toString() + " to " - + backupFile.toString()); + CMS.debug( "Unable to backup " + + mFile.toString() + " to " + + backupFile.toString() ); } - } catch (Exception e) { - CMS.debug("Unable to backup " + mFile.toString() + " to " - + backupFile.toString()); + } catch( Exception e ) { + CMS.debug( "Unable to backup " + + mFile.toString() + " to " + + backupFile.toString() ); } super.open(); // will reset mBytesWritten mFileNumber++; } /** - * Remove any log files which have not been modified in the specified time + * Remove any log files which have not been modified in the specified + * time * <P> - * - * NOTE: automatic removal of log files is currently NOT supported! + * + * NOTE: automatic removal of log files is currently NOT supported! * <P> - * + * * <ul> * <li>signed.audit LOGGING_SIGNED_AUDIT_LOG_DELETE used AFTER audit log * expires (authorization should not allow, but in case authorization gets * compromised make sure it is written AFTER the log expiration happens) * </ul> - * * @param expirationSeconds The number of seconds since the expired files - * have been modified. + * have been modified. * @return the time in milliseconds when the next file expires **/ public long expire(long expirationSeconds) throws ELogException { String auditMessage = null; if (expirationSeconds <= 0) - throw new ELogException( - CMS.getUserMessage("CMS_LOG_EXPIRATION_TIME_ZERO")); + throw new ELogException(CMS.getUserMessage("CMS_LOG_EXPIRATION_TIME_ZERO")); long expirationTime = expirationSeconds * 1000; long currentTime = System.currentTimeMillis(); @@ -304,27 +312,27 @@ public class RollingLogFile extends LogFile { File dir = new File(dirName); // Get just the base name, minus the .date extension - // int len = mFile.getName().length() - LogFile.DATE_PATTERN.length() - - // 1; - // String baseName = mFile.getName().substring(0, len); + //int len = mFile.getName().length() - LogFile.DATE_PATTERN.length() - 1; + //String baseName = mFile.getName().substring(0, len); String fileName = mFile.getName(); String baseName = null, pathName = null; int index = fileName.lastIndexOf("/"); - if (index != -1) { // "/" exist in fileName + if (index != -1) { // "/" exist in fileName pathName = fileName.substring(0, index); baseName = fileName.substring(index + 1); dirName = dirName.concat("/" + pathName); - } else { // "/" NOT exist in fileName + }else { // "/" NOT exist in fileName baseName = fileName; } fileFilter ff = new fileFilter(baseName + "."); String[] filelist = dir.list(ff); - if (filelist == null) { // Crap! Something is wrong. - throw new ELogException(CMS.getUserMessage( - "CMS_LOG_DIRECTORY_LIST_FAILED", dirName, ff.toString())); + if (filelist == null) { // Crap! Something is wrong. + throw new + ELogException(CMS.getUserMessage("CMS_LOG_DIRECTORY_LIST_FAILED", + dirName, ff.toString())); } // Walk through the list of files which match this log file name @@ -332,29 +340,33 @@ public class RollingLogFile extends LogFile { for (int i = 0; i < filelist.length; i++) { if (pathName != null) { filelist[i] = pathName + "/" + filelist[i]; - } else { + }else { filelist[i] = dirName + "/" + filelist[i]; } - + String fullname = dirName + File.separatorChar + filelist[i]; File file = new File(fullname); long fileTime = file.lastModified(); // Java documentation on File says lastModified() should not - // be interpeted. The doc is wrong. See JavaSoft bug #4094538 + // be interpeted. The doc is wrong. See JavaSoft bug #4094538 if ((currentTime - fileTime) > expirationTime) { file.delete(); if (file.exists()) { // log failure in deleting an expired signed audit log file auditMessage = CMS.getLogMessage( - LOGGING_SIGNED_AUDIT_LOG_DELETE, - ILogger.SYSTEM_UID, ILogger.FAILURE, fullname); + LOGGING_SIGNED_AUDIT_LOG_DELETE, + ILogger.SYSTEM_UID, + ILogger.FAILURE, + fullname); } else { // log success in deleting an expired signed audit log file auditMessage = CMS.getLogMessage( - LOGGING_SIGNED_AUDIT_LOG_DELETE, - ILogger.SYSTEM_UID, ILogger.SUCCESS, fullname); + LOGGING_SIGNED_AUDIT_LOG_DELETE, + ILogger.SYSTEM_UID, + ILogger.SUCCESS, + fullname); } audit(auditMessage); @@ -370,7 +382,7 @@ public class RollingLogFile extends LogFile { // // At first glance you may think it's a waste of thread resources to have // two threads for every log file, but the truth is that these threads are - // sleeping 99% of the time. NxN thread implementations (Solaris, NT, + // sleeping 99% of the time. NxN thread implementations (Solaris, NT, // IRIX 6.4, Unixware, etc...) will handle these in user space. // // You may be able to join these into one thread, and deal with @@ -380,8 +392,8 @@ public class RollingLogFile extends LogFile { // /** - * Log rotation thread. Sleep for the rollover interval and rotate the log. - * Changing rollover interval to 0 will cause this thread to exit. + * Log rotation thread. Sleep for the rollover interval and rotate the + * log. Changing rollover interval to 0 will cause this thread to exit. */ final class RolloverThread extends Thread { @@ -401,10 +413,8 @@ public class RollingLogFile extends LogFile { RollingLogFile.this.wait(mRolloverInterval); } catch (InterruptedException e) { // This shouldn't happen very often - CMS.getLogger() - .getLogQueue() - .log(new SystemEvent(CMS.getUserMessage( - "CMS_LOG_THREAD_INTERRUPT", "rollover"))); + CMS.getLogger().getLogQueue().log(new + SystemEvent(CMS.getUserMessage("CMS_LOG_THREAD_INTERRUPT", "rollover"))); } } @@ -416,24 +426,24 @@ public class RollingLogFile extends LogFile { try { rotate(); } catch (IOException e) { - ConsoleError.send(new SystemEvent(CMS.getUserMessage( - "CMS_LOG_ROTATE_LOG_FAILED", mFile.getName(), - e.toString()))); + ConsoleError.send(new + SystemEvent(CMS.getUserMessage("CMS_LOG_ROTATE_LOG_FAILED", mFile.getName(), e.toString()))); break; } } // else - // Don't rotate empty logs - // flag in log summary file? + // Don't rotate empty logs + // flag in log summary file? } mRolloverThread = null; } } + /** - * Log expiration thread. Sleep for the expiration interval and delete any - * files which are too old. Changing expiration interval to 0 will cause - * this thread to exit. + * Log expiration thread. Sleep for the expiration interval and + * delete any files which are too old. + * Changing expiration interval to 0 will cause this thread to exit. */ final class ExpirationThread extends Thread { @@ -442,8 +452,7 @@ public class RollingLogFile extends LogFile { */ public ExpirationThread() { super(); - super.setName(mFileName + ".expiration-" - + (Thread.activeCount() + 1)); + super.setName(mFileName + ".expiration-" + (Thread.activeCount() + 1)); } public void run() { @@ -457,19 +466,19 @@ public class RollingLogFile extends LogFile { try { wakeupTime = expire((long) (mExpirationTime / 1000)); } catch (SecurityException e) { - ConsoleError.send(new SystemEvent(CMS.getUserMessage( - "CMS_LOG_EXPIRE_LOG_FAILED", e.toString()))); + ConsoleError.send(new + SystemEvent(CMS.getUserMessage("CMS_LOG_EXPIRE_LOG_FAILED", e.toString()))); break; } catch (ELogException e) { - ConsoleError.send(new SystemEvent(CMS.getUserMessage( - "CMS_LOG_EXPIRE_LOG_FAILED", e.toString()))); + ConsoleError.send(new + SystemEvent(CMS.getUserMessage("CMS_LOG_EXPIRE_LOG_FAILED", e.toString()))); break; } sleepTime = wakeupTime - System.currentTimeMillis(); - // System.out.println("wakeup " + wakeupTime); - // System.out.println("current "+System.currentTimeMillis()); - // System.out.println("sleep " + sleepTime); + //System.out.println("wakeup " + wakeupTime); + //System.out.println("current "+System.currentTimeMillis()); + //System.out.println("sleep " + sleepTime); // Sleep for the interval and then check the directory // Note: mExpirationTime can only change while we're // sleeping @@ -478,9 +487,8 @@ public class RollingLogFile extends LogFile { mExpLock.wait(sleepTime); } catch (InterruptedException e) { // This shouldn't happen very often - ConsoleError.send(new SystemEvent(CMS - .getUserMessage("CMS_LOG_THREAD_INTERRUPT", - "expiration"))); + ConsoleError.send(new + SystemEvent(CMS.getUserMessage("CMS_LOG_THREAD_INTERRUPT", "expiration"))); } } } @@ -491,11 +499,11 @@ public class RollingLogFile extends LogFile { /** * Write an event to the log file - * - * @param ev The event to be logged. + * + * @param ev The event to be logged. **/ public synchronized void log(ILogEvent ev) throws ELogException { - // xxx, Shall we log first without checking if it exceed the maximum? + //xxx, Shall we log first without checking if it exceed the maximum? super.log(ev); // Will increment mBytesWritten if ((0 != mMaxFileSize) && (mBytesWritten > mMaxFileSize)) { @@ -503,9 +511,7 @@ public class RollingLogFile extends LogFile { try { rotate(); } catch (IOException e) { - throw new ELogException(CMS.getUserMessage( - "CMS_LOG_ROTATE_LOG_FAILED", mFile.getName(), - e.toString())); + throw new ELogException(CMS.getUserMessage("CMS_LOG_ROTATE_LOG_FAILED", mFile.getName(), e.toString())); } } } @@ -513,8 +519,9 @@ public class RollingLogFile extends LogFile { /** * Retrieve log file list. */ - public synchronized NameValuePairs retrieveLogList(Hashtable req) - throws ServletException, IOException, EBaseException { + public synchronized NameValuePairs retrieveLogList(Hashtable req + ) throws ServletException, + IOException, EBaseException { NameValuePairs params = new NameValuePairs(); String[] files = null; @@ -527,7 +534,7 @@ public class RollingLogFile extends LogFile { /** * Get the log file list in the log directory - * + * * @return an array of filenames with related path to cert server root */ protected String[] fileList() { @@ -537,7 +544,7 @@ public class RollingLogFile extends LogFile { String fileName = mFile.getName(); int index = fileName.lastIndexOf("/"); - if (index != -1) { // "/" exist in fileName + if (index != -1) { // "/" exist in fileName pathName = fileName.substring(0, index); baseName = fileName.substring(index + 1); if (dirName == null) { @@ -545,25 +552,24 @@ public class RollingLogFile extends LogFile { } else { dirName = dirName.concat("/" + pathName); } - } else { // "/" NOT exist in fileName + }else { // "/" NOT exist in fileName baseName = fileName; } - + File dir = new File(dirName); fileFilter ff = new fileFilter(baseName + "."); - // There are some difference here. both should work - // error,logs,logs/error jdk115 - // logs/system,., logs/system jdk116 - // System.out.println(mFile.getName()+","+dirName+","+mFile.getPath()); - // //log/system,. - + //There are some difference here. both should work + //error,logs,logs/error jdk115 + //logs/system,., logs/system jdk116 + //System.out.println(mFile.getName()+","+dirName+","+mFile.getPath()); //log/system,. + String[] filelist = dir.list(ff); for (int i = 0; i < filelist.length; i++) { if (pathName != null) { filelist[i] = pathName + "/" + filelist[i]; - } else { + }else { filelist[i] = dirName + "/" + filelist[i]; } } @@ -583,7 +589,7 @@ public class RollingLogFile extends LogFile { v.addElement(PROP_MAX_FILE_SIZE + "="); v.addElement(PROP_ROLLOVER_INTERVAL + "="); - // v.addElement(PROP_EXPIRATION_TIME + "="); + //v.addElement(PROP_EXPIRATION_TIME + "="); return v; } @@ -603,8 +609,7 @@ public class RollingLogFile extends LogFile { else if (mRolloverInterval / 1000 <= 60 * 60 * 24 * 366) v.addElement(PROP_ROLLOVER_INTERVAL + "=" + "Yearly"); - // v.addElement(PROP_EXPIRATION_TIME + "=" + mExpirationTime / - // 1000); + //v.addElement(PROP_EXPIRATION_TIME + "=" + mExpirationTime / 1000); } catch (Exception e) { } return v; @@ -615,21 +620,17 @@ public class RollingLogFile extends LogFile { Vector info = new Vector(); for (int i = 0; i < p.length; i++) { - if (!p[i].startsWith(IExtendedPluginInfo.HELP_TOKEN) - && !p[i].startsWith(IExtendedPluginInfo.HELP_TEXT)) + if (!p[i].startsWith(IExtendedPluginInfo.HELP_TOKEN) && !p[i].startsWith(IExtendedPluginInfo.HELP_TEXT)) info.addElement(p[i]); } - info.addElement(PROP_MAX_FILE_SIZE - + ";integer;If the current log file size if bigger than this parameter in kilobytes(KB), the file will be rotated."); - info.addElement(PROP_ROLLOVER_INTERVAL - + ";choice(Hourly,Daily,Weekly,Monthly,Yearly);The frequency of the log being rotated."); - info.addElement(PROP_EXPIRATION_TIME - + ";integer;The amount of time before a backed up log is removed in seconds"); + info.addElement(PROP_MAX_FILE_SIZE + ";integer;If the current log file size if bigger than this parameter in kilobytes(KB), the file will be rotated."); + info.addElement(PROP_ROLLOVER_INTERVAL + ";choice(Hourly,Daily,Weekly,Monthly,Yearly);The frequency of the log being rotated."); + info.addElement(PROP_EXPIRATION_TIME + ";integer;The amount of time before a backed up log is removed in seconds"); info.addElement(IExtendedPluginInfo.HELP_TOKEN + - // ";configuration-logrules-rollinglogfile"); - ";configuration-adminbasics"); - info.addElement(IExtendedPluginInfo.HELP_TEXT - + ";Write the log messages to a file which will be rotated automatically."); + //";configuration-logrules-rollinglogfile"); + ";configuration-adminbasics"); + info.addElement(IExtendedPluginInfo.HELP_TEXT + + ";Write the log messages to a file which will be rotated automatically."); String[] params = new String[info.size()]; info.copyInto(params); @@ -638,13 +639,14 @@ public class RollingLogFile extends LogFile { } } + /** * A file filter to select the file with a given prefix */ class fileFilter implements FilenameFilter { String patternToMatch = null; - public fileFilter(String pattern) { + public fileFilter (String pattern) { patternToMatch = pattern; } |