Jmx4Perl/Config.pm

filter: Statementstmt 74.1% 43 / 58 Branchbran 53.1% 17 / 32 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 75.0% 6 / 8 Podpod 100% 4 / 4 totaltotal 61.3% 73 / 119 CC 24 SCAR 24.9
CC
24
Cov
62%
_extract_servers
23.7
new
12.3
get_servers
6.0
CRAP 12.1
« Jmx4Perl/Agent/UserAgent.pm ↑ Summary Jmx4Perl/Agent/Jolokia/Logger.pm »
1 #!/usr/bin/perl
2 package JMX::Jmx4Perl::Config;
3 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 my $HAS_CONFIG_GENERAL;
6  
7 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
8 5 eval {
9 5 require "Config/General.pm";
10 };
11 5 ▶ $HAS_CONFIG_GENERAL = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 5 5
12 }
13  
14 =head1 NAME
15  
16 JMX::Jmx4Perl::Config - Configuration file support for Jmx4Perl
17  
18 =head1 SYNOPSIS
19  
20 =over
21  
22 =item Configuration file format
23  
24 # ================================================================
25 # Sample configuration for jmx4perl
26  
27 # localhost is the name how this config could accessed
28 <Server localhost>
29 # Options for JMX::Jmx4Perl->new, case is irrelevant
30 Url = http://localhost:8080/j4p
31 User = roland
32 Password = test
33 Product = JBoss
34  
35 # HTTP proxy for accessing the agent
36 <Proxy>
37 Url = http://proxy:8001
38 User = proxyuser
39 Password = ppaasswwdd
40 </Proxy>
41 # Target for running j4p in proxy mode
42 <Target>
43 Url service:jmx:iiop://....
44 User weblogic
45 Password weblogic
46 </Target>
47 </Server>
48  
49 =item Usage
50  
51 my $config = new JMX::Jmx4Perl::Config($config_file);
52  
53 =back
54  
55  
56 =head1 DESCRIPTION
57  
58  
59 =head1 METHODS
60  
61 =over
62  
63 =item $cfg = JMX::Jmx4Perl::Config->new($file_or_hash)
64  
65 Create a new configuration object with the given file name. If no file name is
66 given the configuration F<~/.j4p> is tried. In case the given file is a
67 directory, a file F<dir/jxm4perl.cfg> is tried as configuration file.
68  
69 If the file does not exist, C<server_config_exists> will alway return C<false> and
70 C<get_server_config> will always return C<undef>
71  
72 If a hash is given as argument, this hash is used to extract the server
73 information.
74  
75 =cut
76  
77 2 ▶sub new {
Subroutine new: called CC=10 SCAR=25.1
Pod documented
78 2 my $class = shift;
79 2 my $file_or_hash = shift;
80 2 my $self = {};
81 2 my $config = undef;;
82 2 ▶ if (!ref($file_or_hash)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $file_or_hash) { } 2 0 2
elsif (ref $file_or_hash eq 'HASH') { } 0 0 0
83 2 ▶ my $file = $file_or_hash ? $file_or_hash : $ENV{HOME} . "/.j4p";
Branch: 1 branch
BranchTrueFalseTotal
$file_or_hash ? : 1 1 2
84 2 ▶ $file = $file . "/jmx4perl.cfg" if -d $file;
Branch: 1 branch
BranchTrueFalseTotal
if -d $file 0 2 2
85 2 ▶ if (-e $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $file) { } 1 1 2
elsif (-d $file) { } 0 1 1
86 1 ▶ if ($HAS_CONFIG_GENERAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_CONFIG_GENERAL) { } 1 0 1
87 1 ▶ local $SIG{__WARN__} = sub {}; # Keep Config::General silent
Subroutine __ANON__: not called CC=1 SCAR=0.0
88 # when including things twice
89 1 $config = {
90 new Config::General(-ConfigFile => $file,-LowerCaseNames => 1,
91 -UseApacheInclude => 1,-IncludeRelative => 1, -IncludeAgain => 0,
92 -IncludeGlob => 1, -IncludeDirectories => 1, -CComments => 0)->getall
93 };
94 } else {
95 0 warn "Configuration file $file found, but Config::General is not installed.\n" .
96 "Please install Config::General, for the moment we are ignoring the content of $file\n\n";
97 }
98 } elsif (-d $file) {
99
100 }
101 } elsif (ref($file_or_hash) eq "HASH") {
102 0 $config = $file_or_hash;
103 } else {
104 0 die "Invalid argument ",$file_or_hash;
105 }
106 2 ▶ if ($config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) 1 1 2
107 1 $self->{server_config} = &_extract_servers($config);
108 1 $self->{servers} = [ values %{$self->{server_config}} ];
109 0 map { $self->{$_} = $config->{$_ } } grep { $_ ne "server" } keys %$config;
110 #print Dumper($self);
111 }
112  
113 2 ▶ bless $self,(ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
114 2 return $self;
115 }
116  
117 =item $exists = $config->server_config_exists($name)
118  
119 Check whether a configuration entry for the server with name $name
120 exist.
121  
122 =cut
123  
124 1 ▶sub server_config_exists {
Subroutine server_config_exists: called CC=3 SCAR=11.4
Pod documented
125 1 my $self = shift;
126 1 ▶ my $name = shift || die "No server name given to reference to get config for";
Condition50%1/2
shift() || die('No server name given to reference to get config for')
l!l
10
MC/DC0%0/1
shift() || die('No server name given to reference to get config for')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
127 1 my $cfg = $self->get_server_config($name);
128 1 ▶ return defined($cfg) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg ? : 1 0 1
129 }
130  
131 =item $server_config = $config->get_server_config($name)
132  
133 Get the configuration for the given server or C<undef>
134 if no such configuration exist.
135  
136 =cut
137  
138 2 ▶sub get_server_config {
Subroutine get_server_config: called CC=3 SCAR=11.7
Pod documented
139 2 my $self = shift;
140 2 ▶ my $name = shift || die "No server name given to reference to get config for";
Condition50%1/2
shift() || die('No server name given to reference to get config for')
l!l
20
MC/DC0%0/1
shift() || die('No server name given to reference to get config for')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
141 2 ▶ return $self->{server_config} ? $self->{server_config}->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'server_config'} ? : 2 0 2
142 }
143  
144 =item $servers = $config->get_servers
145  
146 Get an arrayref to all configured servers or an empty arrayref.
147  
148 =cut
149  
150 0 ▶sub get_servers {
Subroutine get_servers: not called CC=2 SCAR=17.9
Pod documented
151 0 my $self = shift;
152 0 ▶ return $self->{servers} || [];
Condition0%0/2
$self->{'servers'} || []
l!l
00
MC/DC0%0/1
$self->{'servers'} || []
$self->{'servers'}
Truth table0%0/2
A
A= $self->{'servers'}
Aresult
11
00
153 }
154  
155 1 ▶sub _extract_servers {
Subroutine _extract_servers: called CC=9 SCAR=31.7
156 1 my $config = shift;
157 1 my $servers = $config->{server};
158 1 my $ret = {};
159 1 ▶ return $ret unless $servers;
Branch: 1 branch
BranchTrueFalseTotal
unless $servers 0 1 1
160 1 ▶ if (ref($servers) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $servers eq 'ARRAY') { } 0 1 1
elsif (ref $servers eq 'HASH') { } 1 0 1
161 # Its a list of servers using old style (no named section, but with
162 # embedded 'name'
163 0 for my $s (@$servers) {
164 0 ▶ die "No name given for server config " . Dumper($s) . "\n" unless $s->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"name"} 0 0 0
165 0 $ret->{$s->{name}} = $s;
166 }
167 0 return $ret;
168 } elsif (ref($servers) eq "HASH") {
169 1 for my $name (keys %$servers) {
170 2 ▶ if (ref($servers->{$name}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $servers->{$name} eq 'HASH') { } 2 0 2
171 # A single, 'named' server section
172 2 $servers->{$name}->{name} = $name;
173 } else {
174 # It's a single server entry with 'old' style naming (e.g. no
175 # named section but a 'Name' property
176 0 my $ret = {};
177 0 ▶ my $name = $servers->{name} || die "Missing name for server section ",Dumper($servers);
Condition0%0/2
$servers->{'name'} || die('Missing name for server section ', Dumper($servers))
l!l
00
MC/DC0%0/1
$servers->{'name'} || die('Missing name for server section ', Dumper($servers))
$servers->{'name'}
Truth table0%0/2
A
A= $servers->{'name'}
Aresult
11
00
178 0 $ret->{$name} = $servers;
179 0 return $ret;
180 }
181 }
182 1 return $servers;
183 } else {
184 0 die "Invalid configuration type ",ref($servers),"\n";
185 }
186 }
187  
188 =back
189  
190 =head1 LICENSE
191  
192 This file is part of jmx4perl.
193  
194 Jmx4perl is free software: you can redistribute it and/or modify
195 it under the terms of the GNU General Public License as published by
196 the Free Software Foundation, either version 2 of the License, or
197 (at your option) any later version.
198  
199 jmx4perl is distributed in the hope that it will be useful,
200 but WITHOUT ANY WARRANTY; without even the implied warranty of
201 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
202 GNU General Public License for more details.
203  
204 You should have received a copy of the GNU General Public License
205 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
206  
207 A commercial license is available as well. Please contact roland@cpan.org for
208 further details.
209  
210 =head1 PROFESSIONAL SERVICES
211  
212 Just in case you need professional support for this module (or Nagios or JMX in
213 general), you might want to have a look at
214 http://www.consol.com/opensource/nagios/. Contact roland.huss@consol.de for
215 further information (or use the contact form at http://www.consol.com/contact/)
216  
217 =head1 AUTHOR
218  
219 roland@cpan.org
220  
221 =cut
222  
223 5 1;
« Jmx4Perl/Agent/UserAgent.pm ↑ Summary Jmx4Perl/Agent/Jolokia/Logger.pm »