Jmx4Perl/Agent/Jolokia/DownloadAgent.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 111 CC 14 SCAR 34.4
CC
14
Cov
0%
progress
72.0
new
20.0
_progress_bar
12.0
CRAP 31.1
1 #!/usr/bin/perl
2 package JMX::Jmx4Perl::Agent::Jolokia::DownloadAgent;
3 use base qw(LWP::UserAgent);
4 use Data::Dumper;
5 use vars qw($HAS_PROGRESS_BAR $HAS_TERM_READKEY);
6 use strict;
7  
8 BEGIN {
9 $HAS_PROGRESS_BAR = eval "require Term::ProgressBar; 1";
10 $HAS_TERM_READKEY = eval "require Term::ReadKey; 1";
11 }
12
13 =head1 NAME
14  
15 JMX::Jmx4Perl::Agent::Jolokia::DownloadAgent - Specialized L<LWP::UserAgent>
16 adding some bells and whistles for downloading agents and other stuff.
17  
18 =head1 DESCRIPTION
19  
20 User agent for Jolokia artifact downloading. It decorates a regular User Agent
21 with a download bar and allows for proxy handling and authentication. For a
22 progress bar, the optional module L<Term::ProgressBar> must be installed.
23  
24 =head1 METHODS
25  
26 =over 4
27  
28 =item $ua = JMX::Jmx4Perl::Agent::Jolokia::DownloadAgent->new(%args)
29  
30 Create a new user agent, a subclass fro L<LWP::UserAgent>
31  
32 Options:
33  
34 "http_proxy" HTTP Proxy to use
35 "https_proxy" HTTPS Proxy to use
36 "quiet" If true, dont show progressbar
37 "proxy_user" Proxy user for proxy authentication
38 "proxy_password" Proxy password for proxy authentication
39  
40 =back
41  
42 =cut
43  
44 sub new {
45 my $class = shift;
46 my %cfg = ref($_[0]) eq "HASH" ? %{$_[0]} : @_;
47 my $self = LWP::UserAgent::new($class,%cfg);
48 bless $self,(ref($class) || $class);
49  
50 # Proxy setting
51 $self->env_proxy;
52 $self->proxy("http",$cfg{http_proxy}) if $cfg{http_proxy};
53 $self->proxy("https",$cfg{https_proxy}) if $cfg{https_proxy};
54 $self->agent("Jolokia Download Agent/" . $JMX::Jmx4Perl::VERSION);
55 $self->{show_progress} = !$cfg{quiet};
56 return $self;
57 }
58  
59 # Overwriting progress in order to show a progressbar or not
60 sub progress {
61 my($self, $status, $m) = @_;
62 return unless $self->{show_progress};
63 # Use default progress bar if no progress is given
64 unless ($HAS_PROGRESS_BAR) {
65 $self->SUPER::progress($status,$m);
66 return;
67 }
68 if ($status eq "begin") {
69 $self->{progress_bar} = undef;
70 } elsif ($status eq "end") {
71 my $progress = delete $self->{progress_bar};
72 my $next = delete $self->{progress_next};
73 $progress->update(1) if defined($next) && 1 >= $next;
74 } elsif ($status eq "tick") {
75 # Unknown length (todo: probably better switch to the default behaviour
76 # in SUPER::progress())
77 my $progress = $self->_progress_bar($m->filename,undef);
78 $progress->update();
79 } else {
80 # Status contains percentage
81 my $progress = $self->_progress_bar($m->filename,1);
82
83 # print $status," ",$HAS_PROGRESS_BAR,"\n";
84 $self->{progress_next} = $progress->update($status)
85 if $status >= $self->{progress_next};
86 }
87 }
88  
89 sub _progress_bar {
90 my $self = shift;
91 my $name = shift;
92 my $count = shift;
93 my $progress = $self->{progress_bar};
94 unless ($progress) {
95 no strict;
96 local (%SIG);
97 $progress = new Term::ProgressBar({
98 name => " " . $name,
99 count => $count,
100 remove => 1,
101 ETA => linear,
102 !$HAS_TERM_READKEY ? (term_width => 120) : ()
103 }
104 );
105 #$progress->minor(1);
106 $progress->max_update_rate(1);
107 $self->{progress_bar} = $progress;
108 }
109 return $progress;
110  
111 }
112  
113  
114 # Get an optional proxy user
115 sub get_basic_credentials {
116 my ($self, $realm, $uri, $isproxy) = @_;
117
118 if ($isproxy && $self->{proxy_user}) {
119 return ($self->{proxy_user},$self->{proxy_password});
120 } else {
121 return (undef,undef);
122 }
123 }
124  
125 =head1 LICENSE
126  
127 This file is part of jmx4perl.
128 Jmx4perl is free software: you can redistribute it and/or modify
129 it under the terms of the GNU General Public License as published by
130 The Free Software Foundation, either version 2 of the License, or
131 (at your option) any later version.
132
133 jmx4perl is distributed in the hope that it will be useful,
134 but WITHOUT ANY WARRANTY; without even the implied warranty of
135 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136 GNU General Public License for more details.
137
138 You should have received a copy of the GNU General Public License
139 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
140  
141 A commercial license is available as well. Please contact roland@cpan.org for
142 further details.
143  
144 =head1 AUTHOR
145  
146 roland@cpan.org
147  
148 =cut
149  
150 1;