Wombat/Server.pm untested

filter: Statementstmt 0.0% 0 / 193 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 268 CC 12 SCAR 17.4
CC
12
Cov
0%
setConfigFile
12.0
setHome
12.0
removeService
12.0
CRAP 5.7
1 # -*- Mode: Perl; indent-tabs-mode: nil; -*-
2  
3 package Wombat::Server;
4  
5 =pod
6  
7 =head1 NAME
8  
9 Wombat::Server - server class
10  
11 =head1 SYNOPSIS
12  
13 my $wombat = Wombat::Server();
14 $wombat->setHome('/usr/local/wombat');
15 $wombat->setConfigFile('conf/server.xml');
16  
17 $wombat->start();
18 $wombat->await();
19 $wombat->stop();
20  
21 =head1 DESCRIPTION
22  
23 This class implements a Wombat server. It uses I<server.xml> to
24 configure a container hierarchy and provides methods for starting and
25 stopping the server. It is meant to be used in both standalone and
26 embedded mode, so it does not take care of standard daemon things,
27 like processing command line arguments or setting up signal
28 handlers. Those are the responsibilities of the surrounding
29 environment (eg a control script or mod_perl <Perl> section).
30  
31 =cut
32  
33 use fields qw(configFile home mapper services started);
34 use strict;
35 use warnings;
36  
37 use Cwd ();
38 use Servlet::Util::Exception ();
39 use Wombat::Exception ();
40 use Wombat::Globals ();
41 use Wombat::Util::XmlMapper ();
42  
43 =pod
44  
45 =head1 CONSTRUCTOR
46  
47 =over
48  
49 =item new()
50  
51 Create and return an instance, initializing fields to default values.
52  
53 =back
54  
55 =cut
56  
57 sub new {
58 my $self = shift;
59  
60 $self = fields::new($self) unless ref $self;
61  
62 $self->{home} = undef;
63 $self->{mapper} = Wombat::Util::XmlMapper->new();
64 $self->{services} = [];
65 $self->{started} = undef;
66  
67 return $self;
68 }
69  
70 =pod
71  
72 =head1 ACCESSOR METHODS
73  
74 =over
75  
76 =item getConfigFile()
77  
78 Return the location of the configuration file for this
79 Server. Defaults to I<server.xml> in the I<conf> subdirectory of
80 C<getHome()>.
81  
82 =cut
83  
84 sub getConfigFile {
85 my $self = shift;
86  
87 return $self->{configFile} ||
88 File::Spec->catfile($self->getHome(), 'conf', 'server.xml');
89 }
90  
91 =pod
92  
93 =item setConfigFile($configFile)
94  
95 Set the location of the configuration file for this Server. If the
96 specified location is relative, then it will be absolutized using
97 C<getHome()>.
98  
99 =over
100  
101 =item B<Parameters:>
102  
103 =over
104  
105 =item $configFile
106  
107 the location of the configuration file relative to the server's home
108 directory
109  
110 =back
111  
112 =back
113  
114 =cut
115  
116 sub setConfigFile {
117 my $self = shift;
118 my $configFile = shift;
119  
120 return 1 unless $configFile;
121  
122 $configFile = File::Spec->rel2abs($configFile, $self->getHome()) unless
123 File::Spec->file_name_is_absolute($configFile);
124 $self->{configFile} = $configFile;
125  
126 return 1;
127 }
128  
129 =pod
130  
131 =item getHome()
132  
133 Return the home directory for this Server.
134  
135 =cut
136  
137 sub getHome {
138 my $self = shift;
139  
140 return $self->{home};
141 }
142  
143 =pod
144  
145 =item setHome($home)
146  
147 Set the home directory for this Server.
148  
149 B<Parameters:>
150  
151 =over
152  
153 =item $home
154  
155 the server home directory, specified absolutely
156  
157 =back
158  
159 B<Throws:>
160  
161 =over
162  
163 =item B<Servlet::Util::IllegalArgumentException>
164  
165 if the specified directory is not specified absolutely
166  
167 =back
168  
169 =cut
170  
171 sub setHome {
172 my $self = shift;
173 my $home = shift;
174  
175 return 1 unless $home;
176  
177 unless (File::Spec->file_name_is_absolute($home)) {
178 my $msg = "setHome: home directory must be absolute [$home]";
179 Servlet::Util::IllegalArgumentException($msg);
180 }
181  
182 $self->{home} = $home;
183 $ENV{WOMBAT_HOME} = $home;
184  
185 return 1;
186 }
187  
188 =pod
189  
190 =back
191  
192 =head1 PUBLIC METHODS
193  
194 =over
195  
196 =item await()
197  
198 Direct all defined Services to begin listening for requests. Depending
199 on Connector implementations, this method may return immediately (for
200 Connectors that execute asynchrously, eg Apache) or may block (eg Http
201 Connector).
202  
203 =cut
204  
205 sub await {
206 my $self = shift;
207  
208 for my $service (@{ $self->{services} }) {
209 $service->await();
210 }
211  
212 return 1;
213 }
214  
215 =pod
216  
217 =item addService($service)
218  
219 Add a new Service to the set of defined Services.
220  
221 B<Parameters:>
222  
223 =over
224  
225 =item $service
226  
227 the B<Wombat::Core::Service> to add
228  
229 =back
230  
231 =cut
232  
233 sub addService {
234 my $self = shift;
235 my $service = shift;
236  
237 push @{ $self->{services} }, $service;
238  
239 return 1;
240 }
241  
242 =pod
243  
244 =item getServices()
245  
246 Return an array containing all defined Services.
247  
248 =cut
249  
250 sub getServices {
251 my $self = shift;
252  
253 my @services = @{ $self->{services} };
254  
255 return wantarray ? @services : \@services;
256 }
257  
258 =pod
259  
260 =item removeService($service)
261  
262 Remove a Service from the set of defined Services.
263  
264 B<Parameters:>
265  
266 =over
267  
268 =item $service
269  
270 the B<Wombat::Core::Service> to remove
271  
272 =back
273  
274 =cut
275  
276 sub removeService {
277 my $self = shift;
278 my $service = shift;
279  
280 my $j;
281 for (my $i=0; $i < @{ $self->{services} }; $i++) {
282 if ($service->getName() eq $self->{services}->[$i]->getName()) {
283 $j = $i;
284 last;
285 }
286 }
287  
288 splice @{ $self->{services} }, $j, 1 if defined $j;
289  
290 return 1;
291 }
292  
293 =pod
294  
295 =back
296  
297 =head1 LIFECYCLE METHODS
298  
299 =over
300  
301 =item start()
302  
303 Prepare for active use of this component. This method should be called
304 before any of the public methods of the component are utilized.
305  
306 B<Throws:>
307  
308 =over
309  
310 =item B<Wombat::LifecycleException>
311  
312 if the component has already been started
313  
314 =item B<Wombat::XmlException>
315  
316 if a problem occurs while parsing a config file
317  
318 =item B<Wombat::ConfigException>
319  
320 if a validity error is detected in a config file
321  
322 =item B<Servlet::Util::IOException>
323  
324 if an input or output error occurs
325  
326 =back
327  
328 =cut
329  
330 sub start {
331 my $self = shift;
332  
333 if ($self->{started}) {
334 my $msg = "start: server already started";
335 Wombat::LifecycleException->throw($msg);
336 }
337  
338 $self->addServiceRules('Server');
339  
340 my $fh = Symbol::gensym;
341 my $configFile = $self->getConfigFile();
342 unless (open $fh, $configFile) {
343 my $msg = "can't open config file [$configFile]: $!";
344 Servlet::Util::IOException->throw($msg);
345 }
346  
347 $self->{mapper}->readXml($fh, $self);
348 close $fh;
349  
350 $self->{started} = 1;
351  
352 my $msg = sprintf("%s configured -- resuming normal operations",
353 Wombat::Globals::SERVER_INFO);
354  
355 for my $service (@{ $self->{services} }) {
356 $service->start();
357 $service->getContainer()->log($msg);
358 }
359  
360 return 1;
361 }
362  
363 =pod
364  
365 =item stop()
366  
367 Gracefully terminate active use of this component. Once this method
368 has been called, no public methods of the component should be
369 utilized.
370  
371 B<Throws:>
372  
373 =over
374  
375 =item B<Wombat::LifecycleException>
376  
377 if the component is not started
378  
379 =back
380  
381 =cut
382  
383 sub stop {
384 my $self = shift;
385  
386 unless ($self->{started}) {
387 my $msg = "stop: server not started";
388 Wombat::LifecycleException->throw($msg);
389 }
390  
391 undef $self->{started};
392  
393 for my $service (@{ $self->{services} }) {
394 $service->stop();
395 }
396  
397 return 1;
398 }
399  
400 # private methods
401  
402 sub addServiceRules {
403 my $self = shift;
404 my $xpath = shift;
405  
406 $xpath .= '/Service';
407 my $class = 'Wombat::Core::Service';
408  
409 $self->{mapper}->addRule($xpath,
410 $self->{mapper}->objectCreate($class));
411 $self->{mapper}->addRule($xpath,
412 $self->{mapper}->setProperties());
413 $self->{mapper}->addRule($xpath,
414 $self->{mapper}->addChild('addService'));
415  
416 $self->addConnectorRules($xpath);
417 $self->addEngineRules($xpath);
418  
419 return 1;
420 }
421  
422 sub addEngineRules {
423 my $self = shift;
424 my $xpath = shift;
425  
426 $xpath .= '/Engine';
427 my $class = 'Wombat::Core::Engine';
428  
429 $self->{mapper}->addRule($xpath,
430 $self->{mapper}->objectCreate($class));
431 $self->{mapper}->addRule($xpath,
432 $self->{mapper}->setProperties());
433 $self->{mapper}->addRule($xpath,
434 $self->{mapper}->addChild('setContainer'));
435  
436 $self->addLoggerRules($xpath);
437 $self->addRealmRules($xpath);
438 $self->addSessionManagerRules($xpath);
439 $self->addValveRules($xpath);
440  
441 # default host applications
442 $self->addApplicationRules($xpath);
443  
444 $self->addHostRules($xpath);
445  
446 return 1;
447 }
448  
449 sub addHostRules {
450 my $self = shift;
451 my $xpath = shift;
452  
453 $xpath .= '/Host';
454 my $class = 'Wombat::Core::Host';
455  
456 $self->{mapper}->addRule($xpath,
457 $self->{mapper}->objectCreate($class));
458 $self->{mapper}->addRule($xpath,
459 $self->{mapper}->setProperties());
460 $self->{mapper}->addRule($xpath,
461 $self->{mapper}->addChild('addChild'));
462  
463 # aliases
464 $self->{mapper}->addRule("$xpath/Alias",
465 $self->{mapper}->methodSetter('addAlias', 0));
466  
467 $self->addLoggerRules($xpath);
468 $self->addRealmRules($xpath);
469 $self->addSessionManagerRules($xpath);
470 $self->addValveRules($xpath);
471  
472 $self->addApplicationRules($xpath);
473  
474 return 1;
475 }
476  
477 sub addApplicationRules {
478 my $self = shift;
479 my $xpath = shift;
480  
481 $xpath .= '/Application';
482 my $class = 'Wombat::Core::Application';
483  
484 $self->{mapper}->addRule($xpath,
485 $self->{mapper}->objectCreate($class));
486 $self->{mapper}->addRule($xpath,
487 $self->{mapper}->setProperties());
488 # XXX: application config
489 $self->{mapper}->addRule($xpath,
490 $self->{mapper}->addChild('addChild'));
491  
492 $self->addLoggerRules($xpath);
493 $self->addRealmRules($xpath);
494 $self->addSessionManagerRules($xpath);
495 $self->addValveRules($xpath);
496  
497 return 1;
498 }
499  
500 sub addConnectorRules {
501 my $self = shift;
502 my $xpath = shift;
503  
504 $xpath .= '/Connector';
505  
506 $self->{mapper}->addRule($xpath,
507 $self->{mapper}->objectCreate(undef,
508 'className'));
509 $self->{mapper}->addRule($xpath,
510 $self->{mapper}->setProperties());
511 $self->{mapper}->addRule($xpath,
512 $self->{mapper}->addChild('addConnector'));
513  
514 $self->addEngineRules($xpath);
515  
516 return 1;
517 }
518  
519 sub addLoggerRules {
520 my $self = shift;
521 my $xpath = shift;
522  
523 $xpath .= '/Logger';
524  
525 $self->{mapper}->addRule($xpath,
526 $self->{mapper}->objectCreate(undef,
527 'className'));
528 $self->{mapper}->addRule($xpath,
529 $self->{mapper}->setProperties());
530 $self->{mapper}->addRule($xpath,
531 $self->{mapper}->addChild('setLogger'));
532  
533 return 1;
534 }
535  
536 sub addRealmRules {
537 my $self = shift;
538 my $xpath = shift;
539  
540 $xpath .= '/Realm';
541  
542 $self->{mapper}->addRule($xpath,
543 $self->{mapper}->objectCreate(undef,
544 'className'));
545 $self->{mapper}->addRule($xpath,
546 $self->{mapper}->setProperties());
547 $self->{mapper}->addRule($xpath,
548 $self->{mapper}->addChild('setRealm'));
549  
550 return 1;
551 }
552  
553 sub addSessionManagerRules {
554 my $self = shift;
555 my $xpath = shift;
556  
557 $xpath .= '/SessionManager';
558  
559 $self->{mapper}->addRule($xpath,
560 $self->{mapper}->objectCreate(undef,
561 'className'));
562 $self->{mapper}->addRule($xpath,
563 $self->{mapper}->setProperties());
564 $self->{mapper}->addRule($xpath,
565 $self->{mapper}->addChild('setSessionManager'));
566  
567 return 1;
568 }
569  
570 sub addValveRules {
571 my $self = shift;
572 my $xpath = shift;
573  
574 $xpath .= '/Valve';
575  
576 $self->{mapper}->addRule($xpath,
577 $self->{mapper}->objectCreate(undef,
578 'className'));
579 $self->{mapper}->addRule($xpath,
580 $self->{mapper}->setProperties());
581 $self->{mapper}->addRule($xpath,
582 $self->{mapper}->addChild('addValve'));
583  
584 return 1;
585 }
586  
587 1;
588 __END__