LS/Cache/Config.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 205 CC 29 SCAR 52.8
CC
29
Cov
0%
read_cache_settings
380.0
new
42.0
create_dir
30.0
CRAP 196.1
1 # =====================================================================
2 # Copyright (c) 2002,2003 IBM Corporation
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Common Public License v1.0
5 # which accompanies this distribution, and is available at
6 # http://www.opensource.org/licenses/cpl.php
7 #
8 # =====================================================================
9 package LS::Cache::Config;
10  
11 use strict;
12 use warnings;
13  
14 use vars qw( $CACHE_FILENAME $LOG_FILENAME $DEFAULT_ROOT_CACHE_DIR );
15  
16  
17 use Carp qw(:DEFAULT);
18  
19  
20 use LS::Cache::Logger;
21  
22  
23  
24 $CACHE_FILENAME = 'lsid-cache.properties';
25 $LOG_FILENAME = 'lsid-cache.log';
26 $DEFAULT_ROOT_CACHE_DIR = '/tmp/lsid-client/cache';
27  
28  
29 #
30 # new( %options ) -
31 #
32 sub new {
33
34 my ($self, %options) = @_;
35
36 unless (ref $self) {
37
38 $self = bless {
39  
40 # Setup the default cache settingss
41 'cache-root-dir' => $DEFAULT_ROOT_CACHE_DIR,
42 'max-cache-size' => -1,
43 'max-cache-lifetime' => -1,
44 'logging-on' => 'true',
45 'log-file' => "$DEFAULT_ROOT_CACHE_DIR/$LOG_FILENAME",
46 'max-cache-size-high' => 0,
47 'max-cache-lifetime-high' => 0,
48
49 '_logger' => undef,
50 }, $self;
51 }
52  
53 $self->{_logger} = LS::Cache::Logger->new('log-file' => $self->{'log-file'});
54
55
56 # Override the root from the environment
57 if($options{'cache-root-dir'}) {
58
59 $self->{'cache-root-dir'} = $options{'cache-root-dir'};
60  
61 # Update the log file
62 $self->{'log-file'} = $self->{'cache-root-dir'} . "/$LOG_FILENAME";
63 }
64  
65 if($options{'logging-on'}) {
66  
67 $self->{'logging-on'} = 'true';
68 }
69
70 # Detect the path delimiter
71 if($self->{'cache-root-dir'} =~ /^\\.*/) {
72
73 $self->{'cache-root-dir'} =~ s/\\/\//g;
74 }
75  
76 # Make sure it exists
77 if(! -e $self->{'cache-root-dir'}) {
78
79 $self->create_dir($self->{'cache-root-dir'});
80 }
81  
82 # Read the global cache settings
83 my $config_ref = $self->read_cache_settings($self->{'cache-root-dir'}, $self);
84
85 $self->update_config($config_ref);
86
87 return $self;
88 }
89  
90 sub get_config_entry {
91
92 my ($self, $entry_key) = @_;
93
94 return $self->{$entry_key};
95 }
96  
97 sub update_config {
98
99 my ($self, $config_ref) = @_;
100
101 foreach(keys(%$config_ref)) {
102
103 $self->{$_} = $config_ref->{$_};
104 }
105 }
106  
107 sub import_cache_settings {
108
109 my ($self, $directory) = @_;
110
111 my @dirs = split /\//, $directory;
112  
113 # Elminate the blank space etc.
114 while($dirs[0] =~ /^ *$/) {
115  
116 shift @dirs;
117 }
118  
119 my $cwd = shift(@dirs);
120  
121 # Try to load the directory's cache using the template as a default
122 my %cache_settings = %{$self->read_cache_settings($cwd, $self)};
123
124 foreach my $d (@dirs) {
125
126 if( -e "$cwd/$d/$CACHE_FILENAME") {
127
128 %cache_settings = %{$self->read_cache_settings("$cwd/$d", \%cache_settings)};
129 }
130 $cwd .= "/$d";
131 }
132
133 return \%cache_settings;
134 }
135  
136 sub read_cache_settings {
137  
138 my ($self, $directory, $cache_settings_ref) = @_;
139
140 my %cache_settings;
141
142 %cache_settings = %{$cache_settings_ref} if($cache_settings_ref);
143  
144 return $cache_settings_ref if(! -e "$directory/$CACHE_FILENAME");
145
146 open(FILE, "$directory/$CACHE_FILENAME");
147
148 while(<FILE>) {
149
150 # TODO:
151 # Change this so that it is a more strict regex
152 # Add validation to the values as well
153
154 if(/^max-cache-size:(.*)/) {
155
156 if ($cache_settings{'max-cache-size'} > $1 ||
157 ($cache_settings{'max-cache-size'} == -1 && $1 > 0)) {
158
159 $cache_settings{'max-cache-size'} = $1;
160 }
161 else {
162
163 $self->{_logger}->log_message("Invalid max-cache-size, parent: " .
164 $cache_settings{'max-cache-size'} . " current: $1") if($self->{'logging-on'} eq 'true');
165 }
166 }
167 elsif(/^max-cache-lifetime:(.*)/) {
168
169 if($cache_settings{'max-cache-lifetime'} > $1 ||
170 ($cache_settings{'max-cache-lifetime'} == -1 && $1 > 0)) {
171
172 $cache_settings{'max-cache-lifetime'} = $1;
173 }
174 else {
175
176 $self->{_logger}->log_message( 'Invalid max-cache-lifetime, parent ' .
177 $cache_settings{'max-cache-lifetime'} . " current: $1") if($self->{'logging-on'} eq 'true');
178 }
179 }
180 elsif(/^logging-on:(true|false)/) {
181
182 $cache_settings{'logging-on'} = 'false';
183 $cache_settings{'logging-on'} = 'true' if ($1 eq 'true');
184 }
185 elsif(/^log-file:(.*)/) {
186
187 $cache_settings{'log-file'} = $1;
188 }
189 elsif(/^max-cache-size-high:(.*)/) {
190
191 if($cache_settings{'max-cache-size-high'} > $1 ||
192 ($cache_settings{'max-cache-size-high'} == -1 && $1 > 0)) {
193
194 $cache_settings{'max-cache-size-high'} = $1;
195 }
196 else {
197
198 $self->{_logger}->log_message( 'Invalid max-cache-size-high, parent ' .
199 $cache_settings{'max-cache-size-high'} . " current $1") if($self->{'logging-on'} eq 'true');
200 }
201 }
202 elsif(/^max-cache-lifetime-high:(.*)/) {
203
204 if($cache_settings{'max-cache-lifetime-high'} > $1 ||
205 ($cache_settings{'max-cache-lifetime-high'} == -1 && $1 > 0)) {
206
207 $cache_settings{'max-cache-lifetime-high'} = $1;
208 }
209 else {
210
211 $self->{_logger}->log_message( 'Invalid max-cache-lifetime-high, parent ' .
212 $cache_settings{'max-cache-lifetime-high'} . " current $1") if($self->{'logging-on'} eq 'true');
213 }
214 }
215 elsif(/^#/) {
216  
217 # Comment, ignored
218 next;
219 }
220 else {
221 print STDERR Carp::shortmess("Invalid configuration line: $_");
222 }
223 }
224  
225 close(FILE);
226  
227 return \%cache_settings;
228 }
229  
230 sub create_dir {
231
232 my ($self, $directory) = @_;
233
234 my @directories = split(/\//, $directory);
235 my $work = '/' . $directories[1];
236
237 # Elminate the blank space etc.
238 shift @directories;
239 shift @directories;
240
241 foreach(@directories) {
242
243 if(! -e $work) {
244
245 print STDERR Carp::longmess("Unable to make directory $work") unless(mkdir($work));
246 }
247
248 $work .= "/$_";
249 }
250
251 if(! -e $work) {
252
253 print STDERR Carp::longmess("Unable to make directory $work") unless(mkdir($work));
254 }
255 }
256  
257 1;
258  
259 __END__