blib/lib/XAS/Lib/Modules/Environment.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 195 CC 37 SCAR 68.2
CC
37
Cov
0%
init
1190.0
mxmailer
6.0
mqlevel
6.0
CRAP 920.5
1 package XAS::Lib::Modules::Environment;
2  
3 our $VERSION = '0.01';
4  
5 use File::Basename;
6 use Net::Domain qw(hostdomain);
7  
8 use XAS::Class
9 debug => 0,
10 version => $VERSION,
11 base => 'XAS::Singleton',
12 constants => ':logging',
13 filesystem => 'File Dir Path Cwd',
14 accessors => 'path host domain username',
15 mutators => 'mqserver mqport mxserver mxport mxtimeout msgs',
16 ;
17  
18 # ------------------------------------------------------------------------
19 # Public Methods
20 # ------------------------------------------------------------------------
21  
22 sub mxmailer {
23 my $self = shift;
24 my ($mailer) = $self->validate_params(\@_, [
25 { optional => 1, default => undef, regex => qr/sendmail|smtp/ }
26 ]);
27  
28 $self->{mxmailer} = $mailer if (defined($mailer));
29  
30 return $self->{mxmailer};
31  
32 }
33  
34 sub mqlevel {
35 my $self = shift;
36 my ($level) = $self->validate_params(\@_, [
37 { optional => 1, default => undef, regex => qr/(1\.0|1\.1|1\.2)/ },
38 ]);
39  
40 $self->{mqlevel} = $level if (defined($level));
41  
42 return $self->{mqlevel};
43  
44 }
45  
46 sub logtype {
47 my $self = shift;
48 my ($type) = $self->validate_params(\@_, [
49 { optional => 1, default => undef, regex => LOG_TYPES }
50 ]);
51  
52 $self->{logtype} = $type if (defined($type));
53  
54 return $self->{logtype};
55  
56 }
57  
58 # ------------------------------------------------------------------------
59 # Private Methods
60 # ------------------------------------------------------------------------
61  
62 sub init {
63 my $self = shift;
64  
65 my $temp;
66 my $name;
67 my $path;
68 my $suffix;
69  
70 # Initialize variables - these are defaults
71  
72 $self->{mqserver} = defined($ENV{'XAS_MQSERVER'})
73 ? $ENV{'XAS_MQSERVER'}
74 : 'localhost';
75  
76 $self->{mqport} = defined($ENV{'XAS_MQPORT'})
77 ? $ENV{'XAS_MQPORT'}
78 : '61613';
79  
80 $self->{mqlevel} = defined ($ENV{'XAS_MQLEVEL'})
81 ? $ENV{'XAS_MQLEVEL'}
82 : '1.0';
83  
84 $self->{mxserver} = defined($ENV{'XAS_MXSERVER'})
85 ? $ENV{'XAS_MXSERVER'}
86 : 'localhost';
87  
88 $self->{mxport} = defined($ENV{'XAS_MXPORT'})
89 ? $ENV{'XAS_MXPORT'}
90 : '25';
91  
92 $self->{domain} = defined($ENV{'XAS_DOMAIN'})
93 ? $ENV{'XAS_DOMAIN'}
94 : hostdomain();
95  
96 $self->{msgs} = defined($ENV{'XAS_MSGS'})
97 ? qr/$ENV{'XAS_MSGS'}/i
98 : qr/.*\.msg$/i;
99  
100  
101 # platform specific
102  
103 my $OS = $^O;
104  
105 if (($OS eq "aix") or ($OS eq 'linux')) {
106  
107 $self->{host} = defined($ENV{'XAS_HOSTNAME'})
108 ? $ENV{'XAS_HOSTNAME'}
109 : `hostname -s`;
110  
111 chomp($self->{host});
112  
113 $self->{root} = Dir(defined($ENV{'XAS_ROOT'})
114 ? $ENV{'XAS_ROOT'}
115 : ['/', 'usr', 'local']);
116  
117 $self->{tmp} = Dir(defined($ENV{'XAS_TMP'})
118 ? $ENV{'XAS_TMP'}
119 : ['/', 'tmp']);
120  
121 $self->{var} = Dir(defined($ENV{'XAS_VAR'})
122 ? $ENV{'XAS_VAR'}
123 : ['/', 'var']);
124  
125 $self->{lib} = Dir(defined($ENV{'XAS_LIB'})
126 ? $ENV{'XAS_LIB'}
127 : ['/', 'var', 'lib', 'xas']);
128  
129 $self->{log} = Dir(defined($ENV{'XAS_LOG'})
130 ? $ENV{'XAS_LOG'}
131 : ['/', 'var', 'log', 'xas']);
132  
133 $self->{run} = Dir(defined($ENV{'XAS_RUN'})
134 ? $ENV{'XAS_RUN'}
135 : ['/', 'var', 'run', 'xas']);
136  
137 $self->{spool} = Dir(defined($ENV{'XAS_SPOOL'})
138 ? $ENV{'XAS_SPOOL'}
139 : ['/', 'var', 'spool', 'xas']);
140  
141 $self->{mxmailer} = defined($ENV{'XAS_MXMAILER'})
142 ? $ENV{'XAS_MXMAILER'}
143 : 'sendmail';
144  
145 $self->{username} = getpwuid($<);
146  
147 } elsif ($OS eq "MSWin32") {
148  
149 require Win32;
150  
151 $self->{host} = defined($ENV{'XAS_HOSTNAME'})
152 ? $ENV{'XAS_HOSTNAME'}
153 : Win32::NodeName();
154  
155 $self->{root} = Dir(defined($ENV{'XAS_ROOT'})
156 ? $ENV{'XAS_ROOT'}
157 : ['C:', 'xas']);
158  
159 $self->{tmp} = Dir(defined($ENV{'XAS_TMP'})
160 ? $ENV{'XAS_TMP'}
161 : [$self->{root}, 'tmp']);
162  
163 $self->{var} = Dir(defined($ENV{'XAS_VAR'})
164 ? $ENV{'XAS_VAR'}
165 : [$self->{root}, 'var']);
166  
167 $self->{lib} = Dir(defined($ENV{'XAS_LIB'})
168 ? $ENV{'XAS_LIB'}
169 : [$self->{root}, 'var', 'lib']);
170  
171 $self->{log} = Dir(defined($ENV{'XAS_LOG'})
172 ? $ENV{'XAS_LOG'}
173 : [$self->{root}, 'var', 'log']);
174  
175 $self->{run} = Dir(defined($ENV{'XAS_RUN'})
176 ? $ENV{'XAS_RUN'}
177 : [$self->{root}, 'var', 'run']);
178  
179 $self->{spool} = Dir(defined($ENV{'XAS_SPOOL'})
180 ? $ENV{'XAS_SPOOL'}
181 : [$self->{root}, 'var', 'spool']);
182  
183 $self->{mxmailer} = defined($ENV{'XAS_MXMAILER'})
184 ? $ENV{'XAS_MXMAILER'}
185 : 'smtp';
186  
187 $self->{username} = Win32::LoginName();
188  
189 } else {
190  
191 $self->throw_msg(
192 'xas.system.environment.unknownos',
193 'unknownos',
194 $^O
195 );
196  
197 }
198  
199 # build some common paths
200  
201 $self->{etc} = Dir(defined($ENV{'XAS_ETC'})
202 ? $ENV{'XAS_ETC'}
203 : [$self->{root}, 'etc']);
204  
205 $self->{sbin} = Dir(defined($ENV{'XAS_SBIN'})
206 ? $ENV{'XAS_SBIN'}
207 : [$self->{root}, 'sbin']);
208  
209 $self->{bin} = Dir(defined($ENV{'XAS_BIN'})
210 ? $ENV{'XAS_BIN'}
211 : [$self->{root}, 'bin']);
212  
213 $self->{logtype} = defined($ENV{'XAS_LOGTYPE'})
214 ? $ENV{'XAS_LOGTYPE'}
215 : 'console';
216  
217 $self->{path} = $ENV{'PATH'};
218 $self->{mxtimeout} = 60;
219  
220 # create some common file names
221  
222 ($name, $path, $suffix) = fileparse($0, qr{\..*});
223  
224 $self->{logfile} = File($self->{log}, $name . '.log');
225 $self->{pidfile} = File($self->{run}, $name . '.pid');
226 $self->{cfgfile} = File($self->{etc}, $name . '.ini');
227  
228 # build some methods, saves typing
229  
230 for my $datum (qw( logfile pidfile cfgfile )) {
231  
232 $self->class->method($datum => sub {
233 my $self = shift;
234 my ($p) = $self->validate_params(\@_, [
235 {optional => 1, default => undef, isa => 'Badger::Filesystem::File' }
236 ]);
237  
238 $self->{$datum} = $p if (defined($p));
239  
240 return $self->{$datum};
241  
242 });
243  
244 }
245  
246 for my $datum (qw( root etc sbin tmp var bin lib log run spool )) {
247  
248 $self->class->method($datum => sub {
249 my $self = shift;
250 my ($p) = $self->validate_params(\@_, [
251 {optional => 1, default => undef, isa => 'Badger::Filesystem::Directory'}
252 ]);
253  
254 $self->{$datum} = $p if (defined($p));
255  
256 return $self->{$datum};
257  
258 });
259  
260 }
261
262 return $self;
263  
264 }
265  
266 1;
267  
268 __END__
269  
270 =head1 NAME
271  
272 XAS::Lib::Modules::Environment - The base environment for the XAS environment
273  
274 =head1 SYNOPSIS
275  
276 Your program can use this module in the following fashion:
277  
278 use XAS::Class
279 version => '0.01',
280 base => 'XAS::Base',
281 ;
282  
283 $pidfile = $self->env->pidfile;
284 $logfile = $self->env->logfile;
285  
286 printf("The XAS root is %s\n", $self->env->root);
287  
288 =head1 DESCRIPTION
289  
290 This module describes the base environment for XAS. This module is implemented
291 as a singleton and will be auto-loaded when invoked.
292  
293 =head1 METHODS
294  
295 =head2 new
296  
297 This method will initialize the base module. It parses the current environment
298 using the following variables:
299  
300 =over 4
301  
302 =item B<XAS_ROOT>
303  
304 The root of the directory structure. On Unix like boxes this will be
305 /usr/local and Windows this will be C:\xas.
306  
307 =item B<XAS_LOG>
308  
309 The path for log files. On Unix like boxes this will be /var/log/xas and on
310 Windows this will be %XAS_ROOT%\var\log.
311  
312 =item B<XAS_RUN>
313  
314 The path for pid files. On Unix like boxes this will be /var/run/xas and
315 on Windows this will be %XAS_ROOT%\var\run.
316  
317 =item B<XAS_SPOOL>
318  
319 The base path for spool files. On Unix like boxes this will be /var/spool/xas
320 and on Windows this will be %XRS_ROOT%\var\spool.
321  
322 =item B<XAS_LIB>
323  
324 The path to the lib directory. On Unix like boxes this will be /var/lib/xas
325 and on Windows this will be %XAS_ROOT%\var\lib.
326  
327 =item B<XAS_ETC>
328  
329 The path to the etc directory. On Unix like boxes this will be /usr/local/etc
330 and on Windows this will be %XAS_ROOT%\etc
331  
332 =item B<XAS_BIN>
333  
334 The path to the bin directory. On Unix like boxes this will be /usr/local/bin
335 and on Windows this will be %XAS_ROOT%\bin.
336  
337 =item B<XAS_SBIN>
338  
339 The path to the sbin directory. On Unix like boxes this will be /usr/local/sbin
340 and on Windows this will be %XAS_ROOT%\sbin.
341  
342 =item B<XAS_HOSTNAME>
343  
344 The host name of the system. If not provided, on Unix the "hostname -s" command
345 will be used and on Windows Win32::NodeName() will be called.
346  
347 =item B<XAS_DOMAIN>
348  
349 The domain of the system: If not provided, then Net::Domain::hostdomain() will
350 be used.
351  
352 =item B<XAS_MQSERVER>
353  
354 The server where a STOMP enabled message queue server is located. Default
355 is "localhost".
356  
357 =item B<XAS_MQPORT>
358  
359 The port that server is listening on. Default is "61613".
360  
361 =item B<XAS_MQLEVL>
362  
363 This sets the STOMP protocol level. The default is v1.0.
364  
365 =item B<XAS_MXSERVER>
366  
367 The server where a SMTP based mail server resides. Default is "localhost".
368  
369 =item B<XAS_MXPORT>
370  
371 The port it is listening on. Default is "25".
372  
373 =item B<XAS_MXMAILER>
374  
375 The mailer to use for sending email. On Unix like boxes this will be "sendmail"
376 on Windows this will be "smtp".
377  
378 =item B<XAS_MSGS>
379  
380 The regex to use when searching for message files. Defaults to /.*\.msg/i.
381  
382 =back
383  
384 =head2 logtype
385  
386 This method will return the currently defined log type. By default this is
387 "console". i.e. all logging will go to the terminal screen. Valid options
388 are "file", "logstash" and "syslog'.
389  
390 =head2 logfile
391  
392 This method will return a pre-generated name for a log file. The name will be
393 based on the programs name with a ".log" extension, along with the path to
394 the XAS log file directory. Or you can store your own self generated log
395 file name.
396  
397 Example
398  
399 $logfile = $xas->logfile;
400 $xas->logfile("/some/path/mylogfile.log");
401  
402 =head2 pidfile
403  
404 This method will return a pre-generated name for a pid file. The name will be
405 based on the programs name with a ".pid" extension, along with the path to
406 the XAS pid file directory. Or you can store your own self generated pid
407 file name.
408  
409 Example
410  
411 $pidfile = $xas->pidfile;
412 $xas->pidfile("/some/path/myfile.pid");
413  
414 =head2 cfgfile
415  
416 This method will return a pre-generated name for a configuration file. The
417 name will be based on the programs name with a ".ini" extension, along with
418 the path to the XAS configuration file directory. Or you can store your own
419 self generated configuration file name.
420  
421 Example
422  
423 $inifile = $xas->cfgfile;
424 $xas->cfgfile("/some/path/myfile.cfg");
425  
426 =head2 mqserver
427  
428 This method will return the name of the message queue server. Or you can
429 store a different name for the server.
430  
431 Example
432  
433 $mqserver = $xas->mqserver;
434 $xas->mqserver('mq.example.com');
435  
436 =head2 mqport
437  
438 This method will return the port for the message queue server, or you store
439 a different port number for that server.
440  
441 =head2 mqlevel
442  
443 This method will returns the STOMP protocol level. or you store
444 a different level. It can use 1.0, 1.1 or 1.2.
445  
446 Example
447  
448 $mqlevel = $xas->mqlevel;
449 $xas->mqlevel('1.0');
450  
451 =head2 mxserver
452  
453 This method will return the name of the mail server. Or you can
454 store a different name for the server.
455  
456 Example
457  
458 $mxserver = $xas->mxserver;
459 $xas->mxserver('mail.example.com');
460  
461 =head2 mxport
462  
463 This method will return the port for the mail server, or you store
464 a different port number for that server.
465  
466 Example
467  
468 $mxport = $xas->mxport;
469 $xas->mxport('25');
470  
471 =head2 mxmailer
472  
473 This method will return the mailer to use for sending email, or you can
474 change the mailer used.
475  
476 Example
477  
478 $mxmailer = $xas->mxmailer;
479 $xas->mxmailer('smtp');
480  
481 =head1 ACCESSORS
482  
483 =head2 path
484  
485 This accessor returns the currently defined path for this program.
486  
487 =head2 root
488  
489 This accessor returns the root directory of the XAS environment.
490  
491 =head2 bin
492  
493 This accessor returns the bin directory of the XAS environment. The bin
494 directory is used to place executable commands.
495  
496 =head2 sbin
497  
498 This accessor returns the sbin directory of the XAS environment. The sbin
499 directory is used to place system level commands.
500  
501 =head2 log
502  
503 This accessor returns the log directory of the XAS environment.
504  
505 =head2 run
506  
507 This accessor returns the run directory of the XAS environment. The run
508 directory is used to place pid files and other such files.
509  
510 =head2 etc
511  
512 This accessor returns the etc directory of the XAS environment.
513 Application configuration files should go into this directory.
514  
515 =head2 lib
516  
517 This accessor returns the lib directory of the XAS environment. This
518 directory is used to store supporting file for the environment.
519  
520 =head2 spool
521  
522 This accessor returns the spool directory of the XAS environment. This
523 directory is used to store spool files generated within the environment.
524  
525 =head2 tmp
526  
527 This accessor returns the tmp directory of the XAS environment. This
528 directory is used to store temporary files.
529  
530 =head2 var
531  
532 This accessor returns the var directory of the XAS environment.
533  
534 =head2 host
535  
536 This accessor returns the local hostname.
537  
538 =head2 domain
539  
540 This access returns the domain name of the local host.
541  
542 =head2 username
543  
544 This accessor returns the effective username of the current process.
545  
546 =head2 msgs
547  
548 The accessor to return the regex for messages files.
549  
550 =head1 SEE ALSO
551  
552 =over 4
553  
554 =item L<XAS|XAS>
555  
556 =back
557  
558 =head1 AUTHOR
559  
560 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
561  
562 =head1 COPYRIGHT AND LICENSE
563  
564 Copyright (C) 2014 Kevin L. Esteb
565  
566 This library is free software; you can redistribute it and/or modify
567 it under the same terms as Perl itself, either Perl version 5.8.8 or,
568 at your option, any later version of Perl 5 you may have available.
569  
570 See L<http://dev.perl.org/licenses/> for more information.
571  
572 =cut