package net.explorviz.shared.server.helper;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Utility class to read configuration properties. Use the {@link Config} instead.
 */
public final class PropertyHelper {

  private static final Logger LOGGER = LoggerFactory.getLogger(PropertyHelper.class);
  private static final String PROPERTIES_FILENAME = "explorviz.properties";
  private static String propertiesPath;

  private static final Properties PROP = new Properties();

  private PropertyHelper() {
    // don't instantiate
  }

  static {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();

    try {
      propertiesPath = loader.getResource(PROPERTIES_FILENAME).getFile();
      PROP.load(loader.getResourceAsStream(PROPERTIES_FILENAME));
    } catch (final IOException e) {
      LOGGER.error(
          "Couldn't load properties file. Is WEB-INF/explorviz.properties a valid file?. Exception: {}", // NOCS
          e.getMessage());
    }
  }

  public static int getIntegerProperty(final String propName) {
    return Integer.parseInt(getStringProperty(propName));
  }

  public static String getStringProperty(final String propName) {
    return String.valueOf(PROP.get(propName));
  }

  public static boolean getBooleanProperty(final String propName) {
    return Boolean.parseBoolean(getStringProperty(propName));
  }

  /**
   * Set a boolean property inside the explorviz.properties file.
   *
   * @param propName - Property key
   * @param value - Property value. String or string-castable object.
   * @throws FileNotFoundException - Thrown if explorviz.properties was not found.
   * @throws IOException - Thrown if cast to String did not work.
   */
  public static void setBooleanProperty(final String propName, final boolean value)
      throws FileNotFoundException, IOException {
    PROP.setProperty(propName, String.valueOf(value));

    try (OutputStream out = Files.newOutputStream(Paths.get(propertiesPath))) {
      PROP.store(out, null);
    }
  }

}

@@ -133,16 +133,37 @@
 Stream;%0A
+import java.net.URL;%0A
 import j
@@ -235,24 +235,73 @@
 Properties;%0A
+import javax.ws.rs.InternalServerErrorException;%0A
 import org.s
@@ -658,29 +658,36 @@
 tic 
+final 
 String 
-propertiesPath
+PROPERTIES_PATH
 ;%0A%0A 
@@ -745,16 +745,149 @@
 ies();%0A%0A
+  private static final String ERROR_MESSAGE =%0A      %22Couldn't load properties file. Is WEB-INF/explorviz.properties a valid file?%22;%0A%0A
   privat
@@ -1036,34 +1036,200 @@
     
-try %7B%0A      propertiesPath
+final URL urlToProperties = loader.getResource(PROPERTIES_FILENAME);%0A%0A    if (urlToProperties == null) %7B%0A      throw new InternalServerErrorException(ERROR_MESSAGE);%0A    %7D%0A%0A    PROPERTIES_PATH
  = l
@@ -1277,16 +1277,27 @@
 tFile();
+%0A%0A    try %7B
 %0A      P
@@ -1401,159 +1401,63 @@
     
-LOGGER.error(%0A          %22Couldn't load properties file. Is WEB-INF/explorviz.properties a valid file?. Exception: %7B%7D%22, // NOCS%0A          e.getMessage()
+throw new InternalServerErrorException(ERROR_MESSAGE, e
 );%0A 
@@ -2438,22 +2438,23 @@
 get(
-propertiesPath
+PROPERTIES_PATH
 ))) 

handle potential exception in PropertyHelper