require'deprecation'require'erb'require'yaml'moduleActiveFedoraclassFileConfigurator# Initializes ActiveFedora's connection to Fedora and Solr based on the info in fedora.yml and solr.yml## If Rails.env is set, it will use that environment. Defaults to "development".# @param [Hash] options (optional) a list of options for the configuration of active_fedora# @option options [String] :environment The environment within which to run# @option options [String] :fedora_config_path The full path to the fedora.yml config file.# @option options [String] :solr_config_path The full path to the solr.yml config file.## If :environment is not set, order of preference is# 1. Rails.env# 2. ENV['environment']# 3. RAILS_ENV## If :fedora_config_path is not set, it will look in# 1. +Rails.root+/config# 2. +current working directory+/config# 3. (default) the fedora.yml shipped with gem## If :solr_config_path is not set, it will# 1. look in config_options[:fedora_config_path]. If it finds a solr.yml there, it will use it.# 2. If it does not find a solr.yml and the fedora.yml contains a solr url, it will raise an configuration error# 3. If it does not find a solr.yml and the fedora.yml does not contain a solr url, it will look in: +Rails.root+/config, +current working directory+/config, then the solr.yml shipped with gem# 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. http://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#attr_accessor:config_envattr_reader:config_options,:fedora_config_path,:solr_config_path# The configuration hash that gets used by RSolr.connectdefinitializereset!enddefinit(options={})raiseArgumentError,"Calling ActiveFedora.init with a path as an argument has been removed. Use ActiveFedora.init(:fedora_config_path=>#{options})"ifoptions.is_a?(String)reset!@config_options=optionsload_configsenddeffedora_configload_configs@fedora_configenddefsolr_configload_configs@solr_configenddefpathconfig_path(:fedora)enddefreset!@config_loaded=false# Force reload of configs@fedora_config={}@solr_config={}@config_options={}enddefconfig_loaded?@config_loaded||falseenddefload_configsreturnifconfig_loaded?@config_env=ActiveFedora.environmentload_fedora_configload_solr_config@config_loaded=trueenddefload_fedora_configreturn@fedora_configunless@fedora_config.empty?@fedora_config_path=config_path(:fedora)ActiveFedora::Base.logger.info("ActiveFedora: loading fedora config from #{::File.expand_path(@fedora_config_path)}")beginconfig_erb=ERB.new(IO.read(@fedora_config_path)).result(binding)rescueStandardErrorraise("fedora.yml was found, but could not be parsed with ERB. \n#{$ERROR_INFO.inspect}")endbeginfedora_yml=ifGem::Version.new(Psych::VERSION)>=Gem::Version.new('3.1.0.pre1')YAML.safe_load(config_erb,permitted_classes: [],permitted_symbols: [],aliases: true)elseYAML.safe_load(config_erb,[],[],true)# allow YAML aliasesendrescuePsych::SyntaxError=>eraise"fedora.yml was found, but could not be parsed. "\"Error #{e.message}"endconfig=fedora_yml.symbolize_keyscfg=config[ActiveFedora.environment.to_sym]||{}@fedora_config=cfg.is_a?(Array)?cfg.map(&:symbolize_keys):cfg.symbolize_keysenddefload_solr_configreturn@solr_configunless@solr_config.empty?@solr_config_path=config_path(:solr)ActiveFedora::Base.logger.info"ActiveFedora: loading solr config from #{::File.expand_path(@solr_config_path)}"beginconfig_erb=ERB.new(IO.read(@solr_config_path)).result(binding)rescueStandardErrorraise("solr.yml was found, but could not be parsed with ERB. \n#{$ERROR_INFO.inspect}")endbeginsolr_yml=ifGem::Version.new(Psych::VERSION)>=Gem::Version.new('3.1.0.pre1')YAML.safe_load(config_erb,permitted_classes: [],permitted_symbols: [],aliases: true)elseYAML.safe_load(config_erb,[],[],true)# allow YAML aliasesendrescueStandardErrorraise("solr.yml was found, but could not be parsed.\n")endconfig=solr_yml.symbolize_keysraise"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"unlessconfig[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# Given the solr_config that's been loaded for this environment,# determine which solr url to usedefsolr_url(solr_config)returnsolr_config[:url]ifsolr_config.key?(:url)returnsolr_config['url']ifsolr_config.key?('url')if@index_full_text==true&&solr_config.key?(:fulltext)&&solr_config[:fulltext].key?('url')solr_config[:fulltext]['url']elsifsolr_config.key?(:default)&&solr_config[:default].key?('url')solr_config[:default]['url']elseraiseURI::InvalidURIErrorendend# Determine the fedora config file to use. Order of preference is:# 1. Use the config_options[:config_path] 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]defconfig_path(config_type)config_type=config_type.to_sif(config_path=config_options.fetch("#{config_type}_config_path".to_sym,nil))raiseConfigurationError,"file does not exist #{config_path}"unless::File.file?config_pathreturn::File.expand_path(config_path)end# if solr, attempt to use path where fedora.yml is firstifconfig_type=="solr"&&(config_path=check_fedora_path_for_solr)returnconfig_pathendifdefined?(Rails.root)config_path="#{Rails.root}/config/#{config_type}.yml"returnconfig_pathif::File.file?config_pathendreturn"#{Dir.getwd}/config/#{config_type}.yml"if::File.file?"#{Dir.getwd}/config/#{config_type}.yml"# Last choice, check for the default config fileconfig_path=::File.join(ActiveFedora.root,"config","#{config_type}.yml")raiseConfigurationError,"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_pathend# Checks the existing fedora_config.path to see if there is a solr.yml theredefcheck_fedora_path_for_solrpath=::File.dirname(self.path)+"/solr.yml"returnunless::File.file?pathpathendendend