require 'yaml' require 'madeleine/zmarshal' require 'soap/marshal' module Madeleine # Automatic commands for Madeleine # # Author:: Stephen Sykes # Copyright:: Copyright (C) 2003-2004 # Version:: 0.41 # # This module provides a way of automatically generating command objects for madeleine to # store. It works by making a proxy object for all objects of any classes in which it is included. # Method calls to these objects are intercepted, and stored as a command before being # passed on to the real receiver. The command objects remember which object the command was # destined for by using a pair of internal ids that are contained in each of the proxy objects. # # There is also a mechanism for specifying which methods not to intercept calls to by using # automatic_read_only, and its opposite automatic_read_write. # # Should you require it, the snapshots can be stored as yaml, and can be compressed. Just pass # the marshaller you want to use as the second argument to AutomaticSnapshotMadeleine.new. # If the passed marshaller did not successfully deserialize the latest snapshot, the system # will try to automatically detect and read either Marshal, YAML, SOAP, or their corresponding # compressed versions. # # This module is designed to work correctly in the case there are multiple madeleine systems in use by # a single program, and is also safe to use with threads. # # Usage: # # require 'madeleine' # require 'madeleine/automatic' # # class A # include Madeleine::Automatic::Interceptor # attr_reader :foo # automatic_read_only :foo # def initialize(param1, ...) # ... # end # def some_method(paramA, ...) # ... # end # automatic_read_only # def bigfoo # foo.upcase # end # end # # mad = AutomaticSnapshotMadeleine.new("storage_directory") { A.new(param1, ...) } # # mad.system.some_method(paramA, ...) # logged as a command by madeleine # print mad.foo # not logged # print mad.bigfoo # not logged # mad.take_snapshot # module Automatic # # This module should be included (at the top) in any classes that are to be persisted. # It will intercept method calls and make sure they are converted into commands that are logged by Madeleine. # It does this by returning a Prox object that is a proxy for the real object. # # It also handles automatic_read_only and automatic_read_write, allowing user specification of which methods # should be made into commands # module Interceptor # # When included, redefine new so that we can return a Prox object instead, and define methods to handle # keeping track of which methods are read only # def self.included(klass) class < "") x end end # # The AutomaticSnapshotMadeleine class contains an instance of the persister # (default is SnapshotMadeleine) and provides additional automatic functionality. # # The class is instantiated the same way as SnapshotMadeleine: # madeleine_sys = AutomaticSnapshotMadeleine.new("storage_directory") { A.new(param1, ...) } # The second initialisation parameter is the persister. Supported persisters are: # # * Marshal (default) # * YAML # * SOAP::Marshal # * Madeleine::ZMarshal.new(Marshal) # * Madeleine::ZMarshal.new(YAML) # * Madeleine::ZMarshal.new(SOAP::Marshal) # # The class keeps a record of all the systems that currently exist. # Each instance of the class keeps a record of Prox objects in that system by internal id (myid). # # We also add functionality to take_snapshot in order to set things up so that the custom Prox object # marshalling will work correctly. # class AutomaticSnapshotMadeleine attr_accessor :marshaller attr_reader :list, :sysid def initialize(directory_name, marshaller=Marshal, persister=SnapshotMadeleine, &new_system_block) @sysid ||= Time.now.to_f.to_s + Thread.current.object_id.to_s # Gererate a new sysid @myid_count = 0 @list = {} Thread.current[:system] = self # during system startup system should not create commands Thread.critical = true @@systems ||= {} # holds systems by sysid @@systems[@sysid] = self Thread.critical = false @marshaller = marshaller # until attrb begin @persister = persister.new(directory_name, Automatic_marshaller, &new_system_block) @list.delete_if {|k,v| # set all the prox objects that now exist to have the right sysid begin ObjectSpace._id2ref(v).sysid = @sysid false rescue RangeError true # Id was to a GC'd object, delete it end } ensure Thread.current[:system] = false end end # # Add a proxy object to the list, return the myid for that object # def add(proxo) @list[@myid_count += 1] = proxo.object_id @myid_count end # # Restore a marshalled proxy object to list - myid_count is increased as required. # If the object already exists in the system then the existing object must be used. # def restore(proxo) if (@list[proxo.myid]) proxo = myid2ref(proxo.myid) else @list[proxo.myid] = proxo.object_id @myid_count = proxo.myid if (@myid_count < proxo.myid) end proxo end # # Returns a reference to the object indicated by the internal id supplied. # def myid2ref(myid) raise "Internal id #{myid} not found" unless objid = @list[myid] ObjectSpace._id2ref(objid) end # # Take a snapshot of the system. # def take_snapshot begin Thread.current[:system] = self Thread.current[:snapshot_memory] = {} @persister.take_snapshot ensure Thread.current[:snapshot_memory] = nil Thread.current[:system] = false end end # # Returns the hash containing the systems. # def AutomaticSnapshotMadeleine.systems @@systems end # # Close method changes the sysid for Prox objects so they can't be mistaken for real ones in a new # system before GC gets them # def close begin @list.each_key {|k| myid2ref(k).sysid = nil} rescue RangeError # do nothing end @persister.close end # # Pass on any other calls to the persister # def method_missing(symbol, *args, &block) @persister.send(symbol, *args, &block) end end module Deserialize #:nodoc: # # Detect format of an io stream. Leave it rewound. # def Deserialize.detect(io) c = io.getc c1 = io.getc io.rewind if (c == Marshal::MAJOR_VERSION && c1 <= Marshal::MINOR_VERSION) Marshal elsif (c == 31 && c1 == 139) # gzip magic numbers ZMarshal else while (s = io.gets) break if (s !~ /^\s*$/) # ignore blank lines end io.rewind if (s && s =~ /^\s*<\?[xX][mM][lL]/) # " e io.rewind detected_marshaller = detect(io) if (detected_marshaller == ZMarshal) zio = Zlib::GzipReader.new(io) detected_zmarshaller = detect(zio) zio.finish io.rewind if (detected_zmarshaller) ZMarshal.new(detected_zmarshaller).load(io) else raise e end elsif (detected_marshaller) detected_marshaller.load(io) else raise e end end end end end end AutomaticSnapshotMadeleine = Madeleine::Automatic::AutomaticSnapshotMadeleine