Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions examples/snippets/src/main/java/utils/Settings.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package utils;

import com.sinch.sdk.core.utils.StringUtil;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class Settings {

private static final Logger LOGGER = Logger.getLogger(Settings.class.getName());

private static final String CONFIG_FILE = "config.properties";
private static final String LOGGING_CONFIG_FILE = "logging.properties";

private static final Properties properties;

static {
initializeLogger();
properties = new Properties();
try {
// load a properties file from class path, inside static method
properties.load(Settings.class.getClassLoader().getResourceAsStream("config.properties"));
properties.load(Settings.class.getClassLoader().getResourceAsStream(CONFIG_FILE));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: instead of throwing a NPE when the file is not found, you may check if the inputStream is not null before loading the properties and log that the config file is not found if null

} catch (Exception ioe) {
// ignore exception: properties file is just an helper
LOGGER.config(
String.format(
"Error reading '%s' configuration file: %s", CONFIG_FILE, ioe.getMessage()));
}
}

Expand Down Expand Up @@ -70,4 +82,17 @@ public static Optional<String> getConversationRegion() {
public static Optional<String> getPhoneNumber() {
return get("SINCH_PHONE_NUMBER");
}

public static void initializeLogger() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made private

try (InputStream logConfigInputStream =
Settings.class.getClassLoader().getResourceAsStream(LOGGING_CONFIG_FILE)) {
if (logConfigInputStream != null) {
LogManager.getLogManager().readConfiguration(logConfigInputStream);
}
} catch (IOException ioe) {
LOGGER.config(
String.format(
"Error reading '%s' configuration file: %s", LOGGING_CONFIG_FILE, ioe.getMessage()));
Comment on lines +93 to +95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using the logger to log an error about the logger that may have not been initialized. Is it ok to do that, or the error message will be invisible?

}
}
}
Loading