Osgish/Agent.pm untested

filter: Statementstmt 0.0% 0 / 268 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 51 Podpod 0.0% 0 / 46 totaltotal 0.0% 0 / 493 CC 33 SCAR 22.6
CC
33
Cov
0%
_prepare_error_message
30.0
_id_or_symbolic_name
20.0
_do_execute
20.0
CRAP 9.6
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 OSGi::Osgish::Agent - Access to the OSGi agent bundle
6  
7 =head1 SYNOPSIS
8  
9 =head1 DESCRIPTION
10  
11 =cut
12 package OSGi::Osgish::Agent;
13  
14 use strict;
15 use JMX::Jmx4Perl;
16 use JMX::Jmx4Perl::Request;
17 use OSGi::Osgish::Agent::Upload;
18 use Data::Dumper;
19  
20 # Osgi EEG MBeans
21 my $MBEANS_MAP =
22 {
23 "framework" => { key => "type", version => "1.5", domain => "osgi.core" },
24 "bundleState" => { key => "type", version => "1.5", domain => "osgi.core" },
25 "serviceState" => { key => "type", version => "1.5", domain => "osgi.core" },
26 "packageState" => { key => "type", version => "1.5", domain => "osgi.core" },
27 "permissionadmin" => { key => "service", version => "1.2", domain => "osgi.core" },
28 "cm" => { key => "service", version => "1.3", domain => "osgi.compendium" },
29 "provisioning" => { key => "service", version => "1.2", domain => "osgi.compendium" },
30 "useradmin" => { key => "service", version => "1.1", domain => "osgi.compendium" }
31 };
32  
33 # Our own MBean for certain state checks
34 my $OSGISH_SERVICE_NAME = "osgish:type=Service";
35  
36 sub new {
37 my $class = shift;
38 my $cfg = ref($_[0]) eq "HASH" ? $_[0] : { @_ };
39
40 my $jmx4perl = new JMX::Jmx4Perl($cfg);
41 my $upload = new OSGi::Osgish::Agent::Upload($jmx4perl);
42 my $self = {
43 j4p => $jmx4perl,
44 upload => $upload,
45 cfg => $cfg,
46 };
47 bless $self,(ref($class) || $class);
48 return $self;
49 }
50  
51 sub cfg {
52 my $self = shift;
53 my $key = shift || return $self->{cfg};
54 my $val = shift;
55 my $ret = $self->{cfg}->{$key};
56 if (defined $val) {
57 $self->{cfg}->{$key} = $val;
58 }
59 return $ret;
60 }
61  
62 sub upload {
63 return shift->{upload};
64 }
65  
66 sub url {
67 my $self = shift;
68 my $j4p = $self->{j4p};
69 return $j4p->url;
70 }
71  
72 sub init {
73 my $self = shift;
74 my $old_bundle = delete $self->{bundle};
75 my $old_service = delete $self->{service};
76 eval {
77 $self->_fetch_bundles;
78 $self->_fetch_services;
79 };
80 if ($@) {
81 $self->{bundle} = $old_bundle;
82 $self->{service} = $old_service;
83 die $@;
84 }
85 }
86  
87 sub bundles {
88 my $self = shift;
89 $self->_update_bundles(@_);
90 return $self->{bundle}->{list};
91 }
92  
93 sub services {
94 my $self = shift;
95 $self->_update_services(@_);
96 return $self->{service}->{list};
97 }
98  
99 sub packages {
100 my $self = shift;
101 $self->_update_packages(@_);
102 return $self->{package}->{list};
103 }
104  
105 # Return a hashref with symbolic names as keys
106 # and the ids as values
107 sub bundle_symbolic_names {
108 my $self = shift;
109 $self->_update_bundles(@_);
110 return $self->{bundle}->{symbolic_names};
111 }
112  
113 sub bundle_ids {
114 my $self = shift;
115 $self->_update_bundles(@_);
116 return [ sort keys %{$self->{bundle}->{ids}} ];
117 }
118  
119 sub bundle_name {
120 my $self = shift;
121 my $id = shift;
122 $self->_update_bundles(@_);
123 return $self->{bundle}->{ids}->{$id};
124 }
125  
126 sub service_object_classes {
127 my $self = shift;
128 $self->_update_services(@_);
129 return $self->{service}->{object_classes};
130 }
131  
132 sub service_ids {
133 my $self = shift;
134 $self->_update_services(@_);
135 return [sort keys %{$self->{service}->{ids}}];
136 }
137  
138 sub service {
139 my $self = shift;
140 my $id = shift;
141 $self->_update_services(@_);
142 return $self->{service}->{ids}->{$id};
143 }
144  
145 sub resolve_bundle {
146 my $self = shift;
147 $self->_update_bundles();
148 $self->_bulk_bundle_cmd("resolveBundle","resolveBundles",@_);
149 }
150  
151  
152 sub start_bundle {
153 my $self = shift;
154 $self->_update_bundles();
155 $self->_bulk_bundle_cmd("startBundle","startBundles",@_);
156 }
157  
158 sub stop_bundle {
159 my $self = shift;
160 $self->_update_bundles();
161 $self->_bulk_bundle_cmd("stopBundle","stopBundles",@_);
162 }
163  
164 sub uninstall_bundle {
165 my $self = shift;
166 $self->_update_bundles();
167 $self->_bulk_bundle_cmd("uninstallBundle","uninstallBundles",@_);
168 }
169  
170 sub refresh_bundle {
171 my $self = shift;
172 $self->_update_bundles();
173 $self->_bulk_bundle_cmd("refreshBundle","refreshBundles",@_);
174 }
175  
176 sub _bulk_bundle_cmd {
177 my $self = shift;
178 my $what_single = shift || die "Internal: No single type give\n";
179 my $what_multi = shift || die "Internal: No multi type give\n";
180 my @ids = ();
181 for my $i (@_) {
182 push @ids,$self->_id_or_symbolic_name($i);
183 }
184 die "No id given\n" unless @ids;
185 if (@ids > 1) {
186 return $self->execute($self->_mbean_name("framework"),$what_multi,\@ids);
187 } else {
188 $self->execute($self->_mbean_name("framework"),$what_single,$ids[0]);
189 return $ids[0];
190 }
191 }
192  
193 sub install_bundle {
194 my $self = shift;
195 my @locations = @_;
196 my $mbean = $self->_mbean_name("framework");
197 if (@locations > 1) {
198 # Must be posted due to the ";" sign, which, even when escaped,
199 # confuses e.g. Glassfish when part of an GET Url
200 $self->execute_post($mbean,"installBundles([Ljava.lang.String;)",\@locations);
201 } else {
202 $self->execute($mbean,"installBundle(java.lang.String)",$locations[0]);
203 }
204 }
205  
206  
207 sub update_bundle {
208 my $self = shift;
209 my $id = $self->_id_or_symbolic_name(shift);
210 my $location = shift;
211 if ($location) {
212 $self->execute($self->_mbean_name("framework"),"updateBundle(long,java.lang.String)",$id,$location);
213 } else {
214 $self->execute($self->_mbean_name("framework"),"updateBundle(long)",$id);
215 }
216 return $id;
217 }
218  
219 # Return values
220 sub update_bundles {
221 my $self = shift;
222 my @ids;
223 for my $i (@_) {
224 push @ids,$self->_id_or_symbolic_name($i);
225 }
226 if (@ids > 1) {
227 return $self->execute($self->_mbean_name("framework"),"updateBundles([J)",\@ids);
228 } else {
229 return $self->execute($self->_mbean_name("framework"),"updateBundle(long)",$ids[0]);
230 }
231 }
232  
233 sub _id_or_symbolic_name {
234 my $self = shift;
235 my $id = shift;
236 my $ret = $id =~ /^\d+$/ ? $id : $self->{bundle}->{symbolic_names}->{$id};
237 die "Cannot find bundle '$id': Not an id nor a symbolic name\n" unless ($ret);
238 die "No bundle with id $id\n" unless $self->{bundle}->{ids}->{$ret};
239 return $ret;
240 }
241  
242 sub shutdown {
243 my $self = shift;
244 $self->execute($self->_mbean_name("framework"),"shutdownFramework");
245 }
246  
247 sub restart {
248 my $self = shift;
249 $self->execute($self->_mbean_name("framework"),"restartFramework");
250 }
251  
252  
253 sub execute {
254 my $self = shift;
255 my $mbean = shift || die "No MBean name given";
256 my $operation = shift || die "No operation given for MBean $mbean";
257 my @args = @_;
258  
259 return $self->_do_execute(new JMX::Jmx4Perl::Request(EXEC,$mbean,$operation,@args));
260 }
261  
262 sub execute_post {
263 my $self = shift;
264 my $mbean = shift || die "No MBean name given";
265 my $operation = shift || die "No operation given for MBean $mbean";
266 my @args = @_;
267  
268 return $self->_do_execute(new JMX::Jmx4Perl::Request(EXEC,$mbean,$operation,@args,{method => "POST"}));
269 }
270  
271 sub _do_execute {
272 my $self = shift;
273 my $request = shift;
274  
275 my $j4p = $self->{j4p};
276  
277 my $response = $j4p->request($request);
278 if ($response->is_error) {
279 #print Dumper($response);
280 if ($response->status == 404) {
281 die "No agent running [Not found: ",$request->{mbean},",",$request->{operation},"].\n"
282 } else {
283 $self->{last_error} = $response->{error} .
284 ($response->stacktrace ? "\nStacktrace:\n" . $response->stacktrace : "");
285 die $self->_prepare_error_message($response) . ".\n";
286 }
287 }
288 return $response->value;
289 }
290  
291 sub _prepare_error_message {
292 my $self = shift;
293 my $resp = shift;
294 my $st = $resp->stacktrace;
295 return "Connection refused" if $resp->{error} =~ /Connection\s+refused/i;
296  
297 if ($st) {
298 if ($st =~ /BundleException:\s*([^\n]+)\.?/s) {
299 my $txt = $1;
300 chop $txt while $txt =~ /\.$/;
301 return $txt;
302 }
303 }
304 if ($resp->{error} =~ /^(\d{3} [^\n]+)\n/m) {
305 return $1;
306 }
307 return "Server Error: " . $resp->{error};
308 }
309  
310 sub last_error {
311 my $self = shift;
312 return $self->{last_error};
313 }
314  
315 sub _mbean_name {
316 my $self = shift;
317 my $short_name = shift;
318
319 my $d = $MBEANS_MAP->{$short_name} || die "No MBean defined for shortname $short_name";
320 return $d->{domain} . ":" . $d->{key} . "=$short_name,version=" . $d->{version};
321 }
322  
323 sub _update_services {
324 my $self = shift;
325 my $args = shift;
326 $args = { $args, @_ } unless ref($args) eq "HASH";
327 return if ($self->{service} && $args->{use_cached});
328  
329 # TODO: Update policy
330  
331 # Cache bundle list
332 if ($self->_server_state_changed("services",$self->{service}->{timestamp})) {
333 $self->_fetch_services;
334 }
335 }
336  
337 sub _update_bundles {
338 my $self = shift;
339 my $args = shift;
340
341 $args = { $args, @_ } unless ref($args) eq "HASH";
342
343 return if ($self->{bundle} && $args->{use_cached});
344 # TODO: Update policy
345  
346 # Cache bundle list
347 if ($self->_server_state_changed("bundles",$self->{bundle}->{timestamp})) {
348 $self->_fetch_bundles;
349 }
350 }
351  
352 sub _update_packages {
353 my $self = shift;
354 my $args = shift;
355
356 $args = { $args, @_ } unless ref($args) eq "HASH";
357 return if ($self->{package} && $args->{use_cached});
358 # TODO: Update policy
359 # Cache bundle list
360 if (!$self->{package} || $self->_server_state_changed("packages",$self->{bundle}->{timestamp})) {
361 #print "FETCH PKG\n";
362 $self->_fetch_packages;
363 }
364 }
365  
366  
367 sub _fetch_bundles {
368 my $self = shift;
369 my $bundle = $self->_fetch_list("bundleState","listBundles");
370 my $names = {};
371 my $ids = {};
372 my $bundles = $bundle->{list};
373 for my $e (keys %{$bundles}) {
374 my $b = $bundles->{$e};
375 my $sym = $b->{SymbolicName};
376 my $id = $b->{Identifier};
377 $names->{$sym} = $id if $sym;
378 $ids->{$id} = $sym || $b->{Name} || $b->{Location};
379 }
380 ($bundle->{symbolic_names},$bundle->{ids}) = ($names,$ids);
381 $self->{bundle} = $bundle;
382 }
383  
384 sub _fetch_services {
385 my $self = shift;
386 my $service = $self->_fetch_list("serviceState","listServices");
387 ($service->{object_classes},$service->{ids}) = $self->_extract_object_classes($service->{list});
388 $self->{service} = $service;
389 }
390  
391 sub _fetch_packages {
392 my $self = shift;
393 my $package = $self->_fetch_list("packageState","listPackages");
394 #print Dumper($package);
395 $package->{import_export} = $self->_extract_import_export($package->{list});
396 $self->{package} = $package;
397  
398 }
399  
400 sub exporting_bundles {
401 return shift->_exporting_importing_bundle("exporting",@_);
402 }
403  
404 sub importing_bundles {
405 return shift->_exporting_importing_bundle("importing",@_);
406 }
407  
408 sub _exporting_importing_bundle {
409 my $self = shift;
410 my $what = shift;
411 my $package = shift || die "No package given\n";
412 my $version = shift || die "No version given for $package\n";
413 $self->_update_packages(@_);
414 #print Dumper($self->{package}->{import_export});
415 return $self->{package}->{import_export}->{$package}->{$version}->{$what};
416 }
417  
418 sub _extract_import_export {
419 my $self = shift;
420 my $plist = shift;
421
422 my $ret = {};
423 #print Dumper($plist);
424 for my $e (values %{$plist}) {
425 for my $b (values %$e) {
426 for my $v (values %$b) {
427 die "Internal: No version found for ",$v->{Name},"\n" unless $v->{Version};
428 # We are using the chached bundle names here. Should be ok.
429 $ret->{$v->{Name}}->{$v->{Version}} =
430 {
431 importing => $self->_extract_unique_bundles($v->{ImportingBundles}),
432 exporting => $self->_extract_unique_bundles($v->{ExportingBundles})
433 };
434 }
435 }
436 }
437 return $ret;
438 }
439  
440 sub _extract_unique_bundles {
441 my $self = shift;
442 my $bundles = shift;
443 my $ret = {};
444 map {
445 $ret->{$_} = {
446 id => $_,
447 name => $self->{bundle}->{ids}->{$_}
448 }
449 } @$bundles;
450 return [ sort { $a->{id} <=> $b->{id} } values %$ret ];
451 }
452  
453 sub _fetch_list {
454 my $self = shift;
455 my ($mbean,$operation) = @_;
456 my $ret = {};
457 $ret->{list} = $self->execute($self->_mbean_name($mbean),$operation);
458 $ret->{timestamp} = time;
459 return $ret;
460 }
461  
462 sub _server_state_changed {
463 my $self = shift;
464 my $type = shift;
465 my $timestamp = shift;
466 my $state = $self->execute($OSGISH_SERVICE_NAME,"hasStateChanged",$type,$timestamp);
467 return $state eq "true" ? 1 : 0;
468 }
469  
470 sub _extract_object_classes {
471 my $self = shift;
472 my $services = shift;
473 my $cl = {};
474 my $ids = {};
475 for my $s (values %$services) {
476 my $classes = $s->{objectClass};
477 next unless $classes;
478 $classes = [ $classes ] unless ref($classes) eq "ARRAY";
479 my $id = $s->{Identifier};
480 map { $cl->{$_} = $id } @$classes;
481 $ids->{$id} = {
482 classes => $classes,
483 bundle => $s->{BundleIdentifier},
484 using => $s->{UsingBundles}
485 };
486 }
487 return ($cl,$ids);
488 }
489  
490 =head1 LICENSE
491  
492 This file is part of osgish.
493  
494 Osgish is free software: you can redistribute it and/or modify
495 it under the terms of the GNU General Public License as published by
496 the Free Software Foundation, either version 2 of the License, or
497 (at your option) any later version.
498  
499 osgish is distributed in the hope that it will be useful,
500 but WITHOUT ANY WARRANTY; without even the implied warranty of
501 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
502 GNU General Public License for more details.
503  
504 You should have received a copy of the GNU General Public License
505 along with osgish. If not, see <http://www.gnu.org/licenses/>.
506  
507 A commercial license is available as well. Please contact roland@cpan.org for
508 further details.
509  
510 =head1 PROFESSIONAL SERVICES
511  
512 Just in case you need professional support for this module (or JMX or OSGi in
513 general), you might want to have a look at www.consol.com Contact
514 roland.huss@consol.de for further information (or use the contact form at
515 http://www.consol.com/contact/)
516  
517 =head1 AUTHOR
518  
519 roland@cpan.org
520  
521 =cut
522  
523  
524  
525 1;
526