<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cybercom.javaee.config;

import java.math.BigInteger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.ws.rs.Produces;

/**
 *
 * @author <NAME> (<EMAIL>)
 */
@ApplicationScoped
public class ConfigProducer {

   /**
    * Injects a string configuration value.
    *
    * @param ip the injecting class
    * @return string configuration
    */
   @Produces
   @Config
   public String getConfigurationValue(InjectionPoint ip) {
      return getValue(ip.getAnnotated().getAnnotation(Config.class).key());
   }

   /**
    * Injects a BigInteger configuration value.
    *
    * @param ip the injecting class
    * @return string configuration
    */
   @Produces
   @Config
   public BigInteger getConfigurationBigInt(InjectionPoint ip) {
      return new BigInteger(getValue(ip.getAnnotated().getAnnotation(Config.class).key()));
   }

   /**
    * Injects an int configuration value.
    *
    * @param ip the injecting class
    * @return int configuration
    */
   @Config
   @Produces
   public int getConfigurationInt(InjectionPoint ip) {
      try {
         return Integer.parseInt(getValue(ip.getAnnotated().getAnnotation(Config.class).key()));
      } catch (NumberFormatException e) {
//         logger.severe(() -> "Invalid format for " + ip.getAnnotated().getAnnotation(Config.class).key() + "=" + getValue(ip.getAnnotated().getAnnotation(Config.class).key()));
         throw new RuntimeException("Invalid format for " + ip.getAnnotated().getAnnotation(Config.class).key() + "=" + getValue(ip.getAnnotated().getAnnotation(Config.class).key()), e);
      }
   }

   /**
    * Injects a boolean configuration value.
    *
    * @param ip the injecting class
    * @return boolean configuration
    */
   @Config
   @Produces
   public boolean getConfigurationBool(InjectionPoint ip) {

      if (getValue(ip.getAnnotated().getAnnotation(Config.class).key()).equalsIgnoreCase("false")) {
         return Boolean.FALSE;
      }

      return Boolean.TRUE;
   }

   /**
    * Retrieves the configuration value for underlying service.
    *
    * @param configKey configuration key
    * @return configuration value
    */
   protected String getValue(String configKey) {
      if (configKey == null || configKey.isEmpty()) {
         throw new IllegalArgumentException("Configuration key is empty.");
      }
      return "";
   }

   @PostConstruct
   private void init()  {
   }
}
<commit_msg>reading and caching props from file<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cybercom.javaee.config;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.ws.rs.Produces;

/**
 *
 * @author <NAME> (<EMAIL>)
 */
@ApplicationScoped
public class ConfigProducer {

   private int cacheTimeout;
   private long timestamp;
   private final Properties configurationProperties = new Properties();

   /**
    * Injects a string configuration value.
    *
    * @param ip the injecting class
    * @return string configuration
    */
   @Produces
   @Config
   public String getConfigurationValue(InjectionPoint ip) {
      return getValue(ip.getAnnotated().getAnnotation(Config.class).key());
   }

   /**
    * Injects a BigInteger configuration value.
    *
    * @param ip the injecting class
    * @return string configuration
    */
   @Produces
   @Config
   public BigInteger getConfigurationBigInt(InjectionPoint ip) {
      return new BigInteger(getConfigurationValue(ip));
   }

   /**
    * Injects an int configuration value.
    *
    * @param ip the injecting class
    * @return int configuration
    */
   @Config
   @Produces
   public int getConfigurationInt(InjectionPoint ip) {
      try {
         return Integer.parseInt(getConfigurationValue(ip));
      } catch (NumberFormatException e) {
         throw new RuntimeException("Invalid format for " + ip.getAnnotated().getAnnotation(Config.class).key() + "=" + getValue(ip.getAnnotated().getAnnotation(Config.class).key()), e);
      }
   }

   /**
    * Injects a boolean configuration value.
    *
    * @param ip the injecting class
    * @return boolean configuration
    */
   @Config
   @Produces
   public boolean getConfigurationBool(InjectionPoint ip) {

      if (getValue(getConfigurationValue(ip)).equalsIgnoreCase("false")) {
         return Boolean.FALSE;
      }

      return Boolean.TRUE;
   }

   /**
    * Retrieves the configuration value for underlying service.
    *
    * @param configKey configuration key
    * @return configuration value
    */
   protected String getValue(String configKey) {
      if (configKey == null || configKey.isEmpty()) {
         throw new IllegalArgumentException("Configuration key is empty.");
      }

      final long now = System.currentTimeMillis();

      if (cacheTimeout > 0 && now - timestamp > cacheTimeout) {
         loadPropertiesFromFile();
      }

      return configurationProperties.getProperty(configKey);
   }

   @PostConstruct
   private void init() {

      cacheTimeout = Integer.parseInt(System.getProperty("system.config.cache.timeout")) * 60 * 1000;
      loadPropertiesFromFile();
   }

   private void loadPropertiesFromFile() {
      try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(System.getProperty("configuration.properties"));) {
         configurationProperties.load(is);
      } catch (NullPointerException | IOException e) {
      }
   }
}

{'fraction_non_alphanumeric': 0.1078899687036732, 'fraction_numerical': 0.0011530225662987976, 'mean_word_length': 3.2372644801116537, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '2371129', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1642, 'n_words': 420}