com.cinchapi.concourse.server.plugin.PluginConfiguration Maven / Gradle / Ivy
/*
* Copyright (c) 2013-2017 Cinchapi Inc.
*
* 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.
*/
package com.cinchapi.concourse.server.plugin;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.apache.commons.configuration.ConfigurationException;
import ch.qos.logback.classic.Level;
import com.cinchapi.concourse.config.PreferencesHandler;
import com.cinchapi.concourse.util.Logging;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
/**
* A collection of preferences that are used to configure a plugin's JVM and
* possible internal operations.
*
* Defaults can be {@link #addDefault(String, Object) declared} programmatically
* by the plugin and can be overridden by the user by specifying a prefs file in
* the plugin's home directory.
*
*
* @author Jeff Nelson
*/
public abstract class PluginConfiguration {
/**
* The absolute {@link Path} to plugin pref file in the plugin's home dir
*/
protected static Path PLUGIN_PREFS;
/**
* The name of the dev prefs file in the plugin's home directory.
*/
protected static final String PLUGIN_PREFS_DEV_FILENAME = "plugin.prefs.dev";
/**
* The name of the prefs file in the plugin's home directory.
*/
protected static final String PLUGIN_PREFS_FILENAME = "plugin.prefs";
/**
* The default value for the {@link SystemPreference#HEAP_SIZE} preference
* (in bytes).
*/
private static final long DEFAULT_HEAP_SIZE_IN_BYTES = 268435456;
/**
* The default value for the {@link SystemPreference#DEBUG_PORT} preference
*/
private static final int DEFAULT_REMOTE_DEBUGGER_PORT = 0;
/**
* The absolute path to the dev prefs file in the plugin's home directory.
*/
private static final Path PLUGIN_PREFS_DEV_LOCATION = PluginRuntime
.getRuntime().home()
.resolve(Paths.get("conf", PLUGIN_PREFS_DEV_FILENAME))
.toAbsolutePath();
/**
* The absolute path to the prefs file in the plugin's home directory.
*/
private static final Path PLUGIN_PREFS_LOCATION = PluginRuntime
.getRuntime().home()
.resolve(Paths.get("conf", PLUGIN_PREFS_FILENAME)).toAbsolutePath();
static {
// Prevent logging from showing up in the console
Logging.disable(PluginConfiguration.class);
// Set location of the plugin preferences files depending on the
// existence of the preferences files
PLUGIN_PREFS = Files.exists(PLUGIN_PREFS_DEV_LOCATION) ? PLUGIN_PREFS_DEV_LOCATION
: PLUGIN_PREFS_LOCATION;
}
/**
* Default configuration values that are defined within the plugin. These
* defaults are used if they are not overridden in the prefs file.
*/
private final Map defaults = Maps.newHashMap();
/**
* A handler to the underlying prefs file, if it exists within the plugin's
* home directory.
*/
@Nullable
private final PreferencesHandler prefs;
/**
* Construct a new instance.
*/
public PluginConfiguration() {
this(PLUGIN_PREFS);
}
/**
* DO NOT CALL
*
* Provided for the plugin manager to create a local handler for every
* plugin's preferences.
*
*
* @param location
*/
protected PluginConfiguration(Path location) {
if (Files.exists(location)) {
try {
this.prefs = new PreferencesHandler(location.toString()) {};
}
catch (ConfigurationException e) {
throw Throwables.propagate(e);
}
}
else {
this.prefs = null;
}
addDefault(SystemPreference.REMOTE_DEBUGGER_PORT, DEFAULT_REMOTE_DEBUGGER_PORT);
addDefault(SystemPreference.HEAP_SIZE, DEFAULT_HEAP_SIZE_IN_BYTES);
addDefault(SystemPreference.LOG_LEVEL, Level.INFO.levelStr);
}
/**
* Returns the list of aliases. If no aliases available, it will return a
* default empty list.
*
* @return List.
*/
public List getAliases() {
if(prefs != null) {
List © 2015 - 2025 Weber Informatics LLC | Privacy Policy