Class: ActiveFedora::FileConfigurator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/file_configurator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileConfigurator

The configuration hash that gets used by RSolr.connect



51
52
53
# File 'lib/active_fedora/file_configurator.rb', line 51

def initialize
  reset!
end

Instance Attribute Details

#config_envObject

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.

Examples:

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


47
48
49
# File 'lib/active_fedora/file_configurator.rb', line 47

def config_env
  @config_env
end

#config_optionsObject (readonly)

Returns the value of attribute config_options.



48
49
50
# File 'lib/active_fedora/file_configurator.rb', line 48

def config_options
  @config_options
end

#fedora_config_pathObject (readonly)

Returns the value of attribute fedora_config_path.



48
49
50
# File 'lib/active_fedora/file_configurator.rb', line 48

def fedora_config_path
  @fedora_config_path
end

#solr_config_pathObject (readonly)

Returns the value of attribute solr_config_path.



48
49
50
# File 'lib/active_fedora/file_configurator.rb', line 48

def solr_config_path
  @solr_config_path
end

Instance Method Details

#check_fedora_path_for_solrObject

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



172
173
174
175
176
177
178
179
# File 'lib/active_fedora/file_configurator.rb', line 172

def check_fedora_path_for_solr
  path = File.dirname(self.path) + "/solr.yml"
  if File.file? path
    return path
  else
    return nil
  end
end

#config_loaded?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/active_fedora/file_configurator.rb', line 91

def config_loaded?
  @config_loaded || false
end

#config_reload!Object



73
74
75
76
77
# File 'lib/active_fedora/file_configurator.rb', line 73

def config_reload!
  ActiveSupport::Deprecation.warn("config_reload! is not supported")
  reset!
  load_configs
end

#fedora_configObject



64
65
66
67
# File 'lib/active_fedora/file_configurator.rb', line 64

def fedora_config
  load_configs
  @fedora_config
end

#get_config_path(config_type) ⇒ String

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

Parameters:

  • config_type (String)

    Either ‘fedora’ or ‘solr’

Returns:

  • (String)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/active_fedora/file_configurator.rb', line 143

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")
  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."
  return config_path if File.file? config_path
  raise ConfigurationError "Couldn't load #{config_type} config file!"
end

#get_solr_url(solr_config) ⇒ Object

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



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/active_fedora/file_configurator.rb', line 122

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

#init(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/active_fedora/file_configurator.rb', line 55

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

#load_configsObject



95
96
97
98
99
100
101
102
# File 'lib/active_fedora/file_configurator.rb', line 95

def load_configs
  return if config_loaded?
  @config_env = ActiveFedora.environment
  
  load_fedora_config
  load_solr_config
  @config_loaded = true
end

#load_fedora_configObject



104
105
106
107
108
# File 'lib/active_fedora/file_configurator.rb', line 104

def load_fedora_config
  return @fedora_config unless @fedora_config.empty?
  config_path = get_config_path(:fedora)
  @fedora_config = YAML.load(File.open(config_path))[@config_env] || {}
end

#load_solr_configObject



110
111
112
113
114
115
116
117
118
# File 'lib/active_fedora/file_configurator.rb', line 110

def load_solr_config
  return @solr_config unless @solr_config.empty?
  @solr_config_path = get_config_path(:solr)

  logger.info("ActiveFedora: loading solr config from #{File.expand_path(@solr_config_path)}")
  config = YAML.load(File.open(@solr_config_path)).symbolize_keys
  raise "The #{@config_env.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 #{@config_env} environment" unless config[@config_env.to_sym]
  @solr_config = {:url=> get_solr_url(config[ActiveFedora.environment.to_sym].symbolize_keys)}
end

#pathObject



79
80
81
# File 'lib/active_fedora/file_configurator.rb', line 79

def path
  get_config_path(:fedora)
end

#predicate_configObject



181
182
183
184
# File 'lib/active_fedora/file_configurator.rb', line 181

def predicate_config
  @predicate_config_path ||= build_predicate_config_path(File.dirname(self.path))
  YAML::load(File.open(@predicate_config_path)) if File.exist?(@predicate_config_path)
end

#reset!Object



83
84
85
86
87
88
89
# File 'lib/active_fedora/file_configurator.rb', line 83

def reset!
  @config_loaded = false  #Force reload of configs
  @fedora_config = {}
  @solr_config = {}
  @config_options = {}
  @predicate_config_path = nil
end

#solr_configObject



68
69
70
71
# File 'lib/active_fedora/file_configurator.rb', line 68

def solr_config
  load_configs
  @solr_config
end