/*
 * MessagePersistenceLogger.java
 * Version: 1.0
 * Date:    2004/04/01
 *
 */
package com.acme.businessOps;

import java.io.*;
import java.text.*;
import java.util.*;

import com.bea.data.*;
import com.acme.logging.*;
import com.acme.util.*;


/**
 * Utility class used as a business operations in the authorization service for
 * persisting and logging inbound and outbound authorization messages. This
 * class opens/creates the file when loaded using a static initializer block and
 * keeps it open. In addition, a new file is created each day.
 * 
 * @author Selvan V. Rajan
 * @version 1.0 
 * 
 */
public class MessagePersistenceLogger
{
  
  //
  // Hide the constructor to avoid instantiation
  private MessagePersistenceLogger()
  {
    // Do nothing.
  }

  // Holds the date of the current file.
  private static String fileDate;

  // Current file. The file is opened when the class is loaded using a static
  // initializer block.
  private static PrintStream persistenceLog;

  private static SimpleDateFormat fileSuffixDateFormat;
  private static SimpleDateFormat timestampDateFormat;

  /**
   * This method persists and logs a String plus RawData (i.e. binary) message.
   * The String and RawData will be concatenated together before writing.
   * @see com.bea.data.RawData
   * @param stringMessage The String message to persist and log.
   * @param rawData The RawData (i.e. binary) message to persist and log.
   */
  public static void writeMessage(String stringMessage, RawData rawData, AppLogger logger)
  {
    String tempMessage;
    logger.info(
            "MessagePersistenceLogger.writeMessage(String, RawData) - Begin");
    if ((stringMessage != null) && (stringMessage.length() > 15))
    {
      tempMessage = stringMessage.substring(8,16) + " " + rawData.toString();
    }
    else
    {
      tempMessage = rawData.toString();
    }
    writeMessage(tempMessage, logger);
   
    logger.info(
            "MessagePersistenceLogger.writeMessage(String, RawData) - End");
  }
  

  /**
   * This method persists and logs a RawData (i.e. binary) message.
   * @see com.bea.data.RawData
   * @param rawData The RawData (i.e. binary) message to persist and log.
   */
  public static void writeMessage(RawData rawData, AppLogger logger)
  {
    logger.info(
            "MessagePersistenceLogger.writeMessage(RawData) - Begin");
    writeMessage(rawData.toString(), logger);
    logger.info(
            "MessagePersistenceLogger.writeMessage(RawData) - End");
  }

  /**
   * This method persists and logs a text message.
   * @param message The text message to persist and log.
   */
  public static void writeMessage(String message, AppLogger logger)
  {
    logger.info(
            "MessagePersistenceLogger.writeMessage - Begin");
    Date currentDate = new Date();
    String newFileDate = fileSuffixDateFormat.format(currentDate);

    if (! fileDate.equals(newFileDate))
    {
      logger.info(
              "MessagePersistenceLogger.writeMessage - Date changed");
      fileDate = newFileDate;
      openPersistenceLog(logger);
    }

    // Write the message.
    logger.info(
            "MessagePersistenceLogger.writeMessage - Writing message");
    persistenceLog.println(timestampDateFormat.format(currentDate) +
                           " - " + message);

    // Flush the output stream.
    persistenceLog.flush();

    logger.info(
            "MessagePersistenceLogger.writeMessage - End");
  }

  //
  // Convenience method to create a new file. Used upon class loading in the
  // static initializer block. It is also used when the date changes to create
  // the new daily persistence log file.
  private static void openPersistenceLog( AppLogger logger)
  {
    logger.info(
            "MessagePersistenceLogger.openPersistenceLog - Begin");

    // Do we have file open?
    if (persistenceLog != null)
    {
      // Close the file.
      persistenceLog.close();
    }

    logger.info(
            "MessagePersistenceLogger.openPersistenceLog - Getting properties");
    // Retrieve the properties used to create the new file.
    String filename =
            Parameters.getSProperty("message.persistence.log.path") +
            Parameters.getSProperty("file.path.seperator") +
            Parameters.getSProperty("message.persistence.log.filename") + "." +
            fileDate + "." +
            Parameters.getSProperty("message.persistence.log.filetype");
    logger.info(
            "MessagePersistenceLogger.openPersistenceLog - Persistence log filename='" +
            filename + "'");

    try
    {
      logger.info(
              "MessagePersistenceLogger.openPersistenceLog - Opening Persistence log file.");
      // Create the new file.
      persistenceLog = new PrintStream(new BufferedOutputStream(new
              FileOutputStream(filename, true)));
    }
    catch (FileNotFoundException fnfe)
    {
      logSevereError("AU.AS.E0120", logger);
      fnfe.printStackTrace();
    }
    catch (SecurityException se)
    {
      logSevereError("AU.AS.E0120", logger);
      se.printStackTrace();
    }

    logger.info(
            "MessagePersistenceLogger.openPersistenceLog - End");
  }

  private static void logSevereError (String message, AppLogger logger)
  {
      // Write severe error message to debug log.
      logger.error( message );
      
  }

  private static void logWarningError (String message, AppLogger logger)
  {
      // Write warning error message to debug log.
      logger.warning( message );
      
  }


  //
  // Static Initializer - Creates/Opens the persistence log file upon class loading.
  static
  {
    
    fileSuffixDateFormat =
            new SimpleDateFormat(Parameters.getSProperty(
            "file.name.date.format"));

    fileDate = fileSuffixDateFormat.format(new Date());

    timestampDateFormat =
            new SimpleDateFormat(Parameters.getSProperty(
            "timestamp.format"));

    openPersistenceLog(new AppLogger("Persistence"));

  }
}
