Java Code Examples for org.elasticsearch.common.io.PathUtils#get()

The following examples show how to use org.elasticsearch.common.io.PathUtils#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Dictionary.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 6 votes vote down vote up
public List<String> getExtStopWordDictionarys() {
	List<String> extStopWordDictFiles = new ArrayList<String>(2);
	String extStopWordDictCfg = getProperty(EXT_STOP);
	if (extStopWordDictCfg != null) {

		String[] filePaths = extStopWordDictCfg.split(";");
		for (String filePath : filePaths) {
			if (filePath != null && !"".equals(filePath.trim())) {
				Path file = PathUtils.get(filePath.trim());
				extStopWordDictFiles.add(file.toString());

			}
		}
	}
	return extStopWordDictFiles;
}
 
Example 2
Source File: Dictionary.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 6 votes vote down vote up
public List<String> getExtDictionarys() {
	List<String> extDictFiles = new ArrayList<String>(2);
	String extDictCfg = getProperty(EXT_DICT);
	if (extDictCfg != null) {

		String[] filePaths = extDictCfg.split(";");
		for (String filePath : filePaths) {
			if (filePath != null && !"".equals(filePath.trim())) {
				Path file = PathUtils.get(filePath.trim());
				extDictFiles.add(file.toString());

			}
		}
	}
	return extDictFiles;
}
 
Example 3
Source File: MetaDataCreateIndexService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
List<String> getIndexSettingsValidationErrors(Settings settings) {
    String customPath = settings.get(IndexMetaData.SETTING_DATA_PATH, null);
    List<String> validationErrors = new ArrayList<>();
    if (customPath != null && env.sharedDataFile() == null) {
        validationErrors.add("path.shared_data must be set in order to use custom data paths");
    } else if (customPath != null) {
        Path resolvedPath = PathUtils.get(new Path[]{env.sharedDataFile()}, customPath);
        if (resolvedPath == null) {
            validationErrors.add("custom path [" + customPath + "] is not a sub-path of path.shared_data [" + env.sharedDataFile() + "]");
        }
    }
    Integer number_of_primaries = settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, null);
    Integer number_of_replicas = settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, null);
    if (number_of_primaries != null && number_of_primaries <= 0) {
        validationErrors.add("index must have 1 or more primary shards");
    }
    if (number_of_replicas != null && number_of_replicas < 0) {
        validationErrors.add("index must have 0 or more replica shards");
    }
    return validationErrors;
}
 
