openwferu 0.9.16 → 0.9.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. data/examples/about_state.rb +81 -0
  2. data/examples/engine_template.rb +7 -0
  3. data/lib/openwfe/contextual.rb +2 -2
  4. data/lib/openwfe/def.rb +2 -3
  5. data/lib/openwfe/{util/schedulers.rb → engine.rb} +3 -39
  6. data/lib/openwfe/engine/engine.rb +202 -251
  7. data/lib/openwfe/engine/process_status.rb +359 -0
  8. data/lib/openwfe/expool/errorjournal.rb +6 -6
  9. data/lib/openwfe/expool/expressionpool.rb +161 -239
  10. data/lib/openwfe/expool/expstorage.rb +185 -55
  11. data/lib/openwfe/expool/journal.rb +1 -2
  12. data/lib/openwfe/expool/parser.rb +233 -0
  13. data/lib/openwfe/expool/threadedexpstorage.rb +6 -18
  14. data/lib/openwfe/expool/wfidgen.rb +25 -7
  15. data/lib/openwfe/expool/yamlexpstorage.rb +60 -37
  16. data/lib/openwfe/expressions/condition.rb +49 -12
  17. data/lib/openwfe/expressions/environment.rb +45 -15
  18. data/lib/openwfe/expressions/expressionmap.rb +39 -19
  19. data/lib/openwfe/expressions/fe_concurrence.rb +24 -13
  20. data/lib/openwfe/expressions/fe_cron.rb +19 -18
  21. data/lib/openwfe/expressions/fe_cursor.rb +69 -28
  22. data/lib/openwfe/expressions/fe_define.rb +4 -1
  23. data/lib/openwfe/expressions/fe_do.rb +1 -3
  24. data/lib/openwfe/expressions/fe_equals.rb +131 -20
  25. data/lib/openwfe/expressions/fe_fqv.rb +27 -3
  26. data/lib/openwfe/expressions/fe_iterator.rb +14 -7
  27. data/lib/openwfe/expressions/fe_listen.rb +7 -2
  28. data/lib/openwfe/expressions/fe_misc.rb +187 -20
  29. data/lib/openwfe/expressions/fe_participant.rb +8 -7
  30. data/lib/openwfe/expressions/fe_reserve.rb +105 -33
  31. data/lib/openwfe/expressions/fe_save.rb +55 -5
  32. data/lib/openwfe/expressions/{fe_value.rb → fe_set.rb} +6 -82
  33. data/lib/openwfe/expressions/fe_sleep.rb +25 -15
  34. data/lib/openwfe/expressions/fe_subprocess.rb +2 -2
  35. data/lib/openwfe/expressions/fe_wait.rb +3 -2
  36. data/lib/openwfe/expressions/fe_when.rb +7 -15
  37. data/lib/openwfe/expressions/flowexpression.rb +90 -49
  38. data/lib/openwfe/expressions/merge.rb +7 -1
  39. data/lib/openwfe/expressions/raw.rb +261 -63
  40. data/lib/openwfe/expressions/{raw_prog.rb → rprocdef.rb} +94 -179
  41. data/lib/openwfe/expressions/time.rb +36 -12
  42. data/lib/openwfe/expressions/timeout.rb +9 -7
  43. data/lib/openwfe/expressions/value.rb +126 -0
  44. data/lib/openwfe/flowexpressionid.rb +52 -22
  45. data/lib/openwfe/listeners/listeners.rb +3 -3
  46. data/lib/openwfe/listeners/socketlisteners.rb +8 -5
  47. data/lib/openwfe/logging.rb +6 -3
  48. data/lib/openwfe/omixins.rb +8 -6
  49. data/lib/openwfe/orest/xmlcodec.rb +16 -12
  50. data/lib/openwfe/participants.rb +38 -0
  51. data/lib/openwfe/participants/participant.rb +1 -1
  52. data/lib/openwfe/participants/participantmap.rb +24 -10
  53. data/lib/openwfe/participants/participants.rb +4 -3
  54. data/lib/openwfe/participants/soapparticipants.rb +1 -1
  55. data/lib/openwfe/participants/socketparticipants.rb +1 -1
  56. data/lib/openwfe/rudefinitions.rb +7 -5
  57. data/lib/openwfe/storage/yamlcustom.rb +10 -10
  58. data/lib/openwfe/storage/yamlfilestorage.rb +12 -12
  59. data/lib/openwfe/tools/flowtracer.rb +6 -5
  60. data/lib/openwfe/util/dollar.rb +42 -85
  61. data/lib/openwfe/util/ometa.rb +1 -3
  62. data/lib/openwfe/util/workqueue.rb +1 -1
  63. data/lib/openwfe/utils.rb +33 -11
  64. data/lib/openwfe/version.rb +2 -2
  65. data/lib/openwfe/workitem.rb +76 -14
  66. data/lib/openwfe/worklist/storelocks.rb +9 -4
  67. data/lib/openwfe/worklist/storeparticipant.rb +1 -1
  68. data/test/back_0916_test.rb +101 -0
  69. data/test/bm/ft_26_load.rb +1 -1
  70. data/test/bm/ft_26b_load.rb +1 -1
  71. data/test/bm/ft_26c_load.rb +3 -2
  72. data/test/bm/ft_26d_load.rb +97 -0
  73. data/test/bm/ft_recu.rb +71 -0
  74. data/test/concurrence_test.rb +1 -1
  75. data/test/condition_test.rb +152 -0
  76. data/test/description_test.rb +12 -7
  77. data/test/eno_test.rb +1 -1
  78. data/test/expool_20031219_0916.tgz +0 -0
  79. data/test/fe_lookup_att_test.rb +1 -1
  80. data/test/fei_test.rb +16 -0
  81. data/test/file_persistence_test.rb +8 -12
  82. data/test/filep_cancel_test.rb +116 -0
  83. data/test/flowtestbase.rb +47 -25
  84. data/test/ft_0.rb +1 -1
  85. data/test/ft_10_loop.rb +29 -14
  86. data/test/{ft_10b_loop2.rb → ft_10b_loop.rb} +2 -11
  87. data/test/ft_11_ppd.rb +6 -17
  88. data/test/ft_11b_ppd.rb +1 -4
  89. data/test/ft_12_blockparticipant.rb +1 -1
  90. data/test/ft_13_eno.rb +1 -1
  91. data/test/ft_15_iterator.rb +1 -1
  92. data/test/ft_15b_iterator.rb +1 -1
  93. data/test/ft_17_condition.rb +6 -6
  94. data/test/ft_18_pname.rb +1 -1
  95. data/test/ft_20_cron.rb +1 -1
  96. data/test/ft_21_cron.rb +6 -4
  97. data/test/ft_22_history.rb +1 -1
  98. data/test/ft_23_when.rb +1 -1
  99. data/test/ft_23b_when.rb +18 -6
  100. data/test/ft_23c_wait.rb +8 -6
  101. data/test/ft_25_cancel.rb +7 -5
  102. data/test/ft_27_getflowpos.rb +22 -17
  103. data/test/ft_28_fileparticipant.rb +1 -2
  104. data/test/ft_2_concurrence.rb +1 -1
  105. data/test/ft_2b_concurrence.rb +25 -20
  106. data/test/ft_30_socketlistener.rb +0 -3
  107. data/test/ft_34_cancelwfid.rb +9 -9
  108. data/test/ft_35_localdefs.rb +0 -1
  109. data/test/ft_36_subprocids.rb +6 -6
  110. data/test/ft_38_tag.rb +3 -2
  111. data/test/ft_38b_tag.rb +229 -0
  112. data/test/ft_39_reserve.rb +3 -18
  113. data/test/ft_39b_reserve.rb +34 -5
  114. data/test/ft_3b_lookup_vf.rb +83 -0
  115. data/test/ft_40_defined.rb +2 -11
  116. data/test/ft_42_environments.rb +4 -6
  117. data/test/ft_44b_restore.rb +88 -22
  118. data/test/ft_45_citerator.rb +57 -11
  119. data/test/ft_49_condition.rb +4 -2
  120. data/test/ft_4_misc.rb +24 -3
  121. data/test/ft_50_xml_attribute.rb +17 -20
  122. data/test/ft_54_listen.rb +1 -1
  123. data/test/ft_54b_listen.rb +2 -2
  124. data/test/ft_56_timeout.rb +8 -1
  125. data/test/ft_57_a.rb +10 -10
  126. data/test/ft_59_ps.rb +49 -16
  127. data/test/ft_60_ecancel.rb +52 -10
  128. data/test/ft_63_pause.rb +8 -8
  129. data/test/ft_65_stringlaunch.rb +4 -6
  130. data/test/ft_67_schedlaunch.rb +4 -4
  131. data/test/ft_69_cancelmissing.rb +4 -2
  132. data/test/ft_70_lookupvar.rb +2 -2
  133. data/test/ft_72_lookup_processes.rb +2 -2
  134. data/test/ft_73_cancel_sub.rb +8 -8
  135. data/test/ft_77_segments.rb +38 -0
  136. data/test/ft_78_eval.rb +154 -0
  137. data/test/ft_79_tticket.rb +185 -0
  138. data/test/ft_80_spname.rb +95 -0
  139. data/test/ft_81_exp.rb +64 -0
  140. data/test/ft_82_trecu.rb +48 -0
  141. data/test/ft_83_badpause.rb +62 -0
  142. data/test/ft_84_updateexp.rb +125 -0
  143. data/test/ft_9b_cursor.rb +105 -0
  144. data/test/ft_tests.rb +14 -1
  145. data/test/hash_test.rb +7 -7
  146. data/test/hparticipant_test.rb +4 -4
  147. data/test/lookup_vf_test.rb +94 -0
  148. data/test/misc_test.rb +5 -3
  149. data/test/orest_test.rb +4 -3
  150. data/test/param_test.rb +12 -16
  151. data/test/participant_test.rb +36 -0
  152. data/test/pending.rb +10 -10
  153. data/test/rake_ltest.rb +1 -10
  154. data/test/rake_qtest.rb +7 -6
  155. data/test/raw_prog_test.rb +89 -121
  156. data/test/restart_cron_test.rb +84 -36
  157. data/test/restart_paused_test.rb +100 -0
  158. data/test/restart_sleep_test.rb +1 -1
  159. data/test/restart_tests.rb +1 -0
  160. data/test/restart_when_test.rb +33 -22
  161. data/test/ruby_procdef_test.rb +19 -18
  162. data/test/sec_test.rb +74 -35
  163. data/test/storage_test.rb +44 -0
  164. data/test/test.rb +3 -0
  165. data/test/timeout_test.rb +7 -18
  166. data/test/wfid_test.rb +2 -1
  167. data/test/wi_test.rb +29 -18
  168. metadata +121 -57
  169. data/lib/openwfe/expressions/raw_xml.rb +0 -176
  170. data/lib/openwfe/expressions/simplerep.rb +0 -266
  171. data/lib/openwfe/util/kotoba.rb +0 -236
  172. data/lib/openwfe/util/lru.rb +0 -171
  173. data/lib/openwfe/util/otime.rb +0 -246
  174. data/lib/openwfe/util/safe.rb +0 -160
  175. data/lib/openwfe/util/scheduler.rb +0 -1158
  176. data/test/cron_test.rb +0 -113
  177. data/test/cronline_test.rb +0 -60
  178. data/test/dollar_test.rb +0 -90
  179. data/test/kotoba_test.rb +0 -72
  180. data/test/lru_test.rb +0 -79
  181. data/test/safely_test.rb +0 -84
  182. data/test/scheduler_1_test.rb +0 -88
  183. data/test/scheduler_test.rb +0 -363
  184. data/test/time_test.rb +0 -84
