Last active
January 31, 2019 10:01
-
-
Save alexvictoor/0a72f841821a765784fc to your computer and use it in GitHub Desktop.
Playing with jakarta commons configuration, reading external files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.github.alexvictoor</groupId> | |
<artifactId>custom-conf</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>custom-conf</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>commons-configuration</groupId> | |
<artifactId>commons-configuration</artifactId> | |
<version>1.10</version> | |
</dependency> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>18.0</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.11</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.assertj</groupId> | |
<artifactId>assertj-core</artifactId> | |
<version>1.7.0</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.alexvictoor; | |
import com.google.common.base.Throwables; | |
import com.google.common.io.Files; | |
import org.apache.commons.configuration.ConfigurationException; | |
import org.apache.commons.configuration.PropertiesConfiguration; | |
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.Reader; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.net.URL; | |
import java.nio.charset.Charset; | |
public class SingleExternalFileConfiguration extends PropertiesConfiguration{ | |
private File file; | |
public SingleExternalFileConfiguration(URL url) throws ConfigurationException, URISyntaxException { | |
super(url); | |
setReloadingStrategy(new CustomReloadStrategy()); | |
} | |
@Override | |
public synchronized void load(Reader in) throws ConfigurationException { | |
super.load(in); | |
String fileKey = getKeys().next(); | |
String filePath = getString(fileKey); | |
try { | |
URI uri = new URI(getBasePath() + filePath); | |
file = new File(uri); | |
String content = Files.toString(file, Charset.defaultCharset()); | |
addProperty("content." + fileKey, content); | |
} catch (IOException e) { | |
throw Throwables.propagate(e); | |
} catch (URISyntaxException e) { | |
throw Throwables.propagate(e); | |
} | |
} | |
public class CustomReloadStrategy extends FileChangedReloadingStrategy { | |
@Override | |
protected File getFile() { | |
return file; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.alexvictoor; | |
import org.apache.commons.configuration.Configuration; | |
import org.junit.Test; | |
import java.net.URL; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class SingleExternalFileConfigurationTest { | |
@Test | |
public void should_load_file() throws Exception { | |
// given | |
URL url = getClass().getResource("/conf.properties"); | |
Configuration configuration = new SingleExternalFileConfiguration(url); | |
// when | |
String content = configuration.getString("content.my.file.conf"); | |
// then | |
assertThat(content).isEqualTo("Coucou"); | |
/* | |
Integer lock = new Integer(0); | |
synchronized (lock) { | |
for (int i=0; i<100; i++) { | |
lock.wait(1000); | |
System.out.println(configuration.getString("content.my.file.conf")); | |
} | |
}*/ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my.file.conf=toto.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Coucou |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment