Permalink
Browse files

More complete YAML configuration support.

  • Loading branch information...
1 parent b0f15a2 commit 32acdd3625ab5798fb4a46908c47c494ed9a2ffd Jason Voegele committed Nov 3, 2010
Showing with 119 additions and 47 deletions.
  1. +6 −6 config/ehcache.yml
  2. +6 −6 config/ehcache_manual_rmi.yml
  3. +63 −34 lib/ehcache/yaml_config.rb
  4. +15 −0 test/ehcache.yml
  5. +29 −1 test/test_yaml_config.rb
View
@@ -1,15 +1,15 @@
-disk:
+disk_store:
path: java.io.tmpdir
-peer_provider:
- class: net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory
+peer_providers:
+- class: net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory
properties: peerDiscovery=automatic,multicastGroupAddress=230.0.0.1,multicastGroupPort=4446,timeToLive=1
property_separator: "\,"
-peer_listener:
- class: net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory
+peer_listeners:
+- class: net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory
-default:
+default_cache:
name: cache
max_elements_in_memory: 10000
time_to_live_seconds: 0
@@ -1,16 +1,16 @@
-disk:
+disk_store:
path: java.io.tmpdir
-peer_provider:
- class: net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory
+peer_providers:
+- class: net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory
properties: peerDiscovery=manual,rmiUrls=//that_hostname:40001/cache
property_separator: "\,"
-peer_listener:
- class: net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory
+peer_listeners:
+- class: net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory
properties: hostName=this_hostname,port=40000
-default:
+default_cache:
name: cache
max_elements_in_memory: 10000
time_to_live_seconds: 0
@@ -5,19 +5,22 @@
module Ehcache::Config
module YamlConfig
+
+ InvalidYamlConfiguration = Class.new(StandardError)
+
# Not sure why, but "include Java::NetSfEhcacheConfig" does not work,
# so define local constants referring to the Ehcache classes
Configuration = Java::NetSfEhcacheConfig::Configuration
CacheConfiguration = Java::NetSfEhcacheConfig::CacheConfiguration
DiskStoreConfiguration = Java::NetSfEhcacheConfig::DiskStoreConfiguration
FactoryConfiguration = Java::NetSfEhcacheConfig::FactoryConfiguration
- TOP_LEVEL_ATTRIBUTES = %w[
- name update_check monitoring dynamic_config disk_store
- transaction_manager event_listener peer_providers peer_listeners
- terracotta_config default_cache caches
- ]
- TOP_LEVEL_ATTRIBUTES.each do |attribute|
+ %w[name update_check monitoring dynamic_config
+ disk_store transaction_manager event_listener
+ peer_providers peer_listeners
+ terracotta_config default_cache caches
+ event_listeners extensions loaders decorators
+ ].each do |attribute|
const_set(attribute.upcase.to_sym, attribute)
end
@@ -32,6 +35,7 @@ class YamlConfigBuilder
def initialize(yaml_file)
@yaml_file = yaml_file
@data = YAML.load(ERB.new(File.read(yaml_file)).result(binding))
+ raise InvalidYamlConfiguration unless valid?(@data)
end
def build
@@ -51,6 +55,10 @@ def build
private
+ def valid?(data)
+ data.is_a?(Hash)
+ end
+
def set_if_present(key)
if @data.has_key?(key)
setter = "#{key}=".to_sym
@@ -61,62 +69,83 @@ def set_if_present(key)
def set_attributes(object, attributes)
attributes ||= []
attributes.each do |key, value|
- object.send("#{key}=", value)
+ if value.is_a?(Hash) || value.is_a?(Array)
+ create_cache_config_factories(object, key, value)
+ else
+ object.send("#{key}=", value)
+ end
end
object
end
+ def create_cache_config_factories(cache, key, value)
+ [value].flatten.each do |data|
+ create_cache_config_factory(cache, key, data)
+ end
+ end
+
+ def create_cache_config_factory(cache, key, data)
+ singular = key.singularize.sub(/s$/, '')
+ factory_name = "Cache#{singular.camelize}Factory"
+ class_name = "#{factory_name}Configuration"
+ factory_class = CacheConfiguration.const_get(class_name)
+ factory = factory_class.new
+
+ method_name = "add#{factory_name}"
+
+ cache.send(method_name, factory)
+ set_attributes(factory, data)
+ end
+
+ def apply_config(key, config_class)
+ if @data[key]
+ [@data[key]].flatten.each do |data|
+ config = config_class.new
+ set_attributes(config, data)
+ yield config
+ end
+ end
+ end
+
def set_disk_store
- disk_store = DiskStoreConfiguration.new
- set_attributes(disk_store, @data[DISK_STORE])
- @config.add_disk_store(disk_store)
+ apply_config(DISK_STORE, DiskStoreConfiguration) do |disk_store|
+ @config.add_disk_store(disk_store)
+ end
end
def set_transaction_manager
- if @data[TRANSACTION_MANAGER]
- tx_mgr = create_factory_configuration(@data[TRANSACTION_MANAGER])
- @config.transactionManagerLookup(tx_mgr)
+ apply_config(TRANSACTION_MANAGER, FactoryConfiguration) do |tx_mgr|
+ @config.add_transaction_manager_lookup(tx_mgr)
end
end
def set_event_listener
- if @data[EVENT_LISTENER]
- event_listener = create_factory_configuration(@data[EVENT_LISTENER])
+ apply_config(EVENT_LISTENER, FactoryConfiguration) do |event_listener|
@config.addCacheManagerEventListenerFactory(event_listener)
end
end
def add_peer_providers
- if @data[PEER_PROVIDERS]
- @data[PEER_PROVIDERS].each do |data|
- peer_provider = create_factory_configuration(data)
- @config.addCacheManagerPeerProviderFactory(peer_provider)
- end
+ apply_config(PEER_PROVIDERS, FactoryConfiguration) do |peer_provider|
+ @config.addCacheManagerPeerProviderFactory(peer_provider)
end
end
def add_peer_listeners
- if @data[PEER_LISTENERS]
- @data[PEER_LISTENERS].each do |data|
- peer_listener = create_factory_configuration(data)
- @config.addCacheManagerPeerListenerFactory(peer_listener)
- end
+ apply_config(PEER_LISTENERS, FactoryConfiguration) do |peer_listener|
+ @config.addCacheManagerPeerListenerFactory(peer_listener)
end
end
def set_default_cache
- cache_config = CacheConfiguration.new
- set_attributes(cache_config, @data[DEFAULT_CACHE])
- @config.default_cache_configuration = cache_config
+ apply_config(DEFAULT_CACHE, CacheConfiguration) do |cache_config|
+ @config.default_cache_configuration = cache_config
+ end
end
def add_caches
- if @data[CACHES]
- @data[CACHES].each do |data|
- cache_config = CacheConfiguration.new
- set_attributes(cache_config, data)
- @config.addCache(cache_config)
- end
+ apply_config(CACHES, CacheConfiguration) do |cache_config|
+ @config.add_cache(cache_config)
end
end
View
@@ -46,3 +46,18 @@ caches:
eternal: true
overflow_to_disk: false
disk_persistent: true
+
+- name: sampleCacheWithCacheConfigurations
+ max_elements_in_memory: 10
+ eternal: false
+ time_to_idle_seconds: 100
+ time_to_live_seconds: 100
+ overflow_to_disk: false
+ event_listeners:
+ - class: net.sf.ehcache.distribution.RMICacheReplicatorFactory
+ properties: "replicateAsynchronously=false, replicatePuts=false, replicatePutsViaCopy=false, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=false"
+ - class: net.sf.ehcache.distribution.RMICacheReplicatorFactory
+ properties: "replicateAsynchronously=false, replicatePuts=false, replicatePutsViaCopy=false, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=false"
+# extensions:
+# - class: com.example.FileWatchingCacheRefresherExtensionFactory
+# properties: "refreshIntervalMillis=18000, loaderTimeout=3000, flushPeriod=whatever, someOtherProperty=someValue"
@@ -66,7 +66,7 @@ def setup
must 'have two cache configurations' do
caches = @config.getCacheConfigurations
- assert_equal(2, caches.size)
+ assert_equal(3, caches.size)
assert(caches.containsKey("sampleCache1"), "Should have sampleCache1")
assert(caches.containsKey("sampleCache2"), "Should have sampleCache2")
end
@@ -99,6 +99,34 @@ def setup
end
end
+ must 'have valid sampleCacheWithCacheConfigurations configuration' do
+ cache = @config.getCacheConfigurations['sampleCacheWithCacheConfigurations']
+ expected = {
+ :getMaxElementsInMemory => 10,
+ :isEternal => false,
+ :getTimeToIdleSeconds => 100,
+ :getTimeToLiveSeconds => 100,
+ :isOverflowToDisk => false
+ }
+ expected.each do |key, value|
+ assert_equal(value, cache.send(key))
+ end
+ end
+
+ must 'have 2 event listeners in sampleCacheWithCacheConfigurations' do
+ cache = @config.getCacheConfigurations['sampleCacheWithCacheConfigurations']
+ assert_equal(2, cache.getCacheEventListenerConfigurations.size)
+ expected = {
+ :getFullyQualifiedClassPath => 'net.sf.ehcache.distribution.RMICacheReplicatorFactory',
+ :getProperties => "replicateAsynchronously=false, replicatePuts=false, replicatePutsViaCopy=false, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=false"
+ }
+ cache.getCacheEventListenerConfigurations.each do |event_listener|
+ expected.each do |key, value|
+ assert_equal(value, event_listener.send(key))
+ end
+ end
+ end
+
private
def assert_factory_configuration_equals(factory, values)

0 comments on commit 32acdd3

Please sign in to comment.