File Coverage

blib/lib/Lim/RPC/Transports.pm
Criterion Covered Total %
statement 62 75 82.6
branch 12 34 35.2
condition 3 9 33.3
subroutine 12 12 100.0
pod 3 3 100.0
total 92 133 69.1


line stmt bran cond sub pod time code
1             package Lim::RPC::Transports;
2              
3 7     7   153 use common::sense;
  7         15  
  7         67  
4 7     7   377 use Carp;
  7         14  
  7         552  
5              
6 7     7   41 use Log::Log4perl ();
  7         16  
  7         150  
7 7     7   42 use Scalar::Util qw(blessed);
  7         14  
  7         368  
8 7     7   43 use Module::Find qw(findsubmod);
  7         13  
  7         317  
9              
10 7     7   43 use Lim ();
  7         15  
  7         15114  
11              
12             =encoding utf8
13              
14             =head1 NAME
15              
16             Lim::RPC::Transports - Lim's RPC transport loader and container
17              
18             =head1 VERSION
19              
20             See L for version.
21              
22             =cut
23              
24             our $VERSION = $Lim::VERSION;
25             our $INSTANCE;
26              
27             =head1 SYNOPSIS
28              
29             use Lim::RPC::Transports;
30             $transport = Lim::RPC::Transports->instance->transport('name');
31              
32             =head1 METHODS
33              
34             =over 4
35              
36             =cut
37              
38             sub _new {
39 2     2   7 my $this = shift;
40 2   33     33 my $class = ref($this) || $this;
41 2         9 my %args = ( @_ );
42 2         13 my $self = {
43             logger => Log::Log4perl->get_logger,
44             transport => {},
45             transport_name => {}
46             };
47 2         1824 bless $self, $class;
48              
49 2         11 $self->load;
50              
51 2 50       13 Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self);
52 2         2091 $self;
53             }
54              
55             sub DESTROY {
56 2     2   7 my ($self) = @_;
57 2 50       11 Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self);
58            
59 2         1786 delete $self->{transport};
60             }
61              
62             END {
63 7     7   12258 undef($INSTANCE);
64             }
65              
66             =item $instance = Lim::RPC::Transports->instance
67              
68             Returns the singelton instance of this class.
69              
70             =cut
71              
72             sub instance {
73 2   33 2 1 34 $INSTANCE ||= Lim::RPC::Transports->_new;
74             }
75              
76             =item $instance->load
77              
78             Loads all classes that exists on the system under Lim::RPC::Transport::. Returns
79             the reference to itself even on error.
80              
81             =cut
82              
83             sub load {
84 2     2 1 7 my ($self) = @_;
85            
86 2         16 foreach my $module (findsubmod Lim::RPC::Transport) {
87 4 50       4538 if (exists $self->{transport}->{$module}) {
88 0 0       0 Lim::WARN and $self->{logger}->warn('Transport ', $module, ' already loaded');
89 0         0 next;
90             }
91              
92 4 50       51 if ($module =~ /^([\w:]+)$/o) {
93 4         98 $module = $1;
94             }
95             else {
96 0         0 next;
97             }
98              
99 4         10 my $name;
100 4         10 eval {
101 4         502 eval "require $module;";
102 4 50       43 die $@ if $@;
103 4         27 $name = $module->name;
104             };
105            
106 4 50       17 if ($@) {
107 0 0       0 Lim::WARN and $self->{logger}->warn('Unable to load transport ', $module, ': ', $@);
108 0         0 $self->{transport}->{$module} = {
109             name => $name,
110             module => $module,
111             loaded => 0,
112             error => $@
113             };
114 0         0 next;
115             }
116            
117 4 50       44 unless ($name =~ /^[a-z0-9_\-\.]+$/o) {
118 0 0       0 Lim::WARN and $self->{logger}->warn('Unable to load transport ', $module, ': Illegal characters in transport name');
119 0         0 $self->{transport}->{$module} = {
120             module => $module,
121             loaded => 0,
122             error => 'Illegal characters in transport name'
123             };
124 0         0 next;
125             }
126              
127 4 50       22 if (exists $self->{transport_name}->{$name}) {
128 0 0       0 Lim::WARN and $self->{logger}->warn('Transport name ', $name, ' already loaded by module ', $self->{transport_name}->{$name});
129 0         0 next;
130             }
131              
132 4         94 $self->{transport}->{$module} = {
133             name => $name,
134             module => $module,
135             version => $module->VERSION,
136             loaded => 1
137             };
138 4         50 $self->{transport_name}->{$name} = $module;
139             }
140              
141 2         7 $self;
142             }
143              
144             =item $transport = $instance->transport($name, ...)
145              
146             =cut
147              
148             sub transport {
149 2     2 1 7 my $self = shift;
150 2         7 my $name = shift;
151              
152 2 50       12 if (defined $name) {
153 2         4 my $module;
154            
155 2         6 foreach (keys %{$self->{transport}}) {
  2         12  
156 2 50 33     45 if ($self->{transport}->{$_}->{loaded} and $self->{transport}->{$_}->{name} eq $name) {
157 2         9 $module = $self->{transport}->{$_}->{module};
158 2         6 last;
159             }
160             }
161            
162 2 50       11 if (defined $module) {
163 2         12 my $transport;
164 2         6 eval {
165 2         30 $transport = $module->new(@_);
166             };
167 2 50       12 if ($@) {
168 0 0       0 Lim::WARN and $self->{logger}->warn('Unable to create new instance of transport ', $name, '(', $module, '): ', $@);
169             }
170             else {
171 2         23 return $transport;
172             }
173             }
174             }
175 0           return;
176             }
177              
178             =back
179              
180             =head1 AUTHOR
181              
182             Jerry Lundström, C<< >>
183              
184             =head1 BUGS
185              
186             Please report any bugs or feature requests to L.
187              
188             =head1 SUPPORT
189              
190             You can find documentation for this module with the perldoc command.
191              
192             perldoc Lim::RPC::Transports
193              
194             You can also look for information at:
195              
196             =over 4
197              
198             =item * Lim issue tracker (report bugs here)
199              
200             L
201              
202             =back
203              
204             =head1 ACKNOWLEDGEMENTS
205              
206             =head1 LICENSE AND COPYRIGHT
207              
208             Copyright 2012-2013 Jerry Lundström.
209              
210             This program is free software; you can redistribute it and/or modify it
211             under the terms of either: the GNU General Public License as published
212             by the Free Software Foundation; or the Artistic License.
213              
214             See http://dev.perl.org/licenses/ for more information.
215              
216              
217             =cut
218              
219             1; # End of Lim::RPC::Transports