1 |
joko |
1.1 |
## ---------------------------------------------------------------------- |
2 |
joko |
1.9 |
## $Id: Command.pm,v 1.8 2004/06/07 16:45:56 joko Exp $ |
3 |
joko |
1.1 |
## ---------------------------------------------------------------------- |
4 |
joko |
1.2 |
## $Log: Command.pm,v $ |
5 |
joko |
1.9 |
## Revision 1.8 2004/06/07 16:45:56 joko |
6 |
|
|
## now propagates args to "rapcall method" |
7 |
|
|
## |
8 |
joko |
1.8 |
## Revision 1.7 2003/05/13 07:52:14 joko |
9 |
|
|
## enhanced: *hierarchical* containers for context handling |
10 |
|
|
## making methods from foreign context(s) available |
11 |
|
|
## started: import of targets from foreign topics |
12 |
|
|
## |
13 |
joko |
1.7 |
## Revision 1.6 2003/03/29 07:10:42 joko |
14 |
|
|
## + sub _script: |
15 |
|
|
## new rap-command: '<script language="msdos/bat|bash|...">...</script>' |
16 |
|
|
## |
17 |
joko |
1.6 |
## Revision 1.5 2003/03/27 15:31:04 joko |
18 |
|
|
## fixes to modules regarding new namespace(s) below Data::Mungle::* |
19 |
|
|
## |
20 |
joko |
1.5 |
## Revision 1.4 2003/02/22 16:48:58 joko |
21 |
|
|
## modified rapcall behaviour |
22 |
|
|
## |
23 |
joko |
1.4 |
## Revision 1.3 2003/02/21 07:39:13 joko |
24 |
|
|
## modified 'rapcall' processing |
25 |
|
|
## modified merging of options/arguments in there |
26 |
|
|
## |
27 |
joko |
1.3 |
## Revision 1.2 2003/02/20 19:37:09 joko |
28 |
|
|
## renamed modules |
29 |
|
|
## - removed command 'exec' |
30 |
|
|
## + enhanced command 'rapcall' - now handles various different modes and behaviours |
31 |
|
|
## renamed methods |
32 |
|
|
## + command 'exit' |
33 |
|
|
## + command 'use' - switch to foreign namespaces using contexts |
34 |
|
|
## |
35 |
joko |
1.2 |
## Revision 1.1 2003/02/18 15:35:06 joko |
36 |
|
|
## + initial commit |
37 |
|
|
## |
38 |
joko |
1.1 |
## ---------------------------------------------------------------------- |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
package Data::Rap::Command; |
42 |
|
|
|
43 |
|
|
use strict; |
44 |
|
|
use warnings; |
45 |
|
|
|
46 |
|
|
|
47 |
|
|
use Data::Dumper; |
48 |
|
|
use Hash::Merge qw( merge ); |
49 |
joko |
1.2 |
|
50 |
|
|
use DesignPattern::Object; |
51 |
joko |
1.5 |
use Data::Mungle::Transform::Deep qw( merge_to ); |
52 |
joko |
1.1 |
use shortcuts qw( run_cmd ); |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
sub _echo { |
56 |
|
|
my $self = shift; |
57 |
|
|
my $args = shift; |
58 |
|
|
#print Dumper($args); |
59 |
|
|
my $message; |
60 |
|
|
my $level; |
61 |
|
|
|
62 |
|
|
if (ref $args eq 'HASH') { |
63 |
|
|
$message = $args->{message}; |
64 |
|
|
$level = $args->{level}; |
65 |
|
|
} else { |
66 |
|
|
my @buf = (); |
67 |
|
|
push @buf, $args; |
68 |
|
|
push @buf, @_; |
69 |
|
|
$message = join(' ', @buf); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
$message ||= 'n/a'; |
73 |
|
|
$level ||= 'info'; |
74 |
|
|
|
75 |
|
|
$self->log($message, $level); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
sub _context { |
79 |
|
|
my $self = shift; |
80 |
|
|
my $args = shift; |
81 |
joko |
1.7 |
print "_context-args: ", Dumper($args); |
82 |
joko |
1.1 |
foreach my $task_command (keys %$args) { |
83 |
|
|
my $bunch = $args->{$task_command}; |
84 |
|
|
foreach my $task_args (@$bunch) { |
85 |
|
|
#print Dumper($task_args); |
86 |
joko |
1.2 |
$self->perform_command($task_command, $task_args); |
87 |
joko |
1.1 |
} |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
# FIXME |
92 |
|
|
sub _id {} |
93 |
|
|
|
94 |
|
|
# FIXME |
95 |
|
|
sub _description {} |
96 |
|
|
|
97 |
joko |
1.7 |
# NEW [2003-05-08]: Hierarchical handling (stack push/pop instead of set/get). |
98 |
|
|
# TODO: Named Containers. |
99 |
joko |
1.2 |
sub _container { |
100 |
joko |
1.1 |
my $self = shift; |
101 |
joko |
1.2 |
my $args = shift; |
102 |
joko |
1.7 |
$self->pushContainer($args); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
sub _container_end { |
106 |
|
|
my $self = shift; |
107 |
|
|
my $args = shift; |
108 |
|
|
$self->popContainer(); |
109 |
joko |
1.1 |
} |
110 |
|
|
|
111 |
joko |
1.2 |
sub _sync { |
112 |
joko |
1.1 |
my $self = shift; |
113 |
joko |
1.2 |
my $args = shift; |
114 |
|
|
|
115 |
|
|
#print Dumper($args); |
116 |
|
|
|
117 |
|
|
# V1 |
118 |
|
|
#$bizProcess->runSync($task); |
119 |
|
|
|
120 |
|
|
#V2 |
121 |
|
|
my $container = $self->getInstance(); |
122 |
|
|
$container ||= $self; |
123 |
|
|
$container->runSync($args); |
124 |
joko |
1.1 |
|
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
sub _depends_old { |
128 |
|
|
my $self = shift; |
129 |
|
|
my $args = shift; |
130 |
|
|
#print Dumper($args); |
131 |
|
|
$self->performTarget($args); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
sub _dump { |
135 |
|
|
my $self = shift; |
136 |
|
|
my $args = shift; |
137 |
|
|
my $name = $args->{name}; |
138 |
joko |
1.2 |
my $message = Dumper($self->get_property($name)); |
139 |
|
|
$self->rc('echo', "\n" . $message, $args->{level}); |
140 |
joko |
1.1 |
} |
141 |
|
|
|
142 |
joko |
1.2 |
|
143 |
joko |
1.1 |
sub _plugin { |
144 |
|
|
my $self = shift; |
145 |
|
|
my $args = shift; |
146 |
joko |
1.7 |
my $content = shift; |
147 |
joko |
1.2 |
|
148 |
|
|
if (my $instance = $args->{instance}) { |
149 |
|
|
#$self->{$instance} = DesignPattern::Object->fromPackage($args->{module}); |
150 |
|
|
my $object = DesignPattern::Object->fromPackage($args->{module}); |
151 |
|
|
#$self->{$instance} |
152 |
|
|
#print Dumper($object); |
153 |
|
|
#$object->constructor() if $object->can('constructor'); |
154 |
|
|
|
155 |
|
|
} elsif (my $name = $args->{name}) { |
156 |
|
|
|
157 |
|
|
# 1. namespaces - load module into current scope or foreign namespace? |
158 |
|
|
my $container; |
159 |
|
|
if (my $namespace = $args->{namespace}) { |
160 |
|
|
|
161 |
|
|
$self->log("Loading plugin '$name' into foreign namespace '$namespace'.", 'info'); |
162 |
|
|
|
163 |
|
|
eval qq{ |
164 |
|
|
|
165 |
|
|
# 0. pre-flight check|trace |
166 |
|
|
#print "YAI"; |
167 |
|
|
#$self->log('YAIYAI', 'info'); # FIXME! |
168 |
|
|
|
169 |
|
|
# 1. work in foreign namespace |
170 |
|
|
package $namespace; |
171 |
|
|
use strict; |
172 |
|
|
use warnings; |
173 |
|
|
# 2. mungle with/the |
174 |
|
|
# - package namespace (llp) |
175 |
|
|
# - object (lloo) |
176 |
|
|
# - inheritance (lloo) |
177 |
|
|
# - plugin (hlf) |
178 |
|
|
# symbols for scopes: |
179 |
|
|
# - llp = low-level perl |
180 |
|
|
# - lloo = low-level oo |
181 |
|
|
# - hlf = high-level framework |
182 |
|
|
# bless larry for perl having this implemented in an orthogonal way |
183 |
|
|
# 2.1: behaviour=coerce |
184 |
|
|
#use base qw( $name ); |
185 |
|
|
# 2.2: behaviour=plugin |
186 |
|
|
use base qw( DesignPattern::Bridge ); |
187 |
|
|
1; |
188 |
|
|
}; |
189 |
|
|
$self->checkExceptions(); |
190 |
|
|
$container = $namespace->new(); |
191 |
|
|
|
192 |
|
|
} elsif (my $instance = $self->getInstance()) { |
193 |
|
|
$self->log("Loading plugin '$name' into foreign namespace of instance '" . (ref $instance) . "'.", 'info'); |
194 |
|
|
$container = $instance; |
195 |
|
|
|
196 |
|
|
} else { |
197 |
|
|
#$self->log("Loading plugin '$name' into local scope's namespace (ref \$self='" . (ref $self) . "').", 'info'); |
198 |
|
|
$self->log("Loading plugin '$name' into local namespace '" . (ref $self) . "'.", 'info'); |
199 |
|
|
$container = $self; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
# 1.b. intermediate checks |
203 |
|
|
if (!$container) { |
204 |
|
|
$self->log("No container to load '$name' into!", 'warning'); |
205 |
|
|
return; |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
# 1.c. inheritance of (e.g.) properties |
209 |
|
|
if (my $inherit = $args->{inherit}) { |
210 |
|
|
my $properties = $self->get_property($inherit); |
211 |
|
|
merge_to($container, { $inherit => $properties }, { init => 1 } ); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
# 2. plugin - load module into container |
215 |
|
|
$container->load($name); |
216 |
|
|
|
217 |
|
|
# 3. methods - run object methods |
218 |
joko |
1.7 |
|
219 |
|
|
# payload inside $content is the $args to the $method |
220 |
|
|
# convert xml-style payload inside $content to trivial (not-nested) perl hash |
221 |
|
|
# using just encapsulated text of the nodes as hash values |
222 |
|
|
my $method_args; |
223 |
|
|
foreach (@$content) { |
224 |
|
|
my $key = $_->{name}; |
225 |
|
|
my $value = $_->{content}->[0]->{content}; |
226 |
|
|
$method_args->{$key} = $value; |
227 |
|
|
} |
228 |
|
|
# convert hashref to arrayref |
229 |
|
|
$method_args = [ %$method_args ] if ref $method_args eq 'HASH'; |
230 |
|
|
|
231 |
joko |
1.2 |
if (my $methods = $args->{methods}) { |
232 |
|
|
my @methods = split(/,|,\s/, $methods); |
233 |
|
|
foreach (@methods) { |
234 |
|
|
$self->log("Running method '$_'.", 'info'); |
235 |
joko |
1.7 |
#print Dumper($method_args); |
236 |
|
|
# check for existance of method |
237 |
|
|
if ($container->can($_)) { |
238 |
|
|
# dispatch call by being with or without arguments |
239 |
joko |
1.9 |
#print "method_args: ", Dumper($method_args); |
240 |
joko |
1.7 |
if ($method_args) { |
241 |
|
|
$container->$_(@$method_args); |
242 |
|
|
} else { |
243 |
|
|
$container->$_(); |
244 |
|
|
} |
245 |
|
|
} |
246 |
joko |
1.2 |
} |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
# 4. instances - create references in local scope |
250 |
|
|
if (my $instances = $args->{instances}) { |
251 |
|
|
my @instances = split(/,|,\s/, $instances); |
252 |
|
|
foreach (@instances) { |
253 |
|
|
$self->log("Creating instance '$_'.", 'info'); |
254 |
|
|
$self->{$_} = $container; |
255 |
|
|
} |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
|
259 |
joko |
1.1 |
} |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
sub _rapcall { |
263 |
|
|
my $self = shift; |
264 |
|
|
my $args = shift; |
265 |
joko |
1.2 |
|
266 |
joko |
1.7 |
# merge container arguments to local ones if desired/requested/forced |
267 |
joko |
1.2 |
if (my $container = $self->getContainer()) { |
268 |
joko |
1.3 |
#my $opts = merge($container, $args); |
269 |
joko |
1.4 |
#print Dumper($container); |
270 |
|
|
merge_to($args, $container, { init => 1 }); |
271 |
joko |
1.2 |
} |
272 |
joko |
1.3 |
|
273 |
joko |
1.4 |
# trace |
274 |
joko |
1.7 |
#print Dumper($args); |
275 |
joko |
1.4 |
|
276 |
joko |
1.7 |
# action dispatcher, either do: |
277 |
|
|
# - run an executable program [ext] |
278 |
|
|
# - call a command (?) [rap] |
279 |
|
|
# - a rap target [rap] |
280 |
|
|
# - a code method [ext] |
281 |
|
|
|
282 |
joko |
1.4 |
if ($args->{executable}) { |
283 |
|
|
$self->run_executable($args); |
284 |
|
|
return; |
285 |
|
|
} |
286 |
joko |
1.2 |
|
287 |
joko |
1.1 |
if (my $command = $args->{command}) { |
288 |
joko |
1.2 |
$self->perform_command($command, $args); |
289 |
joko |
1.3 |
return; |
290 |
joko |
1.2 |
} |
291 |
|
|
|
292 |
|
|
if (my $target = $args->{target}) { |
293 |
|
|
$self->performTarget($target, $args); |
294 |
joko |
1.3 |
return; |
295 |
joko |
1.2 |
} |
296 |
|
|
|
297 |
|
|
if (my $method = $args->{method}) { |
298 |
|
|
#$self->performTarget($target, $args); |
299 |
|
|
if (my $refkey = $args->{base}) { |
300 |
|
|
#eval($namespace . '::' . $method . '();'); |
301 |
|
|
#die($@) if $@; |
302 |
|
|
#print Dumper($self); |
303 |
joko |
1.7 |
|
304 |
|
|
#if ($args->{args} && lc $args->{args} eq 'array') { |
305 |
|
|
#print Dumper($args); |
306 |
|
|
#print Dumper($self); |
307 |
|
|
#} |
308 |
|
|
|
309 |
|
|
# V1 - no arguments were being propagated |
310 |
|
|
#$self->{$refkey}->$method(); |
311 |
|
|
# V2 - trying this.... |
312 |
joko |
1.8 |
$self->{$refkey}->$method($args->{args}); |
313 |
joko |
1.2 |
|
314 |
|
|
} elsif (my $ref = $self->getInstance()) { |
315 |
joko |
1.8 |
$ref->$method($args->{args}); |
316 |
joko |
1.2 |
} |
317 |
joko |
1.3 |
|
318 |
|
|
return; |
319 |
|
|
|
320 |
joko |
1.1 |
} |
321 |
|
|
|
322 |
joko |
1.2 |
} |
323 |
|
|
|
324 |
|
|
sub _exit { |
325 |
|
|
exit; |
326 |
|
|
} |
327 |
|
|
|
328 |
|
|
sub _use { |
329 |
|
|
my $self = shift; |
330 |
|
|
my $args = shift; |
331 |
joko |
1.7 |
|
332 |
|
|
# new of ~2003-04-15: making methods from foreign context(s) available |
333 |
|
|
if (my $name = $args->{name}) { |
334 |
|
|
$args->{type} ||= ''; |
335 |
|
|
$self->log("Switching to $args->{type} '$name'.", 'info'); |
336 |
|
|
$self->setInstance($self->{$name}) if $args->{type} eq 'instance'; |
337 |
|
|
} |
338 |
|
|
|
339 |
|
|
# new of ~2003-04-15: making methods from foreign context(s) available |
340 |
|
|
if (exists $args->{method_prefix}) { |
341 |
|
|
#print "YAI", "\n"; |
342 |
|
|
#print Dumper($args); |
343 |
|
|
$self->set_property( { name => 'core.method_prefix', value => $args->{method_prefix} } ); |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
# TODO: (possible feature) |
347 |
|
|
# check for $args->{export_rap_commands} to export core rap-commands |
348 |
|
|
# from rap engine's scope to the worker's |
349 |
|
|
|
350 |
joko |
1.1 |
} |
351 |
|
|
|
352 |
joko |
1.6 |
sub _script { |
353 |
|
|
my $self = shift; |
354 |
|
|
my $args = shift; |
355 |
|
|
my $content = shift; |
356 |
|
|
|
357 |
|
|
# trace |
358 |
|
|
#print Dumper($args); |
359 |
|
|
#print Dumper($content); |
360 |
|
|
|
361 |
|
|
my $code = $content->[0]->{content}; |
362 |
|
|
$code ||= ''; |
363 |
|
|
|
364 |
|
|
$self->run_script($args, $code); |
365 |
joko |
1.7 |
|
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
sub _import { |
369 |
|
|
my $self = shift; |
370 |
|
|
my $args = shift; |
371 |
|
|
#my $content = shift; |
372 |
|
|
|
373 |
|
|
if (!$args->{topic}) { |
374 |
|
|
$self->log("Please specify topic.", 'warning'); |
375 |
|
|
return; |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
$self->log("Importing all targets from topic '$args->{topic}'.", 'info'); |
379 |
|
|
|
380 |
|
|
#print Dumper($self); |
381 |
|
|
# FIXME: This was started off, but not finished! |
382 |
|
|
print "registry: ", Dumper($Data::Rap::registry); |
383 |
joko |
1.6 |
|
384 |
|
|
} |
385 |
joko |
1.1 |
|
386 |
|
|
1; |
387 |
|
|
__END__ |