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

com.sourceclear.plugins.config.ConfigService Maven / Gradle / Ivy

/*
  Copyright (c) 2015  SourceClear 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.sourceclear.plugins.config;

import com.sourceclear.api.client.Client;
import java.io.File;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Service implementation to handle securing, reading, and writing saved
 * configurations.
 */
public interface ConfigService {
  /**
   * Indicates the environment variable in which I will check for the
   * {@link ConsoleConfig#getUserToken()}  override.
   * And it is an override, in that the environment variable wins automatically.
   */
  String API_TOKEN_ENV_VAR = "SRCCLR_API_TOKEN";

  /**
    * Indicates the environment variable in which I will check for the
    * {@link ConsoleConfig#getOrgToken()}  override.
    * And it is an override, in that the environment variable wins automatically.
    */
  String ORG_TOKEN_ENV_VAR = "SRCCLR_ORG_TOKEN";


  /** The primary filename used in association with {@link #CONFIG_DIRECTORY}. */
  String CONFIG_FILENAME = "console.conf";

  File CONFIG_DIRECTORY = new File(System.getProperty("user.home"), ".srcclr");

  ////////////////////////////////// Methods \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

  /**
   * Adds any files in the given list that exist and are files to the list
   * of configurations that are parsed on startup.
   * @param extras a possibly empty list of files to be applied in that order,
   *               but after the "normal" config file.
   */
  void addExtraConfigFiles(@Nullable List extras);

  /** Determines only the physical file is there, not checking its validity nor its permissions. */
  boolean isConfigFilePresent();

  /** Enables the user to indicate they are aware the file mode is insecure, but to proceed anyway. */
  void setInsecureConfigFileMode(boolean value);

  /**
   * Creates an entirely blank config file and returns it.
   * @return a File, located in the temp directory, that contains a blank config
   * @throws ConfigException if unable to do so
   */
  @Nonnull
  File createBlankConfig() throws ConfigException;

  /**
   * @throws ConfigException in the general case of the inability to read in or construct the config.
   */
  ConsoleConfig getConfiguration() throws ConfigException;

  Object get(@Nonnull String key);

  void set(@Nonnull String key, Object value);

  /** Returns the config key and the corresponding value (which might be {@code null}. */
  @Nonnull
  Map list();

  /**
   * Returns the config key and the corresponding value (which might be {@code null},
   * limited to the keys that match the provided expression.
   * @param constraint a starts with string, or if the leading and trailing characters are forward-slashes,
   *                   treats the constraint as a regex.
   */
  @Nonnull
  Map list(String constraint);

  /**
   * Returns the Sourceclear username for which the {@code sourceclearToken} was issued.
   * @throws com.sourceclear.plugins.config.ConfigException if unable to do so
   */
  String getUsername(Client client);

  /** Return the credential that will allow successful use of {@link com.sourceclear.api.client.Client}. */
  @Nullable
  String getSourceClearClientToken();


  /** Return the credential that will allow successful storage in an Organization repo. */
  @Nullable
  String getSourceClearOrgToken();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy