package org.apache.logging.log4j.core.appender.rolling;

import java.lang.reflect.Method;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.status.StatusLogger;

@Plugin(
   name = "OnStartupTriggeringPolicy",
   category = "Core",
   printObject = true
)
public class OnStartupTriggeringPolicy implements TriggeringPolicy {
   private static long JVM_START_TIME = initStartTime();
   private boolean evaluated = false;
   private RollingFileManager manager;

   public void initialize(RollingFileManager manager) {
      this.manager = manager;
      if (JVM_START_TIME == 0L) {
         this.evaluated = true;
      }

   }

   private static long initStartTime() {
      try {
         Class<?> factoryClass = Loader.loadSystemClass("java.lang.management.ManagementFactory");
         Method getRuntimeMXBean = factoryClass.getMethod("getRuntimeMXBean");
         Object runtimeMXBean = getRuntimeMXBean.invoke((Object)null);
         Class<?> runtimeMXBeanClass = Loader.loadSystemClass("java.lang.management.RuntimeMXBean");
         Method getStartTime = runtimeMXBeanClass.getMethod("getStartTime");
         Long result = (Long)getStartTime.invoke(runtimeMXBean);
         return result;
      } catch (Throwable var6) {
         StatusLogger.getLogger().error("Unable to call ManagementFactory.getRuntimeMXBean().getStartTime(), using system time for OnStartupTriggeringPolicy", var6);
         return System.currentTimeMillis();
      }
   }

   public boolean isTriggeringEvent(LogEvent event) {
      if (this.evaluated) {
         return false;
      } else {
         this.evaluated = true;
         return this.manager.getFileTime() < JVM_START_TIME;
      }
   }

   public String toString() {
      return "OnStartupTriggeringPolicy";
   }

   @PluginFactory
   public static OnStartupTriggeringPolicy createPolicy() {
      return new OnStartupTriggeringPolicy();
   }
}
