<commit_before>/*
 * Copyright (c) 2015 <NAME>' - All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which accompanies this distribution, and is
 * available at http://creativecommons.org/licenses/MIT/
 */
package coyote.loader;

import coyote.commons.FileUtil;
import coyote.commons.GUID;
import coyote.commons.StringUtil;
import coyote.commons.UriUtil;
import coyote.commons.template.SymbolTable;
import coyote.commons.template.Template;
import coyote.dataframe.DataField;
import coyote.dataframe.DataFrame;
import coyote.i13n.StatBoard;
import coyote.i13n.StatBoardImpl;
import coyote.loader.cfg.Config;
import coyote.loader.cfg.ConfigurationException;
import coyote.loader.component.ManagedComponent;
import coyote.loader.log.Log;
import coyote.loader.log.LogMsg;
import coyote.loader.log.LogMsg.BundleBaseName;
import coyote.loader.log.Logger;
import coyote.loader.thread.ScheduledJob;
import coyote.loader.thread.Scheduler;
import coyote.loader.thread.ThreadJob;
import coyote.loader.thread.ThreadPool;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;


/**
 *
 */
public abstract class AbstractLoader extends ThreadJob implements Loader, Runnable {

  public static final BundleBaseName MSG;

  static {
    MSG = new BundleBaseName("LoaderMsg");
  }

  /**
   * Constant to assist in determining the full class name of loggers
   */
  private static final String LOGGER_PKG = Log.class.getPackage().getName();

  /**
   * The command line arguments used to invoke the loader
   */
  protected String[] commandLineArguments = null;

  /**
   * A map of all the component configurations keyed by their instance
   */
  protected final HashMap<Object, Config> components = new HashMap<Object, Config>();

  /**
   * A map of components to when they last checked in...helps detect hung components.
   */
  protected final HashMap<Object, Long> checkin = new HashMap<Object, Long>();

  /**
   * A map of components to the interval to when they should be considered hung and should be restarted.
   */
  protected final HashMap<Object, Long> hangtime = new HashMap<Object, Long>();

  /**
   * The time to pause (sleep) between idle loop cycles (dflt=3000ms)
   */
  protected long parkTime = 3000;

  /**
   * Our configuration
   */
  protected Config configuration = new Config();

  /**
   * Our very simple thread pool
   */
  protected final ThreadPool threadpool = new ThreadPool();

  protected Scheduler scheduler = null;

  private final Context context = new LoaderContext();

  /**
   * The component responsible for tracking operational statistics for all the components in this runtime
   */
  protected final StatBoard stats = new StatBoardImpl();

  /**
   * Logical identifier for this instance. May not be unique across the system.
   */
  protected String instanceName = null;

  /**
   * A symbol table to support basic template functions
   */
  protected static final SymbolTable symbols = new SymbolTable();

  /**
   * The loader which loaded this loader. Null implies this is the root loader
   */
  protected Loader parent = null;


  /**
   * @see coyote.loader.Loader#getId()
   */
  @Override
  public String getId() {
    return stats.getId();
  }


  /**
   * @see coyote.loader.Loader#getName()
   */
  @Override
  public String getName() {
    return instanceName;
  }


  /**
   * @see coyote.loader.Loader#setName(java.lang.String)
   */
  @Override
  public void setName(String name) {
    instanceName = name;
  }


  /**
   * Default constructor for all loaders
   */
  public AbstractLoader() {
    stats.setVersion(Loader.API_NAME, Loader.API_VERSION);
    stats.setState(LOADER, INITIALIZING);
  }


  protected static void confirmAppHome() {
    // see if there is a system property with a shared configuration directory
    String path = System.getProperties().getProperty(APP_HOME);

    // if there is a application home directory specified
    if (StringUtil.isNotBlank(path)) {

      // remove all the relations and duplicate slashes
      String appDir = FileUtil.normalizePath(path);

      // create a file reference to that shared directory 
      File homeDir = new File(appDir);

      if (homeDir.exists()) {
        // make sure it is a directory
        if (homeDir.isDirectory()) {
          if (!homeDir.canRead()) {
            System.out.println("The app.home property specified an un-readable (permissions) directory: " + appDir);
          }
        } else {
          System.out.println("The app.home property does not specify a directory: " + appDir);
        }
      } else {
        System.out.println("The app.home property does not exist: " + appDir);
      }

    }

  }


