Osgish/CommandHandler.pm untested

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