LS/Config.pm untested

filter: Statementstmt 0.0% 0 / 269 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 49 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 422 CC 27 SCAR 20.9
CC
27
Cov
0%
fromXML
30.0
loadFile
20.0
addForeignAuthority
20.0
CRAP 8.1
1 # =====================================================================
2 # Copyright (c) 2002,2003 IBM Corporation
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Common Public License v1.0
5 # which accompanies this distribution, and is available at
6 # http://www.opensource.org/licenses/cpl.php
7 #
8 # =====================================================================
9 package LS::Config;
10  
11  
12 use strict;
13 use warnings;
14  
15 use vars qw( $CONFIG_FILENAME );
16  
17  
18  
19 #
20 # $CONFIG_FILENAME - The name of the default configuration file
21 #
22 $CONFIG_FILENAME = 'lsid-client.xml';
23  
24  
25  
26  
27  
28 package LS::Config::File;
29  
30 use strict;
31 use warnings;
32  
33 use XML::XPath;
34  
35  
36 use LS;
37  
38  
39 use base 'LS::Base';
40  
41  
42  
43 #
44 # new( %options ) -
45 #
46 sub new {
47  
48 my $self = shift;
49  
50 my %options = @_;
51  
52 unless(ref $self) {
53  
54 $self = bless {
55  
56 _cacheConfig=> undef,
57 _hostMappings=> {},
58 _foreignAuthority=> {},
59 }, $self;
60  
61 $self->fromXML( $options{'xml'} ) if($options{'xml'});
62 }
63  
64 return $self;
65 }
66  
67  
68 #
69 # loadFile( [ $filename ] ) - Loads the configuration file from the current directory.
70 # An optional filename can be provided to override the default.
71 #
72 # Parameters - $filename, Optional. The name of the file to read the configuration from.
73 # If left unspecified, the filename stored in $LS::Config::CONFIG_FILENAME
74 # will be used.
75 #
76 # Returns - An LS::Config::File object with the file parsed and ready to be used.
77 #
78 sub loadFile {
79  
80 my $self = shift;
81  
82 my $filename = shift;
83  
84 # If unspecified, set to the default
85 $filename = $LS::Config::CONFIG_FILENAME unless($filename);
86  
87 unless(open(FILE, "$filename")) {
88  
89 $self->recordError("Unable to open file: $filename");
90 $self->addStackTrace();
91  
92 return undef;
93 }
94  
95 local $/ = undef;
96 my $data = <FILE>;
97  
98 close(FILE);
99  
100 if(UNIVERSAL::isa($self, 'LS::Config::File')) {
101  
102 $self->fromXML( $data );
103 }
104 else {
105  
106 $self = LS::Config::File->fromXML( $data );
107 }
108  
109 return $self;
110 }
111  
112  
113 #
114 # isPresent( ) - Determines whether a configuration file is present in the current
115 # directory.
116 #
117 # This function is a package functio and can be called without i
118 # instantiating an LS::Config::File object.
119 #
120 # Returns - True if the configuration file is present in the current directory,
121 # False if the configuration file is not present.
122 #
123 sub isPresent( ) {
124  
125 my $self = shift;
126  
127 return ( -e "$LS::Config::CONFIG_FILENAME" );
128 }
129  
130  
131 #
132 # cacheConfig( $cacheInfo ) - Updates or retrieves the cache related configuration items.
133 #
134 # Parameter - $cacheInfo, Optional. The LS::Config::Cache object that will
135 # become the new cache configuration.
136 #
137 # Returns - The current cache configuration if no parameter is specified.
138 #
139 sub cacheConfig {
140  
141 my $self = shift;
142  
143 @_ ? $self->{'_cacheConfig'} = shift : return $self->{'_cacheConfig'};
144 }
145  
146  
147 #
148 # useCache( ) - Queries the cache configuration in order to determine whether or not
149 # the cache should be used.
150 #
151 # Returns - True if the cache should be used,
152 # False if it should not.
153 #
154 sub useCache {
155  
156 my $self = shift;
157  
158 return undef unless($self->{'_cacheConfig'});
159  
160 my $value = $self->{'_cacheConfig'}->useConfig();
161  
162 return undef unless($value eq 'false');
163  
164 return 1;
165 }
166  
167  
168 #
169 # cacheDirectory( ) - Access to the directory where the cache should be
170 # stored.
171 #
172 # Returns - The directory, from the LS::Config::Cache object,
173 # where the cache files are stored.
174 #
175 sub cacheDirectory {
176  
177 my $self = shift;
178  
179 return undef unless($self->{'_cacheConfig'});
180  
181 return $self->{'_cacheConfig'}->cacheDirectory();
182 }
183  
184 #
185 # getHostMapping( $authority ) -
186 #
187 sub getHostMapping {
188  
189 my $self = shift;
190  
191 my ($authority) = @_;
192  
193 return $self->{'_hostMappings'}->{ $authority };
194 }
195  
196  
197 #
198 # getHostMappingList( ) -
199 #
200 sub getHostMappingList {
201  
202 my $self = shift;
203  
204 my $hl = [];
205  
206 foreach my $name (keys(%{ $self->{'_hostMappings'} })) {
207  
208 push @{ $hl }, $self->{'_hostMappings'}->{ $name };
209 }
210  
211 return $hl;
212 }
213  
214  
215 #
216 # addHostMapping( $hm ) -
217 #
218 sub addHostMapping {
219  
220 my $self = shift;
221  
222 my ($hm) = @_;
223  
224 unless(UNIVERSAL::isa($hm, 'LS::Config::HostMapping')) {
225  
226 $self->recordError('Invalid LS::Config::HostMapping parameter');
227 $self->addStackTrace();
228  
229 return undef;
230 }
231  
232 $self->{'_hostMappings'}->{ $hm->authority } = $hm;
233 }
234  
235  
236 #
237 # getForeignAuthority( %options ) -
238 #
239 sub getForeignAuthority {
240  
241 my $self = shift;
242  
243 my %options = @_;
244  
245 unless($options{'lsid'} ||
246 ($options{'authority'} && $options{'namespace'}) ) {
247  
248 $self->recordError('Missing parameters');
249 $self->addStackTrace();
250  
251 return undef;
252 }
253  
254 my $key = ($options{'lsid'} || "$options{'authority'}:$options{'namespace'}");
255  
256 return $self->{'_foreignAuthority'}->{ $key };
257 }
258  
259  
260 #
261 # getForeignAuthorityList( ) -
262 #
263 sub getForeignAuthorityList {
264  
265 my $self = shift;
266  
267 my %options = @_;
268  
269 my $list = [];
270  
271 foreach my $fa (keys(%{ $self->{'_foreignAuthority'} }) ) {
272  
273 push @{ $list }, $self->{'_foreignAuthority'}->{ $fa };
274 }
275  
276 return $list;
277 }
278  
279  
280 #
281 # addForeignAuthority( $fa ) -
282 #
283 sub addForeignAuthority {
284  
285 my $self = shift;
286  
287 my ($fa) = @_;
288  
289 unless(UNIVERSAL::isa($fa, 'LS::Config::ForeignAuthority')) {
290  
291 $self->recordError('Invalid LS::Config::ForeignAuthority parameter');
292 $self->addStackTrace();
293  
294 return undef;
295 }
296  
297 # Add each Foreign Authority based on its type (pattern or LSID)
298 if($fa->authority() ) {
299  
300 $self->{'_foreignAuthority'}->{ $fa->authority . ':' . $fa->namespace } = $fa;
301 }
302 elsif($fa->lsid() ) {
303  
304 $self->{'_foreignAuthority'}->{ $fa->lsid } = $fa;
305 }
306 else {
307  
308 $self->recordError('Malformed LS::Config::ForeignAuthority object');
309 $self->addStackTrace();
310  
311 return undef;
312 }
313 }
314  
315  
316 #
317 # fromXML( $xml ) -
318 #
319 sub fromXML {
320  
321 my $self = shift->new;
322  
323 my ($xml) = @_;
324  
325 unless($xml) {
326  
327 $self->recordError('Missing XML parameter.');
328 $self->addStackTrace();
329  
330 return undef;
331 }
332  
333 my $xpath = XML::XPath->new(xml=> $xml);
334  
335  
336 #
337 # Caching
338 #
339 my $caching = $xpath->find('lsidClient/caching');
340  
341 foreach my $c ($caching->get_nodelist()) {
342  
343 $self->cacheConfig( LS::Config::Cache->from_xpath_node($c, $xpath) );
344 }
345  
346  
347 #
348 # Host Mappings
349 #
350 my $hostMappings = $xpath->find('lsidClient/hostMappings/hostMapping');
351  
352 foreach my $hostMappingNode ($hostMappings->get_nodelist()) {
353  
354 my $hm = LS::Config::HostMapping->from_xpath_node($hostMappingNode, $xpath);
355 next unless($hm);
356  
357 $self->addHostMapping($hm);
358 }
359  
360  
361  
362 #
363 # Foreign Authorities (Patterns)
364 #
365 my $foreignAuthoritiesPatterns= $xpath->find('lsidClient/foreignAuthorities/pattern');
366  
367 foreach my $patternNode ($foreignAuthoritiesPatterns->get_nodelist()) {
368  
369 my $fa = LS::Config::ForeignAuthority->from_xpath_pattern_node($patternNode, $xpath);
370 next unless($fa);
371  
372 $self->addForeignAuthority($fa);
373 }
374  
375  
376 #
377 # Foreign Authorities (LSID)
378 #
379 my $foreignAuthoritiesLSID = $xpath->find('lsidClient/foreignAuthorities/lsid');
380  
381 foreach my $lsidNode ($foreignAuthoritiesLSID->get_nodelist()) {
382  
383 my $fa = LS::Config::ForeignAuthority->from_xpath_lsid_node($lsidNode, $xpath);
384 next unless($fa);
385  
386 $self->addForeignAuthority($fa);
387 }
388  
389 return $self;
390 }
391  
392  
393 package LS::Config::Cache;
394  
395 use strict;
396 use warnings;
397  
398 use vars qw( $METHODS );
399  
400 use LS;
401  
402 use base 'LS::Base';
403  
404  
405 #
406 # BEGIN( ) -
407 #
408 sub BEGIN {
409  
410 $METHODS = [
411 'useCache',
412 'cacheDirectory',
413 ];
414 }
415  
416  
417 # Create the accessors
418 for my $field (@{ $METHODS } ) {
419  
420 no strict "refs";
421  
422 my $slot = __PACKAGE__ . $field;
423  
424 *$field = sub {
425  
426 my $self = shift;
427  
428 @_ ? $self->{ $slot } = $_[0] : $self->{ $slot };
429 }
430 }
431  
432  
433 #
434 # new( %options ) -
435 #
436 sub new {
437  
438 my $self = shift;
439  
440 my %options = @_;
441  
442 unless(ref $self) {
443  
444 $self = bless {
445  
446 }, $self;
447  
448 $self->useCache( $options{'useCache'} );
449 $self->cacheDirectory( $options{'cacheDirectory'} );
450 }
451  
452 return $self;
453 }
454  
455  
456 #
457 # from_xpath_node( $node, $xpath ) -
458 #
459 sub from_xpath_node {
460  
461 my $self = shift->new;
462  
463 my ($node, $xpath) = @_;
464  
465 my $useCacheNode = $xpath->find('useCache', $node);
466 my $cacheDirectoryNode = $xpath->find('lsidCacheDir', $node);
467  
468  
469 if($useCacheNode) {
470  
471 $self->useCache( $useCacheNode->string_value() );
472 }
473 else {
474  
475 $self->useCache( undef );
476 }
477  
478  
479 if($cacheDirectoryNode) {
480  
481 $self->cacheDirectory( $cacheDirectoryNode->string_value() );
482 }
483 else {
484  
485 $self->cacheDirectory( undef );
486 }
487  
488 return $self;
489 }
490  
491  
492  
493 package LS::Config::HostMapping;
494  
495 use strict;
496 use warnings;
497  
498 use vars qw( $METHODS );
499  
500 use LS;
501  
502  
503 use base 'LS::Base';
504  
505  
506  
507 #
508 # BEGIN( ) -
509 #
510 sub BEGIN {
511  
512 $METHODS = [
513 'authority',
514 'endpoint',
515 ];
516 }
517  
518  
519 for my $field (@{ $METHODS } ) {
520  
521 no strict "refs";
522  
523 my $slot = __PACKAGE__ . $field;
524  
525 *$field = sub {
526  
527 my $self = shift;
528  
529 @_ ? $self->{ $slot } = $_[0] : $self->{ $slot };
530 }
531 }
532  
533  
534  
535 #
536 # new( %options ) -
537 #
538 sub new {
539  
540 my $self = shift;
541  
542 my %options = @_;
543  
544 unless(ref $self) {
545  
546 $self = bless {
547  
548 }, $self;
549  
550 $self->authority($options{'authority'});
551 $self->endpoint($options{'endpoint'});
552 }
553  
554 return $self;
555 }
556  
557  
558 #
559 # from_xpath_node( $node, $xpath )
560 #
561 sub from_xpath_node {
562  
563 my $self = shift->new;
564  
565 my ($node, $xpath) = @_;
566  
567 my $authority = $xpath->find('authority', $node);
568 my $endpoint = $xpath->find('endpoint', $node);
569  
570 unless($authority && $endpoint) {
571  
572 return undef;
573 }
574  
575 $self->authority($authority);
576 $self->endpoint($endpoint);
577  
578 return $self;
579 }
580  
581  
582  
583 package LS::Config::ForeignAuthority;
584  
585 use strict;
586 use warnings;
587  
588 use vars qw( $METHODS );
589  
590 use LS;
591  
592 use base 'LS::Base';
593  
594  
595  
596 #
597 # BEGIN( ) -
598 #
599 sub BEGIN {
600  
601 $METHODS = [
602 'authority',
603 'namespace',
604 'lsid',
605 ];
606 }
607  
608  
609 for my $field (@{ $METHODS } ) {
610  
611 no strict "refs";
612  
613 my $slot = __PACKAGE__ . $field;
614  
615 *$field = sub {
616  
617 my $self = shift;
618  
619 @_ ? $self->{ $slot } = $_[0] : $self->{ $slot };
620 }
621 }
622  
623  
624  
625 #
626 # new( %options ) -
627 #
628 sub new {
629  
630 my $self = shift;
631  
632 unless(ref $self) {
633  
634 $self = bless {
635  
636 _authorities=> [],
637 }, $self;
638 }
639  
640 return $self
641 }
642  
643  
644 #
645 # from_xpath_pattern_node( $node, $xpath ) -
646 #
647 sub from_xpath_pattern_node {
648  
649 my $self = shift->new;
650  
651 my ($node, $xpath) = @_;
652  
653 my $authority = $node->getAttribute('auth');
654 my $namespace = $node->getAttribute('ns');
655  
656 unless($authority && $namespace) {
657  
658 return undef;
659 }
660  
661 $self->authority( $authority );
662 $self->namespace( $namespace );
663  
664 my $authorities = $xpath->find('authority', $node);
665  
666 foreach my $auth ($authorities->get_nodelist()) {
667  
668 push @{ $self->{'_authorities'} }, $auth->string_value;
669 }
670  
671 return $self;
672 }
673  
674  
675 #
676 # from_xpath_lsid_node( $node, $xpath ) -
677 #
678 sub from_xpath_lsid_node {
679  
680 my $self = shift->new;
681  
682 my ($node, $xpath) = @_;
683  
684 my $lsid = $node->getAttribute('lsid');
685  
686 unless($lsid) {
687  
688 return undef;
689 }
690  
691 $self->lsid( $lsid );
692  
693 my $authorities = $xpath->find('authority', $node);
694  
695 foreach my $auth ($authorities->get_nodelist()) {
696  
697 push @{ $self->{'_authorities'} }, $auth->string_value;
698 }
699  
700 return $self;
701 }
702  
703 1;
704  
705 __END__