Class: OpenWFE::ParticipantMap

Inherits:
Service
  • Object
show all
Includes:
OwfeObservable
Defined in:
lib/openwfe/participants/participantmap.rb

Overview

A very simple directory of participants

Instance Attribute Summary collapse

Attributes included from ServiceMixin

#service_name

Attributes included from Contextual

#application_context

Instance Method Summary collapse

Methods included from OwfeObservable

#add_observer, #remove_observer

Methods included from ServiceMixin

#service_init, #stop

Methods included from Contextual

#get_work_directory, #init_service, #lookup

Methods included from Logging

#ldebug, #ldebug_callstack, #lerror, #lfatal, #linfo, #llog, #lunknown, #lwarn

Constructor Details

#initialize(service_name, application_context) ⇒ ParticipantMap

Returns a new instance of ParticipantMap.



58
59
60
61
62
63
64
# File 'lib/openwfe/participants/participantmap.rb', line 58

def initialize (service_name, application_context)

    super

    @participants = []
    @observers = {}
end

Instance Attribute Details

#participantsObject

Returns the value of attribute participants.



56
57
58
# File 'lib/openwfe/participants/participantmap.rb', line 56

def participants
  @participants
end

Instance Method Details

#dispatch(participant, participant_name, workitem) ⇒ Object

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.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/openwfe/participants/participantmap.rb', line 194

def dispatch (participant, participant_name, workitem)

    unless participant

        participant = lookup_participant participant_name

        raise "there is no participant named '#{participant_name}'" \
            unless participant
    end

    workitem.participant_name = participant_name

    return cancel(participant, workitem) \
        if workitem.is_a?(CancelItem)

    onotify :dispatch, :before_consume, workitem

    workitem.dispatch_time = Time.now

    participant.consume workitem

    onotify :dispatch, :after_consume, workitem
end

#lookup_participant(participant_name) ⇒ Object

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



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/openwfe/participants/participantmap.rb', line 160

def lookup_participant (participant_name)

    #ldebug { "lookup_participant() '#{participant_name}'" }

    participant_name = participant_name.to_s

    @participants.each do |tuple|
        return tuple[1] if tuple[0].match(participant_name)
    end

    nil
end

#register_participant(regex, params, &block) ⇒ Object

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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/openwfe/participants/participantmap.rb', line 87

def register_participant (regex, params, &block)

    participant = params[:participant]
    position = params[:position]

    if not participant

        raise "please provide a participant instance or a block" \
            if not block

        participant = BlockParticipant.new block
    end

    ldebug do 
        "register_participant() "+
        "participant class is #{participant.class}"
    end

    if participant.is_a?(Class)

        ldebug { "register_participant() class #{participant}" }

        begin

            participant = participant.new(regex, @application_context)

        rescue Exception => e
            #ldebug do 
            #    "register_participant() " +
            #    "falling back to no param constructor because of \n" +
            #    OpenWFE::exception_to_s(e)
            #end

            participant = participant.new
        end
    end

    original_string = regex.to_s

    unless regex.kind_of?(Regexp)

        regex = regex.to_s
        regex = "^" + regex unless regex[0, 1] == "^"
        regex = regex  + "$" unless regex[-1, 1] == "$"

        ldebug { "register_participant() '#{regex}'" }

        regex = Regexp.new(regex)
    end
    
    class << regex
        attr_reader :original_string
    end
    regex.instance_variable_set '@original_string', original_string

    participant.application_context = @application_context \
        if participant.respond_to?(:application_context=)

    # now add the participant to the list

    entry = [ regex, participant ]

    index = (position == :first) ? 0 : -1

    @participants.insert index, entry

    participant
end

#sizeObject

Returns how many participants are currently registered here.



69
70
71
72
# File 'lib/openwfe/participants/participantmap.rb', line 69

def size

    @participants.size
end

#unregister_participant(participant_name) ⇒ Object

Deletes the first participant matching the given name.



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/openwfe/participants/participantmap.rb', line 176

def unregister_participant (participant_name)

    participant_name = participant_name.to_s

    p = @participants.find do |tuple|
        tuple[0].original_string == participant_name
    end
    @participants.delete(p) if p
    
    (p != nil)
end