All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thematchbox.river.MatchBoxSettings Maven / Gradle / Ivy

Go to download

This project contains an abstract implementation of an ElasticSearch River and is used as a basis for custom river implementations.

There is a newer version: 1.1.3
Show newest version
package com.thematchbox.river;

/* Copyright 2015 theMatchBox

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import org.elasticsearch.common.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.*;

public abstract class MatchBoxSettings extends Properties {

    public static final Logger logger = LoggerFactory.getLogger(MatchBoxSettings.class);

    protected abstract String getConfigFileName();

    protected MatchBoxSettings(Settings settings) {
        String esHomePath = settings.get("path.home");
        if (esHomePath == null) {
            throw new IllegalStateException("Could not load MatchBoxSettings; ElasticSearch home path not found.");
        }

        File folder = new File(esHomePath);
        if (!folder.exists()) {
            throw new IllegalStateException("Could not load MatchBoxSettings; ElasticSearch home path " + esHomePath + " does not exist.");
        }

        ArrayList propertiesFiles = new ArrayList<>();
        findPropertiesFile(propertiesFiles, folder);
        if (propertiesFiles.isEmpty()) {
            throw new IllegalStateException("Could not load MatchBoxSettings; Settings file " + getConfigFileName() + " was not found.");
        }

        File file = propertiesFiles.get(0);
        if (propertiesFiles.size() > 1) {
            logger.warn("Multiple theMatchBox properties files found:");
            for (File propertiesFile : propertiesFiles) {
                logger.warn("Properties file: " + propertiesFile.getPath());
            }
            logger.warn("Using theMatchBox properties file " + file.getPath());
        } else {
            logger.info("Using theMatchBox properties file " + file.getPath());
        }

        try (InputStream inputStream = new FileInputStream(file)) {
            load(inputStream);
        } catch (IOException e) {
            logger.warn("Could not find settings file %s. No matchbox settings loaded.", getConfigFileName());
        }
    }

    public MatchBoxSettings(String resource) throws IOException {
        try (InputStream inputStream = MatchBoxSettings.class.getResourceAsStream(resource)) {
            load(inputStream);
        }
    }

    @SuppressWarnings("UnusedDeclaration")
    public List getNextKeyParts(String keyPrefix) {
        int nrOfParts = keyPrefix.split("\\.").length;
        Set entries = new HashSet<>();
        for (Object o : keySet()) {
            String key = (String) o;
            if (key.startsWith(keyPrefix)) {
                String[] split = key.split("\\.");
                entries.add(split[nrOfParts]);
            }
        }

        List list = new ArrayList<>(entries);
        Collections.sort(list);
        return list;
    }

    private void findPropertiesFile(List propertiesFiles, File folder) {
        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    String name = pathname.getName().toLowerCase();
                    return pathname.isDirectory() || name.equals(getConfigFileName());
                }
            });
            List folders = new ArrayList<>();
            for (File childFile : files) {
                if (childFile.isDirectory()) {
                    folders.add(childFile);
                } else {
                    propertiesFiles.add(childFile);
                }
            }
            for (File childFolder : folders) {
                findPropertiesFile(propertiesFiles, childFolder);
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy