Adds "if file exists" check functionality on setting configurable providers and fixes bug not closing input stream when error occures. (Issue #183)

This commit is contained in:
gkolokythas 2020-01-03 12:55:12 +02:00
parent b6b2c93096
commit 290aa5260c
2 changed files with 27 additions and 15 deletions

View File

@ -96,15 +96,20 @@ public class DevelConfigLoader implements ConfigLoader {
}
}
public void setConfigurableProviders() {
private void setConfigurableProviders() {
String filePath = environment.getProperty("configuration.configurable_login_providers");
String current = null;
String current;
InputStream is = null;
try {
current = new java.io.File(".").getCanonicalPath();
File tempFile = new File(current + filePath);
if (tempFile.exists()) {
is = new URL("file:///" + current + filePath).openStream();
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.configurableProviders = mapper.readValue(is, ConfigurableProviders.class);
} else {
this.configurableProviders = new ConfigurableProviders();
}
} catch (IOException e) {
e.printStackTrace();
} finally {

View File

@ -11,10 +11,7 @@ import org.springframework.stereotype.Service;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.nio.file.Paths;
import java.util.LinkedList;
@ -88,16 +85,26 @@ public class ProductionConfigLoader implements ConfigLoader {
}
}
public void setConfigurableProviders() {
private void setConfigurableProviders() {
String filePath = environment.getProperty("configuration.configurable_login_providers");
String current = null;
InputStream is = null;
try {
current = new java.io.File(".").getCanonicalPath();
InputStream is = new URL(Paths.get(filePath).toUri().toURL().toString()).openStream();
File tempFile = new File(filePath);
if (tempFile.exists()) {
is = new URL(Paths.get(filePath).toUri().toURL().toString()).openStream();
ObjectMapper objectMapper = new ObjectMapper();
this.configurableProviders = objectMapper.readValue(is, ConfigurableProviders.class);
} else {
this.configurableProviders = new ConfigurableProviders();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
System.out.println("Warning: Could not close a stream after reading from file: " + filePath);
}
}
}