| 1 | # -*- mode: cperl; eval: (follow-mode) -*- | ||
| 2 | # | ||
| 3 | |||
| 4 | package App::Regather::Plugin; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | App::Regather::Plugin - plugin (module) loader | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | This module provides implementation of arbitrary plugin (module) | ||
| 13 | loader. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use diagnostics; | ||
| 20 | use feature 'state'; | ||
| 21 | |||
| 22 | use Carp; | ||
| 23 | use File::Basename; | ||
| 24 | use File::Spec; | ||
| 25 | |||
| 26 | =head1 CONSTRUCTOR | ||
| 27 | |||
| 28 | =head2 new($class, ...) | ||
| 29 | |||
| 30 | A command object fabric. Looks for a perl module for B<$class>, loads | ||
| 31 | it and returns an instance of that class. Surplus arguments (B<...>) | ||
| 32 | are passed as parameters to the underlying class constructor. | ||
| 33 | |||
| 34 | Input data for plugin is a hash: | ||
| 35 | |||
| 36 | cf reference to Config class object | ||
| 37 | force force all actions done on start | ||
| 38 | log reference to Logg class object | ||
| 39 | obj reference to Net::LDAP object | ||
| 40 | out_file_old ... looks like it is not needed any more ... | ||
| 41 | prog program name and version | ||
| 42 | rdn RDN of a LDAP object event relates to | ||
| 43 | s service | ||
| 44 | st syncrepl state | ||
| 45 | ts_fmt timestamp format | ||
| 46 | v verbosity | ||
| 47 | |||
| 48 | Each plugin should provide two methods | ||
| 49 | |||
| 50 | ldap_sync_add_modify and ldap_sync_delete | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my ($class, $command, @args) = @_; | ||
| 56 | croak __PACKAGE__ . ': command not supplied' unless $command; | ||
| 57 | my $modname = __PACKAGE__ . '::' . $command; | ||
| 58 | my $modpath = $modname; | ||
| 59 | $modpath =~ s{::}{/}g; | ||
| 60 | $modpath .= '.pm'; | ||
| 61 | my $cmd = eval { require $modpath; $modname->new(@args) }; | ||
| 62 | if ( $@ ) { | ||
| 63 | if ($@ =~ /Can't locate $modpath/) { | ||
| 64 | die __PACKAGE__ . ": unknown command: $command\n" | ||
| 65 | } | ||
| 66 | croak __PACKAGE__ . ': ERROR: ' . $@; | ||
| 67 | } | ||
| 68 | return $cmd; | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 names | ||
| 72 | |||
| 73 | Returns hash of plugins available, where key is a plugin name and the | ||
| 74 | value is path to plugin file. | ||
| 75 | |||
| 76 | For each plugin an attempt is made to load it, to ensure the module is | ||
| 77 | usable. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub names { | ||
| 82 | my $self = shift; | ||
| 83 | my @classpath = split(/::/, $self); | ||
| 84 | state $return //= { | ||
| 85 | map { $_->[0] => $_->[1] } | ||
| 86 | map { | ||
| 87 | my $name = basename($_); | ||
| 88 | my $filename = File::Spec->catfile(@classpath, $name); | ||
| 89 | if (exists($INC{$filename})) { | ||
| 90 | () | ||
| 91 | } else { | ||
| 92 | eval { require $filename; }; | ||
| 93 | $name =~ s/\.pm$//; | ||
| 94 | $@ ? () : [$name, $_]; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | sort map { glob File::Spec->catfile($_, @classpath, '*.pm') } @INC | ||
| 98 | }; | ||
| 99 | |||
| 100 | return %$return; | ||
| 101 | } | ||
| 102 | |||
| 103 | ###################################################################### | ||
| 104 | |||
| 105 | 1; |