SPOPS/Initialize.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« SPOPS/Exception/LDAP.pm ↑ Summary SPOPS/Utility.pm »
1 package SPOPS::Initialize;
2  
3 # $Id: Initialize.pm,v 3.4 2004/06/02 00:48:21 lachoy Exp $
4  
5 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use SPOPS;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use SPOPS::ClassFactory;
9 use SPOPS::Exception qw( spops_error );
10  
11 my $log = get_logger();
12  
13 $SPOPS::Initialize::VERSION = sprintf("%d.%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/);
14  
15 # Main interface -- take the information read in from 'read_config()'
16 # and create SPOPS classes, then initialize them
17  
18 sub process {
19 my ( $class, $p ) = @_;
20 $p ||= {};
21 my $config = $p->{config};
22 if ( $p->{directory} or $p->{filename} ) {
23 $config = $class->read_config( $p );
24 return unless ( ref $config eq 'HASH' );
25 delete $p->{filename};
26 delete $p->{directory};
27 delete $p->{pattern};
28 }
29  
30 # We were given more than one configuration to process, so merge
31  
32 if ( ref $config eq 'ARRAY' ) {
33 my $full_config = {};
34 foreach my $single_config ( @{ $config } ) {
35 next unless ( ref $single_config eq 'HASH' );
36 foreach my $object_key ( keys %{ $single_config } ) {
37 $full_config->{ $object_key } = $single_config->{ $object_key };
38 }
39 }
40 $config = $full_config;
41 }
42  
43 my $class_created_ok = SPOPS::ClassFactory->create( $config, $p ) || [];
44 unless ( scalar @{ $class_created_ok } ) {
45 $log->warn( "No classes were created by 'SPOPS::ClassFactory->create()'" );
46 return undef;
47 }
48  
49 # Now go through each of the classes created and initialize
50  
51 my @full_success = ();
52 foreach my $spops_class ( @{ $class_created_ok } ) {
53 eval { $spops_class->class_initialize() };
54 if ( $@ ) {
55 spops_error "Failed to run class_initialize() on ",
56 "[$spops_class]: $@";
57 }
58 push @full_success, $spops_class;
59 }
60 return \@full_success;
61 }
62  
63  
64 # Read in one or more configuration files (see POD)
65  
66 sub read_config {
67 my ( $class, $p ) = @_;
68 my @config_files = ();
69  
70 # You can specify one or more filenames to read
71  
72 if ( $p->{filename} ) {
73 if ( ref $p->{filename} eq 'ARRAY' ) {
74 push @config_files, @{ $p->{filename} };
75 }
76 else {
77 push @config_files, $p->{filename};
78 }
79 }
80  
81 # Or specify a directory and, optionally, a pattern to match for
82 # files to read
83  
84 elsif ( $p->{directory} and -d $p->{directory} ) {
85 my $dir = $p->{directory};
86 $log->is_info &&
87 $log->info( "Reading configuration files from ($dir) with pattern ($p->{pattern})" );
88 opendir( CONF, $dir )
89 || spops_error "Cannot read configuration files from directory [$dir]: $!";
90 my @directory_files = readdir( CONF );
91 close( CONF );
92 foreach my $file ( @directory_files ) {
93 my $full_filename = "$dir/$file";
94 next unless ( -f $full_filename );
95 if ( $p->{pattern} ) {
96 next unless ( $file =~ /$p->{pattern}/ );
97 }
98 push @config_files, $full_filename;
99 }
100 }
101  
102 # Now read in each of the files and assign the values to the main
103 # $spops_config.
104  
105 my %spops_config = ();
106 foreach my $file ( @config_files ) {
107 $log->is_info &&
108 $log->info( "Reading configuration from file: ($file)" );
109 my $data = $class->read_perl_file( $file );
110 if ( ref $data eq 'HASH' ) {
111 foreach my $spops_key ( keys %{ $data } ) {
112 $spops_config{ $spops_key } = $data->{ $spops_key };
113 }
114 }
115 }
116  
117 return \%spops_config;
118 }
119  
120  
121 # Read in a Perl data structure from a file and return
122  
123 sub read_perl_file {
124 my ( $class, $filename ) = @_;
125 return undef unless ( -f $filename );
126 eval { open( INFO, $filename ) || die $! };
127 if ( $@ ) {
128 warn "Cannot open config file for evaluation ($filename): $@ ";
129 return undef;
130 }
131 local $/ = undef;
132 no strict;
133 my $info = <INFO>;
134 close( INFO );
135 my $data = eval $info;
136 if ( $@ ) {
137 spops_error "Cannot read data structure from [$filename]: $@";
138 }
139 return $data;
140 }
141  
142 1;
143  
144 __END__
« SPOPS/Exception/LDAP.pm ↑ Summary SPOPS/Utility.pm »