Class: Arrow::Config

Inherits:
Object show all
Extends:
Forwardable
Includes:
HashUtilities
Defined in:
lib/arrow/config.rb

Overview

The Arrow::Config class, instances of which use used to load and save configuration values for an Arrow application.

Description

The configuration values are as follows:

logging

Arrow::Logger configuration. See arrow/logger.rb for specifics about this

  section.
applets

Applet configuration values:

path

An Arrow::Path object or colon-delimited list of directories to search for applet files. Defaults to: “./applets:/www/applets”.

pattern

A glob pattern that will be used to search for applets to load. Default to ’*.rb’.

pollInterval

The number of seconds between checks of the applet path for new/updated/deleted applet files. Defaults to 5.

noSuchAppletHandler
  The URI of the applet which should handle requests for applets that don't
  exist. A value of '(builtin)' (the default) will cause a builtin handler to
  be invoked.
errorHandler
  The URI of the applet which should handle untrapped exceptions raised
  from other applets. A value of '(builtin)' (the default) will cause a
  builtin handler to be invoked.
templates

Template configuration values:

  [<b>loader</b>]
  The name of a class or module to use to load templates for use in the
  appserver. Defaults to 'Arrow::Template'.
path

An Arrow::Path object or colon-delimited list of directories to search for templates. Defaults to “templates:/www/templates”.

cache

Flag that determines whether or not templates are cached in an LRU cache in the TemplateFactory or loaded anew everytime. Default to true (templates are cached).

cacheConfig

Configuration for the template cache. If template caching is turned off, this section is ignored.

maxNum

The maximum number of templates to cache. Default to 20.

maxSize

The maximum estimated size of all cached objects. When

          the cache exceeds this size in bytes, the
          least-recently used one/s will be dropped until the
          cache's total size is less than this value.
maxObjSize

The maximum size of the cache, in bytes. If an

          object exceeeds this number of bytes in estimated
          size, it will not be cached.
expiration

The maximum lifetime of an object in the cache, in

          seconds. Objects which were cached more than this
          number of seconds before retrieval will be dropped.
session

Session configuration values:

idType

A URI which represents the id class to use and its configuration. See the documentation for Arrow::Session::Id and its derivatives for the form of the URI. ‘md5:.’ is the default.

lockType

A URI which specifies what locking class to use and its configuration. If this is the string &#8217;recommended&#8217;, the lock object will be created by calling the #create_recommended_lock method of the store. Defaults to ‘recommended’. See Arrow::Session::Lock and its derivatives for the format of the URI.

storeType

A URI which specifies what backing store class to use for storing the session data between requests and its configuration. Default to ‘file:/tmp’; see the documentation for Arrow::Session::Store and its derivatives for the form of the URI.

idName

The name of the session cookie and/or the session id parameter that will be inserted in rewritten URLs. Defaults to ‘arrow-session’.

rewriteUrls

If set to true, any self-referential URLs in the appserver’s output will be rewritten to include the session id as a parameter. Defaults to true.

expires

Set the expiration time of the session cookie. Defaults to “+48h”; see documentation for Apache::Cookie#expires for the format of the string.

Authors

  • Michael Granger

Please see the file LICENSE in the top-level directory for licensing details.

Defined Under Namespace

Classes: ConfigStruct, Loader, YamlLoader

Constant Summary

DEFAULTS =

Define the layout and defaults for the underlying structs

{
  :logging      => { :global => 'notice' },

  :gems               => {
    :require_signed => false,
    :autoinstall    => false,
    :path           => Arrow::Path.new([ "gems", *Gem.path ]),
    :applets        => {},
  },

  :applets => {
    :path      => Arrow::Path.new( "applets:/www/applets" ),
    :pattern   => '**/*.rb',
    :pollInterval  => 5,
    :layout      => {},
    :config      => {},
    :missingApplet => '/missing',
    :errorApplet => '/error',
  },

  :templates => {
    :loader      => 'Arrow::Template',
    :path      => Arrow::Path.new( "templates:/www/templates" ),
    :cache     => true,
    :cacheConfig => {
      :maxNum     => 20,
      :maxSize    => (1<<17) * 20,
      :maxObjSize   => (1<<17),
      :expiration   => 36
    },
  },

  :session => {
    :idType      => 'md5:.',
    :lockType    => 'recommended',
    :storeType   => 'file:/tmp',
    :idName      => 'arrow-session',
    :rewriteUrls => true,
    :expires   => "+48h",
  },
}

Constants included from HashUtilities

HashMergeFunction

Class Attribute Summary

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Methods included from HashUtilities

#stringify_keys, #symbolify_keys

Methods inherited from Object

deprecate_class_method, deprecate_method, inherited

Methods included from Loggable

#log

Constructor Details

- (Config) initialize(confighash = {})

Create a new Arrow::Config object. Values passed in via the confighash will be used instead of the defaults.



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/arrow/config.rb', line 199

def initialize( confighash={} )
  ihash = internify_keys( untaint_values(confighash) )
  mergedhash = DEFAULTS.merge( ihash, &HashMergeFunction )

  @struct      = ConfigStruct.new( mergedhash )
  @create_time = Time.now
  @name        = nil
  @loader      = nil

  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(sym, *args) (protected)

