Class OpenWFE::ParticipantMap
In: lib/openwfe/participants/participantmap.rb
Parent: Service

A very simple directory of participants

Methods

Included Modules

OwfeObservable

Attributes

participants  [RW] 

Public Class methods

[Source]

    # File lib/openwfe/participants/participantmap.rb, line 58
58:         def initialize (service_name, application_context)
59: 
60:             super
61: 
62:             @participants = []
63:             @observers = {}
64:         end

Public Instance methods

Dispatches to the given participant (participant name (string) or The workitem will be fed to the consume() method of that participant. If it‘s a cancelitem and the participant has a cancel() method, it will get called instead.

[Source]

     # File lib/openwfe/participants/participantmap.rb, line 194
194:         def dispatch (participant, participant_name, workitem)
195: 
196:             unless participant
197: 
198:                 participant = lookup_participant participant_name
199: 
200:                 raise "there is no participant named '#{participant_name}'" \
201:                     unless participant
202:             end
203: 
204:             workitem.participant_name = participant_name
205: 
206:             return cancel(participant, workitem) \
207:                 if workitem.is_a?(CancelItem)
208: 
209:             onotify :dispatch, :before_consume, workitem
210: 
211:             workitem.dispatch_time = Time.now
212: 
213:             participant.consume workitem
214: 
215:             onotify :dispatch, :after_consume, workitem
216:         end

Looks up a participant given a participant_name. Will return the first participant whose name matches.

[Source]

     # File lib/openwfe/participants/participantmap.rb, line 160
160:         def lookup_participant (participant_name)
161: 
162:             #ldebug { "lookup_participant() '#{participant_name}'" }
163: 
164:             participant_name = participant_name.to_s
165: 
166:             @participants.each do |tuple|
167:                 return tuple[1] if tuple[0].match(participant_name)
168:             end
169: 
170:             nil
171:         end

Adds a participant to this map. This method is called by the engine‘s own register_participant() method.

The participant instance is returned by this method call.

The know params are :participant (a participant instance or class) and :position (which can be null or :first).

By default (if :position is not set to :first), the participant will appear at the bottom of the participant list.

[Source]

     # File lib/openwfe/participants/participantmap.rb, line 87
 87:         def register_participant (regex, params, &block)
 88: 
 89:             participant = params[:participant]
 90:             position = params[:position]
 91: 
 92:             if not participant
 93: 
 94:                 raise "please provide a participant instance or a block" \
 95:                     if not block
 96: 
 97:                 participant = BlockParticipant.new block
 98:             end
 99: 
100:             ldebug do 
101:                 "register_participant() "+
102:                 "participant class is #{participant.class}"
103:             end
104: 
105:             if participant.is_a?(Class)
106: 
107:                 ldebug { "register_participant() class #{participant}" }
108: 
109:                 begin
110: 
111:                     participant = participant.new(regex, @application_context)
112: 
113:                 rescue Exception => e
114:                     #ldebug do 
115:                     #    "register_participant() " +
116:                     #    "falling back to no param constructor because of \n" +
117:                     #    OpenWFE::exception_to_s(e)
118:                     #end
119: 
120:                     participant = participant.new
121:                 end
122:             end
123: 
124:             original_string = regex.to_s
125: 
126:             unless regex.kind_of?(Regexp)
127: 
128:                 regex = regex.to_s
129:                 regex = "^" + regex unless regex[0, 1] == "^"
130:                 regex = regex  + "$" unless regex[-1, 1] == "$"
131: 
132:                 ldebug { "register_participant() '#{regex}'" }
133: 
134:                 regex = Regexp.new(regex)
135:             end
136:             
137:             class << regex
138:                 attr_reader :original_string
139:             end
140:             regex.instance_variable_set '@original_string', original_string
141: 
142:             participant.application_context = @application_context \
143:                 if participant.respond_to?(:application_context=)
144: 
145:             # now add the participant to the list
146: 
147:             entry = [ regex, participant ]
148: 
149:             index = (position == :first) ? 0 : -1
150: 
151:             @participants.insert index, entry
152: 
153:             participant
154:         end

Returns how many participants are currently registered here.

[Source]

    # File lib/openwfe/participants/participantmap.rb, line 69
69:         def size
70: 
71:             @participants.size
72:         end

Deletes the first participant matching the given name.

[Source]

     # File lib/openwfe/participants/participantmap.rb, line 176
176:         def unregister_participant (participant_name)
177: 
178:             participant_name = participant_name.to_s
179: 
180:             p = @participants.find do |tuple|
181:                 tuple[0].original_string == participant_name
182:             end
183:             @participants.delete(p) if p
184:             
185:             (p != nil)
186:         end

Protected Instance methods

Will call the cancel method of the participant if it has one, or will simply discard the cancel item else.

[Source]

     # File lib/openwfe/participants/participantmap.rb, line 231
231:             def cancel (participant, cancel_item)
232: 
233:                 participant.cancel(cancel_item) \
234:                     if participant.respond_to?(:cancel)
235: 
236:                 onotify :dispatch, :cancel, cancel_item
237:                     #
238:                     # maybe it'd be better to specifically log that
239:                     # a participant has no cancel() method, but it's OK
240:                     # like that for now.
241:             end

[Validate]