Lim.pm

filter: Statementstmt 20.6% 19 / 92 Branchbran 7.6% 4 / 52 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 50.0% 8 / 16 Podpod 100% 12 / 12 totaltotal 21.5% 43 / 200 CC 33 SCAR 42.3
CC
33
Cov
14%
LoadConfigDirectory
132.0
ParseOptions
110.0
MergeConfig
72.0
CRAP 68.9
« Lim/RPC/Value/Collection.pm ↑ Summary Lim/RPC/Protocol/JSONRPC2.pm »
1 package Lim;
2  
3 10 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use YAML::Any ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 Lim - Framework for RESTful JSON/XML, JSON-RPC, XML-RPC and SOAP
13  
14 =head1 VERSION
15  
16 Version 0.20
17  
18 =cut
19  
20 10 our $VERSION = '0.20';
21 our $CONFIG = {
22 log => {
23 obj_debug => 0,
24 rpc_debug => 0,
25 debug => 0,
26 info => 1,
27 warn => 1,
28 err => 1
29 },
30 prefix => ['', '/usr', '/usr/local'],
31 rpc => {
32 srv_listen => 10,
33 timeout => 30,
34 call_timeout => 300,
35 skip_dns => 0,
36 transport => {
37 http => {
38 host => undef,
39 port => 5353
40 }
41 },
42 protocol => {
43 http => {
44 webroot => '/usr/share/lim/html'
45 }
46 },
47 tls => {
48 method => 'any',
49 verify => 1,
50 verify_require_client_cert => 1,
51 ca_path => '/etc/lim/ssl/certs'
52 },
53 json => {
54 pretty => 0
55 }
56 },
57 agent => {
58 config_file => '',
59 uri => []
60 },
61 cli => {
62 history_length => 1000,
63 history_file => defined $ENV{HOME} ? $ENV{HOME}.($ENV{HOME} =~ /\/$/o ? '' : '/').'.lim_history' : '',
64 config_file => defined $ENV{HOME} ? $ENV{HOME}.($ENV{HOME} =~ /\/$/o ? '' : '/').'.limrc' : '',
65 editor => $ENV{EDITOR},
66 10 ▶ host => 'localhost',
Branch: 4 branches
BranchTrueFalseTotal
defined $ENV{'HOME'} ? : 10 0 10
$ENV{'HOME'} =~ m[/$]ou ? : 0 10 10
defined $ENV{'HOME'} ? : 10 0 10
$ENV{'HOME'} =~ m[/$]ou ? : 0 10 10
67 port => 5353,
68 transport => 'http',
69 protocol => 'rest'
70 },
71 plugin => {
72 load_all => 1,
73 load => {}
74 }
75 };
76  
77 =head1 SYNOPSIS
78  
79 =over 4
80  
81 use Lim;
82  
83 =back
84  
85 =head1 DESCRIPTION
86  
87 L<Lim> provides a framework for calling plugins over multiple protocols.
88  
89 It uses AnyEvent for async operations and SOAP::Lite, XMLRPC::Lite and JSON::XS
90 for processing protocol messages.
91  
92 There are 3 parts in Lim that can work independenly, a Server part, a Client
93 part and a CLI part.
94  
95 All plugins are also divded into these 3 parts and use the base classes
96 L<Lim::Component::Server>, L<Lim::Component::Client> and L<Lim::Component::CLI>.
97  
98 The built in Server part is called L<Lim::Agent> and can be started with
99 lim-agentd. It will use L<Lim::Plugins> to load all available plugins on
100 the system and serve their Server part to L<Lim::Server> if available.
101  
102 The built in CLI part is called L<Lim::CLI> and can be started with lim-cli.
103 It will use L<Lim::Plugins> to load all available plugins on the system and
104 use their CLI part if available.
105  
106 =head1 METHODS
107  
108 =over 4
109  
110 =item Lim::OBJ_DEBUG
111  
112 Semi constant sub that controls if object debugging information should sent to
113 the log.
114  
115 =cut
116  
117 88 ▶sub OBJ_DEBUG { 1 }
Subroutine OBJ_DEBUG: called CC=1 SCAR=0.0
Pod documented
118  
119 =item Lim::RPC_DEBUG
120  
121 Semi constant sub that controls if RPC debugging information should sent to the
122 log.
123  
124 =cut
125  
126 34 ▶sub RPC_DEBUG { 1 }
Subroutine RPC_DEBUG: called CC=1 SCAR=0.0
Pod documented
127  
128 =item Lim::DEBUG
129  
130 Semi constant sub that controls if debugging information should sent to the log.
131  
132 Common usage:
133 Lim::DEBUG and $self->{logger}->debug(...);
134  
135 =cut
136  
137 48 ▶sub DEBUG { 1 }
Subroutine DEBUG: called CC=1 SCAR=0.0
Pod documented
138  
139 =item Lim::INFO
140  
141 Semi constant sub that controls if informational logs should sent to the log.
142  
143 Common usage:
144 Lim::INFO and $self->{logger}->info(...);
145  
146 =cut
147  
148 0 ▶sub INFO { 1 }
Subroutine INFO: not called CC=1 SCAR=6.9
Pod documented
149  
150 =item Lim::WARN
151  
152 Semi constant sub that controls if warnings should sent to the log.
153  
154 Common usage:
155 Lim::WARN and $self->{logger}->warn(...);
156  
157 =cut
158  
159 0 ▶sub WARN { 1 }
Subroutine WARN: not called CC=1 SCAR=6.9
Pod documented
160  
161 =item Lim::ERR
162  
163 Semi constant sub that controls if errors should sent to the log.
164  
165 Common usage:
166 Lim::ERR and $self->{logger}->error(...);
167  
168 =cut
169  
170 0 ▶sub ERR { 1 }
Subroutine ERR: not called CC=1 SCAR=6.9
Pod documented
171  
172 =item Lim::Config ->{}
173  
174 Return a hash reference to the configuration.
175  
176 =cut
177  
178 10 ▶sub Config (){ $CONFIG }
Subroutine Config: called CC=1 SCAR=0.0
Pod documented
179  
180 =item Lim::MergeConfig($config)
181  
182 Try and merge the given hash reference C<$config> into Lim's configuration.
183  
184 =cut
185  
186 0 ▶sub MergeConfig {
Subroutine MergeConfig: not called CC=8 SCAR=42.8
Pod documented
187 0 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 0 0 0
188 0 my @merge = ([$_[0], $CONFIG]);
189  
190 0 ▶ while (defined (my $merge = shift(@merge))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $merge = shift @merge)) 0 0 0
191 0 my ($from, $to) = @$merge;
192 0 foreach my $key (keys %$from) {
193 0 ▶ if (exists $to->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $to->{$key}) 0 0 0
194 0 ▶ unless (ref($from->{$key}) eq ref($to->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $from->{$key} eq ref $to->{$key}) 0 0 0
195 # TODO display what entry is missmatching
196 0 confess __PACKAGE__, 'Can not merge config, entries type missmatch';
197 }
198 0 ▶ if (ref($from->{$key}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $from->{$key} eq "HASH") 0 0 0
199 0 push(@merge, [$from->{$key}, $to->{$key}]);
200 0 next;
201 }
202 }
203 0 ▶ if (ref($to->{$key}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $to->{$key} eq 'ARRAY') { } 0 0 0
204 0 push(@{$to->{$key}}, @{$from->{$key}});
205 }
206 else {
207 0 $to->{$key} = $from->{$key};
208 }
209 }
210 }
211 }
212 0 return;
213 }
214  
215 =item Lim::LoadConfig($filename)
216  
217 Load the given configuration C<$filename> in YAML format and merge it into Lim's
218 configuration.
219  
220 =cut
221  
222 0 ▶sub LoadConfig {
Subroutine LoadConfig: not called CC=5 SCAR=34.0
Pod documented
223 0 my ($filename) = @_;
224  
225 0 ▶ if (defined $filename and -r $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and -r $filename) 0 0 0
Condition0%0/3
defined $filename and -r $filename
!ll&&!rl&&r
000
MC/DC0%0/2
defined $filename and -r $filename
defined $filename -r $filename
Truth table0%0/3
A and B
A= defined $filenameB= -r $filename
ABresult
0X0
100
111
226 0 my $yaml;
227  
228 0 ▶ Lim::DEBUG and Log::Log4perl->get_logger->debug('Loading config ', $filename);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and "Log::Log4perl"->get_logger->debug("Loading config ", $filename) 0 0 0
229 0 eval {
230 0 $yaml = YAML::Any::LoadFile($filename);
231 };
232 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
233 0 confess __PACKAGE__, ': Unable to read configuration file ', $filename, ': ', $@, "\n";
234 }
235 0 Lim::MergeConfig($yaml);
236 0 return 1;
237 }
238 0 return;
239 }
240  
241 =item Lim::LoadConfigDirectory($directory)
242  
243 Load the given configuration in directory C<$directory> and merge it into Lim's
244 configuration.
245  
246 =cut
247  
248 0 ▶sub LoadConfigDirectory {
Subroutine LoadConfigDirectory: not called CC=11 SCAR=48.8
Pod documented
249 0 my ($directory) = @_;
250  
251 0 ▶ if (defined $directory and -r $directory and -x $directory and -d $directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $directory and -r $directory and -x $directory and -d $directory) 0 0 0
Condition0%0/9
defined $directory and -r $directory and -x $directory and -d $directory
!ll&&!rl&&r
000
defined $directory and -r $directory and -x $directory
!ll&&!rl&&r
000
defined $directory and -r $directory
!ll&&!rl&&r
000
MC/DC0%0/4
defined $directory and -r $directory and -x $directory and -d $directory
defined $directory -r $directory -x $directory -d $directory
Truth table0%0/5
A and B and C and D
A= defined $directoryB= -r $directoryC= -x $directoryD= -d $directory
ABCDresult
0XXX0
10XX0
110X0
11100
11111
252 0 ▶ unless(opendir(CONFIGS, $directory)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir CONFIGS, $directory) 0 0 0
253 0 confess __PACKAGE__, ': Unable to read configurations in directory ', $directory, ': ', $!, "\n";
254 }
255  
256 0 foreach my $entry (sort readdir(CONFIGS)) {
257 0 my $yaml;
258  
259 0 ▶ unless(-r $directory.'/'.$entry and $entry =~ /\.yaml$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $directory . '/' . $entry and $entry =~ /\.yaml$/ou) 0 0 0
Condition0%0/3
-r $directory . '/' . $entry and $entry =~ /\.yaml$/ou
!ll&&!rl&&r
000
MC/DC0%0/2
-r $directory . '/' . $entry and $entry =~ /\.yaml$/ou
-r $directory . '/' . $entry $entry =~ /\.yaml$/ou
Truth table0%0/3
A and B
A= -r $directory . '/' . $entryB= $entry =~ /\.yaml$/ou
ABresult
0X0
100
111
260 0 next;
261 }
262  
263 0 ▶ Lim::DEBUG and Log::Log4perl->get_logger->debug('Loading config ', $entry, ' from directory ', $directory);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and "Log::Log4perl"->get_logger->debug("Loading config ", $entry, " from directory ", $directory) 0 0 0
264 0 eval {
265 0 $yaml = YAML::Any::LoadFile($directory.'/'.$entry);
266 };
267 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
268 0 confess __PACKAGE__, ': Unable to read configuration file ', $entry, ' from directory ', $directory, ': ', $@, "\n";
269 }
270 0 Lim::MergeConfig($yaml);
271 }
272 0 closedir(CONFIGS);
273 0 return 1;
274 }
275 0 return;
276 }
277  
278 =item Lim::ParseOptions(@options)
279  
280 Parse options given at command line and add them into configuration. Option
281 subgroups are seperated by . (for example log.obj_debug=0).
282  
283 =cut
284  
285 0 ▶sub ParseOptions {
Subroutine ParseOptions: not called CC=10 SCAR=47.0
Pod documented
286 0 foreach my $option (@_) {
287 0 my ($name, $value) = split(/=/o, $option, 2);
288 0 ▶ unless ($name and defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name and defined $value) 0 0 0
Condition0%0/3
$name and defined $value
!ll&&!rl&&r
000
MC/DC0%0/2
$name and defined $value
$name defined $value
Truth table0%0/3
A and B
A= $nameB= defined $value
ABresult
0X0
100
111
289 0 confess __PACKAGE__, ': Invalid or unknown option: ', $option, "\n";
290 }
291  
292 0 my @parts = split(/\./o, $name);
293 0 my $ref = $CONFIG;
294 0 ▶ while (defined(my $part = shift(@parts))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $part = shift @parts)) 0 0 0
295 0 ▶ unless (scalar @parts) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @parts) 0 0 0
296 0 ▶ if ($part =~ /^(.+)\[\]$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /^(.+)\[\]$/ou) { } 0 0 0
297 0 $part = $1;
298  
299 0 ▶ if (exists $ref->{$part}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{$part}) { } 0 0 0
300 0 ▶ if (ref($ref->{$part}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref->{$part} eq 'ARRAY') { } 0 0 0
301 0 push(@{$ref->{$part}}, $value);
302 }
303 else {
304 0 $ref->{$part} = [ $ref->{$part}, $value ];
305 }
306 }
307 else {
308 0 $ref->{$part} = [ $value ];
309 }
310 }
311 else {
312 0 $ref->{$part} = $value;
313 }
314 0 last;
315 }
316  
317 0 ▶ unless (exists $ref->{$part}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ref->{$part}) 0 0 0
318 0 $ref->{$part} = {};
319 }
320 0 $ref = $ref->{$part};
321 }
322 }
323 }
324  
325 =item Lim::UpdateConfig
326  
327 Used after L<LoadConfig> and/or L<LoadConfigDirectory> to update and do post
328 configuration tasks.
329  
330 =cut
331  
332 0 ▶sub UpdateConfig {
Subroutine UpdateConfig: not called CC=3 SCAR=17.3
Pod documented
333 0 foreach my $key (keys %{$CONFIG->{log}}) {
334 {
335 0 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.2
336 0 ▶ eval 'sub '.uc($key).' {'.($CONFIG->{log}->{$key} ? '1' : '0').'}';
Branch: 1 branch
BranchTrueFalseTotal
$Lim::CONFIG->{'log'}{$key} ? : 0 0 0
337 }
338 }
339 }
340  
341 =back
342  
343 =head1 AUTHOR
344  
345 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
346  
347 =head1 BUGS
348  
349 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
350  
351 =head1 SUPPORT
352  
353 You can find documentation for this module with the perldoc command.
354  
355 perldoc Lim
356  
357 You can also look for information at:
358  
359 =over 4
360  
361 =item * Lim issue tracker (report bugs here)
362  
363 L<https://github.com/jelu/lim/issues>
364  
365 =back
366  
367 =head1 ACKNOWLEDGEMENTS
368  
369 =head1 LICENSE AND COPYRIGHT
370  
371 Copyright 2012-2013 Jerry Lundström.
372  
373 This program is free software; you can redistribute it and/or modify it
374 under the terms of either: the GNU General Public License as published
375 by the Free Software Foundation; or the Artistic License.
376  
377 See http://dev.perl.org/licenses/ for more information.
378  
379  
380 =cut
381  
382 10 1; # End of Lim
« Lim/RPC/Value/Collection.pm ↑ Summary Lim/RPC/Protocol/JSONRPC2.pm »