Example 4
Source File: Security.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/** Adds access to classpath jars/classes for jar hell scan, etc */
@SuppressForbidden(reason = "accesses fully qualified URLs to configure security")
static void addClasspathPermissions(Permissions policy) throws IOException {
    // add permissions to everything in classpath
    // really it should be covered by lib/, but there could be e.g. agents or similar configured)
    for (URL url : JarHell.parseClassPath()) {
        Path path;
        try {
            path = PathUtils.get(url.toURI());
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        // resource itself
        policy.add(new FilePermission(path.toString(), "read,readlink"));
        // classes underneath
        if (Files.isDirectory(path)) {
            policy.add(new FilePermission(path.toString() + path.getFileSystem().getSeparator() + "-", "read,readlink"));
        }
    }
}
 
Example 5
Source File: InternalSettingsPreparerTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomConfigMustNotContainSettingsFromDefaultCrateYml() throws Exception {
    HashMap<String, String> settings = new HashMap<>();
    Path home = PathUtils.get(getClass().getResource(".").toURI());
    settings.put("path.home", home.toString());
    Path config = PathUtils.get(getClass().getResource("config_custom").toURI());
    settings.put("path.conf", config.toString());
    Settings finalSettings = InternalSettingsPreparer
        .prepareEnvironment(Settings.EMPTY, settings, config, () -> "node1").settings();
    // Values from crate.yml
    assertThat(finalSettings.get("cluster.name"), is("custom"));
    // path.logs is not set in config_custom/crate.yml
    // so it needs to use default value and not the value set in config/crate.yml
    assertThat(finalSettings.get("path.logs"), Matchers.anyOf(
        endsWith("org/elasticsearch/node/logs"),
        endsWith("org\\elasticsearch\\node\\logs")
    ));
}
 
Example 6
Source File: InternalSettingsPreparerTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testThatCommandLineArgumentsOverrideSettingsFromConfigFile() throws Exception {
    HashMap<String, String> settings = new HashMap<>();
    settings.put("path.home", ".");
    Path config = PathUtils.get(getClass().getResource("config").toURI());
    settings.put("path.conf", config.toString());
    settings.put("stats.enabled", "false");
    settings.put("cluster.name", "clusterNameOverridden");
    settings.put("path.logs", "/some/other/path");
    Settings finalSettings = InternalSettingsPreparer
        .prepareEnvironment(Settings.EMPTY, settings, config, () -> "node1").settings();
    // Overriding value from crate.yml
    assertThat(finalSettings.getAsBoolean("stats.enabled", null), is(false));
    // Value kept from crate.yml
    assertThat(finalSettings.getAsBoolean("psql.enabled", null), is(false));
    // Overriding value from crate.yml
    assertThat(finalSettings.get("cluster.name"), is("clusterNameOverridden"));
    // Value kept from crate.yml
    assertThat(finalSettings.get("path.logs"), Matchers.anyOf(
        is("/some/other/path"),
        is("D:\\some\\other\\path")
    ));
}
 
Example 7
Source File: InternalSettingsPreparerTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorWithDuplicateSettingInConfigFile() throws Exception {
    HashMap<String, String> settings = new HashMap<>();
    settings.put("path.home", ".");
    Path config = PathUtils.get(getClass().getResource("config_invalid").toURI());
    settings.put("path.conf", config.toString());
    expectedException.expect(SettingsException.class);
    expectedException.expectMessage("Failed to load settings from");
    expectedException.expectCause(Matchers.hasProperty("message", containsString("Duplicate field 'stats.enabled'")));
    InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, settings, config, () -> "node1");
}
 
Example 8
Source File: InternalSettingsPreparerTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testClusterNameMissingFromConfigFile() throws Exception {
    HashMap<String, String> settings = new HashMap<>();
    settings.put("path.home", ".");
    settings.put("cluster.name", "clusterName");
    Path config = PathUtils.get(getClass().getResource("config").toURI());
    Settings finalSettings = InternalSettingsPreparer
        .prepareEnvironment(Settings.EMPTY, settings, config, () -> "node1").settings();
    assertThat(finalSettings.get("cluster.name"), is("clusterName"));
}
 
Example 9
Source File: ESTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link java.nio.file.Path} pointing to the class path relative resource given
 * as the first argument. In contrast to
 * <code>getClass().getResource(...).getFile()</code> this method will not
 * return URL encoded paths if the parent path contains spaces or other
 * non-standard characters.
 */
@Override
public Path getDataPath(String relativePath) {
    // we override LTC behavior here: wrap even resources with mockfilesystems,
    // because some code is buggy when it comes to multiple nio.2 filesystems
    // (e.g. FileSystemUtils, and likely some tests)
    try {
        return PathUtils.get(getClass().getResource(relativePath).toURI());
    } catch (Exception e) {
        throw new RuntimeException("resource not found: " + relativePath, e);
    }
}
 
Example 10
Source File: BlobIndicesService.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Path getGlobalBlobPath(Settings settings) {
    String customGlobalBlobPathSetting = SETTING_BLOBS_PATH.get(settings);
    if (Strings.isNullOrEmpty(customGlobalBlobPathSetting)) {
        return null;
    }
    Path globalBlobPath = PathUtils.get(customGlobalBlobPathSetting);
    ensureExistsAndWritable(globalBlobPath);
    return globalBlobPath;
}
 
Example 11
Source File: OpenNlpProcessorTests.java    From elasticsearch-ingest-opennlp with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void createOpenNlpService() throws Exception {
    Settings settings = Settings.builder()
            .put("ingest.opennlp.model.file.names", "en-ner-persons.bin")
            .put("ingest.opennlp.model.file.locations", "en-ner-locations.bin")
            .put("ingest.opennlp.model.file.dates", "en-ner-dates.bin")
            .build();

    Path path = PathUtils.get(OpenNlpProcessorTests.class.getResource("/models/en-ner-persons.bin").toURI());
    service = new OpenNlpService(path.getParent(), settings).start();
}
 
