| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package JMX::Jmx4Perl::J4psh::CompletionHandler; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use File::Spec; | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | JMX::Jmx4Perl::J4psh::CompletionHandler - Custom completion routines for readline. | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | =head1 METHODS | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $class = shift; | ||
| 24 | my $context = shift || die "No context object given"; | ||
| 25 | my $self = { | ||
| 26 | context => $context | ||
| 27 | }; | ||
| 28 | bless $self,(ref($class) || $class); | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub files_extended { | ||
| 33 | my $self = shift; | ||
| 34 | return sub { | ||
| 35 | my $term = shift; | ||
| 36 | my $cmpl = shift; | ||
| 37 | my $filter = undef; | ||
| 38 | |||
| 39 | $term->suppress_completion_append_character(); | ||
| 40 | |||
| 41 | use File::Spec; | ||
| 42 | my @path = File::Spec->splitdir($cmpl->{str} || "."); | ||
| 43 | my $dir = File::Spec->catdir(@path[0..$#path-1]); | ||
| 44 | my $lookup_dir = $dir; | ||
| 45 | if ($dir =~ /^\~(.*)/) { | ||
| 46 | my $user = $1 || ""; | ||
| 47 | $lookup_dir = glob("~$user"); | ||
| 48 | } | ||
| 49 | my $file = $path[$#path]; | ||
| 50 | $file = '' unless $cmpl->{str}; | ||
| 51 | my $flen = length($file); | ||
| 52 | |||
| 53 | my @files = (); | ||
| 54 | $lookup_dir = length($lookup_dir) ? $lookup_dir : "."; | ||
| 55 | if (opendir(DIR, $lookup_dir)) { | ||
| 56 | if ($filter) { | ||
| 57 | @files = grep { substr($_,0,$flen) eq $file && $file =~ $filter } readdir DIR; | ||
| 58 | } else { | ||
| 59 | @files = grep { substr($_,0,$flen) eq $file } readdir DIR; | ||
| 60 | } | ||
| 61 | closedir DIR; | ||
| 62 | # eradicate dotfiles unless user's file begins with a dot | ||
| 63 | @files = grep { /^[^.]/ } @files unless $file =~ /^\./; | ||
| 64 | # reformat filenames to be exactly as user typed | ||
| 65 | my @ret = (); | ||
| 66 | for my $file (@files) { | ||
| 67 | $file .= "/" if -d $lookup_dir . "/" . $file; | ||
| 68 | $file = $dir eq '/' ? "/$file" : "$dir/$file" if length($dir); | ||
| 69 | push @ret,$file; | ||
| 70 | } | ||
| 71 | return \@ret; | ||
| 72 | } else { | ||
| 73 | $term->completemsg("Couldn't read dir: $!\n"); | ||
| 74 | return []; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | sub servers { | ||
| 80 | my $self = shift; | ||
| 81 | return sub { | ||
| 82 | my ($term,$cmpl) = @_; | ||
| 83 | my $context = $self->{context}; | ||
| 84 | my $server_list = $context->servers->list; | ||
| 85 | return [] unless @$server_list; | ||
| 86 | my $str = $cmpl->{str} || ""; | ||
| 87 | my $len = length($str); | ||
| 88 | return [ grep { substr($_,0,$len) eq $str } map { $_->{name} } @$server_list ]; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | # Complete on mbean names | ||
| 93 | sub mbeans { | ||
| 94 | my $self = shift; | ||
| 95 | my %args = @_; | ||
| 96 | my $attr; | ||
| 97 | return sub { | ||
| 98 | my ($term,$cmpl) = @_; | ||
| 99 | $term->suppress_completion_escape(); | ||
| 100 | my $all = $args{all}; | ||
| 101 | my $domain = $args{domain}; | ||
| 102 | #$term->{debug_complete}=5; | ||
| 103 | my $context = $self->{context}; | ||
| 104 | my $mbeans = $context->mbeans_by_domain; | ||
| 105 | my $str = $cmpl->{str} || ""; | ||
| 106 | my $len = length($str); | ||
| 107 | if ($domain) { | ||
| 108 | my $attrs = $mbeans->{$domain}; | ||
| 109 | return [] unless $attrs; | ||
| 110 | my @kv = map { $_->{string} } @$attrs; | ||
| 111 | return [ map { $_ } grep { substr($_,0,$len) eq $str } @kv ]; | ||
| 112 | } else { | ||
| 113 | ($domain,$attr) = split(/:/,$str,2); | ||
| 114 | if ($attr || $str =~ /:$/) { | ||
| 115 | # Complete on attributes | ||
| 116 | my $attrs = $mbeans->{$domain}; | ||
| 117 | return [] unless $attrs; | ||
| 118 | my @kv = map { $_->{string} } @$attrs; | ||
| 119 | if ($attr) { | ||
| 120 | return [ map { $domain . ":" . $_ } grep { substr($_,0,length($attr)) eq $attr } @kv ]; | ||
| 121 | } else { | ||
| 122 | return [ map { $domain . ":" . $_} @kv ]; | ||
| 123 | } | ||
| 124 | } else { | ||
| 125 | # Complete on domains | ||
| 126 | my $domains = $str ? [ grep { substr($_,0,$len) eq $str } keys %$mbeans ] : [ keys %$mbeans ]; | ||
| 127 | if ($all) { | ||
| 128 | $term->suppress_completion_append_character(); | ||
| 129 | } | ||
| 130 | return $domains; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | }; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub mbean_attributes { | ||
| 137 | return shift->_complete_attr_op(shift,"attr"); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub mbean_operations { | ||
| 141 | return shift->_complete_attr_op(shift,"op"); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub _complete_attr_op { | ||
| 145 | my $self = shift; | ||
| 146 | my $m_info = shift; | ||
| 147 | my $what = shift; | ||
| 148 | my $attr; | ||
| 149 | #print "> ",Dumper($m_info->{info}->{attr}); | ||
| 150 | return sub { | ||
| 151 | my ($term,$cmpl) = @_; | ||
| 152 | $term->suppress_completion_escape(); | ||
| 153 | my $attrs = $m_info->{info}->{$what}; | ||
| 154 | #$term->{debug_complete}=5; | ||
| 155 | my $context = $self->{context}; | ||
| 156 | my $str = $cmpl->{str} || ""; | ||
| 157 | my $len = length($str); | ||
| 158 | return [ grep { substr($_,0,$len) eq $str } keys %$attrs ]; | ||
| 159 | }; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | # Method for completing based on key=value for an | ||
| 164 | # arbitrary order of key, value pairs | ||
| 165 | sub _complete_props { | ||
| 166 | my $self = shift; | ||
| 167 | # List of MBeans for this domain | ||
| 168 | my $mbeans_ref = shift; | ||
| 169 | my @mbeans = ( @{$mbeans_ref} ); | ||
| 170 | my $input = shift; | ||
| 171 | my $context = $self->{context}; | ||
| 172 | # Get all already completed | ||
| 173 | my @parts = split /,/,$input; | ||
| 174 | my $last = pop @parts; | ||
| 175 | # Filter out already set types | ||
| 176 | for my $p (@parts) { | ||
| 177 | my ($k,$v) = split /=/m,$p,2; | ||
| 178 | @mbeans = grep { $_->{props}->{$k} eq $v } @mbeans; | ||
| 179 | } | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | =head1 LICENSE | ||
| 184 | |||
| 185 | This file is part of jmx4perl. | ||
| 186 | |||
| 187 | Jmx4perl is free software: you can redistribute it and/or modify | ||
| 188 | it under the terms of the GNU General Public License as published by | ||
| 189 | the Free Software Foundation, either version 2 of the License, or | ||
| 190 | (at your option) any later version. | ||
| 191 | |||
| 192 | jmx4perl is distributed in the hope that it will be useful, | ||
| 193 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 194 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 195 | GNU General Public License for more details. | ||
| 196 | |||
| 197 | You should have received a copy of the GNU General Public License | ||
| 198 | along with jmx4perl. If not, see <http://www.gnu.org/licenses/>. | ||
| 199 | |||
| 200 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 201 | further details. | ||
| 202 | |||
| 203 | =head1 PROFESSIONAL SERVICES | ||
| 204 | |||
| 205 | Just in case you need professional support for this module (or Nagios or JMX in | ||
| 206 | general), you might want to have a look at | ||
| 207 | http://www.consol.com/opensource/nagios/. Contact roland.huss@consol.de for | ||
| 208 | further information (or use the contact form at http://www.consol.com/contact/) | ||
| 209 | |||
| 210 | =head1 AUTHOR | ||
| 211 | |||
| 212 | roland@cpan.org | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | |||
| 217 | 1; | ||
| 218 | |||
| 219 | |||
| 220 |