Skip to content

Commit

Permalink
Ensure that if System property OPTONS-FILE-ENCODING is specified, tha…
Browse files Browse the repository at this point in the history
…t it would be applied.
  • Loading branch information
hansenmc committed Mar 20, 2024
1 parent 8d22398 commit b9470aa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/main/java/com/marklogic/developer/corb/AbstractManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@

import com.marklogic.developer.corb.util.NumberUtils;
import com.marklogic.developer.corb.util.StringUtils;

import static com.marklogic.developer.corb.util.StringUtils.buildModulePath;
import static com.marklogic.developer.corb.util.StringUtils.isBlank;
import static com.marklogic.developer.corb.util.StringUtils.isInlineModule;
import static com.marklogic.developer.corb.util.StringUtils.isInlineOrAdhoc;
import static com.marklogic.developer.corb.util.StringUtils.isNotBlank;
import static com.marklogic.developer.corb.util.StringUtils.trim;

import com.marklogic.xcc.AdhocQuery;
import com.marklogic.xcc.ContentSource;
import com.marklogic.xcc.Request;
Expand Down Expand Up @@ -52,11 +58,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.marklogic.developer.corb.util.StringUtils.buildModulePath;
import static com.marklogic.developer.corb.util.StringUtils.isBlank;
import static com.marklogic.developer.corb.util.StringUtils.isInlineModule;
import static com.marklogic.developer.corb.util.StringUtils.isInlineOrAdhoc;

public abstract class AbstractManager {

//Obtain the version from META-INF/MANIFEST.MF Implementation-Version attribute
Expand Down Expand Up @@ -87,33 +88,37 @@ public static Properties loadPropertiesFile(String filename) throws IOException
}

public static Properties loadPropertiesFile(String filename, boolean excIfNotFound) throws IOException {
Properties props = new Properties();
return loadPropertiesFile(filename, excIfNotFound, props);
Properties properties = new Properties();
return loadPropertiesFile(filename, excIfNotFound, properties);
}

protected static Properties loadPropertiesFile(String filename, boolean exceptionIfNotFound, Properties props) throws IOException {
protected static Properties loadPropertiesFile(String filename, boolean exceptionIfNotFound, Properties properties) throws IOException {
String name = trim(filename);
if (isNotBlank(name)) {
Charset charset = Charset.defaultCharset(); //Charset.forName("ASCII");//
loadPropertiesFile(filename, charset, exceptionIfNotFound, props);
Charset charset = Charset.defaultCharset();
String encoding = System.getProperty(OPTIONS_FILE_ENCODING);
if (encoding != null) {
charset = Charset.forName(encoding);
}
loadPropertiesFile(name, charset, exceptionIfNotFound, properties);
}
return props;
return properties;
}

private static void loadPropertiesFile(String name, Charset encoding, boolean exceptionIfNotFound, @NotNull Properties properties) throws IOException {
try (InputStream inputStream = Manager.class.getResourceAsStream('/' + name)) {
private static void loadPropertiesFile(String propertiesFilename, Charset encoding, boolean exceptionIfNotFound, @NotNull Properties properties) throws IOException {
try (InputStream inputStream = Manager.class.getResourceAsStream('/' + propertiesFilename)) {
if (inputStream != null) {
LOG.log(INFO, () -> MessageFormat.format("Loading {0} from classpath", name));
loadPropertiesFile(inputStream, encoding, name, exceptionIfNotFound, properties);
LOG.log(INFO, () -> MessageFormat.format("Loading {0} from classpath", propertiesFilename));
loadPropertiesFile(inputStream, encoding, propertiesFilename, exceptionIfNotFound, properties);
} else {
File f = new File(name);
File f = new File(propertiesFilename);
if (f.exists() && !f.isDirectory()) {
LOG.log(INFO, "Loading {0} from filesystem", name);
LOG.log(INFO, "Loading {0} from filesystem", propertiesFilename);
try (FileInputStream fileInputStream = new FileInputStream(f)) {
loadPropertiesFile(fileInputStream, encoding, name, exceptionIfNotFound, properties);
loadPropertiesFile(fileInputStream, encoding, propertiesFilename, exceptionIfNotFound, properties);
}
} else if (exceptionIfNotFound) {
throw new IllegalStateException("Unable to load properties file " + name);
throw new IllegalStateException("Unable to load properties file " + propertiesFilename);
}
}
}
Expand Down Expand Up @@ -217,7 +222,7 @@ public void initProperties(Properties props) throws CorbException {

public void initPropertiesFromOptionsFile() throws IOException {
String propsFileName = System.getProperty(OPTIONS_FILE);
loadPropertiesFile(propsFileName, true, properties);
loadPropertiesFile(propsFileName,true, properties);
}

public void init(String... args) throws CorbException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ public void testLoadPropertiesFileEncoding() {
}
}

@Test
public void testLoadPropertiesFileEncodingSystemProperty() {
Properties properties = new Properties();
System.setProperty(OPTIONS_FILE_ENCODING, "ascii");
try {
Properties result = AbstractManager.loadPropertiesFile(PROPERTIES_FILE_PATH, true, properties);
assertNotEquals("Verify that UTF8 value gets mangled when read as ascii","コンニチハ", result.getProperty("URIS-MODULE.greeting"));
System.setProperty(OPTIONS_FILE_ENCODING, "utf8");
result = AbstractManager.loadPropertiesFile(PROPERTIES_FILE_PATH, true, properties);
assertEquals("Value is read correctly when read as UTF8 encoded","コンニチハ", result.getProperty("URIS-MODULE.greeting"));
} catch(IOException ex) {
fail();
}
System.clearProperty(OPTIONS_FILE_ENCODING);
}

@Test
public void testLoadPropertiesFileStringBoolean() {
try {
Expand Down

0 comments on commit b9470aa

Please sign in to comment.