jruby-ehcache-rails2 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +15 -0
  4. data/History.txt +15 -0
  5. data/License.txt +23 -0
  6. data/Manifest.txt +37 -0
  7. data/PostInstall.txt +2 -0
  8. data/README.txt +9 -0
  9. data/Rakefile +13 -0
  10. data/VERSION +1 -0
  11. data/config/ehcache.yml +22 -0
  12. data/config/ehcache_manual_rmi.yml +27 -0
  13. data/examples/ehcache.xml +44 -0
  14. data/examples/jruby-ehcache.rb +13 -0
  15. data/ext/ehcache-2.4.6/ehcache-core-2.4.6.jar +0 -0
  16. data/ext/ehcache-2.4.6/ehcache-terracotta-2.4.6.jar +0 -0
  17. data/ext/ehcache-2.4.6/slf4j-api-1.6.1.jar +0 -0
  18. data/ext/ehcache-2.4.6/slf4j-jdk14-1.6.1.jar +0 -0
  19. data/ext/ehcache-2.4.6/terracotta-toolkit-1.3-runtime-3.3.0.jar +0 -0
  20. data/ext/marshaled-ruby-object.jar +0 -0
  21. data/jruby-ehcache-rails2.gemspec +27 -0
  22. data/jruby-ehcache-rails3.gemspec +27 -0
  23. data/jruby-ehcache.gemspec +23 -0
  24. data/lib/ehcache.rb +22 -0
  25. data/lib/ehcache/active_support_store.rb +12 -1
  26. data/lib/ehcache/cache.rb +74 -0
  27. data/lib/ehcache/cache_manager.rb +69 -0
  28. data/lib/ehcache/config.rb +51 -0
  29. data/lib/ehcache/element.rb +36 -0
  30. data/lib/ehcache/java.rb +57 -0
  31. data/lib/ehcache/version.rb +3 -0
  32. data/lib/ehcache/yaml_config.rb +251 -0
  33. data/script/console +10 -0
  34. data/script/destroy +14 -0
  35. data/script/generate +14 -0
  36. data/script/txt2html +82 -0
  37. data/spec/cache_manager_spec.rb +81 -0
  38. data/spec/cache_spec.rb +86 -0
  39. data/spec/spec.opts +2 -0
  40. data/spec/spec_helper.rb +12 -0
  41. data/src/net/sf/ehcache/MarshaledRubyObject.java +15 -0
  42. data/tasks/deployment.rake +34 -0
  43. data/tasks/environment.rake +7 -0
  44. data/tasks/website.rake +17 -0
  45. data/test/ehcache.xml +44 -0
  46. data/test/ehcache.yml +67 -0
  47. data/test/test_cache.rb +36 -0
  48. data/test/test_cache_manager.rb +27 -0
  49. data/test/test_configuration.rb +29 -0
  50. data/test/test_ehcache.rb +22 -0
  51. data/test/test_element.rb +37 -0
  52. data/test/test_helper.rb +22 -0
  53. data/test/test_yaml_config.rb +159 -0
  54. data/website/index.html +141 -0
  55. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  56. data/website/stylesheets/screen.css +138 -0
  57. data/website/template.html.erb +48 -0
  58. metadata +76 -97
@@ -0,0 +1,69 @@
1
+ # Enhance net.sf.ehcache.CacheManager with a more Rubyesque API.
2
+ class Java::NetSfEhcache::CacheManager
3
+ include Enumerable
4
+
5
+ class << self
6
+ alias_method :ehcache_create, :create
7
+
8
+ # Enhanced create that provides for some extra configuration options.
9
+ # Specifically, String arguments may be used where native Ehcache expects
10
+ # java.io.File objects, and if the String refers to a YAML file it will be
11
+ # used as the Configuration source.
12
+ def create(*args)
13
+ process_init_args(*args) do |*args|
14
+ ehcache_create(*args)
15
+ end
16
+ end
17
+ end
18
+
19
+ # Enhanced constructor that provides for some extra configuration options.
20
+ # Specifically, String arguments may be used where native Ehcache expects
21
+ # java.io.File objects, and if the String refers to a YAML file it will be
22
+ # used as the Configuration source.
23
+ def initialize(*args)
24
+ process_init_args(*args) do |*args|
25
+ super(*args)
26
+ end
27
+ end
28
+
29
+ # Iterate through each cache managed by this CacheManager.
30
+ def each
31
+ for name in self.cache_names
32
+ yield self.get_ehcache(name)
33
+ end
34
+ end
35
+
36
+ alias [] get_ehcache
37
+
38
+ def cache(cache_name = '__default_jruby_cache')
39
+ self.add_cache_if_absent(cache_name)
40
+ self.get_ehcache(cache_name)
41
+ end
42
+
43
+ # true if cache by given name is being managed, false otherwise
44
+ def include?(cache_name)
45
+ self.cache_exists(cache_name)
46
+ end
47
+ alias_method :exists?, :include?
48
+ end
49
+
50
+ # Helper method for processing initialization arguments passed to
51
+ # CacheManager.create and CacheManager#initialize.
52
+ def process_init_args(*args)
53
+ args.compact!
54
+ if args.empty?
55
+ # First, look relative to the file that is creating the CacheManager.
56
+ # The expression caller[2] finds the entry in the call stack where
57
+ # CacheManager.new or CacheManager.create was called.
58
+ creator = /^(.+?):\d/.match(caller[2])[1]
59
+ if ehcache_config = Java::NetSfEhcacheConfig::Configuration.find(File.dirname(creator))
60
+ yield(ehcache_config)
61
+ else
62
+ yield
63
+ end
64
+ elsif args.size == 1 && args.first.is_a?(String)
65
+ yield(Ehcache::Config::Configuration.create(args.first))
66
+ else
67
+ yield(*args)
68
+ end
69
+ end
@@ -0,0 +1,51 @@
1
+ require 'ehcache/yaml_config'
2
+
3
+ # Enhance net.sf.ehcache.config.Configuration with a more Rubyesque API, and
4
+ # add support for using YAML for configuration.
5
+ class Java::NetSfEhcacheConfig::Configuration
6
+ Factory = Java::NetSfEhcacheConfig::ConfigurationFactory
7
+
8
+ # Searches for an Ehcache configuration file and, if found, returns a
9
+ # Configuration object created from it. The search algorithm looks for
10
+ # files named "ehcache.yml" or "ehcache.xml", first looking in the provided
11
+ # directories in order, and if not found there then looking in the Ruby
12
+ # $LOAD_PATH.
13
+ # Returns nil if no configuration file is found.
14
+ def self.find(*dirs)
15
+ file_names = %w[ehcache.yml ehcache.xml]
16
+ dirs += $LOAD_PATH
17
+ dirs.each do |dir|
18
+ file_names.each do |name|
19
+ candidate = File.join(dir, name)
20
+ return create(candidate) if File.exist?(candidate)
21
+ end
22
+ end
23
+ nil
24
+ end
25
+
26
+ def self.create(*args)
27
+ result = nil
28
+ case args.size
29
+ when 0
30
+ result = Factory.parseConfiguration()
31
+ when 1
32
+ arg = args.first
33
+
34
+ if arg.is_a?(String)
35
+ raise ArgumentError, "Cannot read config file '#{arg}'" unless File.readable?(arg)
36
+ if arg =~ /\.yml$/
37
+ result = Ehcache::Config::YamlConfig.parse_yaml_config(arg)
38
+ else
39
+ result = Factory.parseConfiguration(java.io.File.new(arg))
40
+ end
41
+ else
42
+ result = Factory.parseConfiguration(arg)
43
+ end
44
+ end
45
+
46
+ unless result.is_a?(self)
47
+ raise ArgumentError, "Could not create Configuration from: #{args.inspect}"
48
+ end
49
+ result
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ # Enhance net.sf.ehcache.Element with a more Rubyesque API.
2
+ class Java::NetSfEhcache::Element
3
+ def self.create(key, value, options = {})
4
+ result = self.new(key, value)
5
+ options.each do |key, value|
6
+ setter = "#{key}=".to_sym
7
+ result.send(setter, value) if result.respond_to?(setter)
8
+ end
9
+ result
10
+ end
11
+
12
+ alias element_value value
13
+
14
+ # Wrap the Element#value method to unmarshal Ruby objects if necessary.
15
+ def value
16
+ val = element_value
17
+ if val.kind_of?(Java::NetSfEhcache::MarshaledRubyObject)
18
+ Marshal.load(String.from_java_bytes(val.bytes))
19
+ else
20
+ val
21
+ end
22
+ end
23
+
24
+ alias tti getTimeToIdle
25
+ alias ttl getTimeToLive
26
+
27
+ alias tti= setTimeToIdle
28
+ alias ttl= setTimeToLive
29
+
30
+ alias expires_in getTimeToLive
31
+ def expires_in=(seconds)
32
+ setTimeToLive(seconds.to_i)
33
+ end
34
+ alias expiresIn expires_in
35
+ alias expiresIn= expires_in=
36
+ end
@@ -0,0 +1,57 @@
1
+ require 'java'
2
+
3
+ EHCACHE_LIBS_DIR = "#{Ehcache::EHCACHE_HOME}/ext"
4
+
5
+ module Ehcache
6
+ module Rails
7
+ RAILS_ROOT_DIR = if defined?(::Rails)
8
+ ::Rails.root.to_s
9
+ elsif defined?(RAILS_ROOT)
10
+ RAILS_ROOT
11
+ end
12
+ if RAILS_ROOT_DIR
13
+ RAILS_LIB_DIR = File.join(RAILS_ROOT_DIR, 'lib')
14
+ Dir["#{RAILS_LIB_DIR}/**/*.jar"].each do |jar|
15
+ $CLASSPATH << File.expand_path(jar)
16
+ end
17
+ end
18
+ end
19
+ Dir["#{EHCACHE_LIBS_DIR}/**/*.jar"].each do |jar|
20
+ $CLASSPATH << File.expand_path(jar)
21
+ end
22
+ LOG = Java::OrgSlf4j::LoggerFactory.getLogger("JRubyEhcache")
23
+ LOG.info("Using Ehcache version #{Java::NetSfEhcacheUtil::ProductInfo.new.getVersion()}")
24
+ =begin
25
+ slf4j_loader = lambda { Java::OrgSlf4j::LoggerFactory.getLogger("JRubyEhcache") }
26
+ begin
27
+ LOG = slf4j_loader.call
28
+ LOG.info("Using SLF4J Logger from CLASSPATH")
29
+ rescue NameError
30
+ Dir["#{EHCACHE_LIBS_DIR}/**/*slf4j*.jar"].each do |l| require l end
31
+ LOG = slf4j_loader.call
32
+ LOG.info("Using bundled SLF4J Logger")
33
+ end
34
+
35
+ ehcache_version_loader = lambda {
36
+ Java::NetSfEhcacheUtil::ProductInfo.new.getVersion()
37
+ }
38
+ begin
39
+ # If Ehcache is already on the classpath, use it.
40
+ VERSION = ehcache_version_loader.call
41
+ LOG.info("Using Ehcache #{VERSION} from CLASSPATH")
42
+ rescue NameError
43
+ # If not, use the Ehcache bundled with the jruby-ehcache gem.
44
+ Dir["#{EHCACHE_LIBS_DIR}/**/*.jar"].each do |l|
45
+ require l unless l =~ /slf4j/
46
+ end
47
+ VERSION = ehcache_version_loader.call
48
+ LOG.info("Using bundled Ehcache #{VERSION}")
49
+ end
50
+ =end
51
+ include_package 'net.sf.ehcache'
52
+
53
+ module Config
54
+ include_package 'net.sf.ehcache.config'
55
+ end
56
+
57
+ end
@@ -0,0 +1,3 @@
1
+ module Ehcache
2
+ VERSION = "1.3.0"
3
+ end
@@ -0,0 +1,251 @@
1
+ require 'java'
2
+ require 'ehcache'
3
+ require 'yaml'
4
+ require 'erb'
5
+
6
+ module Ehcache::Config
7
+ # Support for using YAML for Ehcache configuration.
8
+ #
9
+ # <strong>DEPRECATED:</strong> Please use ehcache.xml instead.
10
+ #
11
+ # YAML configuration is similar to XML configuration, but there are some
12
+ # changes to the names of configuration elements to make them simpler or
13
+ # more idiomatic to Ruby and YAML conventions. The changes are described
14
+ # below. For full documentation on the Ehcache configuration elements,
15
+ # see the Ehcache Cache Configuration documentation:
16
+ # http://ehcache.org/documentation/configuration.html
17
+ #
18
+ # The top level YAML configuration attributes and the corresponding XML
19
+ # elements or attributes are shown in the following table.
20
+ #
21
+ # name:: name attribute on ehcache element
22
+ # update_check:: updateCheck attribute on ehcache element
23
+ # monitoring:: monitoring attribute on ehcache element
24
+ # dynamic_config:: dynamicConfig attribute on ehcache element
25
+ # disk_store:: diskStore element
26
+ # transaction_manager:: transactionManagerLookup element
27
+ # event_listener:: cacheManagerEventListenerFactory element
28
+ # peer_providers (Array):: cacheManagerPeerProviderFactory elements
29
+ # peer_listeners (Array):: cacheManagerPeerListenerFactory elements
30
+ # terracotta_config:: terracottaConfig element
31
+ # default_cache:: defaultCache element
32
+ # caches (Array):: cache elements
33
+ #
34
+ # Each top level configuration attribute contains a set of key/value pairs
35
+ # that are equivalent to the Ehcache XML attributes, except that the
36
+ # attribute names are converted to use underscore_names instead of
37
+ # camelCaseNames. For instance, the Ehcache XML attribute
38
+ # diskSpoolBufferSizeMB becomes disk_spool_buffer_size_mb in YAML.
39
+ #
40
+ # Entries in the above table that are marked as (Array) should be YAML lists
41
+ # to allow for multiple values. So, for example, to configure multiple
42
+ # caches in your YAML configuration, use the following syntax:
43
+ #
44
+ # caches:
45
+ # - name: my_cache
46
+ # time_to_idle_seconds: 360
47
+ # time_to_live_seconds: 1000
48
+ # - name: my_other_cache
49
+ # max_elements_in_memory: 1000
50
+ # eternal: true
51
+ # overflow_to_disk: false
52
+ # disk_persistent: true
53
+ #
54
+ # Note the use of the '-' to separate list elements.
55
+ #
56
+ # One further difference between YAML configuration and XML configuration
57
+ # deals with cache configuration. The XML configuration allows for a set
58
+ # of XML sub elements to configure various aspects of caches (or the default
59
+ # cache). In YAML, these sub elements are translated to attributes within
60
+ # the cache configuration (or default_cache configuration) that
61
+ # refer to Hashes or Arrays. The following table shows the mapping.
62
+ #
63
+ # event_listeners (Array):: cacheEventListenerFactory sub elements
64
+ # extensions (Array):: cacheExtensionFactory sub elements
65
+ # loaders (Array):: cacheLoaderFactory sub elements
66
+ # decorators (Array):: cacheDecoratorFactory sub elements
67
+ # bootstrap_loader (Hash):: bootstrapCacheLoaderFactory sub element
68
+ # exception_handler (Hash):: cacheExceptionHandlerFactory sub element
69
+ # terracotta (Hash):: terracotta sub element
70
+ # cache_writer (Hash):: cacheWriter sub element
71
+ # copy_strategy (Hash):: copyStrategy sub element
72
+ #
73
+ # Those marked as (Array) may take a list of values, while those marked as
74
+ # (Hash) may take a single Hash value (set of key/value pairs). Here is an
75
+ # example of a cache configuration that uses one of each style:
76
+ #
77
+ # caches:
78
+ # - name: some_cache
79
+ # time_to_live_seconds: 100
80
+ # event_listeners:
81
+ # - class: net.sf.ehcache.distribution.RMICacheReplicatorFactory
82
+ # properties: "replicateAsynchronously=false"
83
+ # copy_strategy:
84
+ # class: net.sf.ehcache.store.compound.SerializationCopyStrategy
85
+ #
86
+ # Note again the use of the '-' character to separate list elements in the
87
+ # case of Array values, which is not present for Hash values.
88
+ module YamlConfig
89
+
90
+ InvalidYamlConfiguration = Class.new(StandardError)
91
+
92
+ # Not sure why, but "include Java::NetSfEhcacheConfig" does not work,
93
+ # so define local constants referring to the Ehcache classes
94
+ Configuration = Java::NetSfEhcacheConfig::Configuration
95
+ CacheConfiguration = Java::NetSfEhcacheConfig::CacheConfiguration
96
+ DiskStoreConfiguration = Java::NetSfEhcacheConfig::DiskStoreConfiguration
97
+ FactoryConfiguration = Java::NetSfEhcacheConfig::FactoryConfiguration
98
+
99
+ %w[name update_check monitoring dynamic_config
100
+ disk_store transaction_manager event_listener
101
+ peer_providers peer_listeners
102
+ terracotta_config default_cache caches
103
+ event_listeners extensions loaders decorators
104
+ ].each do |attribute|
105
+ const_set(attribute.upcase.to_sym, attribute)
106
+ end
107
+
108
+ # Parses the given yaml_config_file and returns a corresponding
109
+ # Ehcache::Config::Configuration object.
110
+ def self.parse_yaml_config(yaml_config_file)
111
+ Ehcache::LOG.warn("YAML configuration is deprecated. Please use ehcache.xml instead.")
112
+ YamlConfigBuilder.new(yaml_config_file).build
113
+ end
114
+
115
+ private
116
+
117
+ class YamlConfigBuilder
118
+
119
+ def initialize(yaml_file)
120
+ @yaml_file = yaml_file
121
+ @data = YAML.load(ERB.new(File.read(yaml_file)).result(binding))
122
+ raise InvalidYamlConfiguration unless valid?(@data)
123
+ end
124
+
125
+ def build
126
+ @config = Configuration.new
127
+ for attribute in [NAME, UPDATE_CHECK, MONITORING, DYNAMIC_CONFIG]
128
+ set_if_present(attribute)
129
+ end
130
+ set_disk_store
131
+ set_transaction_manager
132
+ set_event_listener
133
+ add_peer_providers
134
+ add_peer_listeners
135
+ set_default_cache
136
+ add_caches
137
+ @config
138
+ end
139
+
140
+ private
141
+
142
+ def valid?(data)
143
+ data.is_a?(Hash)
144
+ end
145
+
146
+ def set_if_present(key)
147
+ if @data.has_key?(key)
148
+ setter = "#{key}=".to_sym
149
+ @config.send(setter, @data[key])
150
+ end
151
+ end
152
+
153
+ def set_attributes(object, attributes)
154
+ attributes ||= []
155
+ attributes.each do |key, value|
156
+ if value.is_a?(Hash) || value.is_a?(Array)
157
+ create_cache_config_factories(object, key, value)
158
+ else
159
+ object.send("#{key}=", value)
160
+ end
161
+ end
162
+ object
163
+ end
164
+
165
+ def create_cache_config_factories(cache, key, value)
166
+ [value].flatten.each do |data|
167
+ create_cache_config_factory(cache, key, data)
168
+ end
169
+ end
170
+
171
+ def names_for_factory(key)
172
+ singular = key.singularize.sub(/s$/, '')
173
+ factory_name = if key == 'bootstrap_loader'
174
+ "BootstrapCacheLoaderFactory"
175
+ else
176
+ "Cache#{singular.camelize}Factory"
177
+ end
178
+ class_name = "#{factory_name}Configuration"
179
+ method_name = "add#{factory_name}"
180
+ return [class_name, method_name]
181
+ end
182
+
183
+ def create_cache_config_factory(cache, key, data)
184
+ class_name, method_name = names_for_factory(key)
185
+ factory_class = CacheConfiguration.const_get(class_name)
186
+ factory = factory_class.new
187
+
188
+ cache.send(method_name, factory)
189
+ set_attributes(factory, data)
190
+ end
191
+
192
+ def apply_config(key, config_class)
193
+ if @data[key]
194
+ [@data[key]].flatten.each do |data|
195
+ config = config_class.new
196
+ set_attributes(config, data)
197
+ yield config
198
+ end
199
+ end
200
+ end
201
+
202
+ def set_disk_store
203
+ apply_config(DISK_STORE, DiskStoreConfiguration) do |disk_store|
204
+ @config.add_disk_store(disk_store)
205
+ end
206
+ end
207
+
208
+ def set_transaction_manager
209
+ apply_config(TRANSACTION_MANAGER, FactoryConfiguration) do |tx_mgr|
210
+ @config.add_transaction_manager_lookup(tx_mgr)
211
+ end
212
+ end
213
+
214
+ def set_event_listener
215
+ apply_config(EVENT_LISTENER, FactoryConfiguration) do |event_listener|
216
+ @config.addCacheManagerEventListenerFactory(event_listener)
217
+ end
218
+ end
219
+
220
+ def add_peer_providers
221
+ apply_config(PEER_PROVIDERS, FactoryConfiguration) do |peer_provider|
222
+ @config.addCacheManagerPeerProviderFactory(peer_provider)
223
+ end
224
+ end
225
+
226
+ def add_peer_listeners
227
+ apply_config(PEER_LISTENERS, FactoryConfiguration) do |peer_listener|
228
+ @config.addCacheManagerPeerListenerFactory(peer_listener)
229
+ end
230
+ end
231
+
232
+ def set_default_cache
233
+ apply_config(DEFAULT_CACHE, CacheConfiguration) do |cache_config|
234
+ @config.default_cache_configuration = cache_config
235
+ end
236
+ end
237
+
238
+ def add_caches
239
+ apply_config(CACHES, CacheConfiguration) do |cache_config|
240
+ @config.add_cache(cache_config)
241
+ end
242
+ end
243
+
244
+ def create_factory_configuration(data)
245
+ result = FactoryConfiguration.new
246
+ set_attributes(result, data)
247
+ result
248
+ end
249
+ end
250
+ end
251
+ end