Class: UU::OS::Application::Config

Inherits:
Object
  • Object
show all
Defined in:
uu_os_application-server-2.7.3/lib/uu/os/application/config.rb

Overview

Class represent application global configuration, configuration is read from configuration files and environment variables. Configuration is merged in this order:

  • Configuration file <app-root>/config/app.rb

  • <uu-home>/config/uu-client.properties

  • Environment variable SERVER_CFG

  • Configuration made by block #load defined in file <app-root>/config/app.rb

Examples:

# static configuration
UU::OS::Application::Config.replace({
  :app_credentials => '77-456-2',
  :my_param => 'my value'
})
# config params: {:app_credentials => '77-456-2', :my_param => 'my value'}

# get value anywhere in application
UU::OS::Application::Config[:app_credentials] # String '77-456-2'

# merge config
UU::OS::Application::Config.merge!({
  :app_credentials => '13-13-2',
  :another_param => 13
})
# config params: {:app_credentials => '13-13-2', :my_param => 'my value', :another_param => 13}

# set value
UU::OS::Application::Config[:my_param] # String 'my value'
UU::OS::Application::Config[:my_param] = 'another value'
UU::OS::Application::Config[:my_param] # String 'another value'

Constant Summary

DEFAULT_TTL =

Default ttl in seconds. TTL is period of time, while configuration is cached by application.

3600

Class Method Summary (collapse)

Class Method Details

+ (Object) [](key)

See Also:

  • Hash#[]


61
62
63
64
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 61

def self.[](key)
  _reload if reload_necessary?
  @@ctx[key]
end

+ (Object) []=(key, value)

See Also:

  • Hash#[]=


67
68
69
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 67

def self.[]=(key, value)
  @@ctx[key] = value
end

+ (Object) clear

See Also:

  • Hash#clear


92
93
94
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 92

def self.clear
  @@ctx.clear
end

+ (Object) delete(key, &block)

See Also:

  • Hash#delete


82
83
84
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 82

def self.delete(key, &block)
  @@ctx.delete(key, &block)
end

+ (Boolean) has_key?(key)

Returns:

  • (Boolean)

See Also:

  • Hash#has_key?


87
88
89
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 87

def self.has_key?(key)
  @@ctx.has_key?(key)
end

+ (Object) load {|config| ... }

Note:

This method should be defined in <app-root>/config/app.rb file.

Method takes a block as parameter, this block is used for dynamic configuration loading. Block is called every time when configuration expire (you can set configuration ttl). If configuration expire, this block is called when application try to read some value (by UU::OS::Application::Config).

Code used in block for loading must be fast, because if configuration expire so all requests are blocked until configuration is loaded.

If configuration load fails (exception is raised from block), then original configuration is restored and all changes made on config hash (hash that comes to block) are discarded. The next attempt to reload configuration after failure is made when normal ttl expire (no special behavior after failure).

In block, you must not set values by modifying UU::OS::Application::Config directly (e.g. UU::OS::Application::Config = 'value') but use config hash that comes to the block.

There is not guaranty which user (if any) will be logged on session when block starts. Every session change (login or logout) is valid only in this block execution scope and is discarded when block ends.

Examples:

# all necessary requires
require 'uu/os/security/session'
require 'uu/os/property'

# load configuration from property and merge
# it with original configuration
UU::OS::Application::Config.load do |config|
  app_credentials = config[:app_credentials]
  UU::OS::Security::Session.(app_credentials)

  config_property_uri = config[:config_property_uri]
  new_config_str = UU::OS::Property.get_value(config_property_uri)
  new_config = JSON.parse(new_config_str, :symbolize_names => true)

  config.merge!(new_config)
end

Yield Parameters:

  • config (UU::OS::Lang::UniformHash)

    Hash with actual configuration, you also must use this hash for storing new configuration.



143
144
145
146
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 143

def self.load(&block)
  return unless block_given?
  @@load_callback = block
end

+ (Object) merge!(other_hash, &block)

See Also:

  • Hash#merge


72
73
74
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 72

def self.merge!(other_hash, &block)
  @@ctx.merge!(other_hash, &block)
end

+ (void) reload

This method returns an undefined value.

Force immediate configuration reload no matter to ttl expiration. Method waits until configuration is loaded. Reload in done by block defined by #load.

See Also:

  • #load


101
102
103
104
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 101

def self.reload
  _reload(true) 
  return
end

+ (Object) replace(other_hash)

See Also:

  • Hash#replace


77
78
79
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 77

def self.replace(other_hash)
  @@ctx.replace(other_hash)
end

+ (Fixnum) ttl

Returns configuration ttl (time to live) in seconds. Default value is defined by DEFAULT_TTL constant. TTL is period of time, while configuration is cached by application.

Examples:

UU::OS::Application::Config.ttl # Fixnum 3600 (means 3600 seconds = 1 hour)

Returns:

  • (Fixnum)

    ttl



155
156
157
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 155

def self.ttl
  @@ttl
end

+ (void) ttl=(new_ttl)

This method returns an undefined value.

Sets configuration ttl (time to live) in seconds. Default value is defined by DEFAULT_TTL constant. Value can be set in interval from 1 to Fixnum maximum. If you set value to zero or less (e.g. -1) it means infinite ttl and reload is performed only once.

Examples:

# set ttl to 86400 seconds = 1 day 
UU::OS::Application::Config.ttl = 86400 

Parameters:

  • new_ttl (Fixnum)

    New ttl value.



169
170
171
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/config.rb', line 169

def self.ttl=(new_ttl)
  @@ttl = new_ttl
end