Connector/Multi.pm

filter: Statementstmt 73.7% 121 / 164 Branchbran 42.3% 22 / 52 Conditioncond 75.0% 6 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 86.9% 20 / 23 Podpod 91.6% 11 / 12 totaltotal 69.7% 184 / 264 CC 32 SCAR 26.8
CC
32
Cov
67%
_route_call
36.4
cleanup
20.0
get_connector
7.8
CRAP 14.5
« Connector/Proxy/HTTP.pm ↑ Summary Connector/Role/SessionCache.pm »
1 # Connector::Multi
2 #
3 # Connector class capable of dealing with multiple personalities.
4 #
5 # Written by Scott Hardin and Martin Bartosch for the OpenXPKI project 2012
6 #
7 package Connector::Multi;
8  
9 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Connector::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 extends 'Connector';
16  
17 6 has 'BASECONNECTOR' => ( is => 'ro', required => 1 );
18  
19 6 has '+LOCATION' => ( required => 0 );
20  
21 6 has '_cache' => ( is => 'rw', required => 0, isa => 'HashRef', builder => '_init_cache' );
22  
23 5 ▶sub _init_cache {
Subroutine _init_cache: called CC=1 SCAR=0.0
24 5 my $self = shift;
25  
26 5 $self->_cache( { 'node' => {} } );
27 }
28  
29 5 ▶sub _build_config {
Subroutine _build_config: called CC=3 SCAR=12.3
30 5 my $self = shift;
31  
32 # Our config is merely a hash of connector instances
33 5 my $config = {};
34 5 my $baseconn = $self->BASECONNECTOR();
35 5 my $baseref;
36  
37 5 ▶ if ( ref($baseconn) ) { # if it's a ref, assume that it's a Connector
Branch: 1 branch
BranchTrueFalseTotal
if (ref $baseconn) { } 5 0 5
38 5 $baseref = $baseconn;
39 } else {
40 0 ▶ eval "use $baseconn;1" or die "Error use'ing $baseconn: $@";
Branch: 1 branch
BranchTrueFalseTotal
eval "use $baseconn;1" or die "Error use'ing ${baseconn}: $@" 0 0 0
41 0 $baseref = $baseconn->new({ LOCATION => $self->LOCATION() });
42 }
43 5 $config->{''} = $baseref;
44 5 $self->_config($config);
45 }
46  
47 # Proxy calls
48 55 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
49 55 my $self = shift;
50 55 unshift @_, 'get';
51 55 return $self->_route_call( @_ );
52 }
53  
54 4 ▶sub get_list {
Subroutine get_list: called CC=1 SCAR=0.0
Pod documented
55 4 my $self = shift;
56 4 unshift @_, 'get_list';
57  
58 4 return $self->_route_call( @_ );
59 }
60  
61 0 ▶sub get_size {
Subroutine get_size: not called CC=1 SCAR=6.9
Pod documented
62 0 my $self = shift;
63 0 unshift @_, 'get_size';
64 0 return $self->_route_call( @_ );
65 }
66  
67 3 ▶sub get_hash {
Subroutine get_hash: called CC=5 SCAR=17.1
Pod documented
68 3 my $self = shift;
69 3 my @args = @_;
70 3 unshift @_, 'get_hash';
71 3 my $hash = $self->_route_call( @_ );
72 3 ▶ return $hash unless (ref $hash); # undef
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash 1 2 3
73  
74 # This assumes that all connectors that can handle references
75 2 my @path;
76 2 foreach my $key (keys %{$hash}) {
77 # Connector in leaf - resolv it!
78 4 ▶ if (ref $hash->{$key} eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash->{$key} eq "SCALAR") 0 4 4
79 0 ▶ @path = $self->_build_path( $args[0] ) unless(@path);
Branch: 1 branch
BranchTrueFalseTotal
unless @path 0 0 0
80 0 $hash->{$key} = $self->get( [ @path , $key ] );
81 }
82 }
83 2 return $hash;
84 }
85  
86 3 ▶sub get_keys {
Subroutine get_keys: called CC=1 SCAR=0.0
Pod documented
87 3 my $self = shift;
88 3 unshift @_, 'get_keys';
89  
90 3 return $self->_route_call( @_ );
91 }
92  
93 5 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
94 5 my $self = shift;
95 5 unshift @_, 'set';
96 5 return $self->_route_call( @_ );
97 }
98  
99 59 ▶sub get_meta {
Subroutine get_meta: called CC=1 SCAR=0.0
Pod documented
100 59 my $self = shift;
101 59 unshift @_, 'get_meta';
102 59 return $self->_route_call( @_ );
103 }
104  
105 2 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
106 2 my $self = shift;
107 2 unshift @_, 'exists';
108 2 return $self->_route_call( @_ );
109 }
110  
111 0 ▶sub cleanup {
Subroutine cleanup: not called CC=4 SCAR=30.0
Pod documented
112 0 my $self = shift;
113 0 foreach my $cache_id (keys %{$self->_config()}) {
114 # do not cleanup the base connector
115 0 ▶ next unless ($cache_id);
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_id 0 0 0
116 0 eval {
117 0 $self->_config()->{$cache_id}->cleanup();
118 0 $self->log()->debug("Cleanup ok on $cache_id");
119 };
120 0 delete $self->_config()->{$cache_id};
121 0 ▶ $self->log()->warn("Error on cleanup in $cache_id: $EVAL_ERROR") if ($EVAL_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
if $Connector::Multi::EVAL_ERROR 0 0 0
122 }
123 }
124  
125 131 ▶sub _route_call {
Subroutine _route_call: called CC=17 SCAR=36.0
126  
127 131 my $self = shift;
128 131 my $call = shift;
129 131 my $location = shift;
130 131 my @args = @_;
131  
132 131 my $delim = $self->DELIMITER();
133  
134 131 my $conn = $self->_config()->{''};
135  
136 131 ▶ if ( ! $conn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conn) 0 131 131
137 0 die "ERR: no default connector for Connector::Multi";
138 }
139  
140 131 my @prefix = ();
141 131 my @suffix = $self->_build_path_with_prefix( $location );
142 131 my $ptr_cache = $self->_cache()->{node};
143  
144 131 $self->log()->debug('Call '.$call.' in Multi to '. join('.', @suffix));
145  
146 131 ▶ while ( @suffix > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@suffix > 0) 418 109 527
147 418 my $node = shift @suffix;
148 418 push @prefix, $node;
149  
150 # Easy Cache - skip all inner nodes, that are not a connector
151 # that might fail if you mix real path and complex path items
152 418 my $path = join($delim, @prefix);
153 418 ▶ if (exists $ptr_cache->{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ptr_cache->{$path}) 287 131 418
154 287 next;
155 }
156  
157 131 my $meta = $conn->get_meta($path);
158  
159 131 ▶ if ( $meta && $meta->{TYPE} eq 'reference' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($meta and $meta->{'TYPE'} eq 'reference') { } 20 111 131
elsif ($meta and $meta->{'TYPE'} eq 'connector') { } 2 109 111
Condition100%3/3
$meta and $meta->{"TYPE"} eq "reference"
!ll&&!rl&&r
565520
MC/DC100%2/2
$meta and $meta->{"TYPE"} eq "reference"
$meta $meta->{"TYPE"} eq "reference"
Truth table100%3/3
A and B
A= $metaB= $meta->{"TYPE"} eq "reference"
ABresult
0X0
100
111
160 20 ▶ if ( $meta->{VALUE} =~ m/^([^:]+):(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{'VALUE'} =~ /^([^:]+):(.+)$/) { } 20 0 20
161 20 my $schema = $1;
162 20 my $target = $2;
163 20 ▶ if ( $schema eq 'connector' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($schema eq 'connector') { } 20 0 20
elsif ($schema eq 'env') { } 0 0 0
164 20 $conn = $self->get_connector($target);
165 20 ▶ if ( ! $conn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conn) 0 20 20
166 0 $self->_log_and_die("Connector::Multi: error creating connector for '$target': $@");
167 }
168 20 $self->log()->debug("Dispatch to connector at $target");
169 # Push path on top of the argument array
170 20 unshift @args, \@suffix;
171 20 return $conn->$call( @args );
172 } elsif ( $schema eq 'env' ) {
173  
174 0 $self->log()->debug("Fetch from ENV with key $target");
175 # warn if the path is not empty
176 0 ▶ $self->log()->warn(sprintf("Call redirected to ENV but path is not final (%s)!", join(".",@suffix))) if (@suffix > 0);
Branch: 1 branch
BranchTrueFalseTotal
if @suffix > 0 0 0 0
177 0 ▶ if (!exists $ENV{$target}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ENV{$target}) 0 0 0
178 0 return $self->_node_not_exists();
179 }
180 0 return $ENV{$target};
181  
182 } else {
183 0 $self->_log_and_die("Connector::Multi: unsupported schema for symlink: $schema");
184 }
185 } else {
186 # redirect
187 0 my @target = split(/[$delim]/, $meta->{VALUE});
188 # relative path - shift one item from prefix for each dot
189 0 ▶ if ($target[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target[0] eq '') { } 0 0 0
190 0 $self->log()->debug("Relative redirect at prefix " . join ".", @prefix);
191 0 ▶ while ($target[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target[0] eq "") 0 0 0
192 0 ▶ $self->_log_and_die("Relative path length exceeds prefix length") unless (scalar @prefix);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @prefix 0 0 0
193 0 pop @prefix;
194 0 shift @target;
195 }
196 } else {
197 0 $self->log()->debug(sprintf("Plain redirect at prefix %s to %s", join(".", @prefix), $meta->{VALUE}));
198 0 @prefix = ();
199 }
200 0 unshift @suffix, @target;
201 0 $self->log()->debug("Final redirect target " . join ".", @suffix);
202 0 unshift @args, [ @prefix, @suffix ];
203 0 return $self->$call( @args );
204 }
205 ▶ } elsif ( $meta && $meta->{TYPE} eq 'connector' ) {
Condition100%3/3
$meta and $meta->{"TYPE"} eq "connector"
!ll&&!rl&&r
56532
MC/DC100%2/2
$meta and $meta->{"TYPE"} eq "connector"
$meta $meta->{"TYPE"} eq "connector"
Truth table100%3/3
A and B
A= $metaB= $meta->{"TYPE"} eq "connector"
ABresult
0X0
100
111
206  
207 2 my $conn = $meta->{VALUE};
208 2 $self->log()->debug("Got conncetor reference of type ". ref $conn);
209 2 $self->log()->debug("Dispatch to connector at " . join(".", @prefix));
210 # Push path on top of the argument array
211 2 unshift @args, \@suffix;
212 2 return $conn->$call( @args );
213  
214 } else {
215 109 $ptr_cache->{$path} = 1;
216 }
217 }
218  
219 # Push path on top of the argument array
220 109 unshift @args, [ @prefix, @suffix ];
221 109 return $conn->$call( @args );
222 }
223  
224 7 ▶sub get_wrapper() {
Subroutine get_wrapper: called CC=1 SCAR=0.0
Pod documented
225 7 my $self = shift;
226 7 my $location = shift;
227 7 return Connector::Wrapper->new({ BASECONNECTOR => $self, TARGET => $location });
228 }
229  
230 # getWrapper() is deprecated - use get_wrapper() instead
231 0 ▶sub getWrapper() {
Subroutine getWrapper: not called CC=1 SCAR=6.9
Pod undocumented
232 0 my $self = shift;
233 0 warn "using deprecated call to getWrapper - use get_wrapper instead";
234 0 $self->get_wrapper(@_);
235 }
236  
237 24 ▶sub get_connector {
Subroutine get_connector: called CC=7 SCAR=20.6
Pod documented
238 24 my $self = shift;
239 24 my $target = shift;
240  
241 # the cache needs to store the absolute path including the prefix
242 24 my @path = $self->_build_path( $target );
243 24 my $cache_id = join($self->DELIMITER(), $self->_build_path_with_prefix( \@path ));
244 24 my $conn = $self->_config()->{$cache_id};
245 24 ▶ if ( ! $conn ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $conn) { } 7 17 24
elsif ($self->log->is_trace) { } 0 17 17
246 # Note - we will use ourselves to read the connectors instance information
247 # this allows to put other connectors inside a connector definition but
248 # also lets connector definition paths depend on PREFIX!
249 7 my $class = $self->get( [ @path, 'class' ] );
250 7 ▶ if (!$class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 7 7
251 0 ▶ my $prefix = $self->_get_prefix() || '-';
Condition0%0/2
$self->_get_prefix || '-'
l!l
00
MC/DC0%0/1
$self->_get_prefix || '-'
$self->_get_prefix
Truth table0%0/2
A
A= $self->_get_prefix
Aresult
11
00
252 0 $self->_log_and_die("Nested connector without class ($target/$prefix)");
253 }
254 7 $self->log()->debug("Initialize connector $class at $target");
255 3 ▶ eval "use $class;1" or $self->_log_and_die("Error use'ing $class: $@");
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use $class;1" or $self->_log_and_die("Error use'ing ${class}: $@") 7 0 7
256 7 $conn = $class->new( { CONNECTOR => $self, TARGET => $target } );
257 7 $self->_config()->{$cache_id} = $conn;
258 7 ▶ $self->log()->trace("Add connector to cache: $cache_id") if ($self->log()->is_trace());
Branch: 1 branch
BranchTrueFalseTotal
if $self->log->is_trace 0 7 7
259 } elsif ($self->log()->is_trace()) {
260 0 $self->log()->trace("Got connector for $target from cache $cache_id");
261 }
262 24 return $conn;
263 }
264  
265 6 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
266 6 __PACKAGE__->meta->make_immutable;
267  
268 6 1;
269 __END__
« Connector/Proxy/HTTP.pm ↑ Summary Connector/Role/SessionCache.pm »