File Coverage

blib/lib/Net/JBoss.pm
Criterion Covered Total %
statement 21 65 32.3
branch 0 14 0.0
condition n/a
subroutine 7 12 58.3
pod 5 5 100.0
total 33 96 34.3


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