<commit_before>/*
 * The MIT License
 *
 * Copyright 2015 <NAME> (<EMAIL>).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package eu.agilejava.snoop.scan;

import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
import eu.agilejava.snoop.SnoopConfigurationException;
import eu.agilejava.snoop.client.SnoopConfig;
import java.io.IOException;
import java.net.URI;
import java.util.Calendar;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.ScheduleExpression;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

/**
 * Registers with Snoop and gives heartbeats every 10 second.
 *
 * @author <NAME> (<EMAIL>)
 */
@ClientEndpoint
@Singleton
@Startup
public class SnoopClient {

   private static final Logger LOGGER = Logger.getLogger("eu.agilejava.snoop");
   private static final String DEFAULT_SERVICE_HOST = "localhost:8080/snoop-service/";
   private static final String REGISTER_ENDPOINT = "snoop";
   private static final String STATUS_ENDPOINT = "snoopstatus/";

   private String serviceUrl;
   private final SnoopConfig applicationConfig = new SnoopConfig();

   @Resource
   private TimerService timerService;

   @PostConstruct
   private void init() {

      LOGGER.config("Checking if snoop is enabled");

      if (SnoopExtensionHelper.isSnoopEnabled()) {

         try {
            readConfiguration();

            LOGGER.config(() -> "Registering " + applicationConfig.getApplicationName());
            register(applicationConfig.getApplicationName());

         } catch (SnoopConfigurationException e) {
            LOGGER.severe(() -> "Snoop is enabled but not configured properly: " + e.getMessage());
         }

      } else {
         LOGGER.config("Snoop is not enabled. Use @EnableSnoopClient!");
      }
   }

   public void register(final String clientId) {

      sendMessage(REGISTER_ENDPOINT, applicationConfig.toJSON());

      ScheduleExpression schedule = new ScheduleExpression();
      schedule.second("*/10").minute("*").hour("*").start(Calendar.getInstance().getTime());

      TimerConfig config = new TimerConfig();
      config.setPersistent(false);

      Timer timer = timerService.createCalendarTimer(schedule, config);

      LOGGER.config(() -> timer.getSchedule().toString());
   }

   /**
    * Handles incoming message.
    *
    * @param session The WebSocket session
    * @param message The message
    */
   @OnMessage
   public void onMessage(Session session, String message) {
      LOGGER.config(() -> "Message: " + message);
      sendMessage(STATUS_ENDPOINT + applicationConfig.getApplicationName(), applicationConfig.toJSON());
   }

   @Timeout
   public void health(Timer timer) {
      LOGGER.config(() -> "health update: " + Calendar.getInstance().getTime());
      LOGGER.config(() -> "Next: " + timer.getNextTimeout());
      sendMessage(STATUS_ENDPOINT + applicationConfig.getApplicationName(), applicationConfig.toJSON());
   }

   /**
    * Sends message to the WebSocket server.
    *
    * @param endpoint The server endpoint
    * @param msg The message
    * @return a return message
    */
   private String sendMessage(String endpoint, String msg) {

      LOGGER.config(() -> "Sending message: " + msg);

      String returnValue = "-1";
      try {
         WebSocketContainer container = ContainerProvider.getWebSocketContainer();
         String uri = serviceUrl + endpoint;
         Session session = container.connectToServer(this, URI.create(uri));
         session.getBasicRemote().sendText(msg != null ? msg : "");
         returnValue = session.getId();

      } catch (DeploymentException | IOException ex) {
         LOGGER.warning(ex.getMessage());
      }

      return returnValue;
   }

   @PreDestroy
   private void deregister() {

      LOGGER.config(() -> "Deregistering " + applicationConfig.getApplicationName());
      sendMessage(STATUS_ENDPOINT + applicationConfig.getApplicationName(), null);
   }

   private void readConfiguration() throws SnoopConfigurationException {

      Yaml yaml = new Yaml();
      Map<String, Object> props = (Map<String, Object>) yaml.load(this.getClass().getResourceAsStream("/application.yml"));

      Map<String, Object> snoopConfig = (Map<String, Object>) props.get("snoop");

      applicationConfig.setApplicationName(SnoopExtensionHelper.getApplicationName());
      final String host = readProperty("host", snoopConfig);
      final String port = readProperty("port", snoopConfig);
      applicationConfig.setApplicationHome(host + ":" + port);
      applicationConfig.setApplicationServiceRoot(readProperty("serviceRoot", snoopConfig));

      LOGGER.config(() -> "application config: " + applicationConfig.toJSON());

      serviceUrl = "ws://" + readProperty("serviceHost", snoopConfig);
   }

   private String readProperty(final String key, Map<String, Object> snoopConfig) {

      String property = Optional.ofNullable(System.getenv(applicationConfig.getApplicationName() + "." + key))
              .orElseGet(() -> {
                 String p = Optional.ofNullable(snoopConfig.get(key))
                 .orElseThrow(() -> {
                    return new SnoopConfigurationException(key + " must be configured either in application.yml or as env parameter");
                 })
                 .toString();
                 return p;
              });

      return property;
   }
}
<commit_msg>And now application.yml is made optional, hence really closing #8<commit_after>/*
 * The MIT License
 *
 * Copyright 2015 <NAME> (<EMAIL>).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package eu.agilejava.snoop.scan;

