| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package OSGi::Osgish::Command::Service; | ||
| 4 | use strict; | ||
| 5 | use vars qw(@ISA); | ||
| 6 | use Term::ANSIColor qw(:constants); | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | @ISA = qw(OSGi::Osgish::Command); | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | OSGi::Osgish::Command::Service - Service related commands | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | =head1 COMMANDS | ||
| 18 | |||
| 19 | =over | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | sub name { "service" } | ||
| 26 | |||
| 27 | sub top_commands { | ||
| 28 | my $self = shift; | ||
| 29 | return $self->agent ? $self->commands : {}; | ||
| 30 | } | ||
| 31 | |||
| 32 | # Commands in context "service" | ||
| 33 | sub commands { | ||
| 34 | my $self = shift; | ||
| 35 | my $cmds = $self->sub_commands; | ||
| 36 | return { | ||
| 37 | "service" => { | ||
| 38 | desc => "Service related operations", | ||
| 39 | proc => $self->push_on_stack("service",$cmds), | ||
| 40 | cmds => $cmds, | ||
| 41 | doc => <<EOT | ||
| 42 | Enter the service menu. | ||
| 43 | EOT | ||
| 44 | }, | ||
| 45 | "s" => { alias => "service", exclude_from_completion => 1}, | ||
| 46 | "serv" => { alias => "service", exclude_from_completion => 1} | ||
| 47 | }; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub sub_commands { | ||
| 51 | my $self = shift; | ||
| 52 | return | ||
| 53 | { | ||
| 54 | "ls" => { | ||
| 55 | desc => "List all services", | ||
| 56 | proc => $self->cmd_list, | ||
| 57 | args => $self->complete->services(no_ids => 1), | ||
| 58 | doc => <<EOT | ||
| 59 | |||
| 60 | ls [-u <using>] [-b <bundle>] [<service id>|<object class>] | ||
| 61 | |||
| 62 | List all services or, when a single service id is given, print | ||
| 63 | details of this service. As argument a object class can be given | ||
| 64 | as well (with optional wildcards). | ||
| 65 | |||
| 66 | Options: | ||
| 67 | |||
| 68 | -u <using> : List all services which are used by bundle <using> | ||
| 69 | -b <bundle> : List all services provided by bundle <bundle> | ||
| 70 | EOT | ||
| 71 | }, | ||
| 72 | # "bls" => { | ||
| 73 | # desc => "List bundles", | ||
| 74 | # proc => \&cmd_bundle_list, | ||
| 75 | # args => sub { &complete_bundles(@_,no_ids => 1) } | ||
| 76 | # }, | ||
| 77 | }; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | # =================================================================================================== | ||
| 82 | |||
| 83 | sub cmd_list { | ||
| 84 | my $self = shift; | ||
| 85 | return sub { | ||
| 86 | my $osgish = $self->osgish; | ||
| 87 | my $osgi = $osgish->agent; | ||
| 88 | print "Not connected to a server\n" and return unless $osgi; | ||
| 89 | my $services = $osgi->services; | ||
| 90 | my ($opts,@filters) = $self->extract_command_options(["u=s","b=s"],@_); | ||
| 91 | |||
| 92 | my $filtered_services = $self->_filter_services($services,$opts,@filters); | ||
| 93 | return unless @$filtered_services; | ||
| 94 | if (@$filtered_services == 1) { | ||
| 95 | $self->print_service_info($filtered_services->[0],$opts); | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | my $text = sprintf("%4.4s %-62.62s %5.5s | %s\n","Id","Classes","Bd-Id","Using bundles"); | ||
| 99 | $text .= "-" x 76 . "+" . "-" x 24 . "\n"; | ||
| 100 | my $nr = 0; | ||
| 101 | for my $s (sort { $a->{Identifier} <=> $b->{Identifier} } @{$filtered_services}) { | ||
| 102 | my $id = $s->{Identifier}; | ||
| 103 | my ($c_id,$c_interf,$c_using,$r) = $osgish->color("service_id","service_interface","bundle_id",RESET); | ||
| 104 | my $using_bundles = $s->{UsingBundles} || []; | ||
| 105 | my $using = $using_bundles ? join (", ",sort { $a <=> $b } @$using_bundles) : ""; | ||
| 106 | my $bundle_id = $s->{BundleIdentifier}; | ||
| 107 | my $classes = $s->{objectClass}; | ||
| 108 | $text .= sprintf "%s%4d%s %s%-65.65s%s %s%3d%s | %s\n",$c_id,$id,$r,$c_interf,$self->trim_string($classes->[0],65),$r,$c_using,$bundle_id,$r,$using; | ||
| 109 | for my $i (1 .. $#$classes) { | ||
| 110 | $text .= sprintf " %s%-69.69s%s |\n",$c_interf,$self->trim_string($classes->[$i],69),$r; | ||
| 111 | } | ||
| 112 | $nr++; | ||
| 113 | } | ||
| 114 | $self->print_paged($text,$nr); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | sub print_service_info { | ||
| 119 | my $self = shift; | ||
| 120 | my $service = shift; | ||
| 121 | my $opts = shift; | ||
| 122 | my $agent = $self->agent; | ||
| 123 | my $osgish = $self->osgish; | ||
| 124 | |||
| 125 | my $id = $service->{Identifier}; | ||
| 126 | my $bundle_id = $service->{BundleIdentifier}; | ||
| 127 | my $bundle_using = $service->{UsingBundles}; | ||
| 128 | my $classes = $service->{objectClass}; | ||
| 129 | |||
| 130 | my ($c_id,$c_class,$c_bid,$c_bversion,$c_prop_val,$c_prop_key,$c_reset) = | ||
| 131 | $osgish->color("service_id","service_interface","bundle_id","bundle_version","header_value","header_name",RESET); | ||
| 132 | |||
| 133 | my %props = | ||
| 134 | map { $_->{Key} => { val => $_->{Value}, type => $_->{Type} } } | ||
| 135 | grep { $_->{Key} !~ /^(objectClass|service\.id)$/ } | ||
| 136 | values %{$service->{Properties}}; | ||
| 137 | my $ret = ""; | ||
| 138 | #$ret .= sprintf("%-14.14s %s\n","Id:",$c_id . $id . $c_reset); | ||
| 139 | $ret .= sprintf("%-14.14s %s (%s)\n","Bundle:",$c_bid . $agent->bundle_name($bundle_id,use_cached => 1) . $c_reset,$bundle_id); | ||
| 140 | if ($bundle_using && @$bundle_using) { | ||
| 141 | my $label = "Used by:"; | ||
| 142 | for my $u (@$bundle_using) { | ||
| 143 | $ret .= sprintf("%-14.14s %s (%s)\n",$label,$c_bid . $agent->bundle_name($u,use_cached => 1) . $c_reset,$u); | ||
| 144 | $label = ""; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | if ($classes && @$classes) { | ||
| 148 | my $label = "Classes:"; | ||
| 149 | for my $c (sort @$classes) { | ||
| 150 | $ret .= sprintf("%-14.14s %s\n",$label,$c_class . $c . $c_reset); | ||
| 151 | $label = ""; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | if (%props) { | ||
| 155 | my $label = "Properties:"; | ||
| 156 | for my $k (sort keys %props) { | ||
| 157 | $ret .= sprintf("%-14.14s %s = %s\n",$label,$c_prop_key . $k . $c_reset,$c_prop_val . $props{$k}->{val} . $c_reset); | ||
| 158 | $label = ""; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | $self->print_paged($ret); | ||
| 162 | } | ||
| 163 | |||
| 164 | # Filter services according to one or more criteria | ||
| 165 | sub _filter_services { | ||
| 166 | my $self = shift; | ||
| 167 | my ($services,$opts,@filters) = @_; | ||
| 168 | my %found = (); | ||
| 169 | my $rest = [values %$services]; | ||
| 170 | my $filtered = undef; | ||
| 171 | if (defined($opts->{u})) { | ||
| 172 | die "No numeric bundle-id ",$opts->{u} unless $opts->{u} =~ /^\d+$/; | ||
| 173 | for my $s (@$rest) { | ||
| 174 | if (grep { $_ == $opts->{u} } @{$s->{UsingBundles}}) { | ||
| 175 | $found{$s->{Identifier}} = $s; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | $filtered = 1; | ||
| 179 | $rest = [values %found]; | ||
| 180 | } | ||
| 181 | if ($opts->{b}) { | ||
| 182 | die "No numeric bundle-id ",$opts->{b} unless $opts->{b} =~ /^\d+$/; | ||
| 183 | for my $s (@$rest) { | ||
| 184 | if ($s->{BundleIdentifier} == $opts->{b}) { | ||
| 185 | $found{$s->{Identifier}} = $s; | ||
| 186 | } elsif ($filtered) { | ||
| 187 | delete $found{$s->{Identifier}}; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | $filtered = 1; | ||
| 191 | $rest = [values %found]; | ||
| 192 | } | ||
| 193 | if (@filters) { | ||
| 194 | for my $f (@filters) { | ||
| 195 | my $regexp = $self->convert_wildcard_pattern_to_regexp($f); | ||
| 196 | for my $s (@$rest) { | ||
| 197 | if ( (grep { $_ =~ $regexp } @{$s->{objectClass}}) || | ||
| 198 | ($f =~ /^\d+$/ && $s->{Identifier} == $f)) { | ||
| 199 | $found{$s->{Identifier}} = $s; | ||
| 200 | } elsif ($filtered) { | ||
| 201 | delete $found{$s->{Identifier}}; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | $filtered = 1; | ||
| 206 | $rest = [values %found]; | ||
| 207 | } | ||
| 208 | return $rest; | ||
| 209 | } | ||
| 210 | |||
| 211 | =head1 LICENSE | ||
| 212 | |||
| 213 | This file is part of osgish. | ||
| 214 | |||
| 215 | Osgish is free software: you can redistribute it and/or modify | ||
| 216 | it under the terms of the GNU General Public License as published by | ||
| 217 | the Free Software Foundation, either version 2 of the License, or | ||
| 218 | (at your option) any later version. | ||
| 219 | |||
| 220 | osgish is distributed in the hope that it will be useful, | ||
| 221 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 222 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 223 | GNU General Public License for more details. | ||
| 224 | |||
| 225 | You should have received a copy of the GNU General Public License | ||
| 226 | along with osgish. If not, see <http://www.gnu.org/licenses/>. | ||
| 227 | |||
| 228 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 229 | further details. | ||
| 230 | |||
| 231 | =head1 PROFESSIONAL SERVICES | ||
| 232 | |||
| 233 | Just in case you need professional support for this module (or JMX or OSGi in | ||
| 234 | general), you might want to have a look at www.consol.com Contact | ||
| 235 | roland.huss@consol.de for further information (or use the contact form at | ||
| 236 | http://www.consol.com/contact/) | ||
| 237 | |||
| 238 | =head1 AUTHOR | ||
| 239 | |||
| 240 | roland@cpan.org | ||
| 241 | |||
| 242 | =cut | ||
| 243 | |||
| 244 | 1; | ||
| 245 |