class ActiveFedora::FileConfigurator

Attributes

config_env[RW]

Options allowed in fedora.yml first level is the environment (e.g. development, test, production and any custom environments you may have) the second level has these keys:

  1. url: url including protocol, host, port and path (e.g. 127.0.0.1:8983/fedora)

  2. user: username

  3. password: password

  4. validateChecksum: indicates to the fedora server whether you want to validate checksums when the datastreams are queried.

@example If you want to shard the fedora instance, you can specify an array of credentials.

production:
- user: user1
  password: password1
  url: http://127.0.0.1:8983/fedora1
- user: user2
  password: password2
  url: http://127.0.0.1:8983/fedora2
config_options[R]
fedora_config_path[R]
solr_config_path[R]

Public Class Methods

new() click to toggle source

The configuration hash that gets used by RSolr.connect

# File lib/active_fedora/file_configurator.rb, line 52
def initialize
  reset!
end

Public Instance Methods

check_fedora_path_for_solr() click to toggle source

Checks the existing fedora_config.path to see if there is a solr.yml there

# File lib/active_fedora/file_configurator.rb, line 201
def check_fedora_path_for_solr
  path = ::File.dirname(self.path) + "/solr.yml"
  return unless ::File.file? path
  path
end
config_loaded?() click to toggle source
# File lib/active_fedora/file_configurator.rb, line 84
def config_loaded?
  @config_loaded || false
end
config_path(config_type) click to toggle source

Determine the fedora config file to use. Order of preference is:

  1. Use the config_options if it exists

  2. Look in Rails.root/config/fedora.yml

  3. Look in +current working directory+/config/fedora.yml

  4. Load the default config that ships with this gem

@param [String] config_type Either ‘fedora’ or ‘solr’ @return [String]

# File lib/active_fedora/file_configurator.rb, line 173
def config_path(config_type)
  config_type = config_type.to_s
  if (config_path = config_options.fetch("#{config_type}_config_path".to_sym, nil))
    raise ConfigurationError, "file does not exist #{config_path}" unless ::File.file? config_path
    return ::File.expand_path(config_path)
  end

  # if solr, attempt to use path where fedora.yml is first
  if config_type == "solr" && (config_path = check_fedora_path_for_solr)
    return config_path
  end

  if defined?(Rails.root)
    config_path = "#{Rails.root}/config/#{config_type}.yml"
    return config_path if ::File.file? config_path
  end

  return "#{Dir.getwd}/config/#{config_type}.yml" if ::File.file? "#{Dir.getwd}/config/#{config_type}.yml"

  # Last choice, check for the default config file
  config_path = ::File.join(ActiveFedora.root, "config", "#{config_type}.yml")
  raise ConfigurationError, "Couldn't load #{config_type} config file!" unless ::File.file?(config_path)

  ActiveFedora::Base.logger.warn "Using the default #{config_type}.yml that comes with active-fedora.  If you want to override this, pass the path to #{config_type}.yml to ActiveFedora - ie. ActiveFedora.init(:#{config_type}_config_path => '/path/to/#{config_type}.yml') - or set Rails.root and put #{config_type}.yml into \#{Rails.root}/config."
  config_path
end
fedora_config() click to toggle source
# File lib/active_fedora/file_configurator.rb, line 63
def fedora_config
  load_configs
  @fedora_config
end
init(options = {}) click to toggle source
# File lib/active_fedora/file_configurator.rb, line 56
def init(options = {})
  raise ArgumentError, "Calling ActiveFedora.init with a path as an argument has been removed.  Use ActiveFedora.init(:fedora_config_path=>#{options})" if options.is_a?(String)
  reset!
  @config_options = options
  load_configs
end
load_configs() click to toggle source
# File lib/active_fedora/file_configurator.rb, line 88
def load_configs
  return if config_loaded?
  @config_env = ActiveFedora.environment

  load_fedora_config
  load_solr_config
  @config_loaded = true
end
load_fedora_config() click to toggle source
# File lib/active_fedora/file_configurator.rb, line 97
def load_fedora_config
  return @fedora_config unless @fedora_config.empty?
  @fedora_config_path = config_path(:fedora)
  ActiveFedora::Base.logger.info("ActiveFedora: loading fedora config from #{::File.expand_path(@fedora_config_path)}")

  begin
    config_erb = ERB.new(IO.read(@fedora_config_path)).result(binding)
  rescue StandardError
    raise("fedora.yml was found, but could not be parsed with ERB. \n#{$ERROR_INFO.inspect}")
  end

  begin
    fedora_yml = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1')
                   YAML.safe_load(config_erb, permitted_classes: [], permitted_symbols: [], aliases: true)
                 else
                   YAML.safe_load(config_erb, [], [], true) # allow YAML aliases
                 end
  rescue Psych::SyntaxError => e
    raise "fedora.yml was found, but could not be parsed. " \
          "Error #{e.message}"
  end

  config = fedora_yml.symbolize_keys

  cfg = config[ActiveFedora.environment.to_sym] || {}
  @fedora_config = cfg.is_a?(Array) ? cfg.map(&:symbolize_keys) : cfg.symbolize_keys
end
load_solr_config() click to toggle source
# File lib/active_fedora/file_configurator.rb, line 125
def load_solr_config
  return @solr_config unless @solr_config.empty?
  @solr_config_path = config_path(:solr)

  ActiveFedora::Base.logger.info "ActiveFedora: loading solr config from #{::File.expand_path(@solr_config_path)}"
  begin
    config_erb = ERB.new(IO.read(@solr_config_path)).result(binding)
  rescue StandardError
    raise("solr.yml was found, but could not be parsed with ERB. \n#{$ERROR_INFO.inspect}")
  end

  begin
    solr_yml = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1')
                 YAML.safe_load(config_erb, permitted_classes: [], permitted_symbols: [], aliases: true)
               else
                 YAML.safe_load(config_erb, [], [], true) # allow YAML aliases
               end
  rescue StandardError
    raise("solr.yml was found, but could not be parsed.\n")
  end

  config = solr_yml.symbolize_keys
  raise "The #{ActiveFedora.environment.to_sym} environment settings were not found in the solr.yml config.  If you already have a solr.yml file defined, make sure it defines settings for the #{ActiveFedora.environment.to_sym} environment" unless config[ActiveFedora.environment.to_sym]
  config = config[ActiveFedora.environment.to_sym].symbolize_keys
  @solr_config = { url: solr_url(config) }.merge(config.slice(:update_path, :select_path))
end
path() click to toggle source
# File lib/active_fedora/file_configurator.rb, line 73
def path
  config_path(:fedora)
end
reset!() click to toggle source
# File lib/active_fedora/file_configurator.rb, line 77
def reset!
  @config_loaded = false # Force reload of configs
  @fedora_config = {}
  @solr_config = {}
  @config_options = {}
end
solr_config() click to toggle source
# File lib/active_fedora/file_configurator.rb, line 68
def solr_config
  load_configs
  @solr_config
end
solr_url(solr_config) click to toggle source

Given the solr_config that’s been loaded for this environment, determine which solr url to use

# File lib/active_fedora/file_configurator.rb, line 154
def solr_url(solr_config)
  return solr_config[:url] if solr_config.key?(:url)
  return solr_config['url'] if solr_config.key?('url')
  if @index_full_text == true && solr_config.key?(:fulltext) && solr_config[:fulltext].key?('url')
    solr_config[:fulltext]['url']
  elsif solr_config.key?(:default) && solr_config[:default].key?('url')
    solr_config[:default]['url']
  else
    raise URI::InvalidURIError
  end
end