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 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2015 ForgeRock AS.
016 */
017package org.opends.server.loggers;
018
019import java.util.ArrayList;
020
021import org.forgerock.i18n.slf4j.LocalizedLogger;
022import org.opends.server.api.DirectoryThread;
023import org.opends.server.config.ConfigEntry;
024
025/**
026 * This thread is spawned off at the time of file rotation to
027 * execute specific actions such as compression, encryption,
028 * and signing of the log files.
029 */
030public class RotationActionThread extends DirectoryThread
031{
032  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
033
034  private ArrayList<ActionType> actions;
035  private String filename;
036  private ConfigEntry configEntry;
037
038  /**
039   * Create the logger thread along with the specified file name,
040   * and the rotation actions.
041   *
042   * @param  filename     The name of the file to be rotated.
043   * @param  actions      The set of actions that should be performed when the
044   *                      file is rotated.
045   * @param  configEntry  The entry that contains the rotation configuration.
046   */
047  public RotationActionThread(String filename,
048            ArrayList<ActionType> actions,
049            ConfigEntry configEntry)
050  {
051    super("Logger Rotation Action Thread");
052
053    this.filename = filename;
054    this.actions = actions;
055    this.configEntry = configEntry;
056  }
057
058  /**
059   * The run method of the thread.
060   */
061  public void run()
062  {
063    try
064    {
065      for(ActionType at : actions)
066      {
067        PostRotationAction action = null;
068        switch(at)
069        {
070          case GZIP_COMPRESS:
071            String gzipFile = filename + ".gz";
072            action = new GZIPAction(filename, gzipFile, true);
073            break;
074          case ZIP_COMPRESS:
075            String zipFile = filename + ".zip";
076            action = new ZIPAction(filename, zipFile, true);
077            break;
078          case SIGN:
079          case ENCRYPT:
080            break;
081          default:
082            System.err.println("Invalid post rollover action:" + at);
083            break;
084        }
085        if(action != null && !action.execute())
086        {
087          System.err.println("Post rotation action failed.");
088        }
089      }
090    } catch(Exception e)
091    {
092      logger.traceException(e);
093    }
094  }
095}