File Coverage

blib/lib/Net/JBoss/Management.pm
Criterion Covered Total %
statement 12 163 7.3
branch 0 46 0.0
condition n/a
subroutine 4 25 16.0
pod 21 21 100.0
total 37 255 14.5


line stmt bran cond sub pod time code
1             package Net::JBoss::Management;
2              
3 1     1   828 use 5.010;
  1         3  
  1         25  
4 1     1   4 use Carp;
  1         1  
  1         48  
5 1     1   341 use URL::Encode qw /url_encode_utf8/;
  1         2870  
  1         36  
6 1     1   380 use Moo;
  1         1261  
  1         3  
7              
8             with 'Net::JBoss';
9              
10             =head1 NAME
11              
12             Net::JBoss::Management - Bindings for JBoss Management API
13              
14             =head1 VERSION
15              
16             Version 0.02
17              
18             =cut
19              
20             our $VERSION = '0.02';
21              
22              
23             =head1 SYNOPSIS
24              
25             use Net::JBoss::Management;
26              
27             my %con = (
28             username => 'admin',
29             password => 'password',
30             server => 'jboss1.example.com',
31             port => 9990, #optional
32             );
33            
34             my $jboss = Net::JBoss::Management->new(%con);
35            
36             my $state = $jboss->get_state();
37             my $jvm_usage = $jboss->get_jvm_usage();
38             my $runtime_stats = $jboss->get_runtime_stats();
39             my $deploy_info = $jboss->get_deployment_info();
40             my $app_runtime_stats = $jboss->get_app_runtime_stats('hawtio.war');
41             my $runtime_details = $jboss->get_runtime_details();
42             my $app_status = $jboss->get_app_status('hawtio.war');
43             my $active_session = $jboss->get_active_sessions('hawtio.war');
44             my $server_env = $jboss->get_server_env();
45             my $datasources = $jboss->get_datasources();
46             my $test = $jboss->test_con_pool('ExampleDS');
47             my $pool_stats = $jboss->get_ds_pool_stats('java:jboss/datasources/jboss_Pool');
48             my $enable_pool_stats = $jboss->get_ds_pool_stats('java:jboss/datasources/jboss_Pool', 'true');
49             my $disable_pool_stats = $jboss->get_ds_pool_stats('java:jboss/datasources/jboss_Pool', 'false');
50             my $min_pool_size = $jboss->set_ds_pool_size('min', 'java:jboss/datasources/jboss_Pool', 20);
51             my $max_pool_size = $jboss->set_ds_pool_size('max', 'java:jboss/datasources/jboss_Pool', 50);
52             my $jndi = $jboss->get_jndi();
53             my $loglevel = $jboss->get_log_level('CONSOLE');
54             my $loglevel = $jboss->set_log_level('CONSOLE', 'ERROR');
55             my $reload = $jboss->reload();
56             my $shutdown = $jboss->shutdown();
57             my $restart = $jboss->restart();
58            
59             =head1 Attributes
60              
61             Other attributes is also inherited from JBoss.pm
62             Check 'perldoc Net::JBoss' for detail
63            
64             notes :
65             ro = read only, can be specified only during initialization
66             rw = read write, user can set this attribute
67             rwp = read write protected, for internal class
68            
69             management_url = (ro) store default management url path
70             reload_time = (rw) set wait time (in seconds) to check the server state after reload is fired (default 10s)
71              
72             =cut
73              
74             has 'management_url' => ( is => 'ro', default => '/management' );
75             has 'reload_time' => ( is => 'rw', default => 10 );
76              
77             =head1 SUBROUTINES/METHODS
78              
79             =head2 BUILD
80              
81             The Constructor, build logging, call pass_log_obj method
82             Build final url
83             =cut
84              
85             sub BUILD {
86 0     0 1   my $self = shift;
87            
88 0           $self->pass_log_obj;
89              
90 0           my $url = $self->base_url . $self->management_url;
91 0           $self->_set_url($url);
92            
93 0           $self->log->debug($url);
94             }
95              
96             =head2 get_state
97              
98             return server state
99             my $state = $jboss->state();
100            
101             =cut
102              
103             sub get_state {
104 0     0 1   my $self = shift;
105            
106 0           $self->_set_http_post(1);
107            
108 0 0         if ($self->http_post) {
109 0           $self->_set_post_json('{"operation":"read-attribute","name":"server-state","json.pretty":1}');
110 0           $self->log->debug($self->post_json);
111             }
112             else {
113 0           $self->_set_resource_url('/?operation=attribute&name=server-state&json.pretty');
114 0           $self->log->debug($self->resource_url);
115             }
116            
117 0           $self->get_api_response();
118             }
119              
120             =head2 get_jvm_usage
121              
122             return jvm usage
123             my $jvm_usage = $jboss->get_jvm_usage();
124            
125             =cut
126              
127             sub get_jvm_usage {
128 0     0 1   my $self = shift;
129            
130 0           $self->_set_resource_url('/core-service/platform-mbean/type/memory?operation=resource&include-runtime=true&json.pretty');
131 0           $self->log->debug($self->resource_url);
132            
133 0           $self->get_api_response();
134             }
135              
136             =head2 get_runtime_stats
137              
138             get HTTP connector runtime statistics
139             my $runtime_stats = $jboss->runtime_stats();
140            
141             =cut
142              
143             sub get_runtime_stats {
144 0     0 1   my $self = shift;
145            
146 0           $self->_set_resource_url('/subsystem/web/connector/http?operation=resource&include-runtime=true&recursive&json.pretty');
147 0           $self->log->debug($self->resource_url);
148            
149 0           $self->get_api_response();
150             }
151              
152             =head2 get_runtime_details
153              
154             get JBoss runtime details
155             my $runtime_details = $jboss->get_runtime_details();
156            
157             =cut
158              
159             sub get_runtime_details {
160 0     0 1   my $self = shift;
161            
162 0           $self->_set_resource_url('/core-service/platform-mbean/type/runtime?operation=resource&include-runtime=true&json.pretty');
163 0           $self->log->debug($self->resource_url);
164            
165 0           $self->get_api_response();
166             }
167              
168             =head2 get_app_runtime_stats
169              
170             get application runtime statistics
171             my $app_runtime_stats = $jboss->get_app_runtime_stats('hawtio.war');
172            
173             =cut
174              
175             sub get_app_runtime_stats {
176 0     0 1   my $self = shift;
177            
178 0           my $app_name = shift;
179 0 0         croak "web application name is required"
180             unless $app_name;
181            
182 0           $self->_set_http_post(1);
183            
184 0           my $json = qq|{"operation":"read-resource","recursive":"true", "include-runtime":"true", "address":["deployment","$app_name"], "json.pretty":1}|;
185            
186 0           $self->_set_post_json($json);
187            
188 0           $self->log->debug($self->post_json);
189 0           $self->get_api_response();
190             }
191              
192             =head2 get_server_env
193              
194             get JBoss server environment
195             my $server_env = $jboss->get_server_env();
196            
197             =cut
198              
199             sub get_server_env {
200 0     0 1   my $self = shift;
201            
202 0           $self->_set_resource_url('/core-service/server-environment?operation=resource&include-runtime=true&json.pretty');
203 0           $self->log->debug($self->resource_url);
204            
205 0           $self->get_api_response();
206             }
207              
208             =head2 get_datasources
209              
210             get data source and driver
211             my $datasources = $jboss->get_datasources();
212            
213             =cut
214              
215             sub get_datasources {
216 0     0 1   my $self = shift;
217            
218 0           $self->_set_resource_url('/subsystem/datasources/?operation=resource&recursive=true&json.pretty');
219 0           $self->log->debug($self->resource_url);
220            
221 0           $self->get_api_response();
222             }
223              
224             =head2 get_app_status
225              
226             get web application status
227             web application name is required
228             my $app_status = $jboss->get_app_status('hawtio.war');
229            
230             =cut
231              
232             sub get_app_status {
233 0     0 1   my $self = shift;
234            
235 0           my $app_name = shift;
236 0 0         croak "web application name is required"
237             unless $app_name;
238            
239 0           $self->_set_resource_url(qq|/deployment/$app_name?operation=attribute&name=status&json.pretty|);
240 0           $self->log->debug($self->resource_url);
241            
242 0           $self->get_api_response();
243             }
244              
245             =head2 get_active_sessions
246              
247             get web application active sessions
248             web application name is required
249             my $active_session = $jboss->get_active_sessions('hawtio.war');
250            
251             =cut
252              
253             sub get_active_sessions {
254 0     0 1   my $self = shift;
255            
256 0           my $app_name = shift;
257 0 0         croak "web application name is required"
258             unless $app_name;
259            
260 0           $self->_set_resource_url(qq|/deployment/$app_name/subsystem/web?operation=attribute&name=active-sessions&json.pretty|);
261 0           $self->log->debug($self->resource_url);
262            
263 0           $self->get_api_response();
264             }
265              
266             =head2 get_ds_pool_stats
267              
268             get usage metric of connection pooling of the data source
269             my $pool_stats = $jboss->get_ds_pool_stats('java:jboss/datasources/jboss_Pool');
270              
271             =cut
272              
273             sub get_ds_pool_stats {
274 0     0 1   my $self = shift;
275 0           my $ds_name = shift;
276            
277 0 0         croak "data source name is required"
278             unless $ds_name;
279 0           $ds_name = url_encode_utf8($ds_name);
280 0           $self->log->debug("encode : $ds_name");
281            
282 0           $self->_set_resource_url(qq|/subsystem/datasources/data-source/$ds_name/statistics/pool/?operation=resource&recursive=true&include-runtime=true&json.pretty|);
283 0           $self->log->debug($self->resource_url);
284            
285 0           $self->get_api_response();
286             }
287              
288             =head2 test_con_pool
289              
290             test datasource connection polling
291             my $test = $jboss->test_con_pool('ExampleDS');
292              
293             =cut
294              
295             sub test_con_pool {
296 0     0 1   my $self = shift;
297            
298 0           my $ds_name = shift;
299            
300 0 0         croak "data source name is required"
301             unless $ds_name;
302            
303 0           $self->_set_http_post(1);
304            
305 0           my $json = qq|{"operation":"test-connection-in-pool","address":[{"subsystem":"datasources"},{"data-source":"$ds_name"}],"json.pretty":1}|;
306            
307 0           $self->_set_post_json($json);
308            
309 0           $self->log->debug($self->post_json);
310 0           $self->get_api_response();
311             }
312              
313             =head2 set_ds_pool_size
314              
315             set data source min/max pool size
316             my $min_pool_size = $jboss->set_ds_pool_size('min', 'java:jboss/datasources/jboss_Pool', 20);
317             my $max_pool_size = $jboss->set_ds_pool_size('max', 'java:jboss/datasources/jboss_Pool', 20);
318              
319             =cut
320              
321             sub set_ds_pool_size {
322 0     0 1   my $self = shift;
323            
324 0           my ($name, $ds_name, $size) = @_;
325            
326 0 0         croak "min/max is required"
327             unless $name;
328            
329 0 0         croak "$name is not valid value, valid argument is min|max"
330             unless $name =~ /^\b(min|max)\b/;
331            
332 0 0         croak "data source name is required"
333             unless $ds_name;
334            
335 0 0         croak "size is required and must be positive integer"
336             unless $size =~ /^\d+$/;
337            
338 0 0         croak "size must be greater than zero"
339             unless $size > 0;
340            
341 0           $self->_set_http_post(1);
342            
343 0           my $json;
344            
345 0 0         if ($name eq 'min') {
    0          
346 0           $json = qq|{"operation":"write-attribute","address":[{"subsystem":"datasources"},{"data-source":"$ds_name"}],"name":"min-pool-size","value":$size,"json.pretty":1}|;
347             }
348             elsif ($name eq 'max') {
349 0           $json = qq|{"operation":"write-attribute","address":[{"subsystem":"datasources"},{"data-source":"$ds_name"}],"name":"max-pool-size","value":$size,"json.pretty":1}|;
350             }
351            
352 0           $self->_set_post_json($json);
353            
354 0           $self->log->debug($self->post_json);
355 0           $self->get_api_response();
356             }
357              
358             =head2 set_ds_pool_stats
359              
360             set enable/disable data source pool statistics
361             my $enable_pool_stats = $jboss->set_ds_pool_stats('java:jboss/datasources/jboss_Pool', 'true');
362             my $disable_pool_stats = $jboss->set_ds_pool_stats('java:jboss/datasources/jboss_Pool', 'false');
363              
364             =cut
365              
366             sub set_ds_pool_stats {
367 0     0 1   my $self = shift;
368            
369 0           my ($ds_name, $value) = @_;
370            
371 0 0         croak "data source name is required"
372             unless $ds_name;
373            
374 0 0         croak "value is not defined"
375             unless $value;
376            
377 0 0         $value = lc ($value) if $value;
378            
379 0 0         croak "value is invalid, valid value is 'true' or 'false'"
380             unless $value =~ /^\b(true|false)\b/;
381            
382 0           $self->_set_http_post(1);
383            
384 0           my $json = qq|{"operation":"write-attribute","address":[{"subsystem":"datasources"},{"data-source":"$ds_name"}],"name":"statistics-enabled","value":"$value","json.pretty":1}|;
385            
386 0           $self->_set_post_json($json);
387            
388 0           $self->log->debug($self->post_json);
389 0           $self->get_api_response();
390             }
391              
392             =head2 reload
393              
394             reload jboss only
395             my $reload = $jboss->reload();
396              
397             =cut
398              
399             sub reload {
400 0     0 1   my $self = shift;
401            
402 0           $self->_set_http_post(1);
403            
404 0           my $json = qq|{"operation":"reload","json.pretty":1}|;
405            
406 0           $self->_set_post_json($json);
407            
408 0           $self->log->debug($self->post_json);
409 0           $self->get_api_response();
410            
411             # wait for the state
412 0           $self->log->debug("wait for " . $self->reload_time . " seconds before checking the state");
413 0           sleep ($self->reload_time);
414 0           $self->get_state();
415             }
416              
417             =head2 shutdown
418              
419             shutdown jboss
420             my $shutdown = $jboss->shutdown();
421              
422             =cut
423              
424             sub shutdown {
425 0     0 1   my $self = shift;
426            
427 0           $self->_set_http_post(1);
428            
429 0           my $json = qq|{"operation":"shutdown","json.pretty":1}|;
430            
431 0           $self->_set_post_json($json);
432            
433 0           $self->log->debug($self->post_json);
434 0           $self->get_api_response();
435             }
436              
437             =head2 restart
438              
439             restart jboss and jvm
440             my $restart = $jboss->restart();
441              
442             =cut
443              
444             sub restart {
445 0     0 1   my $self = shift;
446            
447 0           $self->_set_http_post(1);
448            
449 0           my $json = qq|{"operation":"shutdown","restart":"true","json.pretty":1}|;
450            
451 0           $self->_set_post_json($json);
452            
453 0           $self->log->debug($self->post_json);
454 0           $self->get_api_response();
455             }
456              
457             =head2 get_deployment_info
458              
459             return deployment detail information
460             my $deploy_info = $jboss->get_deployment_info();
461              
462             =cut
463              
464             sub get_deployment_info {
465 0     0 1   my $self = shift;
466            
467 0           $self->_set_http_post(1);
468            
469 0           my $json = qq|{"operation":"read-children-resources","child-type":"deployment","recursive":"true","json.pretty":1}|;
470            
471 0           $self->_set_post_json($json);
472            
473 0           $self->log->debug($self->post_json);
474 0           $self->get_api_response();
475             }
476              
477             =head2 get_jndi
478              
479             return jndi view information
480             my $jndi = $jboss->get_jndi();
481              
482             =cut
483              
484             sub get_jndi {
485 0     0 1   my $self = shift;
486            
487 0           $self->_set_http_post(1);
488            
489 0           my $json = qq|{"operation":"jndi-view", "address":["subsystem","naming"], "json.pretty":1}|;
490            
491 0           $self->_set_post_json($json);
492            
493 0           $self->log->debug($self->post_json);
494 0           $self->get_api_response();
495             }
496              
497             =head2 set_log_level
498              
499             my $loglevel = $jboss->set_log_level('CONSOLE', 'ERROR');
500              
501             =cut
502              
503             sub set_log_level {
504 0     0 1   my $self = shift;
505            
506 0           my ($type, $severity) = @_;
507            
508 0           my $valid_type = 'CONSOLE';
509 0           my $valid_severity = 'ALL|DEBUG|INFO|WARN|WARNING|ERROR|SEVERE|FATAL|OFF';
510            
511 0 0         croak "type of console is required"
512             unless $type;
513            
514 0 0         croak "valid type is $valid_type"
515             unless $type =~ /^\b($valid_type)\b/;
516            
517 0 0         croak "valid log severity is $valid_severity"
518             unless $severity =~ /^\b($valid_severity)\b/;
519            
520 0 0         croak "severity level is required"
521             unless $severity;
522            
523 0           $self->_set_http_post(1);
524            
525 0           my $json = qq|{"operation":"write-attribute","address":[{"subsystem":"logging"},{"console-handler":"$type"}],"name":"level","value":"$severity", "json.pretty":1}|;
526            
527 0           $self->_set_post_json($json);
528            
529 0           $self->log->debug($self->post_json);
530 0           $self->get_api_response();
531             }
532              
533             =head2 get_log_level
534              
535             my $loglevel = $jboss->get_log_level('CONSOLE');
536              
537             =cut
538              
539             sub get_log_level {
540 0     0 1   my $self = shift;
541            
542 0           my $type = shift;
543            
544 0           my $valid_type = 'CONSOLE';
545            
546 0 0         croak "type of console is required"
547             unless $type;
548            
549 0 0         croak "valid type is $valid_type"
550             unless $type =~ /^\b($valid_type)\b/;
551            
552 0           $self->_set_http_post(1);
553            
554 0           my $json = qq|{"operation":"read-attribute","address":[{"subsystem":"logging"},{"console-handler":"$type"}],"name":"level", "json.pretty":1}|;
555            
556 0           $self->_set_post_json($json);
557            
558 0           $self->log->debug($self->post_json);
559 0           $self->get_api_response();
560             }
561              
562             =head1 AUTHOR
563              
564             "Heince Kurniawan", C<< <"heince at cpan.org"> >>
565              
566             =head1 BUGS
567              
568             Please report any bugs or feature requests to "heince at cpan.org", or through
569             the web interface at L. I will be notified, and then you'll
570             automatically be notified of progress on your bug as I make changes.
571              
572              
573             =head1 SUPPORT
574              
575             You can find documentation for this module with the perldoc command.
576              
577             perldoc Net::JBoss
578             perldoc Net::JBoss::Management
579              
580             You can also look for information at:
581              
582             =over 4
583              
584             =item * RT: CPAN's request tracker (report bugs here)
585              
586             L
587              
588             =item * AnnoCPAN: Annotated CPAN documentation
589              
590             L
591              
592             =item * CPAN Ratings
593              
594             L
595              
596             =item * Search CPAN
597              
598             L
599              
600             =back
601              
602              
603             =head1 ACKNOWLEDGEMENTS
604              
605              
606             =head1 LICENSE AND COPYRIGHT
607              
608             Copyright 2015 "Heince Kurniawan".
609              
610             This program is free software; you can redistribute it and/or modify it
611             under the terms of the the Artistic License (2.0). You may obtain a
612             copy of the full license at:
613              
614             L
615              
616             Any use, modification, and distribution of the Standard or Modified
617             Versions is governed by this Artistic License. By using, modifying or
618             distributing the Package, you accept this license. Do not use, modify,
619             or distribute the Package, if you do not accept this license.
620              
621             If your Modified Version has been derived from a Modified Version made
622             by someone other than you, you are nevertheless required to ensure that
623             your Modified Version complies with the requirements of this license.
624              
625             This license does not grant you the right to use any trademark, service
626             mark, tradename, or logo of the Copyright Holder.
627              
628             This license includes the non-exclusive, worldwide, free-of-charge
629             patent license to make, have made, use, offer to sell, sell, import and
630             otherwise transfer the Package with respect to any patent claims
631             licensable by the Copyright Holder that are necessarily infringed by the
632             Package. If you institute patent litigation (including a cross-claim or
633             counterclaim) against any party alleging that the Package constitutes
634             direct or contributory patent infringement, then this Artistic License
635             to you shall terminate on the date that such litigation is filed.
636              
637             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
638             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
639             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
640             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
641             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
642             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
643             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
644             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
645              
646              
647             =cut
648              
649             1; # End of JBoss