File Coverage

blib/lib/Net/JBoss.pm
Criterion Covered Total %
statement 21 67 31.3
branch 0 16 0.0
condition n/a
subroutine 7 12 58.3
pod 5 5 100.0
total 33 100 33.0


line stmt bran cond sub pod time code
1             package Net::JBoss;
2              
3 1     1   781 use 5.010;
  1         4  
  1         50  
4 1     1   586 use HTTP::Request;
  1         25792  
  1         36  
5 1     1   2491 use LWP::UserAgent;
  1         33041  
  1         42  
6 1     1   12 use Scalar::Util qw(looks_like_number);
  1         2  
  1         112  
7 1     1   5 use Carp;
  1         2  
  1         47  
8 1     1   809 use Moo::Role;
  1         19667  
  1         7  
9              
10             =head1 NAME
11              
12             Net::JBoss - Bindings for JBoss Management API
13              
14             =head1 VERSION
15              
16             Version 0.03
17              
18             =cut
19              
20             our $VERSION = '0.03';
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 => 9443, #optional, default is 9990
32             ssl => 'on', #optional, default is 'off'
33             ssl_verify => 'yes', #optional, default is 'no'
34             realm => 'ManagementRealmHTTPS' #optional, default is 'ManagementRealm'
35             );
36              
37             my $jboss = Net::JBoss::Management->new(%con);
38            
39             my $state = $jboss->get_state();
40             my $jvm_usage = $jboss->get_jvm_usage();
41             my $runtime_stats = $jboss->get_runtime_stats();
42             my $deploy_info = $jboss->get_deployment_info();
43             my $app_runtime_stats = $jboss->get_app_runtime_stats('hawtio.war');
44             my $runtime_details = $jboss->get_runtime_details();
45             my $app_status = $jboss->get_app_status('hawtio.war');
46             my $active_session = $jboss->get_active_sessions('hawtio.war');
47             my $server_env = $jboss->get_server_env();
48             my $datasources = $jboss->get_datasources();
49             my $test = $jboss->test_con_pool('ExampleDS');
50             my $pool_stats = $jboss->get_ds_pool_stats('java:jboss/datasources/jboss_Pool');
51             my $enable_pool_stats = $jboss->get_ds_pool_stats('java:jboss/datasources/jboss_Pool', 'true');
52             my $disable_pool_stats = $jboss->get_ds_pool_stats('java:jboss/datasources/jboss_Pool', 'false');
53             my $min_pool_size = $jboss->set_ds_pool_size('min', 'java:jboss/datasources/jboss_Pool', 20);
54             my $max_pool_size = $jboss->set_ds_pool_size('max', 'java:jboss/datasources/jboss_Pool', 50);
55             my $jndi = $jboss->get_jndi();
56             my $loglevel = $jboss->get_log_level('CONSOLE');
57             my $loglevel = $jboss->set_log_level('CONSOLE', 'ERROR');
58             my $reload = $jboss->reload();
59             my $shutdown = $jboss->shutdown();
60             my $restart = $jboss->restart();
61              
62             =head1 Attributes
63              
64             notes :
65             ro = read only, can be specified during initialization
66             rw = read write, user can set this attribute
67             rwp = read write protected, for internal class
68              
69             username = (ro, required) store management user username
70             password = (ro, required) store management user password
71             server = (ro, required) store managemenet address, ip address / hostname only
72             port = (ro) store Ovirt Manager's port (must be number)
73             log_severity = (ro) store log severity level, valid value ERROR|OFF|FATAL|INFO|DEBUG|TRACE|ALL|WARN
74             (default is INFO)
75             realm = (ro) store realm, default to 'ManagementRealm'
76             resource_url = (rwp) store resource url for each method
77             url = (rwp) store final url to be requested
78             log = (rwp) store log from log4perl
79             http_post = (rwp) if true, use http post method instead of get
80             post_json = (rwp) set json content to be post
81             ssl = (ro) if 'on', use https (default is 'off')
82             ssl_verify = (ro) disable host verification, yes/no (default is 'no')
83              
84             =cut
85              
86             has [qw/url log json_data resource_url http_post post_json /] => ( is => 'rwp' );
87             has [qw/username password/] => ( is => 'ro', required => 1 );
88             has [qw/server/] => ( is => 'ro',
89             isa => sub {
90             croak "server can't contain http protocol, use ip / hostname only"
91             if $_[0] =~ /http/i;
92             },
93             required => 1 )
94             ;
95              
96             has 'port' => ( is => 'ro', default => 9990,
97             isa =>
98             sub {
99             croak "$_[0] is not a number!" unless looks_like_number $_[0];
100             }
101             );
102              
103             has 'log_severity' => ( is => 'ro',
104             isa => sub { croak "log severity value not valid\n"
105             unless $_[0] =~ /\b(ERROR|OFF|FATAL|INFO|DEBUG|TRACE|ALL|WARN)\b/;
106             },
107             default => 'INFO'
108             );
109              
110             has 'realm' => ( is => 'ro', default => 'ManagementRealm' );
111             has 'ssl' => ( is => 'ro', default => 'off',
112             isa =>
113             sub {
114             croak "valid ssl value is on or off"
115             unless $_[0] =~ /\b(on|off)\b/;
116             }
117             );
118             has 'ssl_verify' => ( is => 'ro',
119             isa => sub {
120             my $ssl_verify = $_[0];
121             $ssl_verify = lc ($ssl_verify);
122            
123             if ($ssl_verify eq 'yes') {
124             $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 1;
125             }
126             elsif ($ssl_verify eq 'no') {
127             $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
128             }
129             else {
130             croak "ssl_verify valid argument is yes/no";
131             }
132             },
133             default => sub { $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; return 'no'; } );
134              
135             =head1 SUBROUTINES/METHODS
136              
137             You may want to check :
138             - perldoc Net::JBoss::Management
139              
140             =head2 BUILD
141              
142             The Constructor, build logging, call pass_log_obj method
143             =cut
144              
145             sub BUILD {
146 0     0 1   my $self = shift;
147            
148 0           $self->pass_log_obj();
149             }
150              
151             =head2 pass_log_obj
152              
153             it will build the log which stored to $self->log
154             you can assign the severity level by assigning the log_severity
155            
156             # output to console / screen
157             # format :
158             # %d = current date with yyyy/MM/dd hh:mm:ss format
159             # %p = Log Severity
160             # %P = pid of the current process
161             # %L = Line number within the file where the log statement was issued
162             # %M = Method or function where the logging request was issued
163             # %m = The message to be logged
164             # %n = Newline (OS-independent)
165            
166             =cut
167              
168             sub pass_log_obj {
169 0     0 1   my $self = shift;
170            
171             # skip if already set
172 0 0         return if $self->log;
173            
174 0           my $severity = $self->log_severity;
175 0           my $log_conf =
176             qq /
177             log4perl.logger = $severity, Screen
178             log4perl.appender.Screen = Log::Log4perl::Appender::Screen
179             log4perl.appender.Screen.stderr = 0
180             log4perl.appender.Screen.layout = PatternLayout
181             log4perl.appender.Screen.layout.ConversionPattern = %d || %p || %P || %L || %M || %m%n
182             /;
183            
184 1     1   2032 use Log::Log4perl;
  1         46574  
  1         10  
185 0           Log::Log4perl::init(\$log_conf);
186 0           my $log = Log::Log4perl->get_logger();
187 0           $self->_set_log($log);
188             }
189              
190             =head2 base_url
191              
192             return the base url
193             =cut
194              
195             sub base_url {
196 0     0 1   my $self = shift;
197            
198 0           my $url = $self->server;
199            
200 0 0         if ($self->port) {
201 0           $url = $self->server . ":" . $self->port;
202             }
203            
204             # http or https
205 0 0         if ($self->ssl eq 'off') {
206 0           $url = "http://" . $url;
207             }
208             else {
209 0           $url = "https://" . $url;
210             }
211            
212 0           $self->log->debug($url);
213 0           return $url;
214             }
215              
216             =head2 get_api_response
217              
218             return http api response
219             =cut
220              
221             sub get_api_response {
222 0     0 1   my $self = shift;
223            
224 0 0         croak "url required" unless $self->url;
225            
226 0           $self->log->debug("username = " . $self->username);
227 0           $self->log->debug("password = " . $self->password);
228 0           $self->log->debug("port = " . $self->port);
229 0           $self->log->debug("realm = " . $self->realm);
230            
231 0           my $ua = LWP::UserAgent->new();
232 0           $ua->credentials( $self->server . ":" . $self->port,
233             $self->realm ,
234             $self->username ,
235             $self->password ,
236             );
237            
238             # write operation require post method
239 0           my $res;
240 0 0         if ($self->http_post) {
241 0 0         croak "post_json not set"
242             unless $self->post_json;
243            
244 0           my $req = HTTP::Request->new(POST => $self->url);
245 0           $req->content_type ('application/json');
246 0           $req->content ($self->post_json);
247            
248 0           $res = $ua->request ($req);
249             }
250             else {
251 0 0         croak "resource url required" unless $self->resource_url;
252            
253             # set final url
254 0           $self->_set_url($self->url . $self->resource_url);
255            
256 0           $self->log->debug($self->url);
257 0           $res = $ua->get($self->url);
258             }
259              
260 0 0         if ($res->is_success) {
261 0           $self->log->debug($res->decoded_content);
262 0           return $res->decoded_content;
263             }
264             else {
265 0           my $err = $res->status_line;
266 0           $self->log->debug("LWP Error : " . $err);
267 0           return $res->decoded_content;
268             }
269             }
270              
271             =head2 trim
272              
273             trim function to remove whitespace from the start and end of the string
274             =cut
275              
276             sub trim()
277             {
278 0     0 1   my ($self, $string) = @_;
279 0           $string =~ s/^\s+|\s+$//g;
280 0           return $string;
281             }
282              
283             =head1 AUTHOR
284              
285             "Heince Kurniawan", C<< <"heince at cpan.org"> >>
286              
287             =head1 BUGS
288              
289             Please report any bugs or feature requests to "heince at cpan.org", or through
290             the web interface at L. I will be notified, and then you'll
291             automatically be notified of progress on your bug as I make changes.
292              
293              
294             =head1 SUPPORT
295              
296             You can find documentation for this module with the perldoc command.
297              
298             perldoc Net::JBoss
299              
300              
301             You can also look for information at:
302              
303             =over 4
304              
305             =item * RT: CPAN's request tracker (report bugs here)
306              
307             L
308              
309             =item * AnnoCPAN: Annotated CPAN documentation
310              
311             L
312              
313             =item * CPAN Ratings
314              
315             L
316              
317             =item * Search CPAN
318              
319             L
320              
321             =back
322              
323              
324             =head1 ACKNOWLEDGEMENTS
325              
326              
327             =head1 LICENSE AND COPYRIGHT
328              
329             Copyright 2015 "Heince Kurniawan".
330              
331             This program is free software; you can redistribute it and/or modify it
332             under the terms of the the Artistic License (2.0). You may obtain a
333             copy of the full license at:
334              
335             L
336              
337             Any use, modification, and distribution of the Standard or Modified
338             Versions is governed by this Artistic License. By using, modifying or
339             distributing the Package, you accept this license. Do not use, modify,
340             or distribute the Package, if you do not accept this license.
341              
342             If your Modified Version has been derived from a Modified Version made
343             by someone other than you, you are nevertheless required to ensure that
344             your Modified Version complies with the requirements of this license.
345              
346             This license does not grant you the right to use any trademark, service
347             mark, tradename, or logo of the Copyright Holder.
348              
349             This license includes the non-exclusive, worldwide, free-of-charge
350             patent license to make, have made, use, offer to sell, sell, import and
351             otherwise transfer the Package with respect to any patent claims
352             licensable by the Copyright Holder that are necessarily infringed by the
353             Package. If you institute patent litigation (including a cross-claim or
354             counterclaim) against any party alleging that the Package constitutes
355             direct or contributory patent infringement, then this Artistic License
356             to you shall terminate on the date that such litigation is filed.
357              
358             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
359             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
360             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
361             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
362             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
363             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
364             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
365             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
366              
367              
368             =cut
369              
370             1; # End of JBoss