Wombat/Core/Pipeline.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 183 CC 18 SCAR 23.0
CC
18
Cov
0%
setBasic
20.0
removeValve
20.0
new
12.0
CRAP 10.0
1 # -*- Mode: Perl; indent-tabs-mode: nil; -*-
2  
3 package Wombat::Core::Pipeline;
4  
5 use base qw(Wombat::ValveContext);
6 use fields qw(basic container started state valves);
7 use strict;
8 use warnings;
9  
10 use Servlet::ServletException ();
11 use Wombat::Exception ();
12  
13 sub new {
14 my $self = shift;
15 my $container = shift;
16  
17 $self = fields::new($self) unless ref $self;
18  
19 $self->{basic} = undef;
20 $self->{container} = undef;
21 $self->{started} = undef;
22 $self->{state} = undef;
23 $self->{valves} = [];
24  
25 $self->setContainer($container) if $container;
26  
27 return $self;
28 }
29  
30 # accessors
31  
32 sub getContainer {
33 my $self = shift;
34  
35 return $self->{container};
36 }
37  
38 sub setContainer {
39 my $self = shift;
40 my $container = shift;
41  
42 $self->{container} = $container;
43  
44 return 1;
45 }
46  
47 # public methods
48  
49 sub getBasic {
50 my $self = shift;
51  
52 return $self->{basic};
53 }
54  
55 sub setBasic {
56 my $self = shift;
57 my $valve = shift;
58  
59 my $oldBasic = $self->{basic};
60 return 1 if ref $valve eq ref $oldBasic;
61  
62 $oldBasic->setContainer(undef) if $oldBasic;
63  
64 return 1 unless $valve;
65  
66 $valve->setContainer($self->{container});
67 $self->{basic} = $valve;
68 $self->{state} = undef;
69  
70 return 1;
71 }
72  
73 sub addValve {
74 my $self = shift;
75 my $valve = shift;
76  
77 $valve->setContainer($self->{container});
78 push @{ $self->{valves} }, $valve;
79  
80 $valve->start() if $self->{started};
81  
82 return 1;
83 }
84  
85 sub getValves {
86 my $self = shift;
87  
88 return $self->{basic} ?
89 (@{ $self->{valves} }, $self->{basic}) :
90 @{ $self->{valves} };
91 }
92  
93 sub invoke {
94 my $self = shift;
95  
96 $self->{state} = 0;
97 $self->invokeNext(@_);
98  
99 return 1;
100 }
101  
102 sub removeValve {
103 my $self = shift;
104 my $valve = shift;
105  
106 my $j = 0;
107 for (my $i=0; $i < @{ $self->{valves} }; $i++) {
108 if (ref $valve eq ref $self->{valves}->[$i]) {
109 $j = $i;
110 last;
111 }
112 }
113  
114 return 1 unless $j;
115  
116 splice @{ $self->{valves} }, $j, 1;
117 $valve->setContainer(undef);
118  
119 $valve->stop() if $self->{started};
120  
121 return 1;
122 }
123  
124 sub invokeNext {
125 my $self = shift;
126  
127 my $subscript = $self->{state}++;
128 my $numValves = @{ $self->{valves} };
129  
130 if ($subscript < $numValves) {
131 $self->{valves}->[$subscript]->invoke(@_, $self);
132 }
133 elsif ($subscript == $numValves && $self->{basic}) {
134 $self->{basic}->invoke(@_, $self);
135 }
136 else {
137 my $msg = "invokeNext: no further valves configured";
138 Servlet::ServletException->throw($msg);
139 }
140  
141 return 1;
142 }
143  
144 =pod
145  
146 =head1 LIFECYCLE METHODS
147  
148 =over
149  
150 =item start()
151  
152 Prepare for active use of this component. This method should be called
153 before any of the public methods of the component are utilized.
154  
155 B<Throws:>
156  
157 =over
158  
159 =item B<Wombat::LifecycleException>
160  
161 if the component has already been started
162  
163 =back
164  
165 =cut
166  
167 sub start {
168 my $self = shift;
169  
170 if ($self->{started}) {
171 my $msg = "start: pipeline already started";
172 Wombat::LifecycleException->throw($msg);
173 }
174  
175 for my $valve (@{ $self->{valves} }) {
176 $valve->start();
177 }
178  
179 $self->{basic}->start() if $self->{basic};
180  
181 $self->{started} = 1;
182  
183 return 1;
184 }
185  
186 =pod
187  
188 =item stop()
189  
190 Gracefully terminate active use of this component. Once this method
191 has been called, no public methods of the component should be
192 utilized.
193  
194 B<Throws:>
195  
196 =over
197  
198 =item B<Wombat::LifecycleException>
199  
200 if the component is not started
201  
202 =back
203  
204 =cut
205  
206 sub stop {
207 my $self = shift;
208  
209 unless ($self->{started}) {
210 my $msg = "stop: pipeline not started";
211 Wombat::LifecycleException->throw($msg);
212 }
213  
214 undef $self->{started};
215  
216 $self->{basic}->stop() if $self->{basic};
217  
218 for my $valve (@{ $self->{valves} }) {
219 $valve->stop();
220 }
221  
222 return 1;
223 }
224  
225 =pod
226  
227 =back
228  
229 =cut
230  
231 # private methods
232  
233 sub log {
234 my $self = shift;
235  
236 $self->{container}->log(@_) if $self->{container};
237  
238 return 1;
239 }
240  
241 1;
242 __END__