| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package OSGi::Osgish::Command::Bundle; | ||
| 4 | use strict; | ||
| 5 | use vars qw(@ISA); | ||
| 6 | use Term::ANSIColor qw(:constants); | ||
| 7 | use OSGi::Osgish::Command; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | @ISA = qw(OSGi::Osgish::Command); | ||
| 11 | |||
| 12 | my %BUNDLE_STATE_COLOR = ( | ||
| 13 | "installed" => "bundle_installed", | ||
| 14 | "resolved" => "bundle_resolved", | ||
| 15 | "active" => "bundle_active" | ||
| 16 | ); | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | OSGi::Osgish::Command::Bundle - Bundle related commands | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This collection of shell commands provided access to bundle related | ||
| 25 | operations. I.e. these are | ||
| 26 | |||
| 27 | =over | ||
| 28 | |||
| 29 | =item * | ||
| 30 | |||
| 31 | List of bundles ('ls') | ||
| 32 | |||
| 33 | =item * | ||
| 34 | |||
| 35 | Start and Stopping of bundles ('start'/'stop') | ||
| 36 | |||
| 37 | =back | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | =head1 COMMANDS | ||
| 42 | |||
| 43 | =over | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | |||
| 48 | # Name of this command | ||
| 49 | sub name { "bundle" } | ||
| 50 | |||
| 51 | # We hook into as top-level commands | ||
| 52 | sub top_commands { | ||
| 53 | my $self = shift; | ||
| 54 | return $self->agent ? $self->sub_commands : {}; | ||
| 55 | } | ||
| 56 | |||
| 57 | # Context command "bundle" | ||
| 58 | sub commands { | ||
| 59 | my $self = shift; | ||
| 60 | my $cmds = $self->sub_commands; | ||
| 61 | return { | ||
| 62 | "bundle" => { | ||
| 63 | desc => "Bundles related operations", | ||
| 64 | proc => $self->push_on_stack("bundle",$cmds), | ||
| 65 | cmds => $cmds | ||
| 66 | }, | ||
| 67 | "b" => { alias => "bundle", exclude_from_completion => 1}, | ||
| 68 | }; | ||
| 69 | } | ||
| 70 | |||
| 71 | # The 'real' commands | ||
| 72 | sub sub_commands { | ||
| 73 | my $self = shift; | ||
| 74 | return { | ||
| 75 | "ls" => { | ||
| 76 | desc => "List bundles", | ||
| 77 | proc => $self->cmd_list, | ||
| 78 | args => $self->complete->bundles(no_ids => 1), | ||
| 79 | doc => <<EOT, | ||
| 80 | |||
| 81 | ls [-s -e -i] [<bnd>] | ||
| 82 | |||
| 83 | List one or more bundles. | ||
| 84 | |||
| 85 | Options: | ||
| 86 | -i Show wired imports (single bundle) | ||
| 87 | -e Show wired exports (single bundle) | ||
| 88 | -s Show symbolic name (single bundle) | ||
| 89 | -h Show headers (single bundle) | ||
| 90 | <bnd> Bundle id or symbolic name, conditionally | ||
| 91 | with wildcards ('*' and '?') | ||
| 92 | EOT | ||
| 93 | }, | ||
| 94 | "start" => { | ||
| 95 | desc => "Start bundles", | ||
| 96 | proc => $self->cmd_start, | ||
| 97 | args => $self->complete->bundles, | ||
| 98 | doc => <<EOT, | ||
| 99 | |||
| 100 | start <bnd1> <bnd2> ... | ||
| 101 | |||
| 102 | Start one or more bundles. Bundles can be given | ||
| 103 | as ids or symbolic names (or mixed). Wildcards | ||
| 104 | ('*' and '?') are supported on symbolic names. | ||
| 105 | EOT | ||
| 106 | }, | ||
| 107 | "stop" => { | ||
| 108 | desc => "Stop bundles", | ||
| 109 | proc => $self->cmd_stop, | ||
| 110 | args => $self->complete->bundles, | ||
| 111 | doc => <<EOT, | ||
| 112 | |||
| 113 | stop <bnd1> <bnd2> ... | ||
| 114 | |||
| 115 | Stop one or more bundles. Bundles can be given | ||
| 116 | as ids or symbolic names (or mixed). Wildcards | ||
| 117 | ('*' and '?') are supported on symbolic names. | ||
| 118 | EOT | ||
| 119 | }, | ||
| 120 | "resolve" => { | ||
| 121 | desc => "Resolve bundles", | ||
| 122 | proc => $self->cmd_resolve, | ||
| 123 | args => $self->complete->bundles, | ||
| 124 | doc => <<EOT | ||
| 125 | |||
| 126 | resolve <bnd1> <bnd2> ... | ||
| 127 | |||
| 128 | Resolve one or more bundles. Bundles can be given | ||
| 129 | as ids or symbolic names (or mixed). Wildcards | ||
| 130 | ('*' and '?') are supported on symbolic names. | ||
| 131 | EOT | ||
| 132 | |||
| 133 | }, | ||
| 134 | "update" => { | ||
| 135 | desc => "Update a bundle optionally from a new location", | ||
| 136 | proc => $self->cmd_update, | ||
| 137 | args => $self->complete->bundles, | ||
| 138 | doc => <<EOT | ||
| 139 | |||
| 140 | update <bnd1> <bnd2> ... | ||
| 141 | update -l <url> <bnd> | ||
| 142 | |||
| 143 | Update one or more bundles. Bundles can be given | ||
| 144 | as ids or symbolic names (or mixed). Wildcards | ||
| 145 | ('*' and '?') are supported on symbolic names. | ||
| 146 | |||
| 147 | With '-l' a single bundle can be updated from the | ||
| 148 | given location URL. | ||
| 149 | EOT | ||
| 150 | |||
| 151 | }, | ||
| 152 | "install" => { | ||
| 153 | desc => "Install a bundle", | ||
| 154 | proc => $self->cmd_install, | ||
| 155 | # Todo: Complete on file names if no schema is given | ||
| 156 | # and the server is a local host | ||
| 157 | args => $self->complete->bundles, | ||
| 158 | doc => <<EOT | ||
| 159 | |||
| 160 | install <url> | ||
| 161 | |||
| 162 | Install a single bundle from the given URL. | ||
| 163 | EOT | ||
| 164 | |||
| 165 | }, | ||
| 166 | "uninstall" => { | ||
| 167 | desc => "Uninstall bundles", | ||
| 168 | proc => $self->cmd_uninstall, | ||
| 169 | args => $self->complete->bundles, | ||
| 170 | doc => <<EOT | ||
| 171 | |||
| 172 | uninstall <bnd1> <bnd2> ... | ||
| 173 | |||
| 174 | Uninstall one or more bundles. Bundles can be given | ||
| 175 | as ids or symbolic names (or mixed). Wildcards | ||
| 176 | ('*' and '?') are supported on symbolic names. | ||
| 177 | EOT | ||
| 178 | }, | ||
| 179 | "refresh" => { | ||
| 180 | desc => "Refresh bundles", | ||
| 181 | proc => $self->cmd_refresh, | ||
| 182 | args => $self->complete->bundles, | ||
| 183 | doc => <<EOT | ||
| 184 | |||
| 185 | Refresh <bnd1> <bnd2> ... | ||
| 186 | |||
| 187 | Refresh one or more bundles. Bundles can be given | ||
| 188 | as ids or symbolic names (or mixed). Wildcards | ||
| 189 | ('*' and '?') are supported on symbolic names. | ||
| 190 | EOT | ||
| 191 | |||
| 192 | } | ||
| 193 | }; | ||
| 194 | } | ||
| 195 | |||
| 196 | # =================================================================================================== | ||
| 197 | |||
| 198 | |||
| 199 | =item cmd_list | ||
| 200 | |||
| 201 | List commands which can filter bundles by wildcard and knows about the | ||
| 202 | following options: | ||
| 203 | |||
| 204 | =over | ||
| 205 | |||
| 206 | =item -s | ||
| 207 | |||
| 208 | Show symbolic names instead of descriptive names | ||
| 209 | |||
| 210 | =item -i | ||
| 211 | |||
| 212 | |||
| 213 | =item -e | ||
| 214 | |||
| 215 | =item -h | ||
| 216 | |||
| 217 | =back | ||
| 218 | |||
| 219 | If a single bundle is given as argument its details are shown. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | sub cmd_list { | ||
| 224 | my $self = shift; | ||
| 225 | |||
| 226 | return sub { | ||
| 227 | my $osgish = $self->osgish; | ||
| 228 | my $agent = $osgish->agent; | ||
| 229 | print "Not connected to a server\n" and return unless $agent; | ||
| 230 | my ($opts,@filters) = $self->extract_command_options(["s!","i!","e!","h!"],@_); | ||
| 231 | my $bundles = $agent->bundles; | ||
| 232 | my $text = sprintf("%4.4s %-11.11s %3s %s\n","Id","State","Lev","Name"); | ||
| 233 | $text .= "-" x 87 . "\n"; | ||
| 234 | my $nr = 0; | ||
| 235 | |||
| 236 | my $filtered_bundles = $self->_filter_bundles($bundles,@filters); | ||
| 237 | return unless @$filtered_bundles; | ||
| 238 | |||
| 239 | if (@$filtered_bundles == 1) { | ||
| 240 | # Print single info for bundle | ||
| 241 | $self->print_bundle_info($filtered_bundles->[0],$opts); | ||
| 242 | } else { | ||
| 243 | for my $b (sort { $a->{Identifier} <=> $b->{Identifier} } @$filtered_bundles) { | ||
| 244 | my $id = $b->{Identifier}; | ||
| 245 | my ($c_frag,$reset) = $osgish->color("bundle_fragment",RESET); | ||
| 246 | my $state = lc $b->{State}; | ||
| 247 | my $color = $self->_bundle_state_color($b); | ||
| 248 | my $state = $self->_state_info($b); | ||
| 249 | my $level = $b->{StartLevel}; | ||
| 250 | my $fragment = $b->{Fragment} eq "true" ? $c_frag . "F" . $reset : " "; | ||
| 251 | my $name = $b->{Headers}->{'[Bundle-Name]'}->{Value}; | ||
| 252 | my $sym_name = $b->{SymbolicName}; | ||
| 253 | my $version = $b->{Version}; | ||
| 254 | my $location = $b->{Location}; | ||
| 255 | my $desc = $opts->{s} ? | ||
| 256 | $sym_name || $location : | ||
| 257 | $name || $sym_name || $location; | ||
| 258 | $desc .= " ($version)" if $version && $version ne "0.0.0"; | ||
| 259 | |||
| 260 | $text .= sprintf "%s%4d %-11s%s %s%3d %s%s%s\n",$color,$id,$state,$reset,$fragment,$level,$desc; | ||
| 261 | $nr++; | ||
| 262 | } | ||
| 263 | $self->print_paged($text,$nr); | ||
| 264 | } | ||
| 265 | #print $text; | ||
| 266 | #print Dumper($bundles); | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | =item cmd_start | ||
| 272 | |||
| 273 | Resolve one or more bundles by its id or symbolicname | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub cmd_resolve { | ||
| 278 | my $self = shift; | ||
| 279 | return sub { | ||
| 280 | my @args = @_; | ||
| 281 | my $filters = $self->_filter_symbolic_names(@args); | ||
| 282 | $self->agent->resolve_bundle(@$filters); | ||
| 283 | |||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | =item cmd_start | ||
| 289 | |||
| 290 | Start one or more bundles by its id or symbolicname | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub cmd_start { | ||
| 295 | my $self = shift; | ||
| 296 | return sub { | ||
| 297 | my $filters = $self->_filter_symbolic_names(@_); | ||
| 298 | my $ret; | ||
| 299 | eval { | ||
| 300 | $ret = $self->agent->start_bundle(@$filters); | ||
| 301 | }; | ||
| 302 | $self->_print_operation_result("Start",$ret,$filters,$@); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | =item cmd_stop | ||
| 307 | |||
| 308 | Stop one or more bundles by its id or symbolicname | ||
| 309 | |||
| 310 | =cut | ||
| 311 | |||
| 312 | sub cmd_stop { | ||
| 313 | my $self = shift; | ||
| 314 | return sub { | ||
| 315 | my $filters = $self->_filter_symbolic_names(@_); | ||
| 316 | my $ret; | ||
| 317 | eval { | ||
| 318 | $ret = $self->agent->stop_bundle(@$filters); | ||
| 319 | }; | ||
| 320 | $self->_print_operation_result("Stop",$ret,$filters,$@); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | =item cmd_update | ||
| 325 | |||
| 326 | Update a bundle from its current location | ||
| 327 | |||
| 328 | =cut | ||
| 329 | |||
| 330 | sub cmd_update { | ||
| 331 | my $self = shift; | ||
| 332 | return sub { | ||
| 333 | my ($opts,@filters) = $self->extract_command_options(["l=s"],@_); | ||
| 334 | my $agent = $self->osgish->agent; | ||
| 335 | my $filtered_bundles = $self->_filter_symbolic_names(@filters); | ||
| 336 | #print Dumper($filtered_bundles); | ||
| 337 | my $ret; | ||
| 338 | eval { | ||
| 339 | if ($opts->{l} || @$filtered_bundles == 1) { | ||
| 340 | die "Can only update a single bundle with -l. Given : ",join(",",@$filtered_bundles),"\n" | ||
| 341 | if @$filtered_bundles > 1; | ||
| 342 | $ret = $self->agent->update_bundle($filtered_bundles->[0],$opts->{l}); | ||
| 343 | } else { | ||
| 344 | $ret = $self->agent->update_bundles(@$filtered_bundles); | ||
| 345 | } | ||
| 346 | }; | ||
| 347 | $self->_print_operation_result("Updat",$ret,$filtered_bundles,$@); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | =item cmd_install | ||
| 352 | |||
| 353 | Install one or more bundles | ||
| 354 | |||
| 355 | =cut | ||
| 356 | |||
| 357 | sub cmd_install { | ||
| 358 | my $self = shift; | ||
| 359 | my $osgish = $self->osgish; | ||
| 360 | return sub { | ||
| 361 | my @args = @_; | ||
| 362 | |||
| 363 | my $ret; | ||
| 364 | eval { | ||
| 365 | $ret = $self->agent->install_bundle(@args); | ||
| 366 | }; | ||
| 367 | $self->_print_operation_result("Install",$ret,\@args,$@,1); | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | =item cmd_uninstall | ||
| 372 | |||
| 373 | Uninstall one or more bundles | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | sub cmd_uninstall { | ||
| 378 | my $self = shift; | ||
| 379 | return sub { | ||
| 380 | my $filters = $self->_filter_symbolic_names(@_); | ||
| 381 | my $ret; | ||
| 382 | eval { | ||
| 383 | $ret = $self->agent->uninstall_bundle(@$filters); | ||
| 384 | }; | ||
| 385 | $self->_print_operation_result("Uninstall",$ret,$filters,$@); | ||
| 386 | } | ||
| 387 | } | ||
| 388 | |||
| 389 | =item cmd_refresh | ||
| 390 | |||
| 391 | Refresh one or more bundles | ||
| 392 | |||
| 393 | =cut | ||
| 394 | |||
| 395 | sub cmd_refresh { | ||
| 396 | my $self = shift; | ||
| 397 | return sub { | ||
| 398 | my $filters = $self->_filter_symbolic_names(@_); | ||
| 399 | my $ret; | ||
| 400 | eval { | ||
| 401 | $ret = $self->agent->refresh_bundle(@$filters); | ||
| 402 | }; | ||
| 403 | $self->_print_operation_result("Refresh",$ret,$filters,$@); | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | |||
| 408 | # Print a single bundle's info | ||
| 409 | sub print_bundle_info { | ||
| 410 | my $self = shift; | ||
| 411 | my $osgish = $self->osgish; | ||
| 412 | my $agent = $self->agent; | ||
| 413 | |||
| 414 | my $bu = shift; | ||
| 415 | my $opts = shift; | ||
| 416 | my $txt = ""; | ||
| 417 | |||
| 418 | $self->_dump_main_info(\$txt,$bu,$opts); | ||
| 419 | $txt .= "\n"; | ||
| 420 | |||
| 421 | |||
| 422 | my $imports = $self->_extract_imports($bu->{ImportedPackages},$bu->{Headers},$opts->{i}); | ||
| 423 | $self->_dump_imports(\$txt,$imports,$opts); | ||
| 424 | |||
| 425 | my $exports = $self->_extract_exports($bu->{ExportedPackages},$opts->{e}); | ||
| 426 | $self->_dump_exports(\$txt,$exports,$opts); | ||
| 427 | |||
| 428 | $self->_dump_services(\$txt,$bu,$opts); | ||
| 429 | $self->_dump_required(\$txt,$bu,$opts); | ||
| 430 | $self->_dump_headers(\$txt,$bu->{Headers}) if ($opts->{h}); | ||
| 431 | |||
| 432 | $self->print_paged($txt); | ||
| 433 | |||
| 434 | #print Dumper($bu); | ||
| 435 | } | ||
| 436 | |||
| 437 | sub _print_operation_result { | ||
| 438 | my $self = shift; | ||
| 439 | my $label = shift; | ||
| 440 | my $ret = shift; | ||
| 441 | my $bundles = shift; | ||
| 442 | my $error = shift; | ||
| 443 | my $no_cache = shift; | ||
| 444 | my $agent = $self->agent; | ||
| 445 | # Update cache if required | ||
| 446 | $agent->bundles() if $no_cache; | ||
| 447 | my ($c_bid,$c_bname,$c_r) = $self->color("bundle_id","bundle_name",RESET); | ||
| 448 | if ($error) { | ||
| 449 | print $label . "ing failed for " . join(",",map { $c_bname . $_ . $c_r } @$bundles) . ":\n"; | ||
| 450 | print $error; | ||
| 451 | } elsif (ref($ret) eq "HASH") { | ||
| 452 | if (lc $ret->{Success} eq "false") { | ||
| 453 | my $id = $ret->{BundleInError}; | ||
| 454 | my $name = $agent->bundle_name($id,use_cached => 1); | ||
| 455 | print $label . "ing failed for bundle " . $c_bname . $name . $c_r . " (" . $c_bid . $id . $c_r . "): \n"; | ||
| 456 | print $ret->{Error} . "\n\n"; | ||
| 457 | } | ||
| 458 | if ($ret->{Completed} && @{$ret->{Completed}}) { | ||
| 459 | print $label . "ed Bundles:\n"; | ||
| 460 | for my $c (sort { $a <=> $b } @{$ret->{Completed}}) { | ||
| 461 | my $name = $agent->bundle_name($c,use_cached => 1); | ||
| 462 | print " " . $c_bname . $name . $c_r . " (". $c_bid . $c . $c_r . ")\n"; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | if ($ret->{Remaining} && @{$ret->{Remaining}}) { | ||
| 466 | print "Remaining Bundles:\n"; | ||
| 467 | for my $c (sort { $a <=> $b } @{$ret->{Remaining}}) { | ||
| 468 | my $name = $agent->bundle_name($c,use_cached => 1); | ||
| 469 | print " " . $c_bname . $name . $c_r . " (". $c_bid . $c . $c_r . ")\n"; | ||
| 470 | } | ||
| 471 | } | ||
| 472 | } else { | ||
| 473 | my $name = $agent->bundle_name($ret,use_cached => 1); | ||
| 474 | print $label . "ed bundle " . $c_bname . $name . $c_r . " (" . $c_bid . $ret . $c_r . ")\n"; | ||
| 475 | } | ||
| 476 | } | ||
| 477 | |||
| 478 | |||
| 479 | sub _extract_services { | ||
| 480 | my $self = shift; | ||
| 481 | my $ids = shift; | ||
| 482 | my $agent = $self->agent; | ||
| 483 | my $ret = {}; | ||
| 484 | for my $id (@$ids) { | ||
| 485 | my $service = $agent->service($id); | ||
| 486 | $ret->{$id} = { | ||
| 487 | id => $id, | ||
| 488 | class => $service->{classes}->[0], | ||
| 489 | bundle => $service->{bundle}, | ||
| 490 | using => $service->{using} | ||
| 491 | }; | ||
| 492 | } | ||
| 493 | return $ret; | ||
| 494 | } | ||
| 495 | |||
| 496 | sub _dump_services { | ||
| 497 | my $self = shift; | ||
| 498 | my $ret = shift; | ||
| 499 | my $bu = shift; | ||
| 500 | my $opts = shift; | ||
| 501 | my $osgish = $self->osgish; | ||
| 502 | my $agent = $self->agent; | ||
| 503 | |||
| 504 | my $s = ""; | ||
| 505 | my $services = $agent->services; # Update if necessary | ||
| 506 | my $services = $self->_extract_services($bu->{RegisteredServices}); | ||
| 507 | my ($c_bid,$c_bname,$c_using,$c_registered,$c_reset) = $osgish->color("bundle_id","bundle_name","service_using","service_registered",RESET); | ||
| 508 | my $label = " Registered:"; | ||
| 509 | for my $id (keys %{$services}) { | ||
| 510 | #print Dumper($services->{$id}); | ||
| 511 | my @bundles = @{$services->{$id}->{using} || []}; | ||
| 512 | my $class = $services->{$id}->{class}; | ||
| 513 | $s .= $self->_dump_bundle_using($class,$services->{$id}->{using}, | ||
| 514 | {label => $label, use_sym => $opts->{s},color => $c_using, | ||
| 515 | prefix => sprintf("%3.3s: ",$id)}); | ||
| 516 | $label = ""; | ||
| 517 | } | ||
| 518 | $services = $self->_extract_services($bu->{ServicesInUse}); | ||
| 519 | $label = " Using:"; | ||
| 520 | for my $id (keys %{$services}) { | ||
| 521 | my $bundle = $services->{$id}->{bundle}; | ||
| 522 | if ($opts->{s}) { | ||
| 523 | $bundle = $c_bname . $agent->bundle_name($bundle,use_cached => 1) . $c_reset; | ||
| 524 | } else { | ||
| 525 | $bundle = $c_bid . $bundle . $c_reset; | ||
| 526 | } | ||
| 527 | $s .= sprintf("%-14.14s %3.3s: %s <- %s\n",$label,$id,$c_using . $services->{$id}->{class} . $c_reset,$bundle); | ||
| 528 | $label = ""; | ||
| 529 | } | ||
| 530 | |||
| 531 | $$ret .= "\nServices:\n" . $s if length $s; | ||
| 532 | } | ||
| 533 | |||
| 534 | sub _dump_required { | ||
| 535 | my $self = shift; | ||
| 536 | my $ret = shift; | ||
| 537 | my $bu = shift; | ||
| 538 | my $opts = shift; | ||
| 539 | my $osgish = $self->osgish; | ||
| 540 | my $agent = $self->agent; | ||
| 541 | |||
| 542 | my $s = ""; | ||
| 543 | my ($c_bid,$c_bname,$c_reset) = | ||
| 544 | $osgish->color("bundle_id","bundle_name",RESET); | ||
| 545 | for my $e ([ "RequiredBundles","Required" ],[ "RequiringBundles", "Required by" ]) { | ||
| 546 | my $bundles = $bu->{$e->[0]}; | ||
| 547 | if (@$bundles) { | ||
| 548 | my $label = $e->[1]; | ||
| 549 | for my $id (@$bundles) { | ||
| 550 | my $id_c = $c_bid . $id . $c_reset; | ||
| 551 | my $bundle = $opts->{s} ? $c_bname . $agent->bundle_name($id,use_cached => 1) . $c_reset . " (" . $id_c . ")" : $id_c; | ||
| 552 | $s .= sprintf("%-14.14s %s\n",$label,$bundle); | ||
| 553 | $label = ""; | ||
| 554 | } | ||
| 555 | } | ||
| 556 | } | ||
| 557 | $$ret .= "\n" . $s if length($s); | ||
| 558 | } | ||
| 559 | |||
| 560 | sub _dump_bundle_using { | ||
| 561 | my $self = shift; | ||
| 562 | my $main = shift; | ||
| 563 | my $bundles = shift; | ||
| 564 | my $args = shift; | ||
| 565 | my $agent = $self->agent; | ||
| 566 | my ($c_bid,$c_bname,$c_reset) = $self->osgish->color("bundle_id","bundle_name",RESET); | ||
| 567 | my $ret = ""; | ||
| 568 | my $prefix = $args->{prefix} || ""; | ||
| 569 | my $len = $args->{length} || length($main . $prefix); | ||
| 570 | my $main_c = $args->{color} ? $args->{color} . $main . $c_reset : $main; | ||
| 571 | if ($args->{use_sym}) { | ||
| 572 | if ($bundles && @$bundles) { | ||
| 573 | my @names = map { $agent->bundle_name($_,use_cached => 1) } @$bundles; | ||
| 574 | $ret .= sprintf("%-14.14s %s -> %s\n",$args->{label},$prefix . $main_c, | ||
| 575 | $c_bname . shift(@names) . $c_reset); | ||
| 576 | my $indent = " " x (19 + $len); | ||
| 577 | while (@names) { | ||
| 578 | $ret .= $indent . $c_bname . (shift @names) . $c_reset . "\n"; | ||
| 579 | } | ||
| 580 | } else { | ||
| 581 | $ret .= sprintf("%-14.14s %s\n",$args->{label},$prefix . $main); | ||
| 582 | } | ||
| 583 | } else { | ||
| 584 | #my @bundles = map { $c_ps . $_->{id} . $c_re } @{$val->{using}}; | ||
| 585 | my $src = ""; | ||
| 586 | my $c = $main; | ||
| 587 | if ($bundles && @$bundles) { | ||
| 588 | my $txt = join ", ", map { $c_bid . $_ . $c_reset } @$bundles; | ||
| 589 | $src = " -> " . $txt; | ||
| 590 | } | ||
| 591 | $ret .= sprintf("%-14.14s %s%s\n",$args->{label},$prefix . $main_c,$src); | ||
| 592 | } | ||
| 593 | return $ret; | ||
| 594 | } | ||
| 595 | |||
| 596 | sub _dump_main_info { | ||
| 597 | my $self = shift; | ||
| 598 | my $ret = shift; | ||
| 599 | my $bu = shift; | ||
| 600 | my $opts = shift; | ||
| 601 | my $osgish = $self->osgish; | ||
| 602 | my $agent = $self->agent; | ||
| 603 | #print Dumper($bu); | ||
| 604 | my $name = $bu->{Headers}->{'Bundle-Name'}->{Value}; | ||
| 605 | my ($c_bid,$c_bname,$c_version,$c_fragment,$c_reset) = $osgish->color("bundle_id","bundle_name","bundle_version","bundle_fragment",RESET); | ||
| 606 | my $sym = $bu->{SymbolicName} || $bu->{Location}; | ||
| 607 | my $version = $bu->{Version} ? $c_version . $bu->{Version} . $c_reset : ""; | ||
| 608 | my $fragment = $bu->{Fragment} eq "true" ? "(" . $c_fragment . "Fragment" . $c_reset . ")" : ""; | ||
| 609 | my $rest = join(" ",$version,$fragment); | ||
| 610 | my $state_color = $self->_bundle_state_color($bu); | ||
| 611 | my $state = "[" . $state_color . $self->_state_info($bu) . $c_reset . "]"; | ||
| 612 | $sym = $c_bname . $sym . $c_reset; | ||
| 613 | $$ret .= sprintf("%-14.14s %s %s\n","Name:",$c_bid.$bu->{Identifier}.$c_reset,$name ? $name : $sym) if $name; | ||
| 614 | $$ret .= sprintf("%-14.14s %s %s %s\n","",$name ? $sym : "",$state,$rest); | ||
| 615 | $$ret .= sprintf("%-14.14s %s\n","Location:",$bu->{Location}); | ||
| 616 | $$ret .= sprintf("%-14.14s %s\n","Modified:",$self->format_date($bu->{LastModified}/1000)); | ||
| 617 | $self->_add_fragment($ret,$bu->{Hosts},"Hosts:"); | ||
| 618 | $self->_add_fragment($ret,$bu->{Fragments},"Fragments:"); | ||
| 619 | |||
| 620 | my @flags = (); | ||
| 621 | for my $flag ("RemovalPending", "PersistentlyStarted", "Required") { | ||
| 622 | push @flags,$flag if $bu->{$flag} eq "true"; | ||
| 623 | } | ||
| 624 | if (@flags) { | ||
| 625 | $$ret .= sprintf("%-14.14s %s\n","Flags:",join ", ",@flags); | ||
| 626 | } | ||
| 627 | if (length($bu->{StartLevel})) { | ||
| 628 | $$ret .= sprintf("%-14.14s %s\n","Start-Level:",$bu->{StartLevel}); | ||
| 629 | } | ||
| 630 | #print Dumper($bu); | ||
| 631 | } | ||
| 632 | |||
| 633 | sub _add_fragment { | ||
| 634 | my $self = shift; | ||
| 635 | my $ret = shift; | ||
| 636 | my $list = shift; | ||
| 637 | my $label = shift; | ||
| 638 | my $osgish = $self->osgish; | ||
| 639 | my $agent = $self->agent; | ||
| 640 | my ($c_bid,$c_bname,$c_fragment,$c_reset) = $osgish->color("bundle_id","bundle_name","bundle_fragment",RESET); | ||
| 641 | |||
| 642 | if ($list && @{$list}) { | ||
| 643 | # Host can occur multiple times | ||
| 644 | my %uniq = map { $_ => 1 } @$list; | ||
| 645 | for my $f (keys %uniq ) { | ||
| 646 | my $name = $c_fragment . $agent->bundle_name($f,use_cached => 1) . $c_reset . " (${c_bid}${f}${c_reset})"; | ||
| 647 | $$ret .= sprintf("%-14.14s %s\n",$label,$name); | ||
| 648 | $label = ""; | ||
| 649 | } | ||
| 650 | } | ||
| 651 | } | ||
| 652 | |||
| 653 | sub _header { | ||
| 654 | my $self = shift; | ||
| 655 | my $bu = shift; | ||
| 656 | my $key = shift; | ||
| 657 | my $headers = $bu->{Headers}; | ||
| 658 | my @prop = grep { $_->{Key} eq $key } values %$headers; | ||
| 659 | return "" unless @prop; | ||
| 660 | return (shift @prop)->{Value}; | ||
| 661 | } | ||
| 662 | |||
| 663 | sub _dump_imports { | ||
| 664 | my $self = shift; | ||
| 665 | my $ret = shift; | ||
| 666 | my $imports = shift; | ||
| 667 | my $opts = shift; | ||
| 668 | my $osgish = $self->osgish; | ||
| 669 | |||
| 670 | #print Dumper($imports); | ||
| 671 | my $label = "Imports:"; | ||
| 672 | my ($c_pr,$c_pv,$c_po,$c_ps,$c_re) = $osgish->color("package_resolved","package_version","package_optional","package_imported_from",RESET); | ||
| 673 | for my $k (sort { $a cmp $b } keys %$imports) { | ||
| 674 | my $val = $imports->{$k}; | ||
| 675 | my $version = $val->{version}; | ||
| 676 | if ($val->{version}) { | ||
| 677 | $version = $c_pv . $version . $c_re; | ||
| 678 | $version .= " " . $val->{version_spec} if $val->{version_spec}; | ||
| 679 | } else { | ||
| 680 | $version = $val->{version_spec} if $val->{version_spec}; | ||
| 681 | } | ||
| 682 | my $optional = $val->{optional} ? $c_po . " * " . $c_re : ""; | ||
| 683 | my $package = $k; | ||
| 684 | $package = $c_pr . $package . $c_re if ($val->{resolved}); | ||
| 685 | my $src = ""; | ||
| 686 | if (defined($val->{source})) { | ||
| 687 | my $b = $val->{source}; | ||
| 688 | $src = " <- " . join ", ", map { $c_ps . ($opts->{s} ? $_->{name} : $_->{id}) . $c_re } @{$val->{source}}; | ||
| 689 | } | ||
| 690 | $$ret .= sprintf("%-14.14s %s %s%s%s\n",$label,$package,$version,$optional,$src); | ||
| 691 | $label = ""; | ||
| 692 | } | ||
| 693 | } | ||
| 694 | |||
| 695 | sub _dump_exports { | ||
| 696 | my $self = shift; | ||
| 697 | my $ret = shift; | ||
| 698 | my $exports = shift; | ||
| 699 | my $opts = shift; | ||
| 700 | my $osgish = $self->osgish; | ||
| 701 | |||
| 702 | my $label = "Exports:"; | ||
| 703 | my ($c_pv,$c_pr,$c_ps,$c_re) = $osgish->color("package_version","package_resolved","package_exported_to",RESET); | ||
| 704 | for my $k (sort { $a cmp $b } keys %$exports) { | ||
| 705 | my $val = $exports->{$k}; | ||
| 706 | my $version = $val->{version}; | ||
| 707 | $version = $c_pv . $version . $c_re if ($val->{version}); | ||
| 708 | my $len = length($k . " " . $val->{version}); | ||
| 709 | my $package = $k; | ||
| 710 | $package = $c_pr . $package . $c_re if ($val->{using} && @{$val->{using}}); | ||
| 711 | if ($val->{using}) { | ||
| 712 | # print Dumper($val->{using}); | ||
| 713 | $$ret .= $self->_dump_bundle_using($package . " " . $version,[map { $_->{id} } @{$val->{using}} ], | ||
| 714 | {length => $len,label => $label, use_sym => $opts->{s}}); | ||
| 715 | $label = ""; | ||
| 716 | } else { | ||
| 717 | $$ret .= sprintf("%-14.14s %s %s\n",$label,$package,$version); | ||
| 718 | } | ||
| 719 | $label = ""; | ||
| 720 | } | ||
| 721 | } | ||
| 722 | |||
| 723 | sub _dump_headers { | ||
| 724 | my $self = shift; | ||
| 725 | my $ret = shift; | ||
| 726 | my $headers = shift; | ||
| 727 | my $osgish = $self->osgish; | ||
| 728 | my $label = "Headers:"; | ||
| 729 | #print Dumper($headers); | ||
| 730 | my ($c_h,$c_v,$c_r) = $osgish->color("header_name","header_value",RESET); | ||
| 731 | for my $h (sort { $headers->{$a}->{Key} cmp $headers->{$b}->{Key} } keys %$headers) { | ||
| 732 | my $val = $headers->{$h}->{Value}; | ||
| 733 | my $key = $headers->{$h}->{Key}; | ||
| 734 | if ($key =~ /^(Export|Import)-Package$/ || ($val =~ /[,;]\s*version=/)) { | ||
| 735 | my $prop = $val; | ||
| 736 | my @props; | ||
| 737 | while ($prop) { | ||
| 738 | $prop =~ s/([^,]*?(".*?")*)(,|$)//; | ||
| 739 | push @props,$1; | ||
| 740 | } | ||
| 741 | my $l = length $key; | ||
| 742 | $key = $c_h . $key . $c_r; | ||
| 743 | $$ret .= sprintf("%-14.14s %s = %s%s%s,\n",$label,$key,$c_v,shift @props,$c_r); | ||
| 744 | $label = ""; | ||
| 745 | while (@props) { | ||
| 746 | $$ret .= sprintf("%-14.14s %${l}.${l}s %s%s%s%s\n",$label,"",$c_v,shift @props,$c_r,@props ? "," : ""); | ||
| 747 | } | ||
| 748 | } else { | ||
| 749 | $key = $c_h . $key . $c_r; | ||
| 750 | $$ret .= sprintf("%-14.14s %s = %s%s%s\n",$label,$key,$c_v,$val,$c_r); | ||
| 751 | $label = ""; | ||
| 752 | } | ||
| 753 | } | ||
| 754 | } | ||
| 755 | |||
| 756 | sub _extract_imports { | ||
| 757 | my $self = shift; | ||
| 758 | my $agent = $self->agent; | ||
| 759 | my ($imp,$headers,$lookup_sources) = @_; | ||
| 760 | my $imp_headers = {}; | ||
| 761 | for my $i (grep { $_->{Key} eq 'Import-Package' } values %{$headers}) { | ||
| 762 | my $val = $i->{Value}; | ||
| 763 | $imp_headers = { %$imp_headers, %{$self->_split_property($val)} }; | ||
| 764 | } | ||
| 765 | my $imports = {}; | ||
| 766 | my $first = 1; | ||
| 767 | for my $i (@$imp) { | ||
| 768 | my ($package,$version) = $self->_split_package($i); | ||
| 769 | my $e = {}; | ||
| 770 | $e->{version} = $version; | ||
| 771 | $e->{resolved} = 1; | ||
| 772 | if ($imp_headers->{$package}) { | ||
| 773 | $self->_add_imp_header_info($e,$imp_headers->{$package}); | ||
| 774 | } | ||
| 775 | if ($lookup_sources) { | ||
| 776 | $e->{source} = $agent->exporting_bundles($package,$version,use_cached => !$first); | ||
| 777 | $first = 0; | ||
| 778 | } | ||
| 779 | $imports->{$package} = $e; | ||
| 780 | } | ||
| 781 | |||
| 782 | # Add unresolved imports mentioned in the header | ||
| 783 | for my $k (keys %$imp_headers) { | ||
| 784 | if (!$imports->{$k}) { | ||
| 785 | my $e = $self->_add_imp_header_info({},$imp_headers->{$k}); | ||
| 786 | $e->{resolved} = 0; | ||
| 787 | $imports->{$k} = $e; | ||
| 788 | } | ||
| 789 | } | ||
| 790 | return $imports; | ||
| 791 | } | ||
| 792 | |||
| 793 | sub _extract_exports { | ||
| 794 | my $self = shift; | ||
| 795 | my $agent = $self->agent; | ||
| 796 | my ($exp,$lookup_sources) = @_; | ||
| 797 | my $exports = {}; | ||
| 798 | my $first = 1; | ||
| 799 | for my $e (@$exp) { | ||
| 800 | my ($package,$version) = $self->_split_package($e); | ||
| 801 | my $e = {}; | ||
| 802 | $e->{version} = $version; | ||
| 803 | if ($lookup_sources) { | ||
| 804 | $e->{using} = $agent->importing_bundles($package,$version,use_cached => $first ? undef : 1); | ||
| 805 | $first = 0; | ||
| 806 | } | ||
| 807 | $exports->{$package} = $e; | ||
| 808 | } | ||
| 809 | return $exports; | ||
| 810 | } | ||
| 811 | |||
| 812 | sub _add_imp_header_info { | ||
| 813 | my $self = shift; | ||
| 814 | my $e = shift; | ||
| 815 | my $imp = shift; | ||
| 816 | my $attrs = $imp->{attributes}; | ||
| 817 | my $dirs = $imp->{directives}; | ||
| 818 | ($e->{optional} = 1) && delete $dirs->{resolution} if $dirs->{resolution} eq "optional"; | ||
| 819 | $e->{version_spec} = delete $attrs->{version} if $attrs->{version}; | ||
| 820 | $e->{directives} = $dirs if %{$dirs}; | ||
| 821 | $e->{attributes} = $attrs if %{$dirs}; | ||
| 822 | return $e; | ||
| 823 | } | ||
| 824 | |||
| 825 | sub _bundle_state_color { | ||
| 826 | my $self = shift; | ||
| 827 | my $bu = shift; | ||
| 828 | my $osgish = $self->osgish; | ||
| 829 | my $c_name = $BUNDLE_STATE_COLOR{lc($bu->{State})}; | ||
| 830 | return "" unless $c_name; | ||
| 831 | return ($osgish->color($c_name))[0]; | ||
| 832 | } | ||
| 833 | |||
| 834 | sub _state_info { | ||
| 835 | my $self = shift; | ||
| 836 | my $bu = shift; | ||
| 837 | my $state = lc $bu->{State}; | ||
| 838 | return uc(substr($state,0,1)) . substr($state,1); | ||
| 839 | } | ||
| 840 | |||
| 841 | sub _split_package { | ||
| 842 | my $self = shift; | ||
| 843 | return split /;/,shift,2; | ||
| 844 | } | ||
| 845 | |||
| 846 | sub _split_property { | ||
| 847 | my $self = shift; | ||
| 848 | my $prop = shift; | ||
| 849 | my $l = $prop; | ||
| 850 | my $ret = {}; | ||
| 851 | while ($l) { | ||
| 852 | $l =~ s/([^,]*?("[^"]+"[^,]*)*)(\s*,\s*|$)//; | ||
| 853 | my $part = $1; | ||
| 854 | my @targets = (); | ||
| 855 | my $attrs = {}; | ||
| 856 | my $directives = {}; | ||
| 857 | while ($part) { | ||
| 858 | $part =~ s/([^;]*?("[^"]+"[^;]*)*)(\s*;\s*|$)//; | ||
| 859 | my $sub = $1; | ||
| 860 | if ($sub =~ /^(.*):=\"?(.*?)\"?$/) { | ||
| 861 | $directives->{$1} = $2; | ||
| 862 | } elsif ($sub =~ /^(.*)=\"?(.*?)\"?$/) { | ||
| 863 | $attrs->{$1} = $2; | ||
| 864 | } else { | ||
| 865 | push @targets,$sub; | ||
| 866 | } | ||
| 867 | for my $t (@targets) { | ||
| 868 | $ret->{$t} = { }; | ||
| 869 | $ret->{$t}->{attributes} = $attrs if $attrs; | ||
| 870 | $ret->{$t}->{directives} = $directives if $directives; | ||
| 871 | } | ||
| 872 | } | ||
| 873 | } | ||
| 874 | return $ret; | ||
| 875 | } | ||
| 876 | |||
| 877 | # Filter bundles according to some criteria | ||
| 878 | sub _filter_symbolic_names { | ||
| 879 | my $self = shift; | ||
| 880 | my $agent = $self->agent; | ||
| 881 | my $filtered_bundles = [map { $_->{SymbolicName} || $_->{Identifier} } @{$self->_filter_bundles($agent->bundles,@_)} ]; | ||
| 882 | die "No bundle given\n" unless @$filtered_bundles; | ||
| 883 | return $filtered_bundles; | ||
| 884 | } | ||
| 885 | |||
| 886 | |||
| 887 | sub _filter_bundles { | ||
| 888 | my $self = shift; | ||
| 889 | my ($bundles,@filters) = @_; | ||
| 890 | |||
| 891 | if (@filters) { | ||
| 892 | my %filtered_bundles; | ||
| 893 | for my $f (@filters) { | ||
| 894 | my $regexp = $self->convert_wildcard_pattern_to_regexp($f); | ||
| 895 | for my $b (values %$bundles) { | ||
| 896 | if ($b->{SymbolicName} =~ $regexp || ($f =~ /^\d+$/ && $b->{Identifier} == $f)) { | ||
| 897 | $filtered_bundles{$b->{Identifier}} = $b; | ||
| 898 | } | ||
| 899 | } | ||
| 900 | } | ||
| 901 | return [values %filtered_bundles]; | ||
| 902 | } else { | ||
| 903 | return [values %$bundles]; | ||
| 904 | } | ||
| 905 | } | ||
| 906 | |||
| 907 | =back | ||
| 908 | |||
| 909 | =cut | ||
| 910 | |||
| 911 | |||
| 912 | =head1 LICENSE | ||
| 913 | |||
| 914 | This file is part of osgish. | ||
| 915 | |||
| 916 | Osgish is free software: you can redistribute it and/or modify | ||
| 917 | it under the terms of the GNU General Public License as published by | ||
| 918 | the Free Software Foundation, either version 2 of the License, or | ||
| 919 | (at your option) any later version. | ||
| 920 | |||
| 921 | osgish is distributed in the hope that it will be useful, | ||
| 922 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 923 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 924 | GNU General Public License for more details. | ||
| 925 | |||
| 926 | You should have received a copy of the GNU General Public License | ||
| 927 | along with osgish. If not, see <http://www.gnu.org/licenses/>. | ||
| 928 | |||
| 929 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 930 | further details. | ||
| 931 | |||
| 932 | =head1 PROFESSIONAL SERVICES | ||
| 933 | |||
| 934 | Just in case you need professional support for this module (or JMX or OSGi in | ||
| 935 | general), you might want to have a look at www.consol.com Contact | ||
| 936 | roland.huss@consol.de for further information (or use the contact form at | ||
| 937 | http://www.consol.com/contact/) | ||
| 938 | |||
| 939 | =head1 AUTHOR | ||
| 940 | |||
| 941 | roland@cpan.org | ||
| 942 | |||
| 943 | =cut | ||
| 944 | |||
| 945 | |||
| 946 | |||
| 947 | 1; | ||
| 948 |