lib/openwfe/utils.rb in ruote-0.9.19 vs lib/openwfe/utils.rb in ruote-0.9.20

- old
+ new

@@ -1,43 +1,29 @@ -# #-- -# Copyright (c) 2005-2008, John Mettraux, OpenWFE.org -# All rights reserved. +# Copyright (c) 2005-2009, John Mettraux, jmettraux@gmail.com # -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: # -# . Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. # -# . 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. +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. # -# . 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. +# Made in Japan and Costa Rica. #++ -# -# -# "hecho en Costa Rica" and "made in Japan" -# -# john.mettraux@openwfe.org -# require 'openwfe/rexml' require 'tmpdir' require 'open-uri' @@ -70,11 +56,11 @@ # # an automatic dup implementation attempt # def OpenWFE.fulldup (object) - return object.fulldup if object.respond_to?("fulldup") + return object.fulldup if object.respond_to?(:fulldup) # trusting client objects providing a fulldup() implementation # Tomaso Tosolini 2007.12.11 begin return Marshal.load(Marshal.dump(object)) @@ -100,13 +86,11 @@ object.each { |k, v| o[fulldup(k)] = fulldup(v) } end # # duplicate the attributes of the object object.instance_variables.each do |v| - #puts "v is #{v}" value = object.instance_variable_get(v) - #puts "value is '#{value}'" value = fulldup(value) begin o.instance_variable_set(v, value) rescue # ignore, must be readonly @@ -116,16 +100,16 @@ o end def OpenWFE.to_underscore (string) - string.gsub("-", "_") + string.gsub('-', '_') end def OpenWFE.to_dash (string) - string.gsub("_", "-") + string.gsub('_', '-') end def OpenWFE.symbol_to_name (symbol) to_dash(symbol.to_s) @@ -141,62 +125,59 @@ # Turns all the spaces in string into underscores. # Returns the new String. # def OpenWFE.stu (s) - s.gsub("\s", "_") + s.gsub("\s", '_') end # # Returns an URI if the string is one, else returns nil. # No exception is thrown by this method. # def OpenWFE.parse_uri (string) - return nil if string.split("\n").size > 1 + return nil if string.index("\n") + # cheap initial test - begin - return URI::parse(string) - rescue Exception => e - end - - nil + URI.parse(string) rescue nil end # # Returns a URI instance if the given ref is a http, https, file or ftp # URI. Returns nil else. # The sister method parse_uri() is OK with things like mailto:, gopher:, ... # def OpenWFE.parse_known_uri (ref) - uri = OpenWFE::parse_uri(ref.to_s) + uri = OpenWFE.parse_uri(ref.to_s) return nil unless uri - return uri if uri.scheme == "file" - return uri if uri.scheme == "http" - return uri if uri.scheme == "https" - return uri if uri.scheme == "ftp" + return uri if uri.scheme == 'file' + return uri if uri.scheme == 'http' + return uri if uri.scheme == 'https' + return uri if uri.scheme == 'ftp' # what else ... nil end - # + #-- # Returns true if the given string starts with the 'start' string. # - def OpenWFE.starts_with (string, start) - # - # my favourite way of doing that would be by adding this - # method to the String class, but that could be intrusive - # (as OpenWFE is meant at first as an embeddable workflow engine). - # - return false unless string - return false if string.length < start.length - string[0, start.length] == start - end + #def OpenWFE.starts_with (string, start) + # # + # # my favourite way of doing that would be by adding this + # # method to the String class, but that could be intrusive + # # (as OpenWFE is meant at first as an embeddable workflow engine). + # # + # return false unless string + # return false if string.length < start.length + # string[0, start.length] == start + #end + #++ # # Returns true if the given string ends with the '_end' string. # def OpenWFE.ends_with (string, _end) @@ -207,14 +188,11 @@ # # Attempts at displaying a nice stack trace # def OpenWFE.exception_to_s (exception) - s = "exception : " - s << "#{exception}\n" - s << exception.backtrace.join("\n") - s + "exception : #{exception}\n#{exception.backtrace.join("\n")}" end # # Pretty printing a caller() array # @@ -225,25 +203,11 @@ s << " #{line}\n" end s end - # - # Sets the name of the current thread (the attribute :name if it is - # a ruby thread, the java thread name if we're in JRuby) - # - def OpenWFE.set_current_thread_name (name) - - if defined?(JRUBY_VERSION) - require 'java' - java.lang.Thread.current_thread.name = "#{name} (Ruby Thread)" - end - - Thread.current[:name] = name - end - - # + #-- # Some code for writing thinks like : # # if async # OpenWFE::call_in_thread "launch()", self do # raw_expression.apply(wi) @@ -252,40 +216,34 @@ # raw_expression.apply(wi) # end # # Returns the new thread instance. # - def OpenWFE.call_in_thread (caller_name, caller_object=nil, &block) + #def OpenWFE.call_in_thread (caller_name, caller_object=nil, &block) + # return nil unless block + # t = Thread.new do + # begin + # #$SAFE = safe_level + # # + # # (note) + # # doesn't work : the block inherits the safety level + # # of its surroundings, it's a closure, ne ? + # block.call + # rescue Exception => e + # msg = "#{caller_name} caught an exception\n" + exception_to_s(e) + # if caller_object and caller_object.respond_to? :lwarn + # caller_object.lwarn { msg } + # else + # puts msg + # end + # end + # end + # t[:name] = caller_name + # t + #end + #++ - return unless block - - Thread.new do - - #Thread.current[:name] = caller_name - set_current_thread_name caller_name - - begin - #$SAFE = safe_level - # - # (note) - # doesn't work : the block inherits the safety level - # of its surroundings, it's a closure, ne ? - - block.call - - rescue Exception => e - msg = "#{caller_name} caught an exception\n" + exception_to_s(e) - if caller_object and caller_object.respond_to? :lwarn - caller_object.lwarn { msg } - else - puts msg - end - end - end - # returns the thread - end - # # A small Timer class for debug purposes. # # t = Time.new # @@ -498,11 +456,11 @@ return i if i != 0 key end # - # looks up in a container (something that has a [] method) with a key, + # Looks up in a container (something that has a [] method) with a key, # if nothing has been found and the key is a number, turns the # the number into a String a does a last lookup. # def OpenWFE.flex_lookup (container, key) @@ -520,8 +478,7 @@ end end value end - end