Jmx4Perl/Request.pm

filter: Statementstmt 80.9% 106 / 131 Branchbran 55.4% 41 / 74 Conditioncond 44.4% 8 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 94.7% 18 / 19 Podpod 80.0% 4 / 5 totaltotal 69.1% 179 / 259 CC 46 SCAR 31.1
CC
46
Cov
70%
TO_JSON
56.0
new
34.0
_validate
27.6
CRAP 22.3
« Jmx4Perl/Agent/Jolokia/Verifier/GnuPGVerifier.pm ↑ Summary Jmx4Perl/Agent/Jolokia/ArtifactHandler.pm »
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 JMX::Jmx4Perl::Request - A jmx4perl request
6  
7 =head1 SYNOPSIS
8  
9 $req = JMX::Jmx4Perl::Request->new(READ,$mbean,$attribute);
10  
11 =head1 DESCRIPTION
12  
13 A L<JMX::Jmx4Perl::Request> encapsulates a request for various operational
14 types.
15  
16 The following attributes are available:
17  
18 =over
19  
20 =item mbean
21  
22 Name of the targetted mbean in its canonical format.
23  
24 =item type
25  
26 Type of request, which should be one of the constants
27  
28 =over
29  
30 =item READ
31  
32 Get the value of a attribute
33  
34 =item WRITE
35  
36 Write an attribute
37  
38 =item EXEC
39  
40 Execute an JMX operation
41  
42 =item LIST
43  
44 List MBean meta data
45  
46 =item SEARCH
47  
48 Search for MBeans
49  
50 =item AGENT_VERSION
51  
52 Get the agent's version and extra runtime information of the serverside.
53  
54 =item REGISTER_NOTIFICATION
55  
56 Register for a JMX notification (not supported yet)
57  
58 =item REMOVE_NOTIFICATION
59  
60 Remove a JMX notification (not supported yet)
61  
62 =back
63  
64 =item attribute
65  
66 If type is C<READ> or C<WRITE> this specifies the requested
67 attribute
68  
69 =item value
70  
71 For C<WRITE> this specifies the value to set
72  
73 =item arguments
74  
75 List of arguments of C<EXEC> operations
76  
77 =item path
78  
79 This optional parameter can be used to specify a nested value in an complex
80 mbean attribute or nested return value from a JMX operation. For example, the
81 MBean C<java.lang:type=Memory>'s attribute C<HeapMemoryUsage> is a complex
82 value, which looks in the JSON representation like
83  
84 "value":{"init":0,"max":518979584,"committed":41381888,"used":33442568}
85  
86 So, to fetch the C<"used"> value only, specify C<used> as path within the
87 request. You can access deeper nested values by building up a path with "/" as
88 separator. This looks a bit like a simplified form of XPath.
89  
90 =item maxDepth, maxObjects, maxCollectionSize, ignoreErrors
91  
92 With these number you can restrict the size of the JSON structure
93 returned. C<maxDepth> gives the maximum nesting level of the JSON
94 object,C<maxObjects> returns the maximum number of objects to be returned in
95 total and C<maxCollectionSize> restrict the number of all arrays and
96 collections (maps, lists) in the answer. Note, that you should use this
97 restrictions if you are doing massive bulk operations. C<ignoreErrors> is
98 useful for read requests with multiple attributes to skip errors while reading
99 attribute values on the errors side (the error text will be set as value).
100  
101 =item target
102  
103 If given, the request is processed by the agent in proxy mode, i.e. it will
104 proxy to another server exposing via a JSR-160 connector. C<target> is a hash
105 which contains information how to reach the target service via the proxy. This
106 hash knows the following keys:
107  
108 =over
109  
110 =item url
111  
112 JMX service URL as specified in JSR-160 pointing to the target server.
113  
114 =item env
115  
116 Further context information which is another hash.
117  
118 =back
119  
120 =back
121  
122 =head1 METHODS
123  
124 =over
125  
126 =cut
127  
128 package JMX::Jmx4Perl::Request;
129  
130 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 4 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
132 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
136 4 @EXPORT = (
137 "READ","WRITE","EXEC","LIST", "SEARCH",
138 "REGNOTIF","REMNOTIF", "AGENT_VERSION"
139 );
140  
141 4 ▶use constant READ => "read";
Subroutine BEGIN: called CC=1 SCAR=0.0
142 4 ▶use constant WRITE => "write";
Subroutine BEGIN: called CC=1 SCAR=0.0
143 4 ▶use constant EXEC => "exec";
Subroutine BEGIN: called CC=1 SCAR=0.0
144 4 ▶use constant LIST => "list";
Subroutine BEGIN: called CC=1 SCAR=0.0
145 4 ▶use constant SEARCH => "search";
Subroutine BEGIN: called CC=1 SCAR=0.0
146 4 ▶use constant REGNOTIF => "regnotif";
Subroutine BEGIN: called CC=1 SCAR=0.0
147 4 ▶use constant REMNOTIF => "remnotif";
Subroutine BEGIN: called CC=1 SCAR=0.0
148 4 ▶use constant AGENT_VERSION => "version";
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 my $TYPES =
151 4 { map { $_ => 1 } (READ, WRITE, EXEC, LIST, SEARCH,
152 REGNOTIF, REMNOTIF, AGENT_VERSION) };
153  
154 =item $req = new JMX::Jmx4Perl::Request(....);
155  
156 $req = new JMX::Jmx4Perl::Request(READ,$mbean,$attribute,$path, { ... options ... } );
157 $req = new JMX::Jmx4Perl::Request(READ,{ mbean => $mbean,... });
158 $req = new JMX::Jmx4Perl::Request({type => READ, mbean => $mbean, ... });
159  
160 The constructor can be used in various way. In the simplest form, you provide
161 the type as first argument and depending on the type one or more additional
162 attributes which specify the request. The second form uses the type as first
163 parameter and a hashref containing named parameter for the request parameters
164 (for the names, see above). Finally you can specify the arguments completely as
165 a hashref, using 'type' for the entry specifying the request type.
166  
167 For the options C<maxDepth>, C<maxObjects> and C<maxCollectionSize>, you can mix
168 them in into the hashref if using the hashed argument format. For the first
169 format, these options are given as a final hashref.
170  
171 The option C<method> can be used to suggest a HTTP request method to use. By
172 default, the agent decides automatically which HTTP method to use depending on
173 the number of requests and whether an extended format should be used (which is
174 only possible with an HTTP POST request). The value of this option can be
175 either C<post> or C<get>, dependening on your preference.
176  
177 If the request should be proxied through this request, a target configuration
178 needs to be given as optional parameter. The target configuration consists of a
179 JMX service C<url> and a optional environment, which is given as a key-value
180 map. For example
181  
182 $req = new JMX::Jmx4Perl::Request(..., {
183 target => {
184 url => "",
185 env => { ..... }
186 }
187 } );
188  
189 Note, depending on the type, some parameters are mandatory. The mandatory
190 parameters and the order of the arguments for the constructor variant without
191 named parameters are:
192  
193 =over
194  
195 =item C<READ>
196  
197 Order : $mbean, $attribute, $path
198 Mandatory: $mbean, $attribute
199  
200 Note that C<$attribute> can be either a single name or a reference to a list
201 of attribute names.
202  
203 =item C<WRITE>
204  
205 Order : $mbean, $attribute, $value, $path
206 Mandatory: $mbean, $attribute, $value
207  
208 =item C<EXEC>
209  
210 Order : $mbean, $operation, $arg1, $arg2, ...
211 Mandatory: $mbean, $operation
212  
213  
214 =item C<LIST>
215
216 Order : $mbean, $path
217  
218 =item C<SEARCH>
219  
220 Order : $pattern
221 Mandatory: $pattern
222  
223 =back
224  
225 =cut
226  
227 24 ▶sub new {
Subroutine new: called CC=18 SCAR=35.3
Pod documented
228 24 my $class = shift;
229 24 my $type = shift;
230 24 my $self;
231 # Hash as argument
232 24 ▶ if (ref($type) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq "HASH") 0 24 24
233 0 $self = $type;
234 0 $type = $self->{type};
235 }
236 24 ▶ croak "Invalid type '",$type,"' given (should be one of ",join(" ",keys %$TYPES),")" unless $TYPES->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $TYPES->{$type} 0 24 24
237
238 # Hash comes after type
239 24 ▶ if (!$self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 24 0 24
240 24 ▶ if (ref($_[0]) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 24 24
241 0 $self = $_[0];
242 0 $self->{type} = $type;
243 } else {
244 # Unnamed arguments
245 24 $self = {type => $type};
246  
247 # Options are given as last part
248 24 my $opts = $_[scalar(@_)-1];
249 24 ▶ if (ref($opts) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts eq "HASH") 2 22 24
250 2 pop @_;
251 2 map { $self->{$_} = $opts->{$_} } keys %$opts;
252 2 ▶ if ($self->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"method"}) 2 0 2
253 # Canonicalize and verify
254 2 method($self,$self->{method});
255 }
256 }
257 24 ▶ if ($type eq READ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'read') { } 20 4 24
elsif ($type eq 'write') { } 0 4 4
elsif ($type eq 'exec') { } 0 4 4
elsif ($type eq 'list') { } 4 0 4
elsif ($type eq 'search') { } 0 0 0
elsif ($type eq 'version') { } 0 0 0
258 20 $self->{mbean} = shift;
259 20 $self->{attribute} = shift;
260 20 $self->{path} = shift;
261 # Use post for complex read requests
262 20 ▶ if (ref($self->{attribute}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"attribute"} eq "ARRAY") 2 18 20
263 2 my $method = method($self);
264 2 ▶ if (defined($method) && $method eq "GET") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $method and $method eq "GET") 1 1 2
Condition66%2/3
defined $method and $method eq "GET"
!ll&&!rl&&r
101
MC/DC50%1/2
defined $method and $method eq "GET"
defined $method $method eq "GET"
Truth table66%2/3
A and B
A= defined $methodB= $method eq "GET"
ABresult
0X0
100
111
265 # Was already explicitely set
266 1 die "Cannot query for multiple attributes " . join(",",@{$self->{attributes}}) . " with a GET request"
267 1 ▶ if ref($self->{attribute}) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"attribute"} eq "ARRAY" 1 0 1
268 }
269 1 method($self,"POST");
270 }
271 } elsif ($type eq WRITE) {
272 0 $self->{mbean} = shift;
273 0 $self->{attribute} = shift;
274 0 $self->{value} = shift;
275 0 $self->{path} = shift;
276 } elsif ($type eq EXEC) {
277 0 $self->{mbean} = shift;
278 0 $self->{operation} = shift;
279 0 $self->{arguments} = [ @_ ];
280 } elsif ($type eq LIST) {
281 4 $self->{mbean} = shift;
282 4 $self->{path} = shift;
283 } elsif ($type eq SEARCH) {
284 0 $self->{mbean} = shift;
285 #No check here until now, is done on the server side as well.
286 #die "MBean name ",$self->{mbean}," is not a pattern" unless is_mbean_pattern($self);
287 } elsif ($type eq AGENT_VERSION) {
288 # No extra parameters required
289 } else {
290 0 croak "Type ",$type," not supported yet";
291 }
292 }
293 }
294 23 ▶ bless $self,(ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0230
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
295 23 $self->_validate();
296 23 return $self;
297 }
298  
299 =item $req->method()
300  
301 =item $req->method("POST")
302  
303 Set the HTTP request method for this requst excplicitely. If not provided
304 either during construction time (config key 'method') a prefered request
305 method is determined dynamically based on the request contents.
306  
307 =cut
308  
309 8 ▶sub method {
Subroutine method: called CC=5 SCAR=16.5
Pod documented
310 8 my $self = shift;
311 8 my $value = shift;
312 8 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 3 5 8
313 3 ▶ die "Unknown request method ",$value if length($value) && uc($value) !~ /^(POST|GET)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if length $value and not uc($value) =~ /^(POST|GET)$/i 0 3 3
Condition33%1/3
length $value and not uc($value) =~ /^(POST|GET)$/i
!ll&&!rl&&r
030
MC/DC0%0/2
length $value and not uc($value) =~ /^(POST|GET)$/i
length $value not uc($value) =~ /^(POST|GET)$/i
Truth table33%1/3
A and not(B)
A= length $valueB= not uc($value) =~ /^(POST|GET)$/i
ABresult
0X0
100
111
314 3 $self->{method} = uc($value);
315 }
316 8 ▶ return defined($self->{method}) ? $self->{method} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'method'} ? : 6 2 8
317 }
318  
319 =item $req->is_mbean_pattern
320  
321 Returns true, if the MBean name used in this request is a MBean pattern (which
322 can be used in C<SEARCH> or for C<READ>) or not
323  
324 =cut
325  
326 16 ▶sub is_mbean_pattern {
Subroutine is_mbean_pattern: called CC=9 SCAR=22.0
Pod documented
327 16 my $self = shift;
328 16 ▶ my $mbean = shift || $self->{mbean};
Condition33%1/3
shift() || $self->{'mbean'}
l!l&&r!l&&!r
0160
MC/DC0%0/2
shift() || $self->{'mbean'}
shift() $self->{'mbean'}
Truth table33%1/3
A || B
A= shift()B= $self->{'mbean'}
ABresult
1X1
011
000
329 16 ▶ return 1 unless $mbean;
Branch: 1 branch
BranchTrueFalseTotal
unless $mbean 0 16 16
330 16 my ($domain,$rest) = split(/:/,$mbean,2);
331 16 ▶ return 1 if $domain =~ /[*?]/;
Branch: 1 branch
BranchTrueFalseTotal
if $domain =~ /[*?]/ 4 12 16
332 12 ▶ return 1 if $rest =~ /\*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\*$/ 3 9 12
333  
334 9 ▶ while ($rest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest) 12 5 17
335 #print "R: $rest\n";
336 12 $rest =~ s/([^=]+)\s*=\s*//;
337 12 my $key = $1;
338 12 my $value;
339 12 ▶ if ($rest =~ /^"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^"/) { } 6 6 12
340 6 $rest =~ s/("(\\"|[^"])+")(\s*,\s*|$)//;
341 6 $value = $1;
342 # Pattern in quoted values must not be preceded by a \
343 6 ▶ return 1 if $value =~ /(?<!\\)[\*\?]/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /(?<!\\)[\*\?]/ 1 5 6
344 } else {
345 6 $rest =~ s/([^,]+)(\s*,\s*|$)//;
346 6 $value = $1;
347 6 ▶ return 1 if $value =~ /[\*\?]/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[\*\?]/ 3 3 6
348 }
349 #print "K: $key V: $value\n";
350 }
351 5 return 0;
352 }
353  
354 =item $request->get("type")
355  
356 Get a request parameter
357  
358 =cut
359  
360 28 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
361 28 my $self = shift;
362 28 my $name = shift;
363 28 return $self->{$name};
364 }
365  
366 # Internal check for validating that all arguments are given
367 23 ▶sub _validate {
Subroutine _validate: called CC=11 SCAR=33.2
368 23 my $self = shift;
369 23 ▶ if ($self->{type} eq READ || $self->{type} eq WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "read" or $self->{"type"} eq "write") 19 4 23
Condition66%2/3
$self->{"type"} eq "read" or $self->{"type"} eq "write"
l!l&&r!l&&!r
1904
MC/DC50%1/2
$self->{"type"} eq "read" or $self->{"type"} eq "write"
$self->{"type"} eq "read" $self->{"type"} eq "write"
Truth table66%2/3
A or B
A= $self->{"type"} eq "read"B= $self->{"type"} eq "write"
ABresult
1X1
011
000
370 19 ▶ die $self->{type} . ": No mbean name given\n",Dumper($self) unless $self->{mbean};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"mbean"} 0 19 19
371 19 ▶ die $self->{type} . ": No attribute name but path is given\n" if (!$self->{attribute} && $self->{path});
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"attribute"} and $self->{"path"} 0 19 19
Condition33%1/3
not $self->{"attribute"} and $self->{"path"}
!ll&&!rl&&r
1900
MC/DC0%0/2
not $self->{"attribute"} and $self->{"path"}
not $self->{"attribute"} $self->{"path"}
Truth table33%1/3
not(A) and B
A= not $self->{"attribute"}B= $self->{"path"}
ABresult
0X0
100
111
372 }
373 23 ▶ if ($self->{type} eq WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "write") 0 23 23
374 0 ▶ die $self->{type} . ": No value given\n" unless defined($self->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"value"} 0 0 0
375 }
376 23 ▶ if ($self->{type} eq EXEC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "exec") 0 23 23
377 0 ▶ die $self->{type} . ": No mbean name given\n" unless $self->{mbean};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"mbean"} 0 0 0
378 0 ▶ die $self->{type} . ": No operation name given\n" unless $self->{operation};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"operation"} 0 0 0
379 }
380 }
381  
382 # Called for post requests
383 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=7 SCAR=40.3
Pod undocumented
384 0 my $self = shift;
385 my $ret = {
386 0 ▶ type => $self->{type} ? uc($self->{type}) : undef,
Branch: 1 branch
BranchTrueFalseTotal
$self->{'type'} ? : 0 0 0
387 };
388 0 for my $k (qw(mbean attribute path value operation arguments target)) {
389 0 ▶ $ret->{$k} = $self->{$k} if defined($self->{$k});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$k} 0 0 0
390 }
391 0 my %config;
392 0 for my $k (qw(maxDepth maxObjects maxCollectionSize ignoreErrors)) {
393 0 ▶ $config{$k} = $self->{$k} if defined($self->{$k});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$k} 0 0 0
394 }
395 0 ▶ $ret->{config} = \%config if keys(%config);
Branch: 1 branch
BranchTrueFalseTotal
if keys %config 0 0 0
396 0 return $ret;
397 }
398  
399 =back
400  
401 =head1 LICENSE
402  
403 This file is part of jmx4perl.
404  
405 Jmx4perl is free software: you can redistribute it and/or modify
406 it under the terms of the GNU General Public License as published by
407 the Free Software Foundation, either version 2 of the License, or
408 (at your option) any later version.
409  
410 jmx4perl is distributed in the hope that it will be useful,
411 but WITHOUT ANY WARRANTY; without even the implied warranty of
412 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
413 GNU General Public License for more details.
414  
415 You should have received a copy of the GNU General Public License
416 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
417  
418 A commercial license is available as well. Please contact roland@cpan.org for
419 further details.
420  
421 =head1 AUTHOR
422  
423 roland@cpan.org
424  
425 =cut
426  
427 4 1;
« Jmx4Perl/Agent/Jolokia/Verifier/GnuPGVerifier.pm ↑ Summary Jmx4Perl/Agent/Jolokia/ArtifactHandler.pm »