| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package JMX::Jmx4Perl::J4psh::Command; | ||
| 4 | use strict; | ||
| 5 | use POSIX qw(strftime); | ||
| 6 | use Term::Clui; | ||
| 7 | |||
| 8 | use Getopt::Long; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | JMX::Jmx4Perl::J4psh::Command - Base object for commands | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This is the base command from which all j4psh commands should be extended. It | ||
| 17 | provides registration hooks so that the command handler can determine the | ||
| 18 | position of this command in the menu structure. Additionally it provides common | ||
| 19 | methods useful for each command to perform its action. | ||
| 20 | |||
| 21 | A L<JMX::Jmx4Perl::J4psh::Command> is a collection of shell commands, grouped in a | ||
| 22 | certain context. It can be reused in different contexts and hence can occur at | ||
| 23 | different places in the menu structure. | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | =head1 METHODS | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =item $command_handler = new JMX::Jmx4Perl::Command($context) | ||
| 32 | |||
| 33 | Constructor, which should not called be directly on this module but on a | ||
| 34 | submodule. In fact, it will be called (indirectly) only by the | ||
| 35 | L<JMX::Jmx4Perl::J4psh::CommandHandler> during the command registration process. | ||
| 36 | The single argument required is the central context object. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my $class = shift; | ||
| 42 | my $context = shift; | ||
| 43 | my $self = ref($_[0]) eq "HASH" ? $_[0] : { @_ }; | ||
| 44 | bless $self,(ref($class) || $class); | ||
| 45 | $self->{context} = $context; | ||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | =item $global_commands = $cmd->global_commands | ||
| 50 | |||
| 51 | This method is called by the command handler during registration in order to | ||
| 52 | obtain the global commands which are always present in the menu. The default | ||
| 53 | implementation returns C<undef> which means that no global commands should be | ||
| 54 | registered. Overwrite this to provide a command hashref as known to | ||
| 55 | L<Term::ShellUI> for setting the global commands. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub global_commands { | ||
| 60 | return undef; | ||
| 61 | } | ||
| 62 | |||
| 63 | =item $top_commands = $cmd->top_commands | ||
| 64 | |||
| 65 | This method is called by the command handler during registration in order to | ||
| 66 | obtain the top commands which are present in the top level menu. The default | ||
| 67 | implementation returns C<undef> which means that no top commands are to be | ||
| 68 | registered. Overwrite this to provide a command hashref as known to | ||
| 69 | L<Term::ShellUI> for setting the top commands. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub top_commands { | ||
| 74 | return undef; | ||
| 75 | } | ||
| 76 | |||
| 77 | =item $context = $cmd->context | ||
| 78 | |||
| 79 | Get the context object used during construction. This is a convenience method | ||
| 80 | for sublassed commands. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub context { | ||
| 85 | return shift->{context}; | ||
| 86 | } | ||
| 87 | |||
| 88 | =item $complete_handler = $cmd->complete | ||
| 89 | |||
| 90 | Convenience method to get the L<JMX::Jmx4perl::J4psh::CompletionHandler> for getting | ||
| 91 | various command line completions. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub complete { | ||
| 96 | return shift->{context}->complete; | ||
| 97 | } | ||
| 98 | |||
| 99 | =item $agent = $cmd->agent | ||
| 100 | |||
| 101 | Convenience method to get the L<JMX::Jmx4Perl> agent in order to | ||
| 102 | contact the server agent bundle (via L<JMX::Jmx4Perl>) | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub agent { | ||
| 107 | return shift->{context}->agent; | ||
| 108 | } | ||
| 109 | |||
| 110 | =item @colors = $cmd->color(@color_ids) | ||
| 111 | |||
| 112 | Return a list of ANSI color strings for the given symbolic color names which | ||
| 113 | are looked up from the current color theme. If no coloring is enabled, empty | ||
| 114 | strings are returned. This method dispatched directly to the underylying | ||
| 115 | C<context> object. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub color { | ||
| 120 | return shift->{context}->color(@_); | ||
| 121 | } | ||
| 122 | |||
| 123 | =item $cmd->push_on_stack("context",$cmds) | ||
| 124 | |||
| 125 | Rerturn a sub (closure) which can be used as a command to update the context | ||
| 126 | stack managed by the command handler. Update in this sense means push the given | ||
| 127 | context ("C<context>") on the stack, remembering the provided shell commands | ||
| 128 | C<$cmds> for later use when traversing the stack upwards via C<..> | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub push_on_stack { | ||
| 133 | my $self = shift; | ||
| 134 | my @args = @_; | ||
| 135 | return sub { | ||
| 136 | $self->{context}->{commands}->push_on_stack(@args); | ||
| 137 | }; | ||
| 138 | } | ||
| 139 | |||
| 140 | =item $cmd->pop_off_stack | ||
| 141 | |||
| 142 | Go up one level in the stack | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub pop_off_stack { | ||
| 147 | my $self = shift; | ||
| 148 | $self->{context}->{commands}->pop_off_stack(); | ||
| 149 | } | ||
| 150 | |||
| 151 | =item $cmd->reset_stack | ||
| 152 | |||
| 153 | Reset the stack completely effectively jumping on top of it | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub reset_stack { | ||
| 158 | my $self = shift; | ||
| 159 | $self->{context}->{commands}->reset_stack(); | ||
| 160 | } | ||
| 161 | |||
| 162 | =item ($opts,@args) = $cmd->extract_command_options($spec,@args); | ||
| 163 | |||
| 164 | |||
| 165 | Extract any options from a command specified via C<$spec>. This method uses | ||
| 166 | L<Getopt::Long> for extrating the options. It returns a hashref with the | ||
| 167 | extracted options and an array of remaining arguments | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub extract_command_options { | ||
| 172 | my ($self,$spec,@args) = @_; | ||
| 173 | my $opts = {}; | ||
| 174 | { | ||
| 175 | local @ARGV = @args; | ||
| 176 | GetOptions($opts,@{$spec}); | ||
| 177 | @args = @ARGV; | ||
| 178 | } | ||
| 179 | return ($opts,@args); | ||
| 180 | } | ||
| 181 | |||
| 182 | =item $label = $cmd->format_date($time) | ||
| 183 | |||
| 184 | Formats a date like for C<ls -l>: | ||
| 185 | |||
| 186 | Dec 2 18:21 | ||
| 187 | Jun 23 2009 | ||
| 188 | |||
| 189 | This format is especially useful when used in listing. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub format_date { | ||
| 194 | my $self = shift; | ||
| 195 | my $time = shift; | ||
| 196 | if (time - $time > 60*60*24*365) { | ||
| 197 | return strftime "%b %d %Y",localtime($time); | ||
| 198 | } else { | ||
| 199 | return strftime "%b %d %H:%M",localtime($time); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | =item $cmd->print_paged($txt,$nr_lines) | ||
| 204 | |||
| 205 | Use a pager for printing C<$txt> which has C<$nr_lines> lines. Only if | ||
| 206 | C<$nr_lines> exceeds a certain limit (default: 24), then the pager is used, | ||
| 207 | otherwise C<$txt> is printed directly. | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub print_paged { | ||
| 212 | my $self = shift; | ||
| 213 | my $text = shift; | ||
| 214 | my $nr = shift; | ||
| 215 | if (!$nr) { | ||
| 216 | $nr = scalar(split /\n/s,$text); | ||
| 217 | } | ||
| 218 | my $max_rows = $self->context->term_height; | ||
| 219 | if (defined($nr) && $nr < $max_rows) { | ||
| 220 | print $text; | ||
| 221 | } else { | ||
| 222 | view("",$text); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | =item $trimmed = $cmd->trim_string($string,$max) | ||
| 227 | |||
| 228 | Trim a string C<$string> to a certain length C<$max>, i.e. if C<$string> is | ||
| 229 | larger than C<$max>, then it is truncated to to C<$max-3> and C<...> is | ||
| 230 | appended. If it is less or equal, than C<$string> is returned unchanged. | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub trim_string { | ||
| 235 | my $self = shift; | ||
| 236 | my $string = shift; | ||
| 237 | my $max = shift; | ||
| 238 | return length($string) > $max ? substr($string,0,$max-3) . "..." : $string; | ||
| 239 | } | ||
| 240 | |||
| 241 | =item $converted = $cmd->convert_wildcard_pattern_to_regexp($orig) | ||
| 242 | |||
| 243 | Convert the wildcards C<*> and C<.> to their regexp equivalent and return a | ||
| 244 | regular expression. | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub convert_wildcard_pattern_to_regexp { | ||
| 249 | my $self = shift; | ||
| 250 | my $wildcard = shift; | ||
| 251 | $wildcard =~ s/\?/./g; | ||
| 252 | $wildcard =~ s/\*/.*/g; | ||
| 253 | return qr/^$wildcard$/i; | ||
| 254 | } | ||
| 255 | |||
| 256 | =back | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | =head1 LICENSE | ||
| 261 | |||
| 262 | This file is part of jmx4perl. | ||
| 263 | |||
| 264 | Jmx4perl is free software: you can redistribute it and/or modify | ||
| 265 | it under the terms of the GNU General Public License as published by | ||
| 266 | the Free Software Foundation, either version 2 of the License, or | ||
| 267 | (at your option) any later version. | ||
| 268 | |||
| 269 | jmx4perl is distributed in the hope that it will be useful, | ||
| 270 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 271 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 272 | GNU General Public License for more details. | ||
| 273 | |||
| 274 | You should have received a copy of the GNU General Public License | ||
| 275 | along with jmx4perl. If not, see <http://www.gnu.org/licenses/>. | ||
| 276 | |||
| 277 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 278 | further details. | ||
| 279 | |||
| 280 | =head1 PROFESSIONAL SERVICES | ||
| 281 | |||
| 282 | Just in case you need professional support for this module (or Nagios or JMX in | ||
| 283 | general), you might want to have a look at | ||
| 284 | http://www.consol.com/opensource/nagios/. Contact roland.huss@consol.de for | ||
| 285 | further information (or use the contact form at http://www.consol.com/contact/) | ||
| 286 | |||
| 287 | =head1 AUTHOR | ||
| 288 | |||
| 289 | roland@cpan.org | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | 1; | ||
| 294 |