blib/lib/JMX/Jmx4Perl/Agent/Jolokia/DownloadAgent.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 105 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->{show_progress} = !$cfg{quiet};
55 return $self;
56 }
57  
58 # Overwriting progress in order to show a progressbar or not
59 sub progress {
60 my($self, $status, $m) = @_;
61 return unless $self->{show_progress};
62 # Use default progress bar if no progress is given
63 unless ($HAS_PROGRESS_BAR) {
64 $self->SUPER::progress($status,$m);
65 return;
66 }
67 if ($status eq "begin") {
68 $self->{progress_bar} = undef;
69 } elsif ($status eq "end") {
70 my $progress = delete $self->{progress_bar};
71 my $next = delete $self->{progress_next};
72 $progress->update(1) if defined($next) && 1 >= $next;
73 } elsif ($status eq "tick") {
74 # Unknown length (todo: probably better switch to the default behaviour
75 # in SUPER::progress())
76 my $progress = $self->_progress_bar($m->filename,undef);
77 $progress->update();
78 } else {
79 # Status contains percentage
80 my $progress = $self->_progress_bar($m->filename,1);
81
82 # print $status," ",$HAS_PROGRESS_BAR,"\n";
83 $self->{progress_next} = $progress->update($status)
84 if $status >= $self->{progress_next};
85 }
86 }
87  
88 sub _progress_bar {
89 my $self = shift;
90 my $name = shift;
91 my $count = shift;
92 my $progress = $self->{progress_bar};
93 unless ($progress) {
94 no strict;
95 local (%SIG);
96 $progress = new Term::ProgressBar({
97 name => " " . $name,
98 count => $count,
99 remove => 1,
100 ETA => linear,
101 !$HAS_TERM_READKEY ? (term_width => 120) : ()
102 }
103 );
104 #$progress->minor(1);
105 $progress->max_update_rate(1);
106 $self->{progress_bar} = $progress;
107 }
108 return $progress;
109  
110 }
111  
112  
113 # Get an optional proxy user
114 sub get_basic_credentials {
115 my ($self, $realm, $uri, $isproxy) = @_;
116
117 if ($isproxy && $self->{proxy_user}) {
118 return ($self->{proxy_user},$self->{proxy_password});
119 } else {
120 return (undef,undef);
121 }
122 }
123  
124 =head1 LICENSE
125  
126 This file is part of jmx4perl.
127 Jmx4perl is free software: you can redistribute it and/or modify
128 it under the terms of the GNU General Public License as published by
129 The Free Software Foundation, either version 2 of the License, or
130 (at your option) any later version.
131
132 jmx4perl is distributed in the hope that it will be useful,
133 but WITHOUT ANY WARRANTY; without even the implied warranty of
134 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135 GNU General Public License for more details.
136
137 You should have received a copy of the GNU General Public License
138 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
139  
140 A commercial license is available as well. Please contact roland@cpan.org for
141 further details.
142  
143 =head1 AUTHOR
144  
145 roland@cpan.org
146  
147 =cut
148  
149 1;