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