Skip to content

Commit

Permalink
[ issue #58 ] configuration files namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Sep 25, 2018
1 parent 5f8fa20 commit 7dfcd1e
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.sease.rre.search.api.QueryOrSearchResponse;
import io.sease.rre.search.api.SearchPlatform;
import io.sease.rre.search.api.UnableToLoadDataException;
Expand All @@ -26,8 +27,10 @@
import java.io.IOException;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
Expand All @@ -54,6 +57,8 @@ private static class RRENode extends Node {
private Node elasticsearch;
private final ObjectMapper mapper = new ObjectMapper();

private File nodeConfigFolder;

@Override
public void beforeStart(final Map<String, Object> configuration) {
final File logsFolder = new File("target/elasticsearch/logs");
Expand All @@ -65,6 +70,9 @@ public void beforeStart(final Map<String, Object> configuration) {
logsFolder.mkdirs();
dataFolder.mkdirs();

nodeConfigFolder = new File((String) configuration.get("path.home"), "config");
nodeConfigFolder.mkdir();

final Settings.Builder settings = Settings.builder()
.put("path.home", (String) configuration.get("path.home"))
.put("transport.type", "netty4")
Expand All @@ -91,6 +99,17 @@ public void load(final File data, File indexShapeFile, String indexName) {
proxy.admin().indices().delete(deleteIndexRequest(indexName)).actionGet();
}

List<JsonNode> protectedKeywordsPaths = esconfig.findParents("keywords_path");
List<JsonNode> synonymsPaths = esconfig.findParents("synonyms_path");
List<JsonNode> stopwordsPaths = esconfig.findParents("stopwords_path");

final File configurationFolder = indexShapeFile.getParentFile();
final String namespace = configurationFolder.getName();

insertNamespaces(protectedKeywordsPaths, "keywords_path", configurationFolder, namespace);
insertNamespaces(synonymsPaths, "synonyms_path", configurationFolder, namespace);
insertNamespaces(stopwordsPaths, "stopwords_path", configurationFolder, namespace);

final CreateIndexRequest request = createIndexRequest(indexName)
.settings(Settings.builder().loadFromSource(mapper.writeValueAsString(esconfig.get("settings")), XContentType.JSON).build())
.mapping("doc", mapper.writeValueAsString(esconfig.get("mappings")), XContentType.JSON);
Expand Down Expand Up @@ -209,4 +228,23 @@ private List<Class<? extends Plugin>> plugins(final Map<String, Object> configur

return plugins;
}

private void insertNamespaces(final List<JsonNode> parents, final String pathAttributeName, final File configurationFolder, final String namespace) {
parents.forEach(parentNode -> {
final String path = parentNode.get(pathAttributeName).asText();
final File declaredPath = new File(configurationFolder, path);

final String originalFilename = declaredPath.getName();
final String namespacedFilename = namespace + "_" + declaredPath.getName();

final File targetPath = new File(nodeConfigFolder, namespacedFilename);

try {
Files.copy(declaredPath.toPath(), targetPath.toPath(), StandardCopyOption.REPLACE_EXISTING);
((ObjectNode)parentNode).put(pathAttributeName, path.replace(originalFilename, namespacedFilename));
} catch (IOException exception) {
throw new RuntimeException("Unable to deal with configuration file " + declaredPath.getAbsolutePath() + ". Target path was " + targetPath.getAbsolutePath());
}
});
}
}

0 comments on commit 7dfcd1e

Please sign in to comment.