@@ -1,171 +0,0 @@
1
- #
2
- #--
3
- # Copyright (c) 2007, John Mettraux, OpenWFE.org
4
- # All rights reserved.
5
- #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
8
- #
9
- # . Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- #
12
- # . Redistributions in binary form must reproduce the above copyright notice,
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- #
16
- # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
- # used to endorse or promote products derived from this software without
18
- # specific prior written permission.
19
- #
20
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
- # POSSIBILITY OF SUCH DAMAGE.
31
- #++
32
- #
33
-
34
- #
35
- # "made in Japan"
36
- #
37
- # John Mettraux at openwfe.org
38
- #
39
-
40
- #require 'monitor'
41
-
42
-
43
- module OpenWFE
44
-
45
- #
46
- # A Hash that has a max size. After the maxsize has been reached, the
47
- # least recently used entries (LRU hence), will be discared to make
48
- # room for the new entries.
49
- #
50
- class LruHash
51
- #include MonitorMixin
52
- #
53
- # seems not necessary for now, and it collides with expool's
54
- # @monitors own sync
55
-
56
- def initialize (maxsize)
57
-
58
- super()
59
-
60
- @maxsize = maxsize
61
-
62
- @hash = {}
63
- @lru_keys = []
64
- end
65
-
66
- def maxsize= (newsize)
67
-
68
- remove_lru() while @hash.size > newsize
69
- @maxsize = newsize
70
- end
71
-
72
- def maxsize
73
-
74
- @maxsize
75
- end
76
-
77
- def size
78
- @lru_keys.size
79
- end
80
- alias :length :size
81
-
82
- def keys
83
-
84
- @hash.keys
85
- end
86
-
87
- def values
88
-
89
- @hash.values
90
- end
91
-
92
- def clear
93
-
94
- @hash.clear
95
- @lru_keys.clear
96
- end
97
-
98
- def each (&block)
99
-
100
- return unless block
101
-
102
- @hash.each do |k, v|
103
- block.call(k, v)
104
- end
105
- end
106
-
107
- #
108
- # Returns the keys with the lru in front.
109
- #
110
- def ordered_keys
111
-
112
- @lru_keys
113
- end
114
-
115
- def [] (key)
116
-
117
- value = @hash[key]
118
- return nil unless value
119
- touch(key)
120
-
121
- value
122
- end
123
-
124
- def []= (key, value)
125
-
126
- remove_lru() while @hash.size >= @maxsize
127
- @hash[key] = value
128
- touch(key)
129
-
130
- value
131
- end
132
-
133
- def delete (key)
134
-
135
- value = @hash.delete(key)
136
- @lru_keys.delete(key)
137
-
138
- value
139
- end
140
-
141
- def include? (key)
142
-
143
- @hash.include?(key)
144
- end
145
-
146
- protected
147
-
148
- #
149
- # Puts the key on top of the lru 'stack'.
150
- # The bottom being the lru place.
151
- #
152
- def touch (key)
153
-
154
- @lru_keys.delete(key)
155
- @lru_keys << key
156
- end
157
-
158
- #
159
- # Removes the lru value and returns it.
160
- # Returns nil if the cache is currently empty.
161
- #
162
- def remove_lru ()
163
-
164
- return nil if @lru_keys.size < 1
165
- key = @lru_keys.delete_at(0)
166
-
167
- @hash.delete(key)
168
- end
169
- end
170
- end
171
-
@@ -1,246 +0,0 @@
1
- #
2
- #--
3
- # Copyright (c) 2005-2007, John Mettraux, OpenWFE.org
4
- # All rights reserved.
5
- #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
8
- #
9
- # . Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- #
12
- # . Redistributions in binary form must reproduce the above copyright notice,
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- #
16
- # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
- # used to endorse or promote products derived from this software without
18
- # specific prior written permission.
19
- #
20
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
- # POSSIBILITY OF SUCH DAMAGE.
31
- #++
32
- #
33
-
34
- #
35
- # "hecho en Costa Rica"
36
- #
37
- # john.mettraux@openwfe.org
38
- #
39
-
40
- require 'date'
41
- #require 'parsedate'
42
-
43
-
44
- module OpenWFE
45
-
46
- #TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
47
-
48
- #
49
- # Returns the current time as an ISO date string
50
- #
51
- def OpenWFE.now ()
52
-
53
- to_iso8601_date(Time.new())
54
- end
55
-
56
- #
57
- # As the name implies.
58
- #
59
- def OpenWFE.to_iso8601_date (date)
60
-
61
- if date.kind_of? Float
62
- date = to_datetime(Time.at(date))
63
- elsif date.kind_of? Time
64
- date = to_datetime(date)
65
- elsif not date.kind_of? Date
66
- date = DateTime.parse(date)
67
- end
68
-
69
- s = date.to_s # this is costly
70
- s[10] = " "
71
-
72
- s
73
- end
74
-
75
- #
76
- # the old method we used to generate our ISO datetime strings
77
- #
78
- def OpenWFE.time_to_iso8601_date (time)
79
-
80
- s = time.getutc().strftime(TIME_FORMAT)
81
- o = time.utc_offset / 3600
82
- o = o.to_s + "00"
83
- o = "0" + o if o.length < 4
84
- o = "+" + o unless o[0..1] == '-'
85
-
86
- s + " " + o.to_s
87
- end
88
-
89
- #
90
- # Returns a Ruby time
91
- #
92
- def OpenWFE.to_ruby_time (iso_date)
93
-
94
- DateTime.parse(iso_date)
95
- end
96
-
97
- #def OpenWFE.parse_date (date)
98
- #end
99
-
100
- #
101
- # equivalent to java.lang.System.currentTimeMillis()
102
- #
103
- def OpenWFE.current_time_millis ()
104
-
105
- t = Time.new()
106
- t = t.to_f * 1000
107
- t.to_i
108
- end
109
-
110
- #
111
- # turns a string like '1m10s' into a float like '70.0'
112
- #
113
- # w -> week
114
- # d -> day
115
- # h -> hour
116
- # m -> minute
117
- # s -> second
118
- # M -> month
119
- # y -> year
120
- # 'nada' -> millisecond
121
- #
122
- def OpenWFE.parse_time_string (string)
123
-
124
- string = string.strip
125
-
126
- index = -1
127
- result = 0.0
128
-
129
- number = ""
130
-
131
- while true
132
- index = index + 1
133
-
134
- if index >= string.length
135
- if number.length > 0
136
- result = result + (Float(number) / 1000.0)
137
- end
138
- break
139
- end
140
-
141
- c = string[index, 1]
142
-
143
- if is_digit?(c)
144
- number = number + c
145
- next
146
- end
147
-
148
- value = Integer(number)
149
- number = ""
150
-
151
- multiplier = DURATIONS[c]
152
-
153
- raise "unknown time char '#{c}'" \
154
- if not multiplier
155
-
156
- result = result + (value * multiplier)
157
- end
158
-
159
- result
160
- end
161
-
162
- #
163
- # returns true if the character c is a digit
164
- #
165
- def OpenWFE.is_digit? (c)
166
- return false if not c.kind_of?(String)
167
- return false if c.length > 1
168
- (c >= "0" and c <= "9")
169
- end
170
-
171
- #
172
- # conversion methods between Date[Time] and Time
173
-
174
- #
175
- # Ruby Cookbook 1st edition p.111
176
- # http://www.oreilly.com/catalog/rubyckbk/
177
- # a must
178
- #
179
-
180
- #
181
- # converts a Time instance to a DateTime one
182
- #
183
- def OpenWFE.to_datetime (time)
184
-
185
- s = time.sec + Rational(time.usec, 10**6)
186
- o = Rational(time.utc_offset, 3600 * 24)
187
-
188
- begin
189
-
190
- DateTime.new(
191
- time.year,
192
- time.month,
193
- time.day,
194
- time.hour,
195
- time.min,
196
- s,
197
- o)
198
-
199
- rescue Exception => e
200
-
201
- #puts
202
- #puts OpenWFE::exception_to_s(e)
203
- #puts
204
- #puts \
205
- # "\n Date.new() problem. Params :"+
206
- # "\n....y:#{time.year} M:#{time.month} d:#{time.day} "+
207
- # "h:#{time.hour} m:#{time.min} s:#{s} o:#{o}"
208
-
209
- DateTime.new(
210
- time.year,
211
- time.month,
212
- time.day,
213
- time.hour,
214
- time.min,
215
- time.sec,
216
- time.utc_offset)
217
- end
218
- end
219
-
220
- def OpenWFE.to_gm_time (dtime)
221
- to_ttime(dtime.new_offset, :gm)
222
- end
223
-
224
- def OpenWFE.to_local_time (dtime)
225
- to_ttime(dtime.new_offset(DateTime.now.offset-offset), :local)
226
- end
227
-
228
- def OpenWFE.to_ttime (d, method)
229
- usec = (d.sec_fraction * 3600 * 24 * (10**6)).to_i
230
- Time.send(method, d.year, d.month, d.day, d.hour, d.min, d.sec, usec)
231
- end
232
-
233
- protected
234
-
235
- DURATIONS = {
236
- "y" => 365 * 24 * 3600,
237
- "M" => 30 * 24 * 3600,
238
- "w" => 7 * 24 * 3600,
239
- "d" => 24 * 3600,
240
- "h" => 3600,
241
- "m" => 60,
242
- "s" => 1
243
- }
244
-
245
- end
246
-
@@ -1,160 +0,0 @@
1
- #
2
- #--
3
- # Copyright (c) 2007, John Mettraux, OpenWFE.org
4
- # All rights reserved.
5
- #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
8
- #
9
- # . Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- #
12
- # . Redistributions in binary form must reproduce the above copyright notice,
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- #
16
- # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
- # used to endorse or promote products derived from this software without
18
- # specific prior written permission.
19
- #
20
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
- # POSSIBILITY OF SUCH DAMAGE.
31
- #++
32
- #
33
-
34
- #
35
- # "made in Japan"
36
- #
37
- # john.mettraux@openwfe.org
38
- #
39
-
40
- require 'open-uri'
41
-
42
-
43
- module OpenWFE
44
-
45
- #--
46
- # Runs some remote code (uri) at a different $SAFE level.
47
- #
48
- #def OpenWFE.load_safely (uri, safe_level)
49
- # source = ""
50
- # source << "#\n"
51
- # source << "# loaded from #{uri}\n"
52
- # source << "#\n"
53
- # source << open(uri).read
54
- # load_eval_safely(source, safe_level)
55
- #end
56
- #
57
- #
58
- # Makes sure that a piece of Ruby code is run at certain safe level.
59
- # Saves in a temp file that is reloaded in its own anonymous namespace.
60
- #
61
- # (no binding passing allowed like in the basic Kernel.eval() method).
62
- #
63
- #def OpenWFE.load_eval_safely (code, safe_level)
64
- # tmpfname =
65
- # "#{Dir.tmpdir}/"+
66
- # "owfe_#{code.object_id}_#{Time.new.to_f}.tmp.rb"
67
- # File.open tmpfname, "w" do |file|
68
- # file.print code
69
- # end
70
- # Thread.new do
71
- # $SAFE = safe_level
72
- # load(tmpfname, true)
73
- # end.join
74
- # begin
75
- # File.delete tmpfname
76
- # rescue Excpetion => e
77
- # # ignore
78
- # end
79
- #end
80
- #++
81
-
82
- #
83
- # Runs some code within an instance's realm at a certain safety level.
84
- #
85
- def OpenWFE.instance_eval_safely (instance, code, safe_level)
86
-
87
- return instance.instance_eval(code) if on_jruby?
88
-
89
- code.untaint
90
-
91
- r = nil
92
-
93
- Thread.new do
94
- $SAFE = safe_level
95
- r = instance.instance_eval(code)
96
- end.join
97
-
98
- raise "cannot TAMPER with JRUBY_VERSION" if on_jruby?
99
-
100
- r
101
- end
102
-
103
- #
104
- # Runs an eval() call at a certain safety level.
105
- #
106
- def OpenWFE.eval_safely (code, safe_level, binding=nil)
107
-
108
- return eval(code, binding) if on_jruby?
109
-
110
- code.untaint
111
-
112
- r = nil
113
-
114
- Thread.new do
115
- $SAFE = safe_level
116
- r = eval(code, binding)
117
- end.join
118
-
119
- raise "cannot TAMPER with JRUBY_VERSION" if on_jruby?
120
-
121
- r
122
- end
123
-
124
- #
125
- # - not used currently -
126
- #
127
- # evals "requires" at the current safe level and the rest of the code
128
- # at the requested safety level.
129
- #
130
- def OpenWFE.eval_r_safely (code, safe_level, binding=nil)
131
-
132
- code.untaint
133
-
134
- c = ""
135
-
136
- code.split("\n").each do |line|
137
- if line.match "^ *require " and not line.index(";")
138
- eval(line, binding)
139
- else
140
- c << line
141
- c << "\n"
142
- end
143
- end
144
-
145
- eval_safely(c, safe_level, binding)
146
- end
147
-
148
- protected
149
-
150
- #
151
- # Returns true if the JRUBY_VERSION is defined.
152
- # (beware : something else than JRuby may have defined it).
153
- #
154
- def OpenWFE.on_jruby?
155
-
156
- defined?(JRUBY_VERSION) != nil
157
- end
158
-
159
- end
160
-