Hook up delegators to struct-members as they are called



320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/arrow/config.rb', line 320

def method_missing( sym, *args )
  key = sym.to_s.sub( /(=|\?)$/, '' ).to_sym
  return nil unless @struct.member?( key )

  self.log.debug( "Autoloading #{key} accessors." )

  self.class.class_eval %{
    def #{key}; @struct.#{key}; end
    def #{key}=(*args); @struct.#{key} = *args; end
    def #{key}?; @struct.#{key}?; end
  }

  @struct.__send__( sym, *args )
end

Class Attribute Details

+ (Object) default_loader

Returns the value of attribute default_loader



164
165
166
# File 'lib/arrow/config.rb', line 164

def default_loader
  @default_loader
end

+ (Object) loaders

Returns the value of attribute loaders



164
165
166
# File 'lib/arrow/config.rb', line 164

def loaders
  @loaders
end

Instance Attribute Details

- (Object) create_time

The time the configuration was loaded



224
225
226
# File 'lib/arrow/config.rb', line 224

def create_time
  @create_time
end

- (Object) name

The name of the associated record stored on permanent storage for this configuration.



228
229
230
# File 'lib/arrow/config.rb', line 228

def name
  @name
end

Class Method Details

+ (Object) get_loader(name = nil)

Get the loader by the given name, creating a new one if one is not already instantiated.



170
171
172
173
# File 'lib/arrow/config.rb', line 170

def self::get_loader( name=nil )
  name ||= self.default_loader
  self.loaders[name] ||= Arrow::Config::Loader.create( name )
end

+ (Object) load(source, loader_obj = nil)

Read and return an Arrow::Config object from the given file or configuration source using the specified loader.



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/arrow/config.rb', line 178

def self::load( source, loader_obj=nil )
  loader_obj = self.get_loader( loader_obj ) unless
    loader_obj.is_a?( Arrow::Config::Loader )
  my_source = source.dup
  my_source.untaint
  confighash = loader_obj.load( my_source )

  obj = new( confighash )
  obj.loader = loader_obj
  obj.name = my_source

  return obj
end

Instance Method Details

- (Boolean) changed?

Returns true if the configuration has changed since it was last loaded, either by setting one of its members or changing the file from which it was loaded.

Returns:

  • (Boolean)


271
272
273
# File 'lib/arrow/config.rb', line 271

def changed?
  return self.changed_reason ? true : false
end

- (Object) changed_reason

If the configuration has changed, return the reason. If it hasn’t, returns nil.



278
279
280
281
282
283
284
285
286
287
# File 'lib/arrow/config.rb', line 278

def changed_reason
  return "Struct was modified" if @struct.modified?

  if self.name && self.loader.is_newer?( self.name, self.create_time )
    return "Config source (%s) has been updated since %s" %
      [ self.name, self.create_time ]
  end

  return nil
end

- (Object) loader

Fetch the loader from this config object, or create an instance of the default one if none is yet associated with it.



245
246
247
# File 'lib/arrow/config.rb', line 245

def loader
  @loader ||= self.class.get_loader
end

- (Object) loader=(new_loader)

Change the configuration object’s loader. The new_loader argument can be either an Arrow::Config::Loader object or the name of one suitable for passing to Arrow::Config::Loader.create.



234
235
236
237
238
239
240
# File 'lib/arrow/config.rb', line 234

def loader=( new_loader )
  if new_loader.is_a?( Arrow::Config::Loader )
    @loader = new_loader
  else
    @loader = self.class.get_loader( new_loader )
  end
end

- (Object) reload

Reload the configuration from the original source if it has changed. Returns true if it was reloaded and false otherwise.



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/arrow/config.rb', line 292

def reload
  return false unless @loader && @name

  # Even if reloading fails, reset the creation time so we don't keep
  # trying to reload a broken config
  self.create_time = Time.now

  confighash = @loader.load( @name )
  ihash = internify_keys( untaint_values(confighash) )
  mergedhash = DEFAULTS.merge( ihash, &HashMergeFunction )

  @struct = ConfigStruct.new( mergedhash )

rescue => err
  self.log.error "Error while trying to reload the config: %s" % err.message
  err.backtrace.each {|frame| self.log.debug "  " + frame }

  return false
else
  return true
end

- (Boolean) respond_to?(sym)

Returns true for methods which can be autoloaded

Returns:

  • (Boolean)


262
263
264
265
# File 'lib/arrow/config.rb', line 262

def respond_to?( sym )
  return true if @struct.member?( sym.to_s.sub(/(=|\?)$/, '') )
  super
end

- (Object) write(name = @name, *args)

Write the configuration object using the specified name and any additional args.

Raises:

  • (ArgumentError)


252
253
254
255
256
257
258
# File 'lib/arrow/config.rb', line 252

def write( name=@name, *args )
  raise ArgumentError,
    "No name associated with this config." unless name
  lobj = self.loader
  strHash = stringify_keys( @struct.to_h )
  self.loader.save( strHash, name, *args )
end