class ActiveFedora::FileConfigurator
def build_predicate_config_path
def build_predicate_config_path testfile = ::File.expand_path(get_config_path(:predicate_mappings)) if ::File.exist?(testfile) && valid_predicate_mapping?(testfile) return testfile end raise PredicateMappingsNotFoundError end
def check_fedora_path_for_solr
def check_fedora_path_for_solr path = ::File.dirname(self.path) + "/solr.yml" if ::File.file? path return path else return nil end end
def config_loaded?
def config_loaded? @config_loaded || false end
def fedora_config
def fedora_config load_configs @fedora_config end
def get_config_path(config_type)
-
(String)-
Parameters:
-
config_type(String) -- Either ‘fedora’ or ‘solr’
def get_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 if ::File.file? "#{Dir.getwd}/config/#{config_type}.yml" return "#{Dir.getwd}/config/#{config_type}.yml" end # Last choice, check for the default config file config_path = ::File.join(ActiveFedora.root, "config", "#{config_type}.yml") if ::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." if ActiveFedora::Base.logger return config_path else raise ConfigurationError, "Couldn't load #{config_type} config file!" end end
def get_solr_url(solr_config)
Given the solr_config that's been loaded for this environment,
def get_solr_url(solr_config) if @index_full_text == true && solr_config.has_key?(:fulltext) && solr_config[:fulltext].has_key?('url') return solr_config[:fulltext]['url'] elsif solr_config.has_key?(:default) && solr_config[:default].has_key?('url') return solr_config[:default]['url'] elsif solr_config.has_key?('url') return solr_config['url'] elsif solr_config.has_key?(:url) return solr_config[:url] else raise URI::InvalidURIError end end
def init options = {}
def init options = {} if options.is_a?(String) raise ArgumentError, "Calling ActiveFedora.init with a path as an argument has been removed. Use ActiveFedora.init(:fedora_config_path=>#{options})" end reset! @config_options = options load_configs end
def initialize
def initialize reset! end
def load_configs
def load_configs return if config_loaded? @config_env = ActiveFedora.environment load_fedora_config load_solr_config @config_loaded = true end
def load_fedora_config
def load_fedora_config return @fedora_config unless @fedora_config.empty? @fedora_config_path = get_config_path(:fedora) ActiveFedora::Base.logger.info("ActiveFedora: loading fedora config from #{::File.expand_path(@fedora_config_path)}") if ActiveFedora::Base.logger begin config_erb = ERB.new(IO.read(@fedora_config_path)).result(binding) rescue Exception => e raise("fedora.yml was found, but could not be parsed with ERB. \n#{$!.inspect}") end begin fedora_yml = YAML.load(config_erb) 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.kind_of?(Array) ? cfg.map(&:symbolize_keys) : cfg.symbolize_keys end
def load_solr_config
def load_solr_config return @solr_config unless @solr_config.empty? @solr_config_path = get_config_path(:solr) ActiveFedora::Base.logger.info "ActiveFedora: loading solr config from #{::File.expand_path(@solr_config_path)}" if ActiveFedora::Base.logger begin config_erb = ERB.new(IO.read(@solr_config_path)).result(binding) rescue Exception => e raise("solr.yml was found, but could not be parsed with ERB. \n#{$!.inspect}") end begin solr_yml = YAML.load(config_erb) rescue StandardError => e 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] @solr_config = {:url=> get_solr_url(config[ActiveFedora.environment.to_sym].symbolize_keys)} end
def path
def path get_config_path(:fedora) end
def predicate_config
def predicate_config @predicate_config_path ||= build_predicate_config_path YAML.load(::File.open(@predicate_config_path)) if ::File.exist?(@predicate_config_path) end
def reset!
def reset! @config_loaded = false #Force reload of configs @fedora_config = {} @solr_config = {} @config_options = {} @predicate_config_path = nil end
def solr_config
def solr_config load_configs @solr_config end
def valid_predicate_mapping?(testfile)
def valid_predicate_mapping?(testfile) mapping = YAML.load(::File.open(testfile)) return false unless mapping.has_key?(:default_namespace) && mapping[:default_namespace].is_a?(String) return false unless mapping.has_key?(:predicate_mapping) && mapping[:predicate_mapping].is_a?(Hash) true end