  protected static void confirmAppWork() {
    String path = System.getProperties().getProperty(APP_WORK);

    if (StringUtil.isNotBlank(path)) {

      String workDir = FileUtil.normalizePath(path);

      File workingDir = new File(workDir);

      if (workingDir.exists()) {
        if (workingDir.isDirectory()) {
          if (!workingDir.canWrite()) {
            System.out.println("The app.work property specified an un-writable (permissions) directory: " + workDir);<commit_msg>Check environment valueables for APP.HOME and APP.WORK<commit_after>/*
 * Copyright (c) 2015 <NAME>' - All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which accompanies this distribution, and is
 * available at http://creativecommons.org/licenses/MIT/
 */
package coyote.loader;

import coyote.commons.FileUtil;
import coyote.commons.GUID;
import coyote.commons.StringUtil;
import coyote.commons.UriUtil;
import coyote.commons.template.SymbolTable;
import coyote.commons.template.Template;
import coyote.dataframe.DataField;
import coyote.dataframe.DataFrame;
import coyote.i13n.StatBoard;
import coyote.i13n.StatBoardImpl;
import coyote.loader.cfg.Config;
import coyote.loader.cfg.ConfigurationException;
import coyote.loader.component.ManagedComponent;
import coyote.loader.log.Log;
import coyote.loader.log.LogMsg;
import coyote.loader.log.LogMsg.BundleBaseName;
import coyote.loader.log.Logger;
import coyote.loader.thread.ScheduledJob;
import coyote.loader.thread.Scheduler;
import coyote.loader.thread.ThreadJob;
import coyote.loader.thread.ThreadPool;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;


/**
 *
 */
public abstract class AbstractLoader extends ThreadJob implements Loader, Runnable {

  public static final BundleBaseName MSG;

  static {
    MSG = new BundleBaseName("LoaderMsg");
  }

  /**
   * Constant to assist in determining the full class name of loggers
   */
  private static final String LOGGER_PKG = Log.class.getPackage().getName();

  /**
   * The command line arguments used to invoke the loader
   */
  protected String[] commandLineArguments = null;

  /**
   * A map of all the component configurations keyed by their instance
   */
  protected final HashMap<Object, Config> components = new HashMap<Object, Config>();

  /**
   * A map of components to when they last checked in...helps detect hung components.
   */
  protected final HashMap<Object, Long> checkin = new HashMap<Object, Long>();

  /**
   * A map of components to the interval to when they should be considered hung and should be restarted.
   */
  protected final HashMap<Object, Long> hangtime = new HashMap<Object, Long>();

  /**
   * The time to pause (sleep) between idle loop cycles (dflt=3000ms)
   */
  protected long parkTime = 3000;

  /**
   * Our configuration
   */
  protected Config configuration = new Config();

  /**
   * Our very simple thread pool
   */
  protected final ThreadPool threadpool = new ThreadPool();

  protected Scheduler scheduler = null;

  private final Context context = new LoaderContext();

  /**
   * The component responsible for tracking operational statistics for all the components in this runtime
   */
  protected final StatBoard stats = new StatBoardImpl();

  /**
   * Logical identifier for this instance. May not be unique across the system.
   */
  protected String instanceName = null;

  /**
   * A symbol table to support basic template functions
   */
  protected static final SymbolTable symbols = new SymbolTable();

  /**
   * The loader which loaded this loader. Null implies this is the root loader
   */
  protected Loader parent = null;


  /**
   * @see coyote.loader.Loader#getId()
   */
  @Override
  public String getId() {
    return stats.getId();
  }


  /**
   * @see coyote.loader.Loader#getName()
   */
  @Override
  public String getName() {
    return instanceName;
  }


  /**
   * @see coyote.loader.Loader#setName(java.lang.String)
   */
  @Override
  public void setName(String name) {
    instanceName = name;
  }


  /**
   * Default constructor for all loaders
   */
  public AbstractLoader() {
    stats.setVersion(Loader.API_NAME, Loader.API_VERSION);
    stats.setState(LOADER, INITIALIZING);
  }


  protected static void confirmAppHome() {
    // see if there is an environment variable or a system property with a shared configuration directory
    String path = System.getenv().get(APP_HOME);
    if( StringUtil.isNotBlank(System.getProperties().getProperty(APP_HOME))) {
      path = System.getProperties().getProperty(APP_HOME);
    }

    // if there is a application home directory specified
    if (StringUtil.isNotBlank(path)) {

      // remove all the relations and duplicate slashes
      String appDir = FileUtil.normalizePath(path);

      // create a file reference to that shared directory 
      File homeDir = new File(appDir);

      if (homeDir.exists()) {
        // make sure it is a directory
        if (homeDir.isDirectory()) {
          if (!homeDir.canRead()) {
            System.out.println("The app.home property specified an un-readable (permissions) directory: " + appDir);
          }
        } else {
          System.out.println("The app.home property does not specify a directory: " + appDir);
        }
      } else {
        System.out.println("The app.home property does not exist: " + appDir);
      }

    }

  }


  protected static void confirmAppWork() {
    String path = System.getenv().get(APP_WORK);
    if( StringUtil.isNotBlank(System.getProperties().getProperty(APP_WORK))) {
      path = System.getProperties().getProperty(APP_WORK);
    }

    if (StringUtil.isNotBlank(path)) {

      String workDir = FileUtil.normalizePath(path);

      File workingDir = new File(workDir);

      if (workingDir.exists()) {
        if (workingDir.isDirectory()) {
          if (!workingDir.canWrite()) {
            System.out.println("The app.work property specified an un-writable (permissions) directory: " + workDir);
{'fraction_non_alphanumeric': 0.1014074341392999, 'fraction_numerical': 0.00288704438830747, 'mean_word_length': 3.3624557260920898, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6880939', 'n_tokens_mistral': 3202, 'n_tokens_neox': 3047, 'n_words': 961}