import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
import com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException;
import eu.agilejava.snoop.SnoopConfigurationException;
import eu.agilejava.snoop.client.SnoopConfig;
import java.io.IOException;
import java.net.URI;
import java.util.Calendar;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.ScheduleExpression;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

/**
 * Registers with Snoop and gives heartbeats every 10 second.
 *
 * @author <NAME> (<EMAIL>)
 */
@ClientEndpoint
@Singleton
@Startup
public class SnoopClient {

   private static final Logger LOGGER = Logger.getLogger("eu.agilejava.snoop");
   private static final String DEFAULT_SERVICE_HOST = "localhost:8080/snoop-service/";
   private static final String REGISTER_ENDPOINT = "snoop";
   private static final String STATUS_ENDPOINT = "snoopstatus/";

   private String serviceUrl;
   private final SnoopConfig applicationConfig = new SnoopConfig();

   @Resource
   private TimerService timerService;

   @PostConstruct
   private void init() {

      LOGGER.config("Checking if snoop is enabled");

      if (SnoopExtensionHelper.isSnoopEnabled()) {

         try {
            readConfiguration();

            LOGGER.config(() -> "Registering " + applicationConfig.getApplicationName());
            register(applicationConfig.getApplicationName());

         } catch (SnoopConfigurationException e) {
            LOGGER.severe(() -> "Snoop is enabled but not configured properly: " + e.getMessage());
         }

      } else {
         LOGGER.config("Snoop is not enabled. Use @EnableSnoopClient!");
      }
   }

   public void register(final String clientId) {

      sendMessage(REGISTER_ENDPOINT, applicationConfig.toJSON());

      ScheduleExpression schedule = new ScheduleExpression();
      schedule.second("*/10").minute("*").hour("*").start(Calendar.getInstance().getTime());

      TimerConfig config = new TimerConfig();
      config.setPersistent(false);

      Timer timer = timerService.createCalendarTimer(schedule, config);

      LOGGER.config(() -> timer.getSchedule().toString());
   }

   /**
    * Handles incoming message.
    *
    * @param session The WebSocket session
    * @param message The message
    */
   @OnMessage
   public void onMessage(Session session, String message) {
      LOGGER.config(() -> "Message: " + message);
      sendMessage(STATUS_ENDPOINT + applicationConfig.getApplicationName(), applicationConfig.toJSON());
   }

   @Timeout
   public void health(Timer timer) {
      LOGGER.config(() -> "health update: " + Calendar.getInstance().getTime());
      LOGGER.config(() -> "Next: " + timer.getNextTimeout());
      sendMessage(STATUS_ENDPOINT + applicationConfig.getApplicationName(), applicationConfig.toJSON());
   }

   /**
    * Sends message to the WebSocket server.
    *
    * @param endpoint The server endpoint
    * @param msg The message
    * @return a return message
    */
   private String sendMessage(String endpoint, String msg) {

      LOGGER.config(() -> "Sending message: " + msg);

      String returnValue = "-1";
      try {
         WebSocketContainer container = ContainerProvider.getWebSocketContainer();
         String uri = serviceUrl + endpoint;
         Session session = container.connectToServer(this, URI.create(uri));
         session.getBasicRemote().sendText(msg != null ? msg : "");
         returnValue = session.getId();

      } catch (DeploymentException | IOException ex) {
         LOGGER.warning(ex.getMessage());
      }

      return returnValue;
   }

   @PreDestroy
   private void deregister() {

      LOGGER.config(() -> "Deregistering " + applicationConfig.getApplicationName());
      sendMessage(STATUS_ENDPOINT + applicationConfig.getApplicationName(), null);
   }

   private void readConfiguration() throws SnoopConfigurationException {

      try {
         Yaml yaml = new Yaml();
         Map<String, Object> props = (Map<String, Object>) yaml.load(this.getClass().getResourceAsStream("/application.yml"));

         Map<String, Object> snoopConfig = (Map<String, Object>) props.get("snoop");

         applicationConfig.setApplicationName(SnoopExtensionHelper.getApplicationName());
         final String host = readProperty("host", snoopConfig);
         final String port = readProperty("port", snoopConfig);
         applicationConfig.setApplicationHome(host + ":" + port);
         applicationConfig.setApplicationServiceRoot(readProperty("serviceRoot", snoopConfig));

         LOGGER.config(() -> "application config: " + applicationConfig.toJSON());

         serviceUrl = "ws://" + readProperty("serviceHost", snoopConfig);

      } catch (YAMLException e) {
         throw new SnoopConfigurationException(e);
      }
   }

   private String readProperty(final String key, Map<String, Object> snoopConfig) {

      String property = Optional.ofNullable(System.getenv(applicationConfig.getApplicationName() + "." + key))
              .orElseGet(() -> {
                 String p = Optional.ofNullable(snoopConfig.get(key))
                 .orElseThrow(() -> {
                    return new SnoopConfigurationException(key + " must be configured either in application.yml or as env parameter");
                 })
                 .toString();
                 return p;
              });

      return property;
   }
}

{'fraction_non_alphanumeric': 0.10568295114656032, 'fraction_numerical': 0.0019228030195128898, 'mean_word_length': 3.522705314009662, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '5998592', 'n_tokens_mistral': 3814, 'n_tokens_neox': 3605, 'n_words': 1060}