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

org.neo4j.server.configuration.ConfigLoader Maven / Gradle / Ivy

Go to download

This component provides management functionality and product surface for Neo4j instances.

There is a newer version: 5.23.0
Show newest version
/*
 * Copyright (c) 2002-2018 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.server.configuration;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.collection.Pair;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.configuration.Settings;

public class ConfigLoader
{
    public static final String DEFAULT_CONFIG_FILE_NAME = "neo4j.conf";

    private final Function ,Iterable>> settingsClassesSupplier;

    public ConfigLoader( Function ,Iterable>> settingsClassesSupplier )
    {
        this.settingsClassesSupplier = settingsClassesSupplier;
    }

    public ConfigLoader( List> settingsClasses )
    {
        this( settings -> settingsClasses );
    }

    public Config loadConfig( Optional configFile, Pair... configOverrides ) throws IOException
    {
        return loadConfig( Optional.empty(), configFile, configOverrides );
    }

    public Config loadConfig( Optional homeDir, Optional configFile,
            Pair... configOverrides )
    {
        Map overriddenSettings = calculateSettings( homeDir, configOverrides );
        return new Config( configFile, overriddenSettings, ConfigLoader::overrideEmbeddedDefaults,
                settingsClassesSupplier );
    }

    public Config loadOfflineConfig( Optional homeDir, Optional configFile )
    {
        return overrideBoltSettings( loadConfig( homeDir, configFile,
                Pair.of( GraphDatabaseSettings.auth_enabled.name(), Settings.FALSE ) ) );
    }

    private Map calculateSettings( Optional homeDir,
            Pair[] configOverrides )
    {
        HashMap settings = new HashMap<>();
        settings.putAll( toMap( configOverrides ) );
        settings.put( GraphDatabaseSettings.neo4j_home.name(),
                homeDir.map( File::getAbsolutePath ).orElse( System.getProperty( "user.dir" ) ) );
        return settings;
    }

    private Map toMap( Pair[] configOverrides )
    {
        Map overrides = new HashMap<>();
        for ( Pair configOverride : configOverrides )
        {
            overrides.put( configOverride.first(), configOverride.other() );
        }
        return overrides;
    }

    private static Config overrideBoltSettings( Config config )
    {
        Map overrides = new HashMap<>();
        for ( GraphDatabaseSettings.BoltConnector bolt : GraphDatabaseSettings.boltConnectors( config ) )
        {
            overrides.put( bolt.enabled.name(), Settings.FALSE );
        }
        overrides.put( new GraphDatabaseSettings.BoltConnector().enabled.name(), Settings.FALSE );
        return config.with( overrides );
    }

    /*
     * TODO: This means docs will say defaults are something other than what they are in the server. Better
     * make embedded the special case and set the defaults to be what the server will have.
     */
    private static void overrideEmbeddedDefaults( Map config )
    {
        config.putIfAbsent( GraphDatabaseSettings.auth_enabled.name(), "true" );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy