CentralLogStaticService.java

/*
 ** Module   : CentralLogStaticService.java
 ** Abstract : CentralLogService implementation as static service.
 **
 ** Copyright (c) 2023, Golden Code Development Corporation.
 **
 ** -#- -I- --Date-- ----------------------------Description-----------------------------
 ** 001 GBB 20230207 Initial setup
 ** 002 GBB 20230519 Support for %as placeholder in log file names with serverSide.
 ** 003 GBB 20230626 Method added to receive the client log file name to the server and set 
 **                  SESSION:FWD-LOGFILE.
 ** 004 GBB 20230825 SecurityManager context & session methods calls updated.
 */
/*
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU Affero General License as
 ** published by the Free Software Foundation, either version 3 of the
 ** License, or (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU Affero General License for more details.
 **
 ** You may find a copy of the GNU Affero GPL version 3 at the following
 ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
 **
 ** Additional terms under GNU Affero GPL version 3 section 7:
 **
 **   Under Section 7 of the GNU Affero GPL version 3, the following additional
 **   terms apply to the works covered under the License.  These additional terms
 **   are non-permissive additional terms allowed under Section 7 of the GNU
 **   Affero GPL version 3 and may not be removed by you.
 **
 **   0. Attribution Requirement.
 **
 **     You must preserve all legal notices or author attributions in the covered
 **     work or Appropriate Legal Notices displayed by works containing the covered
 **     work.  You may not remove from the covered work any author or developer
 **     credit already included within the covered work.
 **
 **   1. No License To Use Trademarks.
 **
 **     This license does not grant any license or rights to use the trademarks
 **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
 **     of Golden Code Development Corporation. You are not authorized to use the
 **     name Golden Code, FWD, or the names of any author or contributor, for
 **     publicity purposes without written authorization.
 **
 **   2. No Misrepresentation of Affiliation.
 **
 **     You may not represent yourself as Golden Code Development Corporation or FWD.
 **
 **     You may not represent yourself for publicity purposes as associated with
 **     Golden Code Development Corporation, FWD, or any author or contributor to
 **     the covered work, without written authorization.
 **
 **   3. No Misrepresentation of Source or Origin.
 **
 **     You may not represent the covered work as solely your work.  All modified
 **     versions of the covered work must be marked in a reasonable way to make it
 **     clear that the modified work is not originating from Golden Code Development
 **     Corporation or FWD.  All modified versions must contain the notices of
 **     attribution required in this license.
 */
package com.goldencode.p2j.util.logging;

import com.goldencode.p2j.security.SecurityManager;
import com.goldencode.p2j.util.*;

/**
 * Exposes methods for publishing log records on the server. This is a static implementation of
 * {@link CentralLogService}, but also includes an additional method overload used directly from the server.
 */
public class CentralLogStaticService
{
   /**
    * Publishes the log record to {@link CentralLoggerServer}. All log record validations are done on the 
    * client before calling this method. Static implementation of the interface {@link CentralLogService}.
    *
    * @param    logRecord
    *           A log record.
    */
   public static void publish(CentralLogRecord logRecord)
   {
      CentralLoggerServer.publishClientLog(logRecord);
   }
   
   /**
    * Publishes the log records to {@link CentralLoggerServer}. Used by the server-side state synchronizer.
    * 
    * @param    logRecords
    *           A log records array.
    */
   public static void publish(CentralLogRecord[] logRecords)
   {
      if (logRecords == null || logRecords.length == 0)
      {
         return;
      }
      for (CentralLogRecord centralLogRecord : logRecords)
      {
         publish(centralLogRecord);
      }
   }

   /**
    * Creates and configures a file handler for the specified client process.
    *
    * @param    filePath
    *           The path to the log file.
    * @param    fileLimit
    *           The file limit, or the max bytes in a log file before rotation. 
    * @param    fileCount
    *           The file count, or the max number of files before file overwrite starts.
    * @param    pid
    *           The process id of the client.
    * @param    userOS
    *           The OS user.
    * @param    appServerName
    *           The appServer name.
    */
   public static void setupFileHandler(String filePath,
                                       Integer fileLimit,
                                       Integer fileCount,
                                       long pid,
                                       String userOS,
                                       String appServerName)
   {
      CentralLoggerServer.setupClientHandler(filePath, fileLimit, fileCount, pid, userOS, appServerName);
   }
      
   /**
    * Notifies the server that logging is done on the client.
    *
    * @param    pid
    *           The client process ID.
    */
   public static void finalizeLogging(long pid)
   {
      CentralLoggerServer.removeHandler(pid);
   }

   /**
    * Return the session ID.
    *
    * @return   The session ID.
    */
   public static Integer getSessionId()
   {
      SecurityManager sm = SecurityManager.getInstance();
      if (sm != null && SecurityManager.initialized() && sm.contextSm.hasContext())
      {
         return sm.sessionSm.getSessionId();
      }
      return null;
   }
   
   /**
    * Return the user ID.
    *
    * @return   The user ID.
    */
   public static String getUserId()
   {
      SecurityManager sm = SecurityManager.getInstance();
      if (sm != null && SecurityManager.initialized() && sm.contextSm.hasContext())
      {
         return sm.getUserId();
      }
      return null;
   }

   /**
    * Sends the FWD client log file path to the server.
    *
    * @param    fwdLogfile
    *           The FWD client log file path.
    */
   public static void setFwdLogfile(String fwdLogfile)
   {
      EnvironmentOps.setFwdLogfile(fwdLogfile);
   }
}