Jmx4Perl/J4psh/CommandHandler.pm untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 222 CC 15 SCAR 26.8
CC
15
Cov
0%
find_commands
30.0
_prompt
30.0
_register_commands
20.0
CRAP 14.5
1 #!/usr/bin/perl
2  
3 package JMX::Jmx4Perl::J4psh::CommandHandler;
4  
5 use strict;
6 use Data::Dumper;
7 use Term::ANSIColor qw(:constants);
8 use Module::Find;
9  
10 =head1 NAME
11  
12 JMX::Jmx4Perl::J4psh::CommandHandler - Handler for j4psh commands
13  
14 =head1 DESCRIPTION
15  
16 This object is responsible for managing L<JMX::Jmx4Perl::Command> objects which
17 are at the heart of j4psh and provide all features. During startup it
18 registeres commands dynamically and pushes the L<JMX::Jmx4Perl::Shell> context to them
19 for allowing to access the agent and other handlers.
20  
21 Registration is occurs in two phases:
22  
23 ...
24  
25 It also keeps a stack of so called navigational I<context> which can be used to
26 provide a menu like structure (think of it like directories which can be
27 entered). If the stack contains elements, the navigational commands C<..> and
28 C</> are added to traverse the stack. C</> will always jump to the top of the
29 stack (the I<root directory>) whereas C<..> will pop up one level in the stack
30 (the I<parent directory>). Commands which want to manipulate the stack like
31 pushing themselves on the stack should use the methods L</push_on_stack> or
32 L</reset_stack> (for jumping to the top of the menu).
33  
34 =cut
35  
36 =head1 METHODS
37  
38 =over
39  
40 =item $command_handler = new JMX::Jmx4Perl::Shell::CommandHandler($context,$ui)
41  
42 Create a new command handler object. The arguments to be passed are the context
43 object (C<$context>) and the shell object (C<$shell>) in order to update the
44 shell's current command set.
45  
46 =cut
47  
48 sub new {
49 my $class = shift;
50 my $context = shift || "No context object given";
51 my $shell = shift || "No shell given";
52 my $extra = shift;
53 $extra = { $extra, @_ } unless ref($extra) eq "HASH";
54 my $self = {
55 context => $context,
56 shell => $shell,
57 %{$extra}
58 };
59 $self->{stack} = [];
60 bless $self,(ref($class) || $class);
61 $shell->term->prompt($self->_prompt);
62 $self->_register_commands;
63 return $self;
64 }
65  
66 =item $comand_handler->push_on_stack($context,$cmds)
67  
68 Update the stack with an entry of name C<$context> which provides the commands
69 C<$cmds>. C<$cmds> must be a hashref as known to L<Term::ShellUI>, whose
70 C<commands> method is used to update the shell. Additionally it updates the
71 shell's prompt to reflect the state of the stack.
72  
73 =cut
74  
75 sub push_on_stack {
76 my $self = shift;
77 # The new context
78 my $context = shift;
79 # Sub-commands within the context
80 my $sub_cmds = shift;
81 my $separator = shift || "/";
82 my $contexts = $self->{stack};
83 push @$contexts,{ name => $context, cmds => $sub_cmds, separator => $separator };
84 #print Dumper(\@contexts);
85  
86 my $shell = $self->{shell};
87 # Set sub-commands
88 $shell->commands
89 ({
90 %$sub_cmds,
91 %{$self->_global_commands},
92 %{$self->_navigation_commands},
93 }
94 );
95 }
96  
97 =item $command_handler->reset_stack
98  
99 Reset the stack and install the top and global commands as collected from the
100 registered L<OSGi::Osgish::Command>.
101  
102 =cut
103  
104 sub reset_stack {
105 my $self = shift;
106 my $shell = $self->{shell};
107 $shell->commands({ %{$self->_top_commands}, %{$self->_global_commands}});
108 $self->{stack} = [];
109 }
110  
111 =item $command = $command_handler->command($command_name)
112  
113 Get a registered command by name
114  
115 =cut
116  
117 sub command {
118 my $self = shift;
119 my $name = shift || die "No command name given";
120 return $self->{commands}->{$name};
121 }
122  
123 =back
124  
125 =cut
126  
127 # ============================================================================
128  
129 sub _top_commands {
130 my $self = shift;
131 my $top = $self->{top_commands};
132 my @ret = ();
133 for my $command (values %$top) {
134 push @ret, %{$command->top_commands};
135 }
136 return { @ret };
137 }
138  
139 sub _global_commands {
140 my $self = shift;
141 my $globals = $self->{global_commands};
142 my @ret = ();
143 for my $command (values %$globals) {
144 push @ret, %{$command->global_commands};
145 }
146 return { @ret };
147 }
148  
149  
150 sub _navigation_commands {
151 my $self = shift;
152 my $shell = $self->{shell};
153 my $contexts = $self->{stack};
154 if (@$contexts > 0) {
155 return
156 {".." => {
157 desc => "Go up one level",
158 proc =>
159 sub {
160 $self->pop_off_stack();
161 }
162 },
163 "/" => {
164 desc => "Go to the top level",
165 proc =>
166 sub {
167 $self->reset_stack();
168 }
169 }
170 };
171 } else {
172 return {};
173 }
174 }
175  
176 # Go up one in the hierarchy
177 sub pop_off_stack {
178 my $self = shift;
179 my $shell = $self->{shell};
180 my $stack = $self->{stack};
181 my $parent = pop @$stack;
182 if (@$stack > 0) {
183 $shell->commands
184 ({
185 %{$stack->[$#{$stack}]->{cmds}},
186 %{$self->_global_commands},
187 %{$self->_navigation_commands},
188 }
189 );
190 } else {
191 $shell->commands({
192 %{$self->_top_commands},
193 %{$self->_global_commands},
194 });
195 }
196 }
197  
198 sub _register_commands {
199 my $self = shift;
200 my $context = $self->{context};
201 my $modules = $self->find_commands();
202 my $commands = {};
203 my $top = {};
204 my $globals = {};
205 for my $module (@$modules) {
206 my $file = $module;
207 $file =~ s/::/\//g;
208 require $file . ".pm";
209 $module->import;
210 my $command = eval "$module->new(\$context)";
211 die "Cannot register $module: ",$@ if $@;
212 $commands->{$command->name} = $command;
213 my $top_cmd = $command->top_commands;
214 if ($top_cmd) {
215 $top->{$command->name} = $command;
216 }
217 my $global_cmd = $command->global_commands;
218 if ($global_cmd) {
219 $globals->{$command->name} = $command;
220 }
221 }
222 $self->{commands} = $commands;
223 $self->{top_commands} = $top;
224 $self->{global_commands} = $globals;
225 $self->reset_stack;
226 }
227  
228 sub find_commands {
229 my $self = shift;
230 my $command_pkgs = ref($self->{command_packages}) eq "ARRAY" ? $self->{command_packages} : [ $self->{command_packages} ];
231 my @modules = ();
232 for my $pkg (@{$command_pkgs}) {
233 for my $command (findsubmod $pkg) {
234 next unless $command;
235 push @modules,$command;
236 }
237 }
238 if ($self->{command_modules}) {
239 my $command_modules =
240 ref($self->{command_modules}) eq "ARRAY" ? $self->{command_modules} : [ $self->{command_modules} ];
241 for my $command (@$command_modules) {
242 push @modules,$command;
243 }
244 }
245 return \@modules;
246 }
247  
248 sub _prompt {
249 my $self = shift;
250 my $context = $self->{context};
251 my $shell = $self->{shell};
252 return sub {
253 my $term = shift;
254 my $stack = $self->{stack};
255 my $agent = $context->agent;
256 my ($c_host,$c_context,$c_empty,$reset) =
257 $self->{no_color_prompt} ? ("","","","") : $shell->color("host","prompt_context","prompt_empty",RESET,{escape => 1});
258 my $p = "[";
259 $p .= $agent ? $c_host . $context->server : $c_empty . $context->name;
260 $p .= $reset;
261 $p .= " " . $c_context if @$stack;
262 for my $i (0 .. $#{$stack}) {
263 $p .= $stack->[$i]->{name};
264 $p .= $i < $#{$stack} ? $stack->[$i]->{separator} : $reset;
265 }
266 $p .= "] : ";
267 return $p;
268 };
269 }
270  
271 =head1 LICENSE
272  
273 This file is part of jmx4perl.
274  
275 Jmx4perl is free software: you can redistribute it and/or modify
276 it under the terms of the GNU General Public License as published by
277 the Free Software Foundation, either version 2 of the License, or
278 (at your option) any later version.
279  
280 jmx4perl is distributed in the hope that it will be useful,
281 but WITHOUT ANY WARRANTY; without even the implied warranty of
282 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
283 GNU General Public License for more details.
284  
285 You should have received a copy of the GNU General Public License
286 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
287  
288 A commercial license is available as well. Please contact roland@cpan.org for
289 further details.
290  
291 =head1 PROFESSIONAL SERVICES
292  
293 Just in case you need professional support for this module (or Nagios or JMX in
294 general), you might want to have a look at
295 http://www.consol.com/opensource/nagios/. Contact roland.huss@consol.de for
296 further information (or use the contact form at http://www.consol.com/contact/)
297  
298 =head1 AUTHOR
299  
300 roland@cpan.org
301  
302 =cut
303  
304 1;
305  
306  
307