File Coverage

blib/lib/Apache/Hadoop/Watcher/Conf.pm
Criterion Covered Total %
statement 9 11 81.8
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 13 15 86.6


line stmt bran cond sub pod time code
1             #
2             # (C) 2015, Snehasis Sinha
3             #
4             package Apache::Hadoop::Watcher::Conf;
5              
6 2     2   21809 use 5.010001;
  2         7  
7 2     2   10 use strict;
  2         2  
  2         43  
8 2     2   8 use warnings;
  2         4  
  2         62  
9              
10 2     2   6501 use XML::Twig;
  0            
  0            
11             use JSON;
12              
13             require Apache::Hadoop::Watcher::Base;
14              
15             our @ISA = qw();
16             our $VERSION = '0.01';
17              
18              
19             # methods
20             sub new {
21             my $class = shift;
22             my %args = @_;
23             my $self = {
24             url => 'http://'.($args{'host'}||'localhost').':'.($args{'port'}||'50070').'/conf',
25             conf => undef,
26             out => undef,
27             };
28              
29             # init
30             bless $self, $class;
31             $self->_init;
32             return $self;
33             }
34              
35             # conf xml methods
36             sub _parse {
37             my ($self, %opts) = (@_);
38             my ($name, $value, $source);
39             my %conf;
40             my $twig = new XML::Twig;
41             $twig->parse ($opts{'content'});
42            
43             foreach my $p ( $twig->root->children ('property') ) {
44             $name = ($p->children('name'))[0];
45             $value = ($p->children('value'))[0];
46             $source = ($p->children('source'))[0];
47              
48             $conf{$name->text} = { value=>$value->text, source=>$source->text };
49             }
50             $self->{'conf'} = \%conf;
51             }
52              
53             sub _init {
54             my ($self) = (@_);
55             my $base = Apache::Hadoop::Watcher::Base->new;
56             $self->_parse (content => $base->_wget (url => $self->{'url'}));
57             }
58              
59             # dumps output hashref
60             sub print {
61             my ($self) = (@_);
62             Apache::Hadoop::Watcher::Base->new->_print ( output=>$self->{'out'} );
63             }
64              
65             # returns output hashref
66             sub get {
67             my ($self) = (@_);
68             return $self->{'out'};
69             }
70              
71             # search by name, value from current config params
72             sub search {
73             my ($self, %opts) = (@_);
74             my $cfg;
75             my @keys;
76             my $by;
77              
78             $by = defined $opts{'by'} ? $opts{'by'} : 'name';
79             for ($by) {
80             /name/ && do { @keys = grep { /$opts{'pattern'}/i } keys %{$self->{'conf'}}; };
81             /value/ && do { @keys = grep { $self->{'conf'}->{$_}->{'value'} =~ m/$opts{'pattern'}/i } keys %{$self->{'conf'}}; };
82             /source/ && do { @keys = grep { $self->{'conf'}->{$_}->{'source'} =~ m/$opts{'pattern'}/i } keys %{$self->{'conf'}}; };
83             }
84             foreach my $name ( @keys ) {
85             $cfg->{$name}->{'value'} = $self->{'conf'}->{$name}->{'value'};
86             $cfg->{$name}->{'source'} = $self->{'conf'}->{$name}->{'source'};
87             }
88             $self->{'out'} = $cfg;
89             return $self;
90             }
91              
92             1;
93              
94             __END__