/usr/share/perl5/Lire/PluginManager.pm is in lire 2:2.1.1-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | package Lire::PluginManager;
use strict;
use vars qw/ $instance %plugin_types /;
use Carp;
use Lire::Config;
use Lire::Error qw/ file_not_readable /;
use Lire::OldDlfAdapter;
use Lire::Utils qw/ file_content check_param check_object_param /;
$instance = new Lire::PluginManager();
%plugin_types = ( 'output_format' => 'Lire::OutputFormat',
'chart_type' => 'Lire::ChartType',
'dlf_analyser' => 'Lire::DlfAnalyser',
'dlf_converter' => 'Lire::DlfConverter', );
sub new {
return bless { 'output_format' => {},
'dlf_analyser' => {},
'dlf_converter' => {},
'chart_type' => {},
}, shift;
}
=pod
=head1 NAME
Lire::PluginManager - Manages the Lire plugins.
=head1 SYNOPSIS
use Lire::PluginManager;
Lire::PluginManager->register_default_plugins();
my $plugin = new MyDlfConverter();
Lire::PluginManager->register_plugin( $plufin );
my $plugins = Lire::PluginManager->plugins( 'dlf_analyser' );
my $converter = Lire::PluginManager->get_plugin('dlf_analyser', 'combined');
=head1 DESCRIPTION
All method can be called as class method on the Lire::PlugingManager
module or on an instance.
=head2 PLUGIN TYPES
There are four kind of plugins defined in Lire: dlf_analyser,
dlf_converter, chart_type and output_format.
=head2 instance()
Returns the singleton instance of the PluginManager
=cut
sub instance {
return $instance;
}
sub check_type {
my ( $type ) = @_;
check_param( $type, 'type' );
return 1 if exists $plugin_types{$type};
my @types = map { "'$_'" } sort keys %plugin_types;
croak "'type' parameter should be one of " . join ( ", ", @types[0..$#types-1] ) . " or $types[-1]: '$type'";
}
=pod
=head2 plugin_names( $type )
Returns an array reference containing the names of all registered
plugin of type $type.
=cut
sub plugin_names {
my ( $self, $type ) = @_;
$self = $instance unless ref $self;
check_type( $type );
return [ keys %{$self->{$type}} ];
}
=pod
=head2 plugins( $type )
Returns an array reference containing all the Plugin objects registerd
for $type.
=cut
sub plugins {
my ( $self, $type ) = @_;
$self = $instance unless ref $self;
check_type( $type );
return [ values %{$self->{$type}} ];
}
=pod
=head2 has_plugin( $type, $name )
Returns true if there is a Lire::Plugin named $name registered for type
$type.
=cut
sub has_plugin {
my ( $self, $type, $name ) = @_;
$self = $instance unless ref $self;
check_param( $name, 'name' );
check_type( $type );
return exists $self->{$type}{$name};
}
=pod
=head2 get_plugin( $type, $name )
Returns the Lire::Plugin object named $name of type $type. Croaks when
there is no plugin registerd under that name.
=cut
sub get_plugin {
my ( $self, $type, $name ) = @_;
$self = $instance unless ref $self;
check_param( $name, 'name' );
check_type( $type );
croak "no '$type' plugin '$name' registered"
unless $self->has_plugin( $type, $name );
return $self->{$type}{$name};
}
=pod
=head2 register_plugin( $plugin )
Registers the Lire::Plugin $plugin. The type and name of the plugin
are queried using the Lire::Plugin interface. An error will be thrown
if there is already a plugin registered under the same name and type.
=cut
sub register_plugin {
my ( $self, $plugin ) = @_;
$self = $instance unless ref $self;
check_object_param( $plugin, 'plugin', 'Lire::Plugin', );
my $type = $plugin->type();
croak( "plugin '", $plugin->name(), "' has an unknown type: '$type'" )
unless exists $plugin_types{$type};
croak( "'$type' plugin should be a '$plugin_types{$type}' instance: '",
ref($plugin), "'" )
unless UNIVERSAL::isa( $plugin, $plugin_types{$type} );
croak( "there is already a '$type' plugin registered under name '",
$plugin->name(), "'" )
if $self->has_plugin( $type, $plugin->name() );
$self->{$type}{$plugin->name()} = $plugin;
return;
}
=pod
=head2 unregister_plugin( $type, $name )
Unregister the plugin $name of type $type. The method will dies if
there is no such plugin.
=cut
sub unregister_plugin {
my ( $self, $type, $name) = @_;
$self = $instance unless ref $self;
check_param( $name, 'name' );
check_type( $type );
croak "no '$type' plugin '$name' registered"
unless $self->has_plugin( $type, $name );
delete $self->{$type}{$name};
return;
}
=pod
=head2 analysers_by_src( $schema )
Returns in an array reference all the analysers that declare $schema
as their src_schema().
=cut
sub analysers_by_src {
my ( $self, $schema ) = @_;
check_param( $schema, 'schema' );
my @result = ();
foreach my $analyser ( @{$self->plugins( 'dlf_analyser' )} ) {
push @result, $analyser
if $analyser->src_schema() eq $schema;
}
return \@result;
}
=pod
=head2 analysers_by_dst( $schema )
Returns in an array reference all the analysers that declare $schema
as their dst_schema().
=cut
sub analysers_by_dst {
my ( $self, $schema ) = @_;
check_param( $schema, 'schema' );
my @result = ();
foreach my $analyser ( @{$self->plugins( 'dlf_analyser' )} ) {
push @result, $analyser
if $analyser->dst_schema() eq $schema;
}
return \@result;
}
=pod
=head2 register_default_plugins()
This method will load and initialize all the plugins available. The
plugins are registered by executing all the perl scripts found in the
directories listed in the 'plugins_init_path' configuration variable.
These scripts should call Lire::PluginManager->register_plugin() to
register the plugins.
For compatibility with previous versions of Lire, it will also
registers the DlfConverters initialized using older mechanism.
=cut
sub register_default_plugins {
my $self = $_[0];
$self = $instance unless ref $self;
my $init_files =
$self->_init_files( Lire::Config->get( 'plugins_init_path' ) );
foreach my $init_file ( @$init_files ) {
eval file_content( $init_file );
warn( "error in initialiser '$init_file': $@\n" )
if $@;
}
$self->_create_old_dlf_adapters();
$self->_load_dlf_adapters();
return;
}
sub _create_old_dlf_adapters {
my $self = $_[0];
my $convertors_dir = Lire::Config->get( 'lr_old_convertors_dir' );
my $address_file = Lire::Config->get( 'lr_old_address_file' );
my $service2schema = $self->_parse_old_map_file( $address_file );
while ( my ($service, $schema) = each %$service2schema ) {
unless ( Lire::DlfSchema->has_superservice( $schema )) {
warn "invalid superservice '$schema' assigned to service '$service'\n";
next;
}
my $script = $convertors_dir .'/' . $service . '2dlf';
if ( -x $script ) {
my $adapter = new Lire::OldDlfAdapter( $schema, $script );
$self->register_plugin( $adapter );
} else {
warn "can't find executable $service" . "2dlf in $convertors_dir\n";
}
}
}
sub _parse_old_map_file {
my ($self, $file) = @_;
my %map = ();
open my $fh, $file
or croak( file_not_readable( $file ) );
my $line;
while ( defined($line = <$fh>) ) {
next if $line =~ /^#/; # Skip comments
next if $line =~ /^\s*$/; # Skip empty lines
my ($key, $value) = $line =~ /^(\S+)\s+(\S+)\s*(#.*)?$/
or warn "can't parse line $. of file '$file'\n";
$map{$key} = $value
if defined $key;
}
close $fh;
return \%map;
}
sub _load_dlf_adapters {
my $self = $_[0];
my $init_files =
$self->_init_files( Lire::Config->get( 'lr_converters_init_path' ) );
foreach my $init_file ( @$init_files )
{
my $initializer = eval { file_content( $init_file ) };
if ( $@ ) {
warn "error reading DLF converter initializer file '$init_file': $@\n";
next;
}
my @converters = eval $initializer;
if ( $@ ) {
warn "error while running initializer in '$init_file': $@\n";
next;
}
foreach my $c ( @converters ) {
if ( UNIVERSAL::isa( $c, 'Lire::DlfConverter' ) ) {
$self->register_plugin( $c );
} else {
warn "initializaer '$init_file' didn't return a Lire::DlfConverter object: $c\n";
}
}
}
}
sub _init_files {
my ( $self, $dirs ) = @_;
my @initializers = ();
my %dirs;
foreach my $dir ( @$dirs ) {
next if exists $dirs{$dir};
$dirs{$dir} = 'done';
opendir my $dh, $dir
or croak "opendir failed '$dir': $!";
foreach my $file ( map { "$dir/$_" } readdir $dh ) {
next unless -f $file;
push @initializers, $file;
}
closedir $dh;
}
return \@initializers;
}
# keep perl happy
1;
__END__
=pod
=head1 SEE ALSO
Lire::Plugin(3pm), Lire::OutputFormat(3pm), Lire::DlfAnalyser(3pm),
Lire::DlfConverter(3pm)
=head1 AUTHOR
Francis J. Lacoste <flacoste@logreport.org>
=head1 VERSION
$Id: PluginManager.pm,v 1.8 2006/07/23 13:16:29 vanbaal Exp $
=head1 COPYRIGHT
Copyright (C) 2004 Stichting LogReport Foundation LogReport@LogReport.org
This file is part of Lire.
Lire is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html.
=cut
|