Example 12
Source File: BootstrapChecks.java    From crate with Apache License 2.0 4 votes vote down vote up
@SuppressForbidden(reason = "access /proc/sys/vm/max_map_count")
private Path getProcSysVmMaxMapCountPath() {
    return PathUtils.get("/proc/sys/vm/max_map_count");
}
 
Example 13
Source File: Environment.java    From crate with Apache License 2.0 4 votes vote down vote up
Environment(final Settings settings, final Path configPath, final Path tmpPath) {
    final Path homeFile;
    if (PATH_HOME_SETTING.exists(settings)) {
        homeFile = PathUtils.get(PATH_HOME_SETTING.get(settings)).normalize();
    } else {
        throw new IllegalStateException(PATH_HOME_SETTING.getKey() + " is not configured");
    }

    if (configPath != null) {
        configFile = configPath.normalize();
    } else {
        configFile = homeFile.resolve("config");
    }

    tmpFile = Objects.requireNonNull(tmpPath);

    pluginsFile = homeFile.resolve("plugins");

    List<String> dataPaths = PATH_DATA_SETTING.get(settings);
    final ClusterName clusterName = ClusterName.CLUSTER_NAME_SETTING.get(settings);
    if (DiscoveryNode.nodeRequiresLocalStorage(settings)) {
        if (dataPaths.isEmpty() == false) {
            dataFiles = new Path[dataPaths.size()];
            dataWithClusterFiles = new Path[dataPaths.size()];
            for (int i = 0; i < dataPaths.size(); i++) {
                dataFiles[i] = PathUtils.get(dataPaths.get(i));
                dataWithClusterFiles[i] = dataFiles[i].resolve(clusterName.value());
            }
        } else {
            dataFiles = new Path[]{homeFile.resolve("data")};
            dataWithClusterFiles = new Path[]{homeFile.resolve("data").resolve(clusterName.value())};
        }
    } else {
        if (dataPaths.isEmpty()) {
            dataFiles = dataWithClusterFiles = EMPTY_PATH_ARRAY;
        } else {
            final String paths = String.join(",", dataPaths);
            throw new IllegalStateException("node does not require local storage yet path.data is set to [" + paths + "]");
        }
    }
    if (PATH_SHARED_DATA_SETTING.exists(settings)) {
        sharedDataFile = PathUtils.get(PATH_SHARED_DATA_SETTING.get(settings)).normalize();
    } else {
        sharedDataFile = null;
    }
    List<String> repoPaths = PATH_REPO_SETTING.get(settings);
    if (repoPaths.isEmpty()) {
        repoFiles = EMPTY_PATH_ARRAY;
    } else {
        repoFiles = new Path[repoPaths.size()];
        for (int i = 0; i < repoPaths.size(); i++) {
            repoFiles[i] = PathUtils.get(repoPaths.get(i));
        }
    }

    // this is trappy, Setting#get(Settings) will get a fallback setting yet return false for Settings#exists(Settings)
    if (PATH_LOGS_SETTING.exists(settings)) {
        logsFile = PathUtils.get(PATH_LOGS_SETTING.get(settings)).normalize();
    } else {
        logsFile = homeFile.resolve("logs");
    }

    if (PIDFILE_SETTING.exists(settings)) {
        pidFile = PathUtils.get(PIDFILE_SETTING.get(settings)).normalize();
    } else {
        pidFile = null;
    }

    binFile = homeFile.resolve("bin");
    libFile = homeFile.resolve("lib");
    modulesFile = homeFile.resolve("modules");

    Settings.Builder finalSettings = Settings.builder().put(settings);
    finalSettings.put(PATH_HOME_SETTING.getKey(), homeFile);
    if (PATH_DATA_SETTING.exists(settings)) {
        finalSettings.putList(PATH_DATA_SETTING.getKey(), dataPaths);
    }
    finalSettings.put(PATH_LOGS_SETTING.getKey(), logsFile.toString());
    this.settings = finalSettings.build();
}
 
Example 14
Source File: Environment.java    From crate with Apache License 2.0 4 votes vote down vote up
public Environment(final Settings settings, final Path configPath) {
    this(settings, configPath, PathUtils.get(System.getProperty("java.io.tmpdir")));
}
 
Example 15
Source File: Environment.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Environment(Settings settings) {
    this.settings = settings;
    final Path homeFile;
    if (settings.get("path.home") != null) {
        homeFile = PathUtils.get(cleanPath(settings.get("path.home")));
    } else {
        throw new IllegalStateException("path.home is not configured");
    }

    if (settings.get("path.conf") != null) {
        configFile = PathUtils.get(cleanPath(settings.get("path.conf")));
    } else {
        configFile = homeFile.resolve("config");
    }

    if (settings.get("path.scripts") != null) {
        scriptsFile = PathUtils.get(cleanPath(settings.get("path.scripts")));
    } else {
        scriptsFile = configFile.resolve("scripts");
    }

    if (settings.get("path.plugins") != null) {
        pluginsFile = PathUtils.get(cleanPath(settings.get("path.plugins")));
    } else {
        pluginsFile = homeFile.resolve("plugins");
    }

    String[] dataPaths = settings.getAsArray("path.data");
    if (dataPaths.length > 0) {
        dataFiles = new Path[dataPaths.length];
        dataWithClusterFiles = new Path[dataPaths.length];
        dataPathLimits = new String[dataPaths.length];
        for (int i = 0; i < dataPaths.length; i++) {
            String[] dataPathConfig = dataPaths[i].split("#");
            dataFiles[i] = PathUtils.get(dataPathConfig[0]);
            dataWithClusterFiles[i] = dataFiles[i].resolve(ClusterName.clusterNameFromSettings(settings).value());
            if (dataPathConfig.length > 1) {
                dataPathLimits[i] = dataPathConfig[1];
            } else {
                dataPathLimits[i] = "-1";
            }
        }
    } else {
        dataFiles = new Path[]{homeFile.resolve("data")};
        dataWithClusterFiles = new Path[]{homeFile.resolve("data").resolve(ClusterName.clusterNameFromSettings(settings).value())};
        dataPathLimits = new String[]{"-1"};
    }
    if (settings.get("path.shared_data") != null) {
        sharedDataFile = PathUtils.get(cleanPath(settings.get("path.shared_data")));
    } else {
        sharedDataFile = null;
    }
    String[] repoPaths = settings.getAsArray("path.repo");
    if (repoPaths.length > 0) {
        repoFiles = new Path[repoPaths.length];
        for (int i = 0; i < repoPaths.length; i++) {
            repoFiles[i] = PathUtils.get(repoPaths[i]);
        }
    } else {
        repoFiles = new Path[0];
    }
    if (settings.get("path.logs") != null) {
        logsFile = PathUtils.get(cleanPath(settings.get("path.logs")));
    } else {
        logsFile = homeFile.resolve("logs");
    }

    if (settings.get("pidfile") != null) {
        pidFile = PathUtils.get(cleanPath(settings.get("pidfile")));
    } else {
        pidFile = null;
    }

    binFile = homeFile.resolve("bin");
    libFile = homeFile.resolve("lib");
    modulesFile = homeFile.resolve("modules");
}
 
Example 16
Source File: Environment.java    From crate with Apache License 2.0 2 votes vote down vote up
/**
 * Resolves the specified location against the list of configured repository roots
 *
 * If the specified location doesn't match any of the roots, returns null.
 */
public Path resolveRepoFile(String location) {
    return PathUtils.get(repoFiles, location);
}
 
Example 17
Source File: Environment.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Resolves the specified location against the list of configured repository roots
 *
 * If the specified location doesn't match any of the roots, returns null.
 */
public Path resolveRepoFile(String location) {
    return PathUtils.get(repoFiles, location);
}