Skip to content

Instantly share code, notes, and snippets.

@reikje
Created July 14, 2015 15:55
Show Gist options
  • Save reikje/cdc406c681cc9215562c to your computer and use it in GitHub Desktop.
Save reikje/cdc406c681cc9215562c to your computer and use it in GitHub Desktop.
package me.rschatz;
import com.glassdoor.planout4j.config.Planout4jConfigBackend;
import com.typesafe.config.Config;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.google.common.io.Files.getNameWithoutExtension;
public class ClasspathBackend implements Planout4jConfigBackend {
/**
* {@link ClasspathBackend} does not need to be configured.
* @param config any {@link Config}
*/
@Override
public void configure(Config config) { }
/**
* Loads and returns a mapping of namespace names to json based on files in the {@code planout} resource directory.
* @return Map
*/
@Override
public Map<String, String> loadAll() {
final Map<String, String> name2Content = new HashMap<>();
for (String jsonFile : getJsonResourceFiles()) {
final String namespace = getNameWithoutExtension(jsonFile);
final String json = readJson("planout/" + jsonFile);
name2Content.put(namespace, json);
}
return name2Content;
}
private String readJson(final String jsonFileName) {
try {
return IOUtils.toString(ClasspathBackend.class.getClassLoader().getResourceAsStream(jsonFileName));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
private Iterable<String> getJsonResourceFiles() {
final List<String> files = new ArrayList<>();
try {
for (String fileName : IOUtils.readLines(
ClasspathBackend.class.getClassLoader().getResourceAsStream("planout/"), Charsets.UTF_8)) {
if (fileName.endsWith(".json")) {
files.add(fileName);
}
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
return files;
}
/**
* This backend does not persist anything.
* @param namespace2Content - ??
*/
@Override
public void persist(Map<String, String> namespace2Content) {
throw new UnsupportedOperationException("Not implemented!");
}
@Override
public String persistenceLayer() {
return "CLASSPATH";
}
@Override
public String persistenceDestination() {
return this.getClass().getName();
}
// for test purposes only
public static void main(String[] args) {
final Planout4jConfigBackend backend = new ClasspathBackend();
final Map<String, String> namespaces = backend.loadAll();
System.out.println(namespaces);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment