# #-- # Copyright (c) 2006-2007, Nicolas Modryzk and John Mettraux, OpenWFE.org # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # . Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # . Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # . Neither the name of the "OpenWFE" nor the names of its contributors may be # used to endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. #++ # # $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $ # # # "made in Japan" # # Nicolas Modrzyk at openwfe.org # John Mettraux at openwfe.org # require 'yaml' require 'fileutils' require 'openwfe/utils' require 'openwfe/service' require 'openwfe/expressions/flowexpression' require 'openwfe/expressions/raw_xml' # # making sure classes in those files are loaded # before their yaml persistence is tuned # (else the reopening of the class is interpreted as # a definition of the class...) module OpenWFE # # yaml expression storage # class YamlFileStorage include ServiceMixin attr_accessor :basepath def initialize (service_name, application_context, path) service_init(service_name, application_context) @basepath = path FileUtils.makedirs @basepath end def []= (id, object) fei_path = compute_file_path(id) fei_parent_path = File.dirname(fei_path) FileUtils.makedirs(fei_parent_path) \ if not File.exist?(fei_parent_path) fd = IO.sysopen(fei_path , "w+") io = IO.open(fd , "w+") data = YAML.dump(object) io.write(data) io.close end # # deletes the whole storage directory... beware... # def purge FileUtils.remove_dir @basepath end # # check whether the key has a file in the file storage def has_key? (fei) File.exist?(compute_file_path(fei)) end def remove (fei, workitem) fei_path = compute_file_path(fei) if File.exist?(fei_path) File.delete(fei_path) else raise "Object not found at #{fei_path}" end end def [] (fei) fei_path = compute_file_path(fei) return nil if not File.exist?(fei_path) data = IO.read(compute_file_path(fei)) object = YAML.load(data) object.application_context = @application_context return object end def length return count_expression(0, @basepath) end protected def count_expression (count, item) return count + 1 if OpenWFE::ends_with(item, ".yaml") if File.stat(item).directory? d = Dir.new(item) d.each do |i| next if i == "." or i == ".." i = item + "/" + i count = count_expression(count, i) end d.close() end return count end def compute_file_path (fei) raise "this should be implemented in a subclass" end end end