File Coverage

blib/lib/JMX/Jmx4Perl/Config.pm
Criterion Covered Total %
statement 40 56 71.4
branch 15 28 53.5
condition 3 11 27.2
subroutine 6 8 75.0
pod 4 4 100.0
total 68 107 63.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             package JMX::Jmx4Perl::Config;
3 4     4   38513 use Data::Dumper;
  4         7  
  4         351  
4              
5             my $HAS_CONFIG_GENERAL;
6              
7             BEGIN {
8 4     4   8 eval {
9 4         4686 require "Config/General.pm";
10             };
11 4 50       126975 $HAS_CONFIG_GENERAL = $@ ? 0 : 1;
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            
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            
37             Url = http://proxy:8001
38             User = proxyuser
39             Password = ppaasswwdd
40            
41             # Target for running j4p in proxy mode
42            
43             Url service:jmx:iiop://....
44             User weblogic
45             Password weblogic
46            
47            
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
66             is given the configuration F<~/.j4p> is tried. If the file does not
67             exist, C will alway return C and
68             C will always return C
69              
70             If a hash is given as argument, this hash is used to extract the server
71             information.
72              
73             =cut
74              
75             sub new {
76 2     2 1 718 my $class = shift;
77 2         6 my $file_or_hash = shift;
78 2         5 my $self = {};
79 2         5 my $config = undef;;
80 2 50       11 if (!ref($file_or_hash)) {
    0          
81 2 100       12 my $file = $file_or_hash ? $file_or_hash : $ENV{HOME} . "/.j4p";
82 2 100       56 if (-e $file) {
83 1 50       4 if ($HAS_CONFIG_GENERAL) {
84 1     0   10 local $SIG{__WARN__} = sub {}; # Keep Config::General silent
  0         0  
85             # when including things twice
86 1         20 $config = {
87             new Config::General(-ConfigFile => $file,-LowerCaseNames => 1,
88             -UseApacheInclude => 1,-IncludeRelative => 1, -IncludeAgain => 0,
89             -IncludeGlob => 1, -IncludeDirectories => 1, -CComments => 0)->getall
90             };
91             } else {
92 0         0 warn "Configuration file $file found, but Config::General is not installed.\n" .
93             "Please install Config::General, for the moment we are ignoring the content of $file\n\n";
94             }
95             }
96             } elsif (ref($file_or_hash) eq "HASH") {
97 0         0 $config = $file_or_hash;
98             } else {
99 0         0 die "Invalid argument ",$file_or_hash;
100             }
101 2 100       1639 if ($config) {
102 1         5 $self->{server_config} = &_extract_servers($config);
103 1         2 $self->{servers} = [ values %{$self->{server_config}} ];
  1         4  
104 1         3 map { $self->{$_} = $config->{$_ } } grep { $_ ne "server" } keys %$config;
  0         0  
  1         4  
105             #print Dumper($self);
106             }
107              
108 2   33     20 bless $self,(ref($class) || $class);
109 2         9 return $self;
110             }
111              
112             =item $exists = $config->server_config_exists($name)
113              
114             Check whether a configuration entry for the server with name $name
115             exist.
116              
117             =cut
118              
119             sub server_config_exists {
120 1     1 1 778 my $self = shift;
121 1   50     5 my $name = shift || die "No server name given to reference to get config for";
122 1         5 my $cfg = $self->get_server_config($name);
123 1 50       8 return defined($cfg) ? 1 : 0;
124             }
125              
126             =item $server_config = $config->get_server_config($name)
127              
128             Get the configuration for the given server or C
129             if no such configuration exist.
130              
131             =cut
132              
133             sub get_server_config {
134 2     2 1 4 my $self = shift;
135 2   50     6 my $name = shift || die "No server name given to reference to get config for";
136 2 50       10 return $self->{server_config} ? $self->{server_config}->{$name} : undef;
137             }
138              
139             =item $servers = $config->get_servers
140              
141             Get an arrayref to all configured servers or an empty arrayref.
142              
143             =cut
144              
145             sub get_servers {
146 0     0 1 0 my $self = shift;
147 0   0     0 return $self->{servers} || [];
148             }
149              
150             sub _extract_servers {
151 1     1   2 my $config = shift;
152 1         3 my $servers = $config->{server};
153 1         3 my $ret = {};
154 1 50       6 return $ret unless $servers;
155 1 50       7 if (ref($servers) eq "ARRAY") {
    50          
156             # Its a list of servers using old style (no named section, but with
157             # embedded 'name'
158 0         0 for my $s (@$servers) {
159 0 0       0 die "No name given for server config " . Dumper($s) . "\n" unless $s->{name};
160 0         0 $ret->{$s->{name}} = $s;
161             }
162 0         0 return $ret;
163             } elsif (ref($servers) eq "HASH") {
164 1         4 for my $name (keys %$servers) {
165 2 50       7 if (ref($servers->{$name}) eq "HASH") {
166             # A single, 'named' server section
167 2         7 $servers->{$name}->{name} = $name;
168             } else {
169             # It's a single server entry with 'old' style naming (e.g. no
170             # named section but a 'Name' property
171 0         0 my $ret = {};
172 0   0     0 my $name = $servers->{name} || die "Missing name for server section ",Dumper($servers);
173 0         0 $ret->{$name} = $servers;
174 0         0 return $ret;
175             }
176             }
177 1         5 return $servers;
178             } else {
179 0           die "Invalid configuration type ",ref($servers),"\n";
180             }
181             }
182              
183             =back
184              
185             =head1 LICENSE
186              
187             This file is part of jmx4perl.
188              
189             Jmx4perl is free software: you can redistribute it and/or modify
190             it under the terms of the GNU General Public License as published by
191             the Free Software Foundation, either version 2 of the License, or
192             (at your option) any later version.
193              
194             jmx4perl is distributed in the hope that it will be useful,
195             but WITHOUT ANY WARRANTY; without even the implied warranty of
196             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
197             GNU General Public License for more details.
198              
199             You should have received a copy of the GNU General Public License
200             along with jmx4perl. If not, see .
201              
202             A commercial license is available as well. Please contact roland@cpan.org for
203             further details.
204              
205             =head1 PROFESSIONAL SERVICES
206              
207             Just in case you need professional support for this module (or Nagios or JMX in
208             general), you might want to have a look at
209             http://www.consol.com/opensource/nagios/. Contact roland.huss@consol.de for
210             further information (or use the contact form at http://www.consol.com/contact/)
211              
212             =head1 AUTHOR
213              
214             roland@cpan.org
215              
216             =cut
217